SettingsAutodelOverviewController.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import UIKit
  2. class SettingsAutodelOverviewController: UITableViewController {
  3. var dcContext: DcContext
  4. private struct SectionConfigs {
  5. let headerTitle: String?
  6. let footerTitle: String?
  7. let cells: [UITableViewCell]
  8. }
  9. private enum CellTags: Int {
  10. case autodelDevice = 0
  11. case autodelServer = 1
  12. }
  13. func autodelSummary() -> String {
  14. return String.localized("off")
  15. }
  16. private lazy var autodelDeviceCell: UITableViewCell = {
  17. let cell = UITableViewCell(style: .value1, reuseIdentifier: nil)
  18. cell.tag = CellTags.autodelDevice.rawValue
  19. cell.textLabel?.text = String.localized("autodel_device_title")
  20. cell.accessoryType = .disclosureIndicator
  21. cell.detailTextLabel?.text = SettingsAutodelSetController.getSummary(dcContext, fromServer: false)
  22. return cell
  23. }()
  24. private lazy var autodelServerCell: UITableViewCell = {
  25. let cell = UITableViewCell(style: .value1, reuseIdentifier: nil)
  26. cell.tag = CellTags.autodelServer.rawValue
  27. cell.textLabel?.text = String.localized("autodel_server_title")
  28. cell.accessoryType = .disclosureIndicator
  29. cell.detailTextLabel?.text = SettingsAutodelSetController.getSummary(dcContext, fromServer: true)
  30. return cell
  31. }()
  32. private lazy var sections: [SectionConfigs] = {
  33. let autodelSection = SectionConfigs(
  34. headerTitle: nil,
  35. footerTitle: nil,
  36. cells: [autodelDeviceCell, autodelServerCell]
  37. )
  38. return [autodelSection]
  39. }()
  40. init(dcContext: DcContext) {
  41. self.dcContext = dcContext
  42. super.init(style: .grouped)
  43. self.title = String.localized("autodel_title")
  44. hidesBottomBarWhenPushed = true
  45. }
  46. required init?(coder aDecoder: NSCoder) {
  47. fatalError("init(coder:) has not been implemented")
  48. }
  49. override func viewDidLoad() {
  50. super.viewDidLoad()
  51. }
  52. override func viewWillAppear(_ animated: Bool) {
  53. super.viewWillAppear(animated)
  54. autodelDeviceCell.detailTextLabel?.text = SettingsAutodelSetController.getSummary(dcContext, fromServer: false)
  55. autodelServerCell.detailTextLabel?.text = SettingsAutodelSetController.getSummary(dcContext, fromServer: true)
  56. }
  57. // MARK: - Table view data source
  58. override func numberOfSections(in tableView: UITableView) -> Int {
  59. return sections.count
  60. }
  61. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  62. return sections[section].cells.count
  63. }
  64. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  65. return sections[indexPath.section].cells[indexPath.row]
  66. }
  67. override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  68. return sections[section].headerTitle
  69. }
  70. override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
  71. return sections[section].footerTitle
  72. }
  73. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  74. guard let cell = tableView.cellForRow(at: indexPath), let cellTag = CellTags(rawValue: cell.tag) else {
  75. safe_fatalError()
  76. return
  77. }
  78. tableView.deselectRow(at: indexPath, animated: false) // to achieve highlight effect
  79. switch cellTag {
  80. case .autodelDevice:
  81. let controller = SettingsAutodelSetController(dcContext: dcContext, fromServer: false)
  82. navigationController?.pushViewController(controller, animated: true)
  83. case .autodelServer:
  84. let controller = SettingsAutodelSetController(dcContext: dcContext, fromServer: true)
  85. navigationController?.pushViewController(controller, animated: true)
  86. }
  87. }
  88. }