// // SettingsSeismicNetworkNotificationsViewController.swift // Earthquake Network // // Created by Andrea Busi on 06/06/24. // Copyright © 2024 Earthquake Network. All rights reserved. // import UIKit import Shogun class SettingsSeismicNetworkNotificationsViewController: SettingsBaseTableViewController { private enum RowIdentifier: Int { case abilitaNotifiche case magnitudoMinima case distanzaMassima } private var isNotificationEnabled = false private var currentMinimumMagnitude = EQNData.DefaultSettingSeismicNetworkNotificationMagitude private var currentMaximumDistance = EQNData.DefaultSettingSeismicNetworkNotificationRadius private let dataSourceMinimumMagnitude = EQNData.settingSeismicNetworkNotificationMagnitudes private let dataSourceMaximumDistance = EQNData.settingSeismicNetworkNotificationRadius private let settings: [SettingItem] = [ .init(type: .enable, title: NSLocalizedString("options_notification_enable_official", comment: "")), .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 = EQNSettingSeismicNetworkNotification.shared isNotificationEnabled = saved.isAbilitato // magnitudo minima let magnitudoMinima = EQNData.getSettingSeismicNetworkNotificationMagnitudes(for: saved.magnitudoMinima) currentMinimumMagnitude = magnitudoMinima // raggio dalla tua posizione let distanzaMassima = EQNData.getSettingSeismicNetworkNotificationRadius(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 .slider: let cell = tableView.dequeueReusableCell(cellIdentifiable: SettingSliderTableViewCell.self, for: indexPath) cell.titleLabel.text = setting.displayTitle let filtersEnabled = isNotificationEnabled 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() } } private func onChangeNotificationEnabled(_ enabled: Bool) { isNotificationEnabled = enabled EQNSettingSeismicNetworkNotification.shared.isAbilitato = isNotificationEnabled EQNSettingSeismicNetworkNotification.shared.saveUserInfo() tableView.reloadData() } private func onChangeMinimumMagnitude(_ item: EQNGenericValue) { EQNSettingSeismicNetworkNotification.shared.magnitudoMinima = item.value EQNSettingSeismicNetworkNotification.shared.saveUserInfo() loadDataSource() } private func onChangeMaximumDistance(_ item: EQNGenericValue) { EQNSettingSeismicNetworkNotification.shared.distanzaMassima = item.value EQNSettingSeismicNetworkNotification.shared.saveUserInfo() loadDataSource() } }