From a12cecb6470df4853905024d66d57e8a7de13f68 Mon Sep 17 00:00:00 2001 From: Davide De Rosa Date: Thu, 25 Apr 2019 21:13:22 +0200 Subject: [PATCH] Fix Pool.secondaryId and use it for sorting Account for extraCountries. --- .../Scenes/ProviderPoolViewController.swift | 14 ++----- Passepartout/Sources/Services/Pool.swift | 39 ++++++++----------- 2 files changed, 19 insertions(+), 34 deletions(-) diff --git a/Passepartout-iOS/Scenes/ProviderPoolViewController.swift b/Passepartout-iOS/Scenes/ProviderPoolViewController.swift index c4a8c08f..48239a4a 100644 --- a/Passepartout-iOS/Scenes/ProviderPoolViewController.swift +++ b/Passepartout-iOS/Scenes/ProviderPoolViewController.swift @@ -132,7 +132,7 @@ extension ProviderPoolViewController: UITableViewDataSource, UITableViewDelegate cell.rightText = pool.area?.uppercased() cell.accessoryType = .detailDisclosureButton // no checkmark! } else { - cell.rightText = pool.areaId?.uppercased() + cell.rightText = pool.secondaryId } cell.isTappable = true return cell @@ -154,17 +154,9 @@ extension ProviderPoolViewController: UITableViewDataSource, UITableViewDelegate } let vc = OptionViewController() vc.title = group.localizedCountry - vc.options = group.pools.sorted { - guard let lnum = $0.num else { - return true - } - guard let rnum = $1.num else { - return false - } - return lnum < rnum - } + vc.options = group.pools.sorted { $0.secondaryId < $1.secondaryId } vc.selectedOption = currentPool - vc.descriptionBlock = { $0.areaId ?? "" } // XXX: fail gracefully + vc.descriptionBlock = { $0.secondaryId } vc.selectionBlock = { self.currentPool = $0 self.delegate?.providerPoolController(self, didSelectPool: $0) diff --git a/Passepartout/Sources/Services/Pool.swift b/Passepartout/Sources/Services/Pool.swift index eea52966..a2806040 100644 --- a/Passepartout/Sources/Services/Pool.swift +++ b/Passepartout/Sources/Services/Pool.swift @@ -31,6 +31,8 @@ public struct Pool: Codable, Hashable { case id case country + + case extraCountries = "extra_countries" case area @@ -48,6 +50,8 @@ public struct Pool: Codable, Hashable { public let id: String public let country: String + + public let extraCountries: [String]? public let area: String? @@ -95,31 +99,20 @@ extension Pool { } public var localizedId: String { - let countryString = localizedCountry - let zone: String - if let area = area, let num = num { - zone = "\(area) #\(num)" - } else if let area = area { - zone = area - } else if let num = num { - zone = "#\(num)" - } else { - return countryString - } - return String.init(format: Pool.localizedFormat, countryString, zone.uppercased()) + return String.init(format: Pool.localizedFormat, localizedCountry, secondaryId) } - public var areaId: String? { - let id: String - if let area = area, let num = num { - id = "\(area) #\(num)" - } else if let area = area { - id = area - } else if let num = num { - id = "#\(num)" - } else { - return nil + public var secondaryId: String { + var comps: [String] = [] + if let extraCountries = extraCountries { + comps.append(contentsOf: extraCountries.map { Utils.localizedCountry($0) }) } - return id.uppercased() + if let area = area { + comps.append(area.uppercased()) + } + if let num = num { + comps.append("#\(num)") + } + return comps.joined(separator: " ") } }