ProfileInfoViewController.swift 4.9 KB

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