// // SeismicCardSettingsViewController.swift // Earthquake Network // // Created by Busi Andrea on 26/09/2020. // Copyright © 2020 Earthquake Network. All rights reserved. // import UIKit protocol SeismicCardSettingsViewControllerDelegate: class { func seismicCardSettingsDidComplete(_ controller: SeismicCardSettingsViewController) } class SeismicCardSettingsViewController: UIViewController { weak var delegate: SeismicCardSettingsViewControllerDelegate? // MARK: - Internal @IBOutlet private weak var containerView: UIView! @IBOutlet private weak var informationDistanceSwitch: UISwitch! @IBOutlet private weak var informationLocationSwitch: UISwitch! @IBOutlet private weak var informationPopulationSwitch: UISwitch! private var informations = [SeismicNetworkTableViewCell.InformationType]() // MARK: - View Lifecycle override func viewDidLoad() { super.viewDidLoad() if let saved = UserDefaults.standard.array(forKey: EQNUserDefaultKeySesmicInformations) as? [Int] { informations = saved.compactMap { SeismicNetworkTableViewCell.InformationType(rawValue: $0) } } setupUI() updateUI() } // MARK: - Private private func setupUI() { containerView.layer.cornerRadius = AppTheme.shared.borderCornerRadius containerView.layer.masksToBounds = true } private func updateUI() { informationDistanceSwitch.isOn = informations.contains(.distance) informationLocationSwitch.isOn = informations.contains(.coordinate) informationPopulationSwitch.isOn = informations.contains(.population) } private func toggle(information: SeismicNetworkTableViewCell.InformationType) { if informations.contains(information) { informations.removeAll(where: { $0 == information }) } else { informations.append(information) } } // MARK: - Action @IBAction func switchChanged(_ sender: UISwitch) { if sender == informationDistanceSwitch { toggle(information: .distance) } else if sender == informationLocationSwitch { toggle(information: .coordinate) } else if sender == informationPopulationSwitch { toggle(information: .population) } UserDefaults.standard.set(informations.map { $0.rawValue }, forKey: EQNUserDefaultKeySesmicInformations) updateUI() } @IBAction func closeTapped(_ sender: UISwitch) { delegate?.seismicCardSettingsDidComplete(self) dismiss(animated: true, completion: nil) } }