SettingsAutodelOverviewController.swift 3.8 KB

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