PortSettingsController.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import UIKit
  2. class PortSettingsController: UITableViewController {
  3. var ports: [Int]
  4. private var sectionTitle: String?
  5. var onSave: ((String) -> Void)?
  6. var okButton: UIBarButtonItem {
  7. let button = UIBarButtonItem(title: String.localized("ok"), style: .done, target: self, action: #selector(okButtonPressed))
  8. return button
  9. }
  10. var cancelButton: UIBarButtonItem {
  11. let button = UIBarButtonItem(title: String.localized("cancel"), style: .plain, target: self, action: #selector(cancelButtonPressed))
  12. return button
  13. }
  14. var currentPort: Int
  15. var selectedIndex: Int?
  16. var staticCells: [UITableViewCell] {
  17. return ports.map({
  18. let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
  19. cell.textLabel?.text = "\($0)"
  20. cell.selectionStyle = .none
  21. return cell
  22. })
  23. }
  24. lazy var customCell: TextFieldCell = {
  25. let cell = TextFieldCell(description: "", placeholder: "\(self.currentPort)")
  26. cell.selectionStyle = .none
  27. cell.textLabel?.text = nil
  28. cell.textField.keyboardType = .numberPad
  29. cell.onTextFieldChange = textFieldDidChange
  30. return cell
  31. }()
  32. init(sectionTitle: String?, ports: [Int], currentPort: Int?) {
  33. self.ports = ports
  34. self.sectionTitle = sectionTitle
  35. self.currentPort = currentPort ?? 0
  36. for (index, port) in ports.enumerated() where currentPort == port {
  37. selectedIndex = index
  38. }
  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. navigationItem.rightBarButtonItem = okButton
  48. navigationItem.leftBarButtonItem = cancelButton
  49. }
  50. // MARK: - Table view data source
  51. override func numberOfSections(in tableView: UITableView) -> Int {
  52. return 2
  53. }
  54. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  55. if section == 0 {
  56. return ports.count
  57. } else {
  58. return 1
  59. }
  60. }
  61. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  62. if indexPath.section == 0 {
  63. let row = indexPath.row
  64. selectItem(at: row)
  65. }
  66. }
  67. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  68. let section = indexPath.section
  69. let row = indexPath.row
  70. if section == 0 {
  71. let cell = staticCells[row]
  72. if row == selectedIndex || cell.textLabel?.text == "\(currentPort)" {
  73. cell.accessoryType = .checkmark
  74. } else {
  75. cell.accessoryType = .none
  76. }
  77. return cell
  78. } else {
  79. // section == 1
  80. return customCell
  81. }
  82. }
  83. override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  84. if section == 0 {
  85. return sectionTitle
  86. } else {
  87. return String.localized("custom_port")
  88. }
  89. }
  90. private func textFieldDidChange(_ textField: UITextField) {
  91. selectItem(at: nil)
  92. if let text = textField.text, let port = Int(text) {
  93. self.currentPort = port
  94. }
  95. }
  96. private func selectItem(at index: Int? ) {
  97. // unselect old cell
  98. // select new cell
  99. // update port
  100. if let oldIndex = selectedIndex {
  101. let cell = tableView.cellForRow(at: IndexPath.init(row: oldIndex, section: 0))
  102. cell?.accessoryType = .none
  103. }
  104. if let newIndex = index {
  105. // activate accesoryType on selected cell
  106. let cell = tableView.cellForRow(at: IndexPath.init(row: newIndex, section: 0))
  107. cell?.accessoryType = .checkmark
  108. currentPort = ports[newIndex]
  109. // update customCell
  110. customCell.textField.placeholder = "\(currentPort)"
  111. customCell.textField.resignFirstResponder()
  112. customCell.textField.text = nil // will display currentValue as placeholder
  113. }
  114. selectedIndex = index
  115. }
  116. @objc private func okButtonPressed() {
  117. onSave?("\(currentPort)")
  118. navigationController?.popViewController(animated: true)
  119. }
  120. @objc private func cancelButtonPressed() {
  121. navigationController?.popViewController(animated: true)
  122. }
  123. }