Files
eqn.ios/Sources/Earthquake Network/Controllers/Settings/SettingsSeismicNetworkAlertsViewController.swift
T

201 lines
7.4 KiB
Swift

//
// SettingsSeismicNetworkAlertsViewController.swift
// Earthquake Network
//
// Created by Andrea Busi on 06/06/24.
// Copyright © 2024 Earthquake Network. All rights reserved.
//
import UIKit
import Shogun
class SettingsSeismicNetworkAlertsViewController: SettingsBaseViewController {
private enum RowIdentifier: Int {
case abilitaNotifiche
case filtroNotifiche
case magnitudoMinima
case distanzaMassima
}
private static let SegueFilters = "ShowFilters"
private var isNotificationEnabled = false
private var currentFilter: EQNNotificationsSesmicNetwork.FilterType = .soloRilevanti
private var currentMinimumMagnitude = EQNData.DefaultSettingSeismicMagnitude
private var currentMaximumDistance = EQNData.DefaultSettingSeismicRadius
private let dataSourceMinimumMagnitude = EQNData.settingSeismicNetworkAlertMagnitude
private let dataSourceMaximumDistance = EQNData.settingSeismicNetworkAlertRadius
private let settings: [SettingItem] = [
.init(type: .enable, title: NSLocalizedString("options_notification_enable_official", comment: "")),
.init(type: .multiValues, title: NSLocalizedString("options_official_type", comment: ""), segue: SegueFilters),
.init(type: .slider, title: NSLocalizedString("options_official_minmag", comment: "")),
.init(type: .slider, title: NSLocalizedString("options_official_maxdist", comment: ""))
]
// MARK: - View Liefcycle
override func viewDidLoad() {
super.viewDidLoad()
setupUI()
tableView.reloadData()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
loadDataSource()
tableView.reloadData()
}
// MARK: - Private
private func setupUI() {
navigationItem.largeTitleDisplayMode = .never
tableView.estimatedRowHeight = 200.0
tableView.rowHeight = UITableView.automaticDimension
tableView.registerHeaderFooterView(for: SettingSectionHeaderView.self)
tableView.registerCell(for: SettingEnableTableViewCell.self)
tableView.registerCell(for: SettingSliderTableViewCell.self)
tableView.registerCell(for: SettingMultivaluesTableViewCell.self)
}
private func loadDataSource() {
let saved = EQNNotificationsSesmicNetwork.shared
isNotificationEnabled = saved.isAbilitato
// filtro notifiche
currentFilter = saved.filtro
// magnitudo minima
let magnitudoMinima = EQNData.settingSeismicNetworkAlertMagnitude(for: saved.magnitudoMinima)
currentMinimumMagnitude = magnitudoMinima
// raggio dalla tua posizione
let distanzaMassima = EQNData.settingSeismicNetworkAlertRadius(for: saved.distanzaMassima)
currentMaximumDistance = distanzaMassima
}
// MARK: - Table view data source
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = tableView.dequeueHeaderFooterView(cellIdentifiable: SettingSectionHeaderView.self)
view.titleLabel.text = NSLocalizedString("options_notification_official", comment: "")
return view
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
SettingSectionHeaderView.Height
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return settings.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let identifier = RowIdentifier(rawValue: indexPath.row) else {
return UITableViewCell()
}
let setting = settings[indexPath.row]
switch setting.type {
case .enable:
let cell = tableView.dequeueReusableCell(cellIdentifiable: SettingEnableTableViewCell.self, for: indexPath)
cell.titleLabel.text = setting.displayTitle
cell.descriptionLabel.text = setting.subtitle
switch identifier {
case .abilitaNotifiche:
cell.toggleSwitch.isOn = isNotificationEnabled
cell.valueChanged = { [weak self] enabled in
self?.onChangeNotificationEnabled(enabled)
}
default:
break
}
return cell
case .multiValues:
let cell = tableView.dequeueReusableCell(cellIdentifiable: SettingMultivaluesTableViewCell.self, for: indexPath)
cell.accessoryType = .disclosureIndicator
cell.titleLabel.text = setting.title
switch identifier {
case .filtroNotifiche:
cell.isDisabled = !isNotificationEnabled
cell.isUserInteractionEnabled = isNotificationEnabled
cell.valuesLabel.text = currentFilter.displayName
default:
break
}
return cell
case .slider:
let cell = tableView.dequeueReusableCell(cellIdentifiable: SettingSliderTableViewCell.self, for: indexPath)
cell.titleLabel.text = setting.displayTitle
let filtersEnabled = isNotificationEnabled && currentFilter == .condizionati
switch identifier {
case .magnitudoMinima:
cell.isDisabled = !filtersEnabled
cell.configureSlider(with: dataSourceMinimumMagnitude, current: currentMinimumMagnitude)
cell.valueChanged = { [weak self] item in
self?.onChangeMinimumMagnitude(item)
}
case .distanzaMassima:
cell.isDisabled = !filtersEnabled
cell.configureSlider(with: dataSourceMaximumDistance, current: currentMaximumDistance)
cell.valueChanged = { [weak self] item in
self?.onChangeMaximumDistance(item)
}
default:
break
}
return cell
default:
fatalError()
}
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let setting = settings[indexPath.row]
switch setting.segue {
case Self.SegueFilters:
let controller = SettingsSeismicNotificationFiltersViewController()
navigationController?.pushViewController(controller, animated: true)
default:
break
}
}
private func onChangeNotificationEnabled(_ enabled: Bool) {
isNotificationEnabled = enabled
EQNNotificationsSesmicNetwork.shared.isAbilitato = isNotificationEnabled
EQNNotificationsSesmicNetwork.shared.saveUserInfo()
tableView.reloadData()
}
private func onChangeMinimumMagnitude(_ item: EQNGenericValue) {
EQNNotificationsSesmicNetwork.shared.magnitudoMinima = item.value
EQNNotificationsSesmicNetwork.shared.saveUserInfo()
loadDataSource()
}
private func onChangeMaximumDistance(_ item: EQNGenericValue) {
EQNNotificationsSesmicNetwork.shared.distanzaMassima = item.value
EQNNotificationsSesmicNetwork.shared.saveUserInfo()
loadDataSource()
}
}