123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- import UIKit
- class SecuritySettingsController: UITableViewController {
- private var options: [String]
- private var selectedIndex: Int {
- didSet {
- print(selectedIndex)
- }
- }
- private var backupIndex: Int
- var onDismiss: ((String) -> Void)?
- private var resetButton: UIBarButtonItem!
- private var staticCells: [UITableViewCell] {
- return options.map {
- let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
- cell.textLabel?.text = $0
- cell.selectionStyle = .none
- return cell
- }
- }
- init(title: String, options: [String], selectedOption: String) {
- self.options = options
- selectedIndex = options.index(of: selectedOption)!
- backupIndex = selectedIndex
- super.init(style: .grouped)
- self.title = title
- }
- required init?(coder aDecoder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
- override func viewDidLoad() {
- super.viewDidLoad()
- resetButton = UIBarButtonItem(title: String.localized("reset"), style: .done, target: self, action: #selector(resetButtonPressed))
- resetButton.isEnabled = false
- navigationItem.rightBarButtonItem = resetButton
- }
- override func viewWillDisappear(_ animated: Bool) {
- let selectedOption = options[selectedIndex]
- onDismiss?(selectedOption)
- }
- // MARK: - Table view data source
- override func numberOfSections(in tableView: UITableView) -> Int {
- return 1
- }
- override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return options.count
- }
- override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- let cell = staticCells[indexPath.row]
- if selectedIndex == indexPath.row {
- cell.accessoryType = .checkmark
- } else {
- cell.accessoryType = .none
- }
- return cell
- }
- override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
- // uselect old
- if let cell = tableView.cellForRow(at: IndexPath(item: selectedIndex, section: 0)) {
- cell.accessoryType = .none
- }
- // select new
- if let cell = tableView.cellForRow(at: indexPath) {
- cell.accessoryType = .checkmark
- }
- selectedIndex = indexPath.row
- resetButton.isEnabled = true
- }
- @objc func resetButtonPressed() {
- selectedIndex = backupIndex
- tableView.reloadData()
- }
- }
- enum SecurityType {
- case IMAPSecurity
- case SMTPSecurity
- }
- enum SecurityValue: String {
- case AUTO = "Automatic"
- case TLS = "SSL / TLS"
- case STARTTLS = "STARTTLS"
- case PLAIN = "OFF"
- }
- class SecurityConverter {
- static func convertValueToInt(type: SecurityType, value: SecurityValue) -> Int {
- switch type {
- case .IMAPSecurity:
- switch value {
- case .AUTO:
- return 0x000
- case .STARTTLS:
- return 0x100
- case .TLS:
- return 0x200
- case .PLAIN:
- return 0x400
- }
- case .SMTPSecurity:
- switch value {
- case .AUTO:
- return 0x00000
- case .STARTTLS:
- return 0x10000
- case .TLS:
- return 0x20000
- case .PLAIN:
- return 0x40000
- }
- }
- }
- // TODO: discuss if we want to internationalize OFF and Automatic
- static func convertHexToString(type: SecurityType, hex value: Int) -> String {
- switch type {
- case .IMAPSecurity:
- switch value {
- case 0x00:
- return "Automatic"
- case 0x100:
- return "STARTTLS"
- case 0x200:
- return "SSL / TLS"
- case 0x400:
- return "OFF"
- default:
- return "Undefined"
- }
- case .SMTPSecurity:
- switch value {
- case 0x00000:
- return "Automatic"
- case 0x10000:
- return "STARTTLS"
- case 0x20000:
- return "SSL / TLS"
- case 0x40000:
- return "OFF"
- default:
- return "Undefined"
- }
- }
- }
- }
|