// // SettingsViewController.swift // Earthquake Network // // Created by Busi Andrea on 30/08/2020. // Copyright © 2020 Earthquake Network. All rights reserved. // import UIKit import SafariServices class SettingsViewController: UITableViewController { private static let SegueIdentifierAllertaSismica = "ShowAllertaSismica" private static let SegueIdentifierNotificheSegnalazioniUtente = "ShowNotificheSegnalazioniUtente" private static let SegueIdentifierNotificheRetiSismiche = "ShowNotificheRetiSismiche" private static let SegueIdentifierSafari = "ShowSafari" private enum InfoItem: Int { case informations case faq case help case author case sponsors case disclaimer case privacy var externalUrl: URL? { let baseUrl = EQNWebsiteAddress switch self { case .informations: return URL(string: "\(baseUrl)") case .faq: return URL(string: "\(baseUrl)/f-a-q/") case .help: return URL(string: "\(baseUrl)/help/") case .author: return URL(string: "\(baseUrl)/contact/") case .sponsors: return URL(string: "\(baseUrl)/sponsors/") case .disclaimer: return URL(string: "\(baseUrl)/terms-conditions/") case .privacy: return URL(string: "\(baseUrl)/privacy/") } } } private var settings: [[SettingItem]] = [ [ SettingItem(type: .detail, title: NSLocalizedString("options_alarms", comment: ""), segue: SegueIdentifierAllertaSismica, emoji: "🚨"), SettingItem(type: .detail, title: NSLocalizedString("options_notification_official", comment: ""), segue: SegueIdentifierNotificheRetiSismiche, emoji: "🔔"), SettingItem(type: .detail, title: NSLocalizedString("options_notification_manual", comment: ""), segue: SegueIdentifierNotificheSegnalazioniUtente, emoji: "🔔") ], [ SettingItem(type: .detail, title: NSLocalizedString("drawer_main_info", comment: ""), segue: SegueIdentifierSafari, icon: "menu-icon-info"), SettingItem(type: .detail, title: NSLocalizedString("drawer_main_faq", comment: ""), segue: SegueIdentifierSafari, icon: "menu-icon-faq"), SettingItem(type: .detail, title: NSLocalizedString("drawer_main_help", comment: ""), segue: SegueIdentifierSafari, icon: "menu-icon-help"), SettingItem(type: .detail, title: NSLocalizedString("drawer_author", comment: ""), segue: SegueIdentifierSafari, icon: "menu-icon-author"), SettingItem(type: .detail, title: NSLocalizedString("drawer_main_sponsors", comment: ""), segue: SegueIdentifierSafari, icon: "menu-icon-sponsors"), SettingItem(type: .detail, title: NSLocalizedString("drawer_main_disclaimer", comment: ""), segue: SegueIdentifierSafari, icon: "menu-icon-terms"), SettingItem(type: .detail, title: NSLocalizedString("drawer_privacy", comment: ""), segue: SegueIdentifierSafari, icon: "menu-icon-privacy") ] ] // MARK: - View Lifecycle override func viewDidLoad() { super.viewDidLoad() title = NSLocalizedString("drawer_main_settings", comment: "").capitalized setupUI() } // MARK: - Private private func setupUI() { tableView.register(SettingDetailTableViewCell.self, forCellReuseIdentifier: SettingDetailTableViewCell.Identifier) tableView.contentInset = UIEdgeInsets(top: 20.0, left: 0.0, bottom: 0.0, right: 0.0) } // MARK: - Table view data source override func numberOfSections(in tableView: UITableView) -> Int { settings.count } override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? { if section == 0 { return nil } var title = "" if let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String { let version = NSLocalizedString("main_version", comment: "etichetta versione app") title = "\(version): \(appVersion)" } let userId = EQNUser.default().user_ID ?? "n.d." title = "\(title) - User id: \(userId)" return title } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { let aSection = settings[section] return aSection.count } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let setting = settings[indexPath.section][indexPath.row] let cell = tableView.dequeueReusableCell(withIdentifier: SettingDetailTableViewCell.Identifier, for: indexPath) as! SettingDetailTableViewCell cell.textLabel?.text = setting.displayTitle if let icon = setting.icon { cell.imageView?.tintColor = cell.textLabel?.textColor cell.imageView?.image = UIImage(named: icon) } cell.accessoryType = .disclosureIndicator return cell } override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { tableView.deselectRow(at: indexPath, animated: true) let setting = settings[indexPath.section][indexPath.row] if let segue = setting.segue { if segue == Self.SegueIdentifierSafari, let item = InfoItem(rawValue: indexPath.row), let url = item.externalUrl { // show Safari with the given URL let controller = SFSafariViewController(url: url) present(controller, animated: true, completion: nil) } else { performSegue(withIdentifier: segue, sender: nil) } } } }