118 lines
4.2 KiB
Swift
118 lines
4.2 KiB
Swift
//
|
|
// SeismicSettingsNetworksViewController.swift
|
|
// Earthquake Network
|
|
//
|
|
// Created by Busi Andrea on 14/09/2020.
|
|
// Copyright © 2020 Earthquake Network. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
|
|
protocol SeismicSettingsNetworksViewControllerDelegate: AnyObject {
|
|
func seismicSettingsNetworksControllerDidComplete(_ controller: SeismicSettingsNetworksViewController)
|
|
}
|
|
|
|
class SeismicSettingsNetworksViewController: UITableViewController {
|
|
|
|
weak var delegate: SeismicSettingsNetworksViewControllerDelegate?
|
|
|
|
// MARK: - Private
|
|
|
|
private var networks = [EQNSeismicNetwork]()
|
|
private var savedNetworks = [String]()
|
|
|
|
// MARK: - View Lifecycle
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
|
|
tableView.register(SettingDetailTableViewCell.self, forCellReuseIdentifier: SettingDetailTableViewCell.Identifier)
|
|
tableView.register(SettingSectionHeaderView.self, forHeaderFooterViewReuseIdentifier: SettingSectionHeaderView.Identifier)
|
|
|
|
loadData()
|
|
}
|
|
|
|
// MARK: - Private
|
|
|
|
private func loadData() {
|
|
networks = EQNData.seismicNetworks().sorted(by: { $0.acronym < $1.acronym })
|
|
|
|
// load saved selected networks or fill with all available networks
|
|
if let savedNetworks = UserDefaults.standard.object(forKey: IMPOSTAZIONE_ENTI_RETI_SISMICHEI) as? [String] {
|
|
self.savedNetworks = savedNetworks
|
|
} else {
|
|
self.savedNetworks = EQNData.seismicNetworkAcronyms()
|
|
}
|
|
}
|
|
|
|
// MARK: - Table view data source
|
|
|
|
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
|
|
let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: SettingSectionHeaderView.Identifier) as! SettingSectionHeaderView
|
|
headerView.titleLabel.text = NSLocalizedString("options_agencies", comment: "");
|
|
return headerView
|
|
}
|
|
|
|
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
|
|
CGFloat(SettingSectionHeaderView.Height)
|
|
}
|
|
|
|
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
|
networks.count
|
|
}
|
|
|
|
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
|
let network = networks[indexPath.row]
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: SettingDetailTableViewCell.Identifier, for: indexPath) as! SettingDetailTableViewCell
|
|
cell.textLabel?.text = "\(network.acronym) (\(network.country))"
|
|
|
|
if savedNetworks.contains(network.acronym) {
|
|
cell.accessoryType = .checkmark
|
|
} else {
|
|
cell.accessoryType = .none
|
|
}
|
|
|
|
return cell
|
|
}
|
|
|
|
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
|
tableView.deselectRow(at: indexPath, animated: true)
|
|
|
|
let network = networks[indexPath.row]
|
|
if let index = savedNetworks.firstIndex(of: network.acronym) {
|
|
savedNetworks.remove(at: index)
|
|
} else {
|
|
savedNetworks.append(network.acronym)
|
|
}
|
|
|
|
// reload all rows with the given acronym
|
|
let indexes = networks
|
|
.enumerated()
|
|
.filter { $0.element.acronym == network.acronym }
|
|
.map { IndexPath(row: $0.offset, section: 0) }
|
|
tableView.reloadRows(at: indexes, with: .automatic)
|
|
}
|
|
|
|
// MARK: - Actions
|
|
|
|
@IBAction func cancelTapped(_ sender: Any) {
|
|
dismiss(animated: true, completion: nil)
|
|
}
|
|
|
|
@IBAction func saveTapped(_ sender: Any) {
|
|
// save selected networks
|
|
UserDefaults.standard.set(savedNetworks, forKey: IMPOSTAZIONE_ENTI_RETI_SISMICHEI)
|
|
|
|
// se solo un'ente è selezionato, salviamolo anche come nazione
|
|
if savedNetworks.count == 1 {
|
|
UserDefaults.standard.set(savedNetworks.first!, forKey: IMPOSTAZIONE_NAZIONE_RETI_SISMICHE)
|
|
} else {
|
|
UserDefaults.standard.removeObject(forKey: IMPOSTAZIONE_NAZIONE_RETI_SISMICHE)
|
|
}
|
|
|
|
delegate?.seismicSettingsNetworksControllerDidComplete(self)
|
|
dismiss(animated: true, completion: nil)
|
|
}
|
|
}
|