PortSettingsController.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import UIKit
  2. class PortSettingsController: UITableViewController {
  3. var ports: [Int]
  4. private var sectionTitle: String?
  5. var resetButton: UIBarButtonItem!
  6. var onDismiss: ((String) -> Void)?
  7. var currentPort: Int {
  8. didSet {
  9. // activate resetButton once something was changed
  10. resetButton.isEnabled = true
  11. }
  12. }
  13. var selectedIndex: Int?
  14. let backupValue: Int
  15. var staticCells: [UITableViewCell] {
  16. return ports.map({
  17. let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
  18. cell.textLabel?.text = "\($0)"
  19. cell.selectionStyle = .none
  20. return cell
  21. })
  22. }
  23. lazy var customCell: TextFieldCell = {
  24. let cell = TextFieldCell(description: "", placeholder: "\(self.currentPort)")
  25. cell.selectionStyle = .none
  26. cell.textLabel?.text = nil
  27. cell.textField.keyboardType = .numberPad
  28. cell.onTextFieldChange = textFieldDidChange
  29. return cell
  30. }()
  31. init(sectionTitle: String?, ports: [Int], currentPort: Int?) {
  32. self.ports = ports
  33. self.sectionTitle = sectionTitle
  34. self.currentPort = currentPort ?? 0
  35. for (index, port) in ports.enumerated() where currentPort == port {
  36. selectedIndex = index
  37. }
  38. backupValue = self.currentPort
  39. super.init(style: .grouped)
  40. self.title = sectionTitle
  41. }
  42. required init?(coder aDecoder: NSCoder) {
  43. fatalError("init(coder:) has not been implemented")
  44. }
  45. override func viewDidLoad() {
  46. super.viewDidLoad()
  47. resetButton = UIBarButtonItem(title: String.localized("reset"), style: .plain, target: self, action: #selector(resetButtonPressed))
  48. navigationItem.rightBarButtonItem = resetButton
  49. resetButton.isEnabled = false
  50. }
  51. override func viewWillDisappear(_ animated: Bool) {
  52. onDismiss?("\(currentPort)")
  53. }
  54. // MARK: - Table view data source
  55. override func numberOfSections(in tableView: UITableView) -> Int {
  56. return 2
  57. }
  58. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  59. if section == 0 {
  60. return ports.count
  61. } else {
  62. return 1
  63. }
  64. }
  65. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  66. if indexPath.section == 0 {
  67. let row = indexPath.row
  68. selectItem(at: row)
  69. }
  70. }
  71. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  72. let section = indexPath.section
  73. let row = indexPath.row
  74. if section == 0 {
  75. let cell = staticCells[row]
  76. if row == selectedIndex || cell.textLabel?.text == "\(currentPort)" {
  77. cell.accessoryType = .checkmark
  78. } else {
  79. cell.accessoryType = .none
  80. }
  81. return cell
  82. } else {
  83. // section == 1
  84. return customCell
  85. }
  86. }
  87. override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  88. if section == 0 {
  89. return sectionTitle
  90. } else {
  91. return String.localized("custom_port")
  92. }
  93. }
  94. private func textFieldDidChange(_ textField: UITextField) {
  95. selectItem(at: nil)
  96. if let text = textField.text, let port = Int(text) {
  97. self.currentPort = port
  98. }
  99. }
  100. private func selectItem(at index: Int? ) {
  101. // unselect old cell
  102. // select new cell
  103. // update port
  104. if let oldIndex = selectedIndex {
  105. let cell = tableView.cellForRow(at: IndexPath.init(row: oldIndex, section: 0))
  106. cell?.accessoryType = .none
  107. }
  108. if let newIndex = index {
  109. // activate accesoryType on selected cell
  110. let cell = tableView.cellForRow(at: IndexPath.init(row: newIndex, section: 0))
  111. cell?.accessoryType = .checkmark
  112. currentPort = ports[newIndex]
  113. // update customCell
  114. customCell.textField.placeholder = "\(currentPort)"
  115. customCell.textField.resignFirstResponder()
  116. customCell.textField.text = nil // will display currentValue as placeholder
  117. }
  118. selectedIndex = index
  119. }
  120. @objc private func resetButtonPressed() {
  121. if let index = ports.index(of: backupValue) {
  122. selectItem(at: index)
  123. } else {
  124. selectItem(at: nil)
  125. }
  126. self.currentPort = backupValue
  127. customCell.textField.placeholder = "\(currentPort)"
  128. customCell.textField.resignFirstResponder()
  129. customCell.textField.text = nil // will display currentValue as placeholder
  130. tableView.reloadData()
  131. }
  132. }