diff --git a/Passepartout-iOS/Scenes/ProviderPoolViewController.swift b/Passepartout-iOS/Scenes/ProviderPoolViewController.swift index dff6d00f..6fd06643 100644 --- a/Passepartout-iOS/Scenes/ProviderPoolViewController.swift +++ b/Passepartout-iOS/Scenes/ProviderPoolViewController.swift @@ -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() + 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) + } } } diff --git a/Passepartout-iOS/Scenes/ServiceViewController.swift b/Passepartout-iOS/Scenes/ServiceViewController.swift index 1ac89003..a5ea97ab 100644 --- a/Passepartout-iOS/Scenes/ServiceViewController.swift +++ b/Passepartout-iOS/Scenes/ServiceViewController.swift @@ -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 diff --git a/Passepartout/Sources/Services/Pool.swift b/Passepartout/Sources/Services/Pool.swift index 175188b9..8838fa2b 100644 --- a/Passepartout/Sources/Services/Pool.swift +++ b/Passepartout/Sources/Services/Pool.swift @@ -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 diff --git a/Passepartout/Sources/Services/PoolGroup.swift b/Passepartout/Sources/Services/PoolGroup.swift index 25d3554e..54ac9369 100644 --- a/Passepartout/Sources/Services/PoolGroup.swift +++ b/Passepartout/Sources/Services/PoolGroup.swift @@ -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 ?? "--")}" + } }