EditSettingsController.swift 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import UIKit
  2. import DcCore
  3. class EditSettingsController: UITableViewController, MediaPickerDelegate {
  4. private let dcContext: DcContext
  5. private let dcAccounts: DcAccounts
  6. private let section1 = 0
  7. private let section1Name = 0
  8. private let section1Avatar = 1
  9. private let section1Status = 2
  10. private let section1RowCount = 3
  11. private let section2 = 1
  12. private let section2AccountSettings = 0
  13. private let section2RowCount = 1
  14. private let sectionCount = 2
  15. private let tagAccountSettingsCell = 1
  16. private lazy var mediaPicker: MediaPicker? = {
  17. let mediaPicker = MediaPicker(navigationController: navigationController)
  18. mediaPicker.delegate = self
  19. return mediaPicker
  20. }()
  21. private lazy var statusCell: MultilineTextFieldCell = {
  22. let cell = MultilineTextFieldCell(description: String.localized("pref_default_status_label"),
  23. multilineText: dcContext.selfstatus,
  24. placeholder: String.localized("pref_default_status_label"))
  25. return cell
  26. }()
  27. private lazy var accountSettingsCell: UITableViewCell = {
  28. let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
  29. cell.textLabel?.text = String.localized("pref_password_and_account_settings")
  30. cell.accessoryType = .disclosureIndicator
  31. cell.tag = tagAccountSettingsCell
  32. return cell
  33. }()
  34. private lazy var avatarSelectionCell: AvatarSelectionCell = {
  35. return AvatarSelectionCell(image: dcContext.getSelfAvatarImage())
  36. }()
  37. private lazy var nameCell: TextFieldCell = {
  38. let cell = TextFieldCell(description: String.localized("pref_your_name"), placeholder: String.localized("pref_your_name"))
  39. cell.setText(text: dcContext.displayname)
  40. cell.textFieldDelegate = self
  41. cell.textField.returnKeyType = .default
  42. return cell
  43. }()
  44. init(dcAccounts: DcAccounts) {
  45. self.dcAccounts = dcAccounts
  46. self.dcContext = dcAccounts.get()
  47. super.init(style: .grouped)
  48. hidesBottomBarWhenPushed = true
  49. }
  50. required init?(coder aDecoder: NSCoder) {
  51. fatalError("init(coder:) has not been implemented")
  52. }
  53. override func viewDidLoad() {
  54. super.viewDidLoad()
  55. title = String.localized("pref_profile_info_headline")
  56. avatarSelectionCell.onAvatarTapped = { [weak self] in
  57. self?.onAvatarTapped()
  58. }
  59. tableView.rowHeight = UITableView.automaticDimension
  60. }
  61. override func viewWillDisappear(_ animated: Bool) {
  62. dcContext.selfstatus = statusCell.getText()
  63. dcContext.displayname = nameCell.getText()
  64. }
  65. // MARK: - Table view data source
  66. override func numberOfSections(in tableView: UITableView) -> Int {
  67. return sectionCount
  68. }
  69. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  70. if section == section1 {
  71. return section1RowCount
  72. } else {
  73. return section2RowCount
  74. }
  75. }
  76. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  77. if indexPath.section == section1 {
  78. switch indexPath.row {
  79. case section1Avatar:
  80. return avatarSelectionCell
  81. case section1Name:
  82. return nameCell
  83. case section1Status:
  84. return statusCell
  85. default:
  86. return UITableViewCell()
  87. }
  88. } else {
  89. return accountSettingsCell
  90. }
  91. }
  92. override func tableView(_: UITableView, titleForFooterInSection section: Int) -> String? {
  93. if section == section1 {
  94. return String.localized("pref_who_can_see_profile_explain")
  95. } else {
  96. return nil
  97. }
  98. }
  99. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  100. guard let cell = tableView.cellForRow(at: indexPath) else { return }
  101. if cell.tag == tagAccountSettingsCell {
  102. tableView.deselectRow(at: indexPath, animated: false)
  103. guard let nc = navigationController else { return }
  104. let accountSetupVC = AccountSetupController(dcAccounts: dcAccounts, editView: true)
  105. nc.pushViewController(accountSetupVC, animated: true)
  106. }
  107. }
  108. // MARK: - actions
  109. private func galleryButtonPressed(_ action: UIAlertAction) {
  110. mediaPicker?.showPhotoGallery()
  111. }
  112. private func cameraButtonPressed(_ action: UIAlertAction) {
  113. mediaPicker?.showCamera(allowCropping: true, supportedMediaTypes: .photo)
  114. }
  115. private func deleteProfileIconPressed(_ action: UIAlertAction) {
  116. dcContext.selfavatar = nil
  117. updateAvatarCell()
  118. }
  119. private func onAvatarTapped() {
  120. let alert = UIAlertController(title: String.localized("pref_profile_photo"), message: nil, preferredStyle: .safeActionSheet)
  121. alert.addAction(PhotoPickerAlertAction(title: String.localized("camera"), style: .default, handler: cameraButtonPressed(_:)))
  122. alert.addAction(PhotoPickerAlertAction(title: String.localized("gallery"), style: .default, handler: galleryButtonPressed(_:)))
  123. if dcContext.getSelfAvatarImage() != nil {
  124. alert.addAction(UIAlertAction(title: String.localized("delete"), style: .destructive, handler: deleteProfileIconPressed(_:)))
  125. }
  126. alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
  127. self.present(alert, animated: true, completion: nil)
  128. }
  129. func onImageSelected(image: UIImage) {
  130. AvatarHelper.saveSelfAvatarImage(dcContext: dcContext, image: image)
  131. updateAvatarCell()
  132. }
  133. private func updateAvatarCell() {
  134. self.avatarSelectionCell.setAvatar(image: dcContext.getSelfAvatarImage())
  135. }
  136. }
  137. extension EditSettingsController: UITextFieldDelegate {
  138. func textFieldShouldReturn(_ textField: UITextField) -> Bool {
  139. textField.resignFirstResponder()
  140. return true
  141. }
  142. }