// // SettingsUserReportNotificationsViewController.swift // Earthquake Network // // Created by Andrea Busi on 10/06/24. // Copyright © 2024 Earthquake Network. All rights reserved. // import UIKit import Shogun class SettingsUserReportNotificationsViewController: SettingsBaseTableViewController { private enum RowIdentifier: Int { case abilitaNotifiche case distanzaMassima } private var isNotificationEnabled = false private var currentMaximumDistance = EQNData.DefaultSettingUserReportNotificationRadius private let dataSourceMaximumDistance = EQNData.settingUserReportNotificationRadius private let settings: [SettingItem] = [ .init(type: .enable, title: NSLocalizedString("options_notification_enable_manual", comment: "")), .init(type: .slider, title: NSLocalizedString("options_radius", comment: "")) ] // MARK: - View Lifecycle 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) } private func loadDataSource() { let saved = EQNSettingUserReportNotification.shared isNotificationEnabled = saved.isAbilitato let distanzaMassima = EQNData.getSettingUserReportNotificationRadius(for: saved.distanzaMassima) currentMaximumDistance = distanzaMassima } // MARK: - Table view delegate and data source override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { let view = tableView.dequeueHeaderFooterView(cellIdentifiable: SettingSectionHeaderView.self) view.titleLabel.text = NSLocalizedString("options_notification_manual", 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 switch identifier { case .distanzaMassima: cell.isDisabled = !isNotificationEnabled cell.configureSlider(with: dataSourceMaximumDistance, current: currentMaximumDistance) cell.valueChanged = { [weak self] item in self?.onChangeMaximumDistance(item) } default: break } return cell default: fatalError() } } // MARK: - Private private func onChangeNotificationEnabled(_ enabled: Bool) { isNotificationEnabled = enabled EQNSettingUserReportNotification.shared.isAbilitato = isNotificationEnabled EQNSettingUserReportNotification.shared.saveUserInfo() tableView.reloadData() } private func onChangeMaximumDistance(_ item: EQNGenericValue) { EQNSettingUserReportNotification.shared.distanzaMassima = item.value EQNSettingUserReportNotification.shared.saveUserInfo() loadDataSource() } }