EditSettingsController.swift 3.9 KB

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