diff --git a/Passepartout-iOS/Scenes/Organizer/ShortcutsConnectToViewController.swift b/Passepartout-iOS/Scenes/Organizer/ShortcutsConnectToViewController.swift index ecf4d5b8..fac8dd30 100644 --- a/Passepartout-iOS/Scenes/Organizer/ShortcutsConnectToViewController.swift +++ b/Passepartout-iOS/Scenes/Organizer/ShortcutsConnectToViewController.swift @@ -89,7 +89,7 @@ class ShortcutsConnectToViewController: UITableViewController, ProviderPoolViewC guard let provider = selectedProfile as? ProviderConnectionProfile else { return } - vc.pools = provider.sortedPools() + vc.setPools(provider.pools()) vc.delegate = self } diff --git a/Passepartout-iOS/Scenes/ProviderPoolViewController.swift b/Passepartout-iOS/Scenes/ProviderPoolViewController.swift index 3cbac7f3..032410b5 100644 --- a/Passepartout-iOS/Scenes/ProviderPoolViewController.swift +++ b/Passepartout-iOS/Scenes/ProviderPoolViewController.swift @@ -33,12 +33,21 @@ protocol ProviderPoolViewControllerDelegate: class { class ProviderPoolViewController: UIViewController { @IBOutlet private weak var tableView: UITableView! - var pools: [Pool] = [] - + private var poolsByGroup: [PoolGroup: [Pool]] = [:] + + private var sortedGroups: [PoolGroup] = [] + var currentPoolId: String? weak var delegate: ProviderPoolViewControllerDelegate? + func setPools(_ pools: [Pool]) { + for p in pools { + poolsByGroup[p.group()] = [p] + } + sortedGroups = poolsByGroup.keys.sorted() + } + // MARK: UIViewController override func awakeFromNib() { @@ -62,18 +71,26 @@ class ProviderPoolViewController: UIViewController { extension ProviderPoolViewController: UITableViewDataSource, UITableViewDelegate { private var selectedIndexPath: IndexPath? { - guard let row = pools.index(where: { $0.id == currentPoolId }) else { - return nil + for entries in poolsByGroup.enumerated() { + guard let _ = entries.element.value.index(where: { $0.id == currentPoolId }) else { + continue + } + guard let row = sortedGroups.index(of: entries.element.key) else { + continue + } + return IndexPath(row: row, section: 0) } - return IndexPath(row: row, section: 0) + return nil } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { - return pools.count + return sortedGroups.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { - let pool = pools[indexPath.row] + let group = sortedGroups[indexPath.row] + let groupPools = poolsByGroup[group] + let pool = groupPools!.first! let cell = Cells.setting.dequeue(from: tableView, for: indexPath) cell.imageView?.image = pool.logo @@ -85,7 +102,10 @@ extension ProviderPoolViewController: UITableViewDataSource, UITableViewDelegate } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { - let pool = pools[indexPath.row] + let group = sortedGroups[indexPath.row] + let groupPools = poolsByGroup[group] + let pool = groupPools!.first! + currentPoolId = pool.id delegate?.providerPoolController(self, didSelectPool: pool) } diff --git a/Passepartout-iOS/Scenes/ServiceViewController.swift b/Passepartout-iOS/Scenes/ServiceViewController.swift index 9414acea..2f46c276 100644 --- a/Passepartout-iOS/Scenes/ServiceViewController.swift +++ b/Passepartout-iOS/Scenes/ServiceViewController.swift @@ -151,7 +151,7 @@ class ServiceViewController: UIViewController, TableModelHost { case .providerPoolSegueIdentifier: let vc = destination as? ProviderPoolViewController - vc?.pools = uncheckedProviderProfile.sortedPools() + vc?.setPools(uncheckedProviderProfile.pools()) vc?.currentPoolId = uncheckedProviderProfile.poolId vc?.delegate = self diff --git a/Passepartout/Sources/Model/Profiles/ProviderConnectionProfile.swift b/Passepartout/Sources/Model/Profiles/ProviderConnectionProfile.swift index 4a587805..b0d2170c 100644 --- a/Passepartout/Sources/Model/Profiles/ProviderConnectionProfile.swift +++ b/Passepartout/Sources/Model/Profiles/ProviderConnectionProfile.swift @@ -72,8 +72,8 @@ public class ProviderConnectionProfile: ConnectionProfile, Codable, Equatable { presetId = infrastructure.defaults.preset } - public func sortedPools() -> [Pool] { - return infrastructure.pools.sorted() + public func pools() -> [Pool] { + return infrastructure.pools } private func validateEndpoint() {