ProfileInfoViewController.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import UIKit
  2. import DcCore
  3. class ProfileInfoViewController: UITableViewController {
  4. weak var coordinator: EditSettingsCoordinator?
  5. private let dcContext: DcContext
  6. private var displayName: String?
  7. private lazy var doneButtonItem: UIBarButtonItem = {
  8. return UIBarButtonItem(
  9. title: String.localized("done"),
  10. style: .done,
  11. target: self,
  12. action: #selector(doneButtonPressed(_:))
  13. )
  14. }()
  15. private lazy var headerCell: TextCell = {
  16. let cell = TextCell(style: .default, reuseIdentifier: nil)
  17. let email = dcContext.addr ?? ""
  18. cell.content = String.localizedStringWithFormat(NSLocalizedString("qraccount_success_enter_name", comment: ""), email)
  19. return cell
  20. }()
  21. private lazy var emailCell: TextFieldCell = {
  22. let cell = TextFieldCell(description: "Meine Email Adresse", placeholder: "Meine Email")
  23. cell.setText(text: dcContext.addr)
  24. return cell
  25. }()
  26. private lazy var avatarCell: AvatarSelectionCell = {
  27. let cell = AvatarSelectionCell(context: self.dcContext)
  28. cell.onAvatarTapped = avatarTapped
  29. return cell
  30. }()
  31. private lazy var nameCell: TextFieldCell = {
  32. let cell = TextFieldCell.makeNameCell()
  33. cell.placeholder = String.localized("pref_your_name")
  34. cell.setText(text: dcContext.displayname)
  35. cell.onTextFieldChange = {[unowned self] textField in
  36. self.displayName = textField.text
  37. }
  38. return cell
  39. }()
  40. private lazy var cells = [headerCell, avatarCell, nameCell]
  41. init(context: DcContext) {
  42. self.dcContext = context
  43. super.init(style: .grouped)
  44. tableView.estimatedRowHeight = Constants.defaultCellHeight
  45. }
  46. required init?(coder: NSCoder) {
  47. fatalError("init(coder:) has not been implemented")
  48. }
  49. // MARK: - lifecycle
  50. override func viewDidLoad() {
  51. super.viewDidLoad()
  52. title = String.localized("pref_profile_info_headline")
  53. navigationItem.rightBarButtonItem = doneButtonItem
  54. }
  55. // MARK: - tableviewDelegate
  56. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  57. return cells.count
  58. }
  59. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  60. return cells[indexPath.row]
  61. }
  62. override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  63. let cell = cells[indexPath.row]
  64. if let textCell = cell as? TextCell {
  65. return textCell.intrinsicCellHeight
  66. }
  67. if cell is AvatarSelectionCell {
  68. return AvatarSelectionCell.cellHeight
  69. }
  70. return Constants.defaultCellHeight
  71. }
  72. // MARK: - updates
  73. private func updateAvatarCell() {
  74. if let avatarImage = dcContext.getSelfAvatarImage() {
  75. avatarCell.updateAvatar(image: avatarImage)
  76. }
  77. self.tableView.beginUpdates()
  78. let indexPath = IndexPath(row: 1, section: 0)
  79. self.tableView.reloadRows(at: [indexPath], with: UITableView.RowAnimation.none)
  80. self.tableView.endUpdates()
  81. }
  82. // MARK: - actions
  83. private func avatarTapped() {
  84. let sender = avatarCell.badge
  85. let alert = UIAlertController(
  86. title: String.localized("pref_profile_photo"),
  87. message: nil,
  88. preferredStyle: .actionSheet
  89. )
  90. let photoAction = PhotoPickerAlertAction(
  91. title: String.localized("gallery"),
  92. style: .default,
  93. handler: galleryButtonPressed(_:)
  94. )
  95. let videoAction = PhotoPickerAlertAction(
  96. title: String.localized("camera"),
  97. style: .default,
  98. handler: cameraButtonPressed(_:)
  99. )
  100. alert.addAction(photoAction)
  101. alert.addAction(videoAction)
  102. alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
  103. if let popoverController = alert.popoverPresentationController {
  104. popoverController.sourceView = self.avatarCell
  105. popoverController.sourceRect = CGRect(
  106. x: sender.frame.minX - 10,
  107. y: sender.frame.minY,
  108. width: sender.frame.width,
  109. height: sender.frame.height
  110. )
  111. }
  112. self.present(alert, animated: true, completion: nil)
  113. }
  114. @objc private func doneButtonPressed(_ sender: UIBarButtonItem) {
  115. dcContext.displayname = displayName
  116. self.dismiss(animated: true, completion: nil)
  117. }
  118. private func galleryButtonPressed(_ action: UIAlertAction) {
  119. coordinator?.showPhotoPicker(delegate: self)
  120. }
  121. private func cameraButtonPressed(_ action: UIAlertAction) {
  122. coordinator?.showCamera(delegate: self)
  123. }
  124. }
  125. extension ProfileInfoViewController: MediaPickerDelegate {
  126. func onImageSelected(image: UIImage) {
  127. AvatarHelper.saveSelfAvatarImage(dcContext: dcContext, image: image)
  128. updateAvatarCell()
  129. }
  130. }