SecuritySettingsController.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import UIKit
  2. class SecuritySettingsController: UITableViewController {
  3. private var options: [String]
  4. private var selectedIndex: Int {
  5. didSet {
  6. print(selectedIndex)
  7. }
  8. }
  9. private var backupIndex: Int
  10. var onDismiss: ((String) -> Void)?
  11. private var resetButton: UIBarButtonItem!
  12. private var staticCells: [UITableViewCell] {
  13. return options.map {
  14. let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
  15. cell.textLabel?.text = $0
  16. cell.selectionStyle = .none
  17. return cell
  18. }
  19. }
  20. init(title: String, options: [String], selectedOption: String) {
  21. self.options = options
  22. selectedIndex = options.index(of: selectedOption)!
  23. backupIndex = selectedIndex
  24. super.init(style: .grouped)
  25. self.title = title
  26. }
  27. required init?(coder aDecoder: NSCoder) {
  28. fatalError("init(coder:) has not been implemented")
  29. }
  30. override func viewDidLoad() {
  31. super.viewDidLoad()
  32. resetButton = UIBarButtonItem(title: String.localized("reset"), style: .done, target: self, action: #selector(resetButtonPressed))
  33. resetButton.isEnabled = false
  34. navigationItem.rightBarButtonItem = resetButton
  35. }
  36. override func viewWillDisappear(_ animated: Bool) {
  37. let selectedOption = options[selectedIndex]
  38. onDismiss?(selectedOption)
  39. }
  40. // MARK: - Table view data source
  41. override func numberOfSections(in tableView: UITableView) -> Int {
  42. return 1
  43. }
  44. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  45. return options.count
  46. }
  47. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  48. let cell = staticCells[indexPath.row]
  49. if selectedIndex == indexPath.row {
  50. cell.accessoryType = .checkmark
  51. } else {
  52. cell.accessoryType = .none
  53. }
  54. return cell
  55. }
  56. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  57. // uselect old
  58. if let cell = tableView.cellForRow(at: IndexPath(item: selectedIndex, section: 0)) {
  59. cell.accessoryType = .none
  60. }
  61. // select new
  62. if let cell = tableView.cellForRow(at: indexPath) {
  63. cell.accessoryType = .checkmark
  64. }
  65. selectedIndex = indexPath.row
  66. resetButton.isEnabled = true
  67. }
  68. @objc func resetButtonPressed() {
  69. selectedIndex = backupIndex
  70. tableView.reloadData()
  71. }
  72. }
  73. enum SecurityType {
  74. case IMAPSecurity
  75. case SMTPSecurity
  76. }
  77. enum SecurityValue: String {
  78. case AUTO = "Automatic"
  79. case TLS = "SSL / TLS"
  80. case STARTTLS = "STARTTLS"
  81. case PLAIN = "OFF"
  82. }
  83. class SecurityConverter {
  84. static func convertValueToInt(type: SecurityType, value: SecurityValue) -> Int {
  85. switch type {
  86. case .IMAPSecurity:
  87. switch value {
  88. case .AUTO:
  89. return 0x000
  90. case .STARTTLS:
  91. return 0x100
  92. case .TLS:
  93. return 0x200
  94. case .PLAIN:
  95. return 0x400
  96. }
  97. case .SMTPSecurity:
  98. switch value {
  99. case .AUTO:
  100. return 0x00000
  101. case .STARTTLS:
  102. return 0x10000
  103. case .TLS:
  104. return 0x20000
  105. case .PLAIN:
  106. return 0x40000
  107. }
  108. }
  109. }
  110. // TODO: discuss if we want to internationalize OFF and Automatic
  111. static func convertHexToString(type: SecurityType, hex value: Int) -> String {
  112. switch type {
  113. case .IMAPSecurity:
  114. switch value {
  115. case 0x00:
  116. return "Automatic"
  117. case 0x100:
  118. return "STARTTLS"
  119. case 0x200:
  120. return "SSL / TLS"
  121. case 0x400:
  122. return "OFF"
  123. default:
  124. return "Undefined"
  125. }
  126. case .SMTPSecurity:
  127. switch value {
  128. case 0x00000:
  129. return "Automatic"
  130. case 0x10000:
  131. return "STARTTLS"
  132. case 0x20000:
  133. return "SSL / TLS"
  134. case 0x40000:
  135. return "OFF"
  136. default:
  137. return "Undefined"
  138. }
  139. }
  140. }
  141. }