mirror of
https://github.com/passepartoutvpn/passepartout-apple.git
synced 2025-01-18 06:29:05 +00:00
Create a section for free and paid servers
No section if only one exists.
This commit is contained in:
parent
d627b2ab36
commit
4343c99b10
@ -26,6 +26,32 @@
|
|||||||
import UIKit
|
import UIKit
|
||||||
import Passepartout_Core
|
import Passepartout_Core
|
||||||
|
|
||||||
|
private class PoolModel {
|
||||||
|
let title: String
|
||||||
|
|
||||||
|
var poolsByGroup: [PoolGroup: [Pool]] = [:]
|
||||||
|
|
||||||
|
private(set) var sortedGroups: [PoolGroup] = []
|
||||||
|
|
||||||
|
init(title: String) {
|
||||||
|
self.title = title
|
||||||
|
}
|
||||||
|
|
||||||
|
func addPool(_ p: Pool) {
|
||||||
|
let group = p.group()
|
||||||
|
if var existingPools = poolsByGroup[group] {
|
||||||
|
existingPools.append(p)
|
||||||
|
poolsByGroup[group] = existingPools
|
||||||
|
} else {
|
||||||
|
poolsByGroup[group] = [p]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func sort() {
|
||||||
|
sortedGroups = poolsByGroup.keys.sorted()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protocol ProviderPoolViewControllerDelegate: class {
|
protocol ProviderPoolViewControllerDelegate: class {
|
||||||
func providerPoolController(_: ProviderPoolViewController, didSelectPool pool: Pool)
|
func providerPoolController(_: ProviderPoolViewController, didSelectPool pool: Pool)
|
||||||
}
|
}
|
||||||
@ -33,28 +59,35 @@ protocol ProviderPoolViewControllerDelegate: class {
|
|||||||
class ProviderPoolViewController: UIViewController {
|
class ProviderPoolViewController: UIViewController {
|
||||||
@IBOutlet private weak var tableView: UITableView!
|
@IBOutlet private weak var tableView: UITableView!
|
||||||
|
|
||||||
private var poolsByGroup: [PoolGroup: [Pool]] = [:]
|
private var models: [PoolModel] = []
|
||||||
|
|
||||||
private var sortedGroups: [PoolGroup] = []
|
|
||||||
|
|
||||||
private var currentPool: Pool?
|
private var currentPool: Pool?
|
||||||
|
|
||||||
weak var delegate: ProviderPoolViewControllerDelegate?
|
weak var delegate: ProviderPoolViewControllerDelegate?
|
||||||
|
|
||||||
func setPools(_ pools: [Pool], currentPoolId: String?) {
|
func setPools(_ pools: [Pool], currentPoolId: String?) {
|
||||||
|
let freeModel = PoolModel(title: L10n.Provider.Pool.Sections.Free.header)
|
||||||
|
let paidModel = PoolModel(title: L10n.Provider.Pool.Sections.Paid.header)
|
||||||
for p in pools {
|
for p in pools {
|
||||||
let group = p.group()
|
if p.isFree ?? false {
|
||||||
if var existingPools = poolsByGroup[group] {
|
freeModel.addPool(p)
|
||||||
existingPools.append(p)
|
|
||||||
poolsByGroup[group] = existingPools
|
|
||||||
} else {
|
} else {
|
||||||
poolsByGroup[group] = [p]
|
paidModel.addPool(p)
|
||||||
}
|
}
|
||||||
if p.id == currentPoolId {
|
if p.id == currentPoolId {
|
||||||
currentPool = p
|
currentPool = p
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sortedGroups = poolsByGroup.keys.sorted()
|
freeModel.sort()
|
||||||
|
paidModel.sort()
|
||||||
|
|
||||||
|
models = []
|
||||||
|
if !freeModel.sortedGroups.isEmpty {
|
||||||
|
models.append(freeModel)
|
||||||
|
}
|
||||||
|
if !paidModel.sortedGroups.isEmpty {
|
||||||
|
models.append(paidModel)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: UIViewController
|
// MARK: UIViewController
|
||||||
@ -80,25 +113,41 @@ class ProviderPoolViewController: UIViewController {
|
|||||||
|
|
||||||
extension ProviderPoolViewController: UITableViewDataSource, UITableViewDelegate {
|
extension ProviderPoolViewController: UITableViewDataSource, UITableViewDelegate {
|
||||||
private var selectedIndexPath: IndexPath? {
|
private var selectedIndexPath: IndexPath? {
|
||||||
for entries in poolsByGroup.enumerated() {
|
for (i, model) in models.enumerated() {
|
||||||
guard let _ = entries.element.value.index(where: { $0.id == currentPool?.id }) else {
|
for entries in model.poolsByGroup.enumerated() {
|
||||||
continue
|
guard let _ = entries.element.value.index(where: { $0.id == currentPool?.id }) else {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
guard let row = model.sortedGroups.index(of: entries.element.key) else {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
return IndexPath(row: row, section: i)
|
||||||
}
|
}
|
||||||
guard let row = sortedGroups.index(of: entries.element.key) else {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
return IndexPath(row: row, section: 0)
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func numberOfSections(in tableView: UITableView) -> Int {
|
||||||
|
return models.count
|
||||||
|
}
|
||||||
|
|
||||||
|
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
|
||||||
|
guard models.count > 1 else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
let model = models[section]
|
||||||
|
return model.title
|
||||||
|
}
|
||||||
|
|
||||||
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
||||||
return sortedGroups.count
|
let model = models[section]
|
||||||
|
return model.sortedGroups.count
|
||||||
}
|
}
|
||||||
|
|
||||||
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
||||||
let group = sortedGroups[indexPath.row]
|
let model = models[indexPath.section]
|
||||||
let groupPools = poolsByGroup[group]!
|
let group = model.sortedGroups[indexPath.row]
|
||||||
|
let groupPools = model.poolsByGroup[group]!
|
||||||
guard let pool = groupPools.first else {
|
guard let pool = groupPools.first else {
|
||||||
fatalError("Empty pools in group \(group)")
|
fatalError("Empty pools in group \(group)")
|
||||||
}
|
}
|
||||||
@ -118,8 +167,9 @@ extension ProviderPoolViewController: UITableViewDataSource, UITableViewDelegate
|
|||||||
}
|
}
|
||||||
|
|
||||||
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
||||||
let group = sortedGroups[indexPath.row]
|
let model = models[indexPath.section]
|
||||||
let groupPools = poolsByGroup[group]!
|
let group = model.sortedGroups[indexPath.row]
|
||||||
|
let groupPools = model.poolsByGroup[group]!
|
||||||
guard let pool = groupPools.first else {
|
guard let pool = groupPools.first else {
|
||||||
fatalError("Empty pools in group \(group)")
|
fatalError("Empty pools in group \(group)")
|
||||||
}
|
}
|
||||||
@ -128,8 +178,9 @@ extension ProviderPoolViewController: UITableViewDataSource, UITableViewDelegate
|
|||||||
}
|
}
|
||||||
|
|
||||||
func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {
|
func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {
|
||||||
let group = sortedGroups[indexPath.row]
|
let model = models[indexPath.section]
|
||||||
let groupPools = poolsByGroup[group]!
|
let group = model.sortedGroups[indexPath.row]
|
||||||
|
let groupPools = model.poolsByGroup[group]!
|
||||||
guard let pool = groupPools.first else {
|
guard let pool = groupPools.first else {
|
||||||
fatalError("Empty pools in group \(group)")
|
fatalError("Empty pools in group \(group)")
|
||||||
}
|
}
|
||||||
|
@ -143,6 +143,9 @@
|
|||||||
"account.cells.open_guide.caption" = "See your credentials";
|
"account.cells.open_guide.caption" = "See your credentials";
|
||||||
"account.cells.signup.caption" = "Register with %@";
|
"account.cells.signup.caption" = "Register with %@";
|
||||||
|
|
||||||
|
"provider.pool.sections.free.header" = "Free";
|
||||||
|
"provider.pool.sections.paid.header" = "Paid";
|
||||||
|
|
||||||
"endpoint.sections.location_addresses.header" = "Addresses";
|
"endpoint.sections.location_addresses.header" = "Addresses";
|
||||||
"endpoint.sections.location_protocols.header" = "Protocols";
|
"endpoint.sections.location_protocols.header" = "Protocols";
|
||||||
"endpoint.cells.any_address.caption" = "Any";
|
"endpoint.cells.any_address.caption" = "Any";
|
||||||
|
@ -493,6 +493,18 @@ public enum L10n {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public enum Provider {
|
public enum Provider {
|
||||||
|
public enum Pool {
|
||||||
|
public enum Sections {
|
||||||
|
public enum Free {
|
||||||
|
/// Free
|
||||||
|
public static let header = L10n.tr("Localizable", "provider.pool.sections.free.header")
|
||||||
|
}
|
||||||
|
public enum Paid {
|
||||||
|
/// Paid
|
||||||
|
public static let header = L10n.tr("Localizable", "provider.pool.sections.paid.header")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
public enum Preset {
|
public enum Preset {
|
||||||
public enum Cells {
|
public enum Cells {
|
||||||
public enum TechDetails {
|
public enum TechDetails {
|
||||||
|
Loading…
Reference in New Issue
Block a user