Disclose if poolGroup is > 1

List pool server numbers and pop to delegate.
This commit is contained in:
Davide De Rosa 2019-04-06 15:12:39 +02:00
parent 8db2b70e65
commit 5bb3a49a84
4 changed files with 57 additions and 13 deletions

View File

@ -43,7 +43,16 @@ class ProviderPoolViewController: UIViewController {
func setPools(_ pools: [Pool], currentPoolId: String?) {
for p in pools {
poolsByGroup[p.group()] = [p]
let group = p.group()
if var existingPools = poolsByGroup[group] {
existingPools.append(p)
poolsByGroup[group] = existingPools
} else {
poolsByGroup[group] = [p]
}
if p.id == currentPoolId {
currentPool = p
}
}
sortedGroups = poolsByGroup.keys.sorted()
}
@ -89,24 +98,47 @@ extension ProviderPoolViewController: UITableViewDataSource, UITableViewDelegate
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let group = sortedGroups[indexPath.row]
let groupPools = poolsByGroup[group]
let pool = groupPools!.first!
let groupPools = poolsByGroup[group]!
guard let pool = groupPools.first else {
fatalError("Empty pools in group \(group)")
}
let cell = Cells.setting.dequeue(from: tableView, for: indexPath)
cell.imageView?.image = pool.logo
cell.leftText = pool.localizedName
cell.rightText = pool.areaId?.uppercased()
cell.applyChecked(pool.id == currentPool?.id, Theme.current)
// FIXME: checkmark overridden when count > 1
if groupPools.count > 1 {
cell.rightText = pool.area?.uppercased()
cell.accessoryType = .disclosureIndicator
} else {
cell.rightText = pool.areaId?.uppercased()
cell.applyChecked(pool.id == currentPool?.id, Theme.current)
}
cell.isTappable = true
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let group = sortedGroups[indexPath.row]
let groupPools = poolsByGroup[group]
let pool = groupPools!.first!
let groupPools = poolsByGroup[group]!
guard let pool = groupPools.first else {
fatalError("Empty pools in group \(group)")
}
currentPool = pool
delegate?.providerPoolController(self, didSelectPool: pool)
if groupPools.count > 1 {
let vc = OptionViewController<Pool>()
vc.title = pool.localizedCountry
vc.options = groupPools
vc.selectedOption = currentPool
vc.descriptionBlock = { $0.areaId ?? "" } // XXX: fail gracefully
vc.selectionBlock = {
self.currentPool = $0
self.delegate?.providerPoolController(self, didSelectPool: $0)
}
navigationController?.pushViewController(vc, animated: true)
} else {
currentPool = pool
delegate?.providerPoolController(self, didSelectPool: pool)
}
}
}

View File

@ -1178,7 +1178,7 @@ extension ServiceViewController: EndpointViewControllerDelegate {
extension ServiceViewController: ProviderPoolViewControllerDelegate {
func providerPoolController(_ vc: ProviderPoolViewController, didSelectPool pool: Pool) {
navigationController?.popViewController(animated: true)
navigationController?.popToViewController(self, animated: true)
guard pool.id != uncheckedProviderProfile.poolId else {
return

View File

@ -26,7 +26,7 @@
import Foundation
import TunnelKit
public struct Pool: Codable, Comparable, CustomStringConvertible {
public struct Pool: Codable, Hashable, Comparable, CustomStringConvertible {
public enum CodingKeys: String, CodingKey {
case id
@ -96,6 +96,12 @@ public struct Pool: Codable, Comparable, CustomStringConvertible {
public func group() -> PoolGroup {
return PoolGroup(country: country, area: area)
}
// MARK: Hashable
public func hash(into hasher: inout Hasher) {
id.hash(into: &hasher)
}
// MARK: Comparable

View File

@ -25,7 +25,7 @@
import Foundation
public struct PoolGroup: Hashable, Comparable {
public struct PoolGroup: Hashable, Comparable, CustomStringConvertible {
public let country: String
public let area: String?
@ -53,4 +53,10 @@ public struct PoolGroup: Hashable, Comparable {
public static func <(lhs: PoolGroup, rhs: PoolGroup) -> Bool {
return lhs.id < rhs.id
}
// MARK: CustomStringConvertible
public var description: String {
return "{\(country), \(area ?? "--")}"
}
}