EditSettingsController.swift 6.8 KB

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