passforios/pass/Controllers/BasicStaticTableViewController.swift

139 lines
5 KiB
Swift
Raw Normal View History

2017-02-09 01:41:17 +08:00
//
2021-08-28 07:32:31 +02:00
// BasicStaticTableViewController.swift
2017-02-09 01:41:17 +08:00
// pass
//
// Created by Mingshen Sun on 9/2/2017.
// Copyright © 2017 Bob Sun. All rights reserved.
//
2017-02-09 16:33:39 +08:00
import MessageUI
import passKit
import SafariServices
import UIKit
2017-02-09 01:41:17 +08:00
enum CellDataType {
2017-02-09 22:13:07 +08:00
case link, segue, empty, detail
2017-02-09 01:41:17 +08:00
}
2017-02-10 16:44:59 +08:00
enum CellDataStyle {
case value1, defaultStyle
}
2017-02-09 01:41:17 +08:00
enum CellDataKey {
2017-02-10 16:44:59 +08:00
case style, type, title, link, accessoryType, detailDisclosureAction, detailDisclosureData, detailText, action
2017-02-09 01:41:17 +08:00
}
2017-02-09 16:33:39 +08:00
class BasicStaticTableViewController: UITableViewController, MFMailComposeViewControllerDelegate {
var tableData = [[[CellDataKey: Any]]]()
2017-02-09 01:41:17 +08:00
var navigationItemTitle: String?
2018-12-09 16:59:07 -08:00
2017-02-09 01:41:17 +08:00
override func viewDidLoad() {
super.viewDidLoad()
if navigationItemTitle != nil {
navigationItem.title = navigationItemTitle
}
2017-02-09 01:41:17 +08:00
}
2018-12-09 16:59:07 -08:00
override func numberOfSections(in _: UITableView) -> Int {
tableData.count
2017-02-09 01:41:17 +08:00
}
2018-12-09 16:59:07 -08:00
override func tableView(_: UITableView, numberOfRowsInSection section: Int) -> Int {
tableData[section].count
2017-02-09 01:41:17 +08:00
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
2018-12-09 16:59:07 -08:00
override func tableView(_: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
2017-02-09 01:41:17 +08:00
let cellData = tableData[indexPath.section][indexPath.row]
2017-02-10 16:44:59 +08:00
let cellDataStyle = cellData[CellDataKey.style] as? CellDataStyle
2017-02-09 22:13:07 +08:00
var cell: UITableViewCell?
2018-12-09 16:59:07 -08:00
2017-02-10 16:44:59 +08:00
switch cellDataStyle ?? .defaultStyle {
case .value1:
2017-02-09 22:13:07 +08:00
cell = UITableViewCell(style: .value1, reuseIdentifier: "value1 cell")
cell?.selectionStyle = .none
2017-02-09 22:13:07 +08:00
default:
cell = UITableViewCell(style: .default, reuseIdentifier: "default cell")
2017-02-09 11:12:14 +08:00
}
2018-12-09 16:59:07 -08:00
2017-02-10 16:44:59 +08:00
if let detailText = cellData[CellDataKey.detailText] as? String {
cell?.detailTextLabel?.text = detailText
}
2019-05-01 17:49:27 +02:00
if let accessoryType = cellData[CellDataKey.accessoryType] as? UITableViewCell.AccessoryType {
2017-02-10 16:44:59 +08:00
cell?.accessoryType = accessoryType
} else {
cell?.accessoryType = .disclosureIndicator
cell?.selectionStyle = .default
2017-02-10 16:44:59 +08:00
}
2018-12-09 16:59:07 -08:00
2017-02-09 22:13:07 +08:00
cell?.textLabel?.text = cellData[CellDataKey.title] as? String
return cell ?? UITableViewCell()
2017-02-09 01:41:17 +08:00
}
2018-12-09 16:59:07 -08:00
override func tableView(_: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {
2017-02-09 11:12:14 +08:00
let cellData = tableData[indexPath.section][indexPath.row]
let selector = cellData[CellDataKey.detailDisclosureAction] as? Selector
perform(selector, with: cellData[CellDataKey.detailDisclosureData])
}
2018-12-09 16:59:07 -08:00
2017-02-09 01:41:17 +08:00
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let cellData = tableData[indexPath.section][indexPath.row]
2017-02-10 16:44:59 +08:00
let cellDataAction = cellData[CellDataKey.action] as? String
switch cellDataAction ?? "" {
case "segue":
2017-02-09 01:41:17 +08:00
let link = cellData[CellDataKey.link] as? String
performSegue(withIdentifier: link!, sender: self)
2017-02-10 16:44:59 +08:00
case "link":
let link = cellData[CellDataKey.link] as! String
2017-02-09 16:33:39 +08:00
let url = URL(string: link)!
switch url.scheme! {
case "mailto":
2017-02-09 16:44:02 +08:00
let urlComponents = URLComponents(string: link)!
let subject = urlComponents.queryItems![0].value ?? ""
if MFMailComposeViewController.canSendMail() {
sendEmail(toRecipients: [urlComponents.path], subject: subject)
} else {
let email = urlComponents.path
2019-01-14 20:57:45 +01:00
let alertTitle = "CannotOpenMail".localize()
let alertMessage = "CopiedEmail".localize(email)
Utils.copyToPasteboard(textToCopy: email)
Utils.alert(title: alertTitle, message: alertMessage, controller: self, completion: nil)
}
2017-02-09 16:33:39 +08:00
case "http", "https":
let svc = SFSafariViewController(url: URL(string: link)!)
present(svc, animated: true, completion: nil)
2017-02-09 16:33:39 +08:00
default:
break
}
2017-02-09 01:41:17 +08:00
default:
break
}
}
2018-12-09 16:59:07 -08:00
2021-01-31 13:17:37 +01:00
override func tableView(_: UITableView, heightForRowAt _: IndexPath) -> CGFloat {
2021-01-05 23:27:35 -08:00
UITableView.automaticDimension
}
2021-01-31 13:17:37 +01:00
override func tableView(_: UITableView, estimatedHeightForRowAt _: IndexPath) -> CGFloat {
2021-01-05 23:27:35 -08:00
UITableView.automaticDimension
}
2017-02-09 16:44:02 +08:00
func sendEmail(toRecipients recipients: [String], subject: String) {
2017-02-09 16:33:39 +08:00
let mailVC = MFMailComposeViewController()
mailVC.mailComposeDelegate = self
mailVC.setToRecipients(recipients)
2017-02-09 16:44:02 +08:00
mailVC.setSubject(subject)
mailVC.setMessageBody("", isHTML: false)
present(mailVC, animated: true, completion: nil)
2017-02-09 16:33:39 +08:00
}
2018-12-09 16:59:07 -08:00
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith _: MFMailComposeResult, error _: Error?) {
2017-02-09 16:33:39 +08:00
controller.dismiss(animated: true)
}
2017-02-09 01:41:17 +08:00
}