EditSettingsController.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import UIKit
  2. class EditSettingsController: UITableViewController {
  3. private let dcContext: DcContext
  4. private var displayNameBackup: String?
  5. private var statusCellBackup: String?
  6. private let section1 = 0
  7. private let section1Name = 0
  8. private let section1Status = 1
  9. private let section1RowCount = 2
  10. private let section2 = 1
  11. private let section2AccountSettings = 0
  12. private let section2RowCount = 1
  13. private let sectionCount = 2
  14. private var childCoordinators: Coordinator?
  15. private lazy var displayNameCell: TextFieldCell = {
  16. let cell = TextFieldCell(description: String.localized("pref_your_name"), placeholder: String.localized("pref_your_name"))
  17. cell.setText(text: DcConfig.displayname ?? nil)
  18. return cell
  19. }()
  20. private lazy var statusCell: TextFieldCell = {
  21. let cell = TextFieldCell(description: String.localized("pref_default_status_label"), placeholder: String.localized("pref_default_status_label"))
  22. cell.setText(text: DcConfig.selfstatus ?? nil)
  23. return cell
  24. }()
  25. lazy var accountSettingsCell: UITableViewCell = {
  26. let cell = UITableViewCell(style: .value1, reuseIdentifier: nil)
  27. cell.textLabel?.text = String.localized("pref_password_and_account_settings")
  28. cell.accessoryType = .disclosureIndicator
  29. cell.accessibilityIdentifier = "accountSettingsCell"
  30. return cell
  31. }()
  32. init(dcContext: DcContext) {
  33. self.dcContext = dcContext
  34. super.init(style: .grouped)
  35. hidesBottomBarWhenPushed = true
  36. }
  37. required init?(coder aDecoder: NSCoder) {
  38. fatalError("init(coder:) has not been implemented")
  39. }
  40. override func viewDidLoad() {
  41. super.viewDidLoad()
  42. title = String.localized("pref_profile_info_headline")
  43. }
  44. override func viewWillAppear(_ animated: Bool) {
  45. displayNameBackup = DcConfig.displayname
  46. statusCellBackup = DcConfig.selfstatus
  47. }
  48. override func viewWillDisappear(_ animated: Bool) {
  49. if displayNameBackup != displayNameCell.getText() || statusCellBackup != displayNameCell.getText() {
  50. DcConfig.selfstatus = statusCell.getText()
  51. DcConfig.displayname = displayNameCell.getText()
  52. dc_configure(mailboxPointer)
  53. }
  54. }
  55. // MARK: - Table view data source
  56. override func numberOfSections(in tableView: UITableView) -> Int {
  57. return sectionCount
  58. }
  59. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  60. if section == section1 {
  61. return section1RowCount
  62. } else {
  63. return section2RowCount
  64. }
  65. }
  66. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  67. if indexPath.section == section1 {
  68. if indexPath.row == section1Name {
  69. return displayNameCell
  70. } else {
  71. return statusCell
  72. }
  73. } else {
  74. return accountSettingsCell
  75. }
  76. }
  77. override func tableView(_: UITableView, titleForFooterInSection section: Int) -> String? {
  78. if section == section1 {
  79. return String.localized("pref_who_can_see_profile_explain")
  80. } else {
  81. return nil
  82. }
  83. }
  84. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  85. guard let cell = tableView.cellForRow(at: indexPath) else { return }
  86. if cell.accessibilityIdentifier == "accountSettingsCell" {
  87. tableView.deselectRow(at: indexPath, animated: true)
  88. guard let nc = navigationController else { return }
  89. let accountSetupVC = AccountSetupController(dcContext: dcContext, editView: true)
  90. let coordinator = AccountSetupCoordinator(dcContext: dcContext, navigationController: nc)
  91. self.childCoordinators = coordinator
  92. accountSetupVC.coordinator = coordinator
  93. nc.pushViewController(accountSetupVC, animated: true)
  94. }
  95. }
  96. }