EditSettingsController.swift 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import UIKit
  2. class EditSettingsController: UITableViewController, MediaPickerDelegate {
  3. private let dcContext: DcContext
  4. weak var coordinator: EditSettingsCoordinator?
  5. private var displayNameBackup: String?
  6. private var statusCellBackup: String?
  7. private let groupBadgeSize: CGFloat = 72
  8. private let section1 = 0
  9. private let section1Avatar = 0
  10. private let section1Name = 1
  11. private let section1Status = 2
  12. private let section1RowCount = 3
  13. private let section2 = 1
  14. private let section2AccountSettings = 0
  15. private let section2RowCount = 1
  16. private let sectionCount = 2
  17. private let tagAccountSettingsCell = 1
  18. private var childCoordinators: Coordinator?
  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. private 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.tag = tagAccountSettingsCell
  29. return cell
  30. }()
  31. private lazy var avatarSelectionCell: AvatarSelectionCell = {
  32. return createPictureAndNameCell()
  33. }()
  34. private lazy var nameCell: TextFieldCell = {
  35. let cell = TextFieldCell(description: String.localized("pref_your_name"), placeholder: String.localized("pref_your_name"))
  36. cell.setText(text: DcConfig.displayname)
  37. return cell
  38. }()
  39. init(dcContext: DcContext) {
  40. self.dcContext = dcContext
  41. super.init(style: .grouped)
  42. hidesBottomBarWhenPushed = true
  43. }
  44. required init?(coder aDecoder: NSCoder) {
  45. fatalError("init(coder:) has not been implemented")
  46. }
  47. override func viewDidLoad() {
  48. super.viewDidLoad()
  49. title = String.localized("pref_profile_info_headline")
  50. avatarSelectionCell.onAvatarTapped = onAvatarTapped
  51. }
  52. override func viewWillDisappear(_ animated: Bool) {
  53. DcConfig.selfstatus = statusCell.getText()
  54. DcConfig.displayname = nameCell.getText()
  55. dc_configure(mailboxPointer)
  56. }
  57. // MARK: - Table view data source
  58. override func numberOfSections(in tableView: UITableView) -> Int {
  59. return sectionCount
  60. }
  61. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  62. if section == section1 {
  63. return section1RowCount
  64. } else {
  65. return section2RowCount
  66. }
  67. }
  68. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  69. if indexPath.section == section1 {
  70. switch indexPath.row {
  71. case section1Avatar:
  72. return avatarSelectionCell
  73. case section1Name:
  74. return nameCell
  75. case section1Status:
  76. return statusCell
  77. default:
  78. return UITableViewCell()
  79. }
  80. } else {
  81. return accountSettingsCell
  82. }
  83. }
  84. override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  85. if indexPath.section == section1 && indexPath.row == section1Avatar {
  86. return AvatarSelectionCell.cellSize
  87. } else {
  88. return Constants.defaultCellHeight
  89. }
  90. }
  91. override func tableView(_: UITableView, titleForFooterInSection section: Int) -> String? {
  92. if section == section1 {
  93. return String.localized("pref_who_can_see_profile_explain")
  94. } else {
  95. return nil
  96. }
  97. }
  98. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  99. guard let cell = tableView.cellForRow(at: indexPath) else { return }
  100. if cell.tag == tagAccountSettingsCell {
  101. tableView.deselectRow(at: indexPath, animated: true)
  102. guard let nc = navigationController else { return }
  103. let accountSetupVC = AccountSetupController(dcContext: dcContext, editView: true)
  104. let coordinator = AccountSetupCoordinator(dcContext: dcContext, navigationController: nc)
  105. self.childCoordinators = coordinator
  106. accountSetupVC.coordinator = coordinator
  107. nc.pushViewController(accountSetupVC, animated: true)
  108. }
  109. }
  110. private func galleryButtonPressed(_ action: UIAlertAction) {
  111. coordinator?.showPhotoPicker(delegate: self)
  112. }
  113. private func cameraButtonPressed(_ action: UIAlertAction) {
  114. coordinator?.showCamera(delegate: self)
  115. }
  116. private func onAvatarTapped() {
  117. let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
  118. let photoAction = PhotoPickerAlertAction(title: String.localized("gallery"), style: .default, handler: galleryButtonPressed(_:))
  119. let videoAction = PhotoPickerAlertAction(title: String.localized("camera"), style: .default, handler: cameraButtonPressed(_:))
  120. alert.addAction(photoAction)
  121. alert.addAction(videoAction)
  122. alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
  123. self.present(alert, animated: true, completion: nil)
  124. }
  125. func onImageSelected(image: UIImage) {
  126. AvatarHelper.saveSelfAvatarImage(image: image)
  127. self.avatarSelectionCell = createPictureAndNameCell()
  128. self.avatarSelectionCell.onAvatarTapped = onAvatarTapped
  129. self.tableView.beginUpdates()
  130. let indexPath = IndexPath(row: section1Avatar, section: section1)
  131. self.tableView.reloadRows(at: [indexPath], with: UITableView.RowAnimation.none)
  132. self.tableView.endUpdates()
  133. }
  134. private func createPictureAndNameCell() -> AvatarSelectionCell {
  135. let cell = AvatarSelectionCell(context: dcContext)
  136. return cell
  137. }
  138. }