// // MenuViewController.swift // Earthquake Network // // Created by Busi Andrea on 27/07/2020. // Copyright © 2020 Earthquake Network. All rights reserved. // import UIKit import SafariServices class MenuViewController: UITableViewController { enum MenuItem: CaseIterable { case header case settings case informations case faq case help case author case sponsors case disclaimer case privacy var title: String { switch self { case .header: return "" case .settings: return NSLocalizedString("Impostazioni", comment: "voce menu") case .informations: return NSLocalizedString("Informazioni", comment: "voce menu") case .faq: return NSLocalizedString("F.A.Q.", comment: "voce menu") case .help: return NSLocalizedString("Help", comment: "voce menu") case .author: return NSLocalizedString("Autore", comment: "voce menu") case .sponsors: return NSLocalizedString("Patrocinatori", comment: "voce menu") case .disclaimer: return NSLocalizedString("Disclaimer", comment: "voce menu") case .privacy: return NSLocalizedString("Privacy", comment: "voce menu") } } var icon: String? { switch self { case .header: return nil case .settings: return "menu-icon-settings" case .informations: return "menu-icon-info" case .faq: return "menu-icon-faq" case .help: return "menu-icon-help" case .author: return "menu-icon-author" case .sponsors: return "menu-icon-sponsors" case .disclaimer: return "menu-icon-terms" case .privacy: return "menu-icon-privacy" } } } private let items = MenuItem.allCases private static let CellIdentifierHeader = "HeaderCell" private static let CellIdentifierItem = "MenuItemCell" // MARK: - View Lifecycle override func viewDidLoad() { super.viewDidLoad() } // MARK: - Table view data source override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { items.count } override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { let item = items[indexPath.row] if item == .header { return 160 } return 60 } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let item = items[indexPath.row] if item == .header { let cell = tableView.dequeueReusableCell(withIdentifier: Self.CellIdentifierHeader, for: indexPath) as! MenuHeaderTableViewCell cell.updateUI() return cell } let cell = tableView.dequeueReusableCell(withIdentifier: Self.CellIdentifierItem, for: indexPath) as! MenuItemTableViewCell cell.item = item return cell } override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { self.revealViewController()?.revealToggle(animated: true) let item = items[indexPath.row] switch item { case .header: break case .settings: NotificationCenter.default.post(name: NSNotification.Name(rawValue: NOTIFICHE_SISMI), object: nil) default: // open url if available if let url = externalUrl(for: item) { let controller = SFSafariViewController(url: url) present(controller, animated: true, completion: nil) } } } // MARK: - Private private func externalUrl(for item: MenuItem) -> URL? { let baseUrl = EQNWebsiteAddress switch item { 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/") default: return nil } } }