Files
2025-03-06 15:49:26 +01:00

94 lines
3.2 KiB
Swift

//
// SeismicCardSettingsViewController.swift
// Earthquake Network
//
// Created by Busi Andrea on 26/09/2020.
// Copyright © 2020 Earthquake Network. All rights reserved.
//
import UIKit
protocol SeismicCardSettingsViewControllerDelegate: AnyObject {
func seismicCardSettingsDidComplete(_ controller: SeismicCardSettingsViewController)
}
class SeismicCardSettingsViewController: UIViewController {
weak var delegate: SeismicCardSettingsViewControllerDelegate?
// MARK: - Internal
@IBOutlet private weak var containerView: UIView!
@IBOutlet private weak var titleLabel: UILabel!
@IBOutlet private weak var informationDistanceLabel: UILabel!
@IBOutlet private weak var informationDistanceSwitch: UISwitch!
@IBOutlet private weak var informationLocationLabel: UILabel!
@IBOutlet private weak var informationLocationSwitch: UISwitch!
@IBOutlet private weak var informationPopulationLabel: UILabel!
@IBOutlet private weak var informationPopulationSwitch: UISwitch!
@IBOutlet private weak var closeButton: UIButton!
private var informations: [SeismicNetworkTableViewCell.InformationType] {
get { AppPreferences.shared.seismicNetworksInformations }
set { AppPreferences.shared.seismicNetworksInformations = newValue }
}
// MARK: - View Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
setupUI()
updateUI()
}
// MARK: - Private
private func setupUI() {
containerView.layer.cornerRadius = AppTheme.shared.cardCornerRadius
containerView.layer.masksToBounds = true
// localize
titleLabel.text = NSLocalizedString("official_card_settings", comment: "")
informationDistanceLabel.text = NSLocalizedString("official_card_distance", comment: "")
informationLocationLabel.text = NSLocalizedString("official_card_coordinates", comment: "")
informationPopulationLabel.text = NSLocalizedString("official_card_population", comment: "")
closeButton.setLocalizedTitle(key: "official_close", uppercased: false)
}
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)
}
updateUI()
}
@IBAction func closeTapped(_ sender: UISwitch) {
delegate?.seismicCardSettingsDidComplete(self)
dismiss(animated: true, completion: nil)
}
}