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
|
2017-06-13 11:42:49 +08:00
|
|
|
import passKit
|
2020-06-28 21:25:40 +02:00
|
|
|
import SafariServices
|
|
|
|
|
import UIKit
|
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 {
|
2021-12-31 07:35:17 +01:00
|
|
|
case style, 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 {
|
2020-06-28 21:25:40 +02:00
|
|
|
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()
|
2017-04-07 01:59:03 +08:00
|
|
|
if navigationItemTitle != nil {
|
|
|
|
|
navigationItem.title = navigationItemTitle
|
|
|
|
|
}
|
2017-02-09 01:41:17 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2020-06-28 21:25:40 +02: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
|
|
|
|
2020-06-28 21:25:40 +02:00
|
|
|
override func tableView(_: UITableView, numberOfRowsInSection section: Int) -> Int {
|
|
|
|
|
tableData[section].count
|
2017-02-09 01:41:17 +08:00
|
|
|
}
|
|
|
|
|
|
2020-06-28 21:25:40 +02: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")
|
2017-04-05 11:07:05 -07:00
|
|
|
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
|
2017-04-05 11:07:05 -07:00
|
|
|
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
|
|
|
|
2020-06-28 21:25:40 +02: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":
|
2017-02-09 13:52:49 +08:00
|
|
|
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 ?? ""
|
2017-02-28 17:10:53 +08:00
|
|
|
if MFMailComposeViewController.canSendMail() {
|
|
|
|
|
sendEmail(toRecipients: [urlComponents.path], subject: subject)
|
2017-05-04 21:12:37 +08:00
|
|
|
} else {
|
|
|
|
|
let email = urlComponents.path
|
2019-01-14 20:57:45 +01:00
|
|
|
let alertTitle = "CannotOpenMail".localize()
|
|
|
|
|
let alertMessage = "CopiedEmail".localize(email)
|
2017-05-04 21:12:37 +08:00
|
|
|
Utils.copyToPasteboard(textToCopy: email)
|
|
|
|
|
Utils.alert(title: alertTitle, message: alertMessage, controller: self, completion: nil)
|
2017-02-28 17:10:53 +08:00
|
|
|
}
|
2017-02-09 16:33:39 +08:00
|
|
|
case "http", "https":
|
2021-01-31 14:02:09 +01:00
|
|
|
let svc = SFSafariViewController(url: URL(string: link)!)
|
2020-06-28 21:25:40 +02:00
|
|
|
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)
|
2020-06-28 21:25:40 +02:00
|
|
|
present(mailVC, animated: true, completion: nil)
|
2017-02-09 16:33:39 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2020-06-28 21:25:40 +02: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
|
|
|
}
|