Fix Pool.secondaryId and use it for sorting

Account for extraCountries.
This commit is contained in:
Davide De Rosa 2019-04-25 21:13:22 +02:00
parent d9a0ebd923
commit a12cecb647
2 changed files with 19 additions and 34 deletions

View File

@ -132,7 +132,7 @@ extension ProviderPoolViewController: UITableViewDataSource, UITableViewDelegate
cell.rightText = pool.area?.uppercased() cell.rightText = pool.area?.uppercased()
cell.accessoryType = .detailDisclosureButton // no checkmark! cell.accessoryType = .detailDisclosureButton // no checkmark!
} else { } else {
cell.rightText = pool.areaId?.uppercased() cell.rightText = pool.secondaryId
} }
cell.isTappable = true cell.isTappable = true
return cell return cell
@ -154,17 +154,9 @@ extension ProviderPoolViewController: UITableViewDataSource, UITableViewDelegate
} }
let vc = OptionViewController<Pool>() let vc = OptionViewController<Pool>()
vc.title = group.localizedCountry vc.title = group.localizedCountry
vc.options = group.pools.sorted { vc.options = group.pools.sorted { $0.secondaryId < $1.secondaryId }
guard let lnum = $0.num else {
return true
}
guard let rnum = $1.num else {
return false
}
return lnum < rnum
}
vc.selectedOption = currentPool vc.selectedOption = currentPool
vc.descriptionBlock = { $0.areaId ?? "" } // XXX: fail gracefully vc.descriptionBlock = { $0.secondaryId }
vc.selectionBlock = { vc.selectionBlock = {
self.currentPool = $0 self.currentPool = $0
self.delegate?.providerPoolController(self, didSelectPool: $0) self.delegate?.providerPoolController(self, didSelectPool: $0)

View File

@ -31,6 +31,8 @@ public struct Pool: Codable, Hashable {
case id case id
case country case country
case extraCountries = "extra_countries"
case area case area
@ -48,6 +50,8 @@ public struct Pool: Codable, Hashable {
public let id: String public let id: String
public let country: String public let country: String
public let extraCountries: [String]?
public let area: String? public let area: String?
@ -95,31 +99,20 @@ extension Pool {
} }
public var localizedId: String { public var localizedId: String {
let countryString = localizedCountry return String.init(format: Pool.localizedFormat, localizedCountry, secondaryId)
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())
} }
public var areaId: String? { public var secondaryId: String {
let id: String var comps: [String] = []
if let area = area, let num = num { if let extraCountries = extraCountries {
id = "\(area) #\(num)" comps.append(contentsOf: extraCountries.map { Utils.localizedCountry($0) })
} else if let area = area {
id = area
} else if let num = num {
id = "#\(num)"
} else {
return nil
} }
return id.uppercased() if let area = area {
comps.append(area.uppercased())
}
if let num = num {
comps.append("#\(num)")
}
return comps.joined(separator: " ")
} }
} }