feat: Improve country picker selection

This commit is contained in:
Andrea Busi
2020-09-28 10:24:40 +02:00
parent 88c1c5aa16
commit 2b304cd7aa
3 changed files with 24 additions and 3 deletions
@@ -44,10 +44,13 @@ class SeismicSettingsViewController: UIViewController {
// load saved country (if exists)
let savedCountry = UserDefaults.standard.object(forKey: IMPOSTAZIONE_NAZIONE_RETI_SISMICHE) as? String
countryTextField.text = EQNData.seismic(for: savedCountry)?.country
selectedNetwork = EQNData.seismic(for: savedCountry)
countryTextField.text = selectedNetwork?.country
countryTextField.inputView = picker.view
picker.configure(with: networks) { [unowned self] (network) in
let selectedIndex: Int? = selectedNetwork != nil ? networks.firstIndex(of: selectedNetwork!) : nil
picker.configure(with: networks, selectedIndex: selectedIndex) { [unowned self] (network) in
guard let network = network as? EQNSeismicNetwork else { return }
self.view.endEditing(true)
@@ -68,11 +68,16 @@ class EQNGenericPickerViewController: UIViewController, UIPickerViewDelegate, UI
// MARK: - Public
public func configure(with items: [PickerRepresentable], completion: @escaping DoneCompletion) {
public func configure(with items: [PickerRepresentable], selectedIndex: Int? = nil, completion: @escaping DoneCompletion) {
self.onCompletion = completion
self.items = items
self.selectedIndex = items.count > 0 ? 1 : nil
self.pickerView.reloadAllComponents()
if let selectedIndex = selectedIndex, 0..<items.count ~= selectedIndex {
self.selectedIndex = selectedIndex
self.pickerView.selectRow(selectedIndex, inComponent: 0, animated: false)
}
}
// MARK: - Actions
@@ -22,6 +22,19 @@ class EQNSeismicNetwork: NSObject {
self.country = country
self.extended = extended
}
// MARK: - Equatable
override func isEqual(_ object: Any?) -> Bool {
if let anObject = object as? EQNSeismicNetwork {
return anObject.acronym.lowercased() == acronym.lowercased()
}
return false
}
override var hash: Int {
acronym.hash
}
}