EditGroupViewController.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import UIKit
  2. import DcCore
  3. class EditGroupViewController: UITableViewController, MediaPickerDelegate {
  4. private let dcContext: DcContext
  5. private let chat: DcChat
  6. private var groupImage: UIImage?
  7. private let rowGroupName = 0
  8. private let rowAvatar = 1
  9. var avatarSelectionCell: AvatarSelectionCell
  10. private lazy var mediaPicker: MediaPicker? = {
  11. let mediaPicker = MediaPicker(navigationController: navigationController)
  12. mediaPicker.delegate = self
  13. return mediaPicker
  14. }()
  15. lazy var groupNameCell: TextFieldCell = {
  16. let cell = TextFieldCell(description: String.localized("group_name"), placeholder: self.chat.name)
  17. cell.setText(text: self.chat.name)
  18. cell.onTextFieldChange = self.groupNameEdited(_:)
  19. return cell
  20. }()
  21. lazy var doneButton: UIBarButtonItem = {
  22. let button = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(saveContactButtonPressed))
  23. button.isEnabled = false
  24. return button
  25. }()
  26. lazy var cancelButton: UIBarButtonItem = {
  27. let button = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(cancelButtonPressed))
  28. return button
  29. }()
  30. init(dcContext: DcContext, chat: DcChat) {
  31. self.dcContext = dcContext
  32. self.chat = chat
  33. self.avatarSelectionCell = AvatarSelectionCell(image: chat.profileImage)
  34. super.init(style: .grouped)
  35. self.avatarSelectionCell.hintLabel.text = String.localized("group_avatar")
  36. self.avatarSelectionCell.onAvatarTapped = onAvatarTapped
  37. title = String.localized("menu_edit_group")
  38. }
  39. required init?(coder aDecoder: NSCoder) {
  40. fatalError("init(coder:) has not been implemented")
  41. }
  42. override func viewDidLoad() {
  43. super.viewDidLoad()
  44. navigationItem.rightBarButtonItem = doneButton
  45. navigationItem.leftBarButtonItem = cancelButton
  46. tableView.rowHeight = UITableView.automaticDimension
  47. }
  48. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  49. if indexPath.row == rowAvatar {
  50. return avatarSelectionCell
  51. } else {
  52. return groupNameCell
  53. }
  54. }
  55. override func numberOfSections(in tableView: UITableView) -> Int {
  56. return 1
  57. }
  58. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  59. return 2
  60. }
  61. @objc func saveContactButtonPressed() {
  62. let newName = groupNameCell.getText()
  63. if let groupImage = groupImage {
  64. AvatarHelper.saveChatAvatar(dcContext: dcContext, image: groupImage, for: Int(chat.id))
  65. }
  66. _ = dcContext.setChatName(chatId: chat.id, name: newName ?? "")
  67. navigationController?.popViewController(animated: true)
  68. }
  69. @objc func cancelButtonPressed() {
  70. navigationController?.popViewController(animated: true)
  71. }
  72. private func groupNameEdited(_ textField: UITextField) {
  73. doneButton.isEnabled = true
  74. }
  75. private func onAvatarTapped() {
  76. let alert = UIAlertController(title: String.localized("group_avatar"), message: nil, preferredStyle: .safeActionSheet)
  77. let cameraAction = PhotoPickerAlertAction(title: String.localized("camera"), style: .default, handler: cameraButtonPressed(_:))
  78. let galleryAction = PhotoPickerAlertAction(title: String.localized("gallery"), style: .default, handler: galleryButtonPressed(_:))
  79. alert.addAction(cameraAction)
  80. alert.addAction(galleryAction)
  81. alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
  82. self.present(alert, animated: true, completion: nil)
  83. }
  84. private func galleryButtonPressed(_ action: UIAlertAction) {
  85. mediaPicker?.showPhotoGallery()
  86. }
  87. private func cameraButtonPressed(_ action: UIAlertAction) {
  88. mediaPicker?.showCamera(allowCropping: true, supportedMediaTypes: .photo)
  89. }
  90. func onImageSelected(image: UIImage) {
  91. groupImage = image
  92. doneButton.isEnabled = true
  93. avatarSelectionCell = AvatarSelectionCell(image: groupImage)
  94. avatarSelectionCell.hintLabel.text = String.localized("group_avatar")
  95. avatarSelectionCell.onAvatarTapped = onAvatarTapped
  96. self.tableView.beginUpdates()
  97. let indexPath = IndexPath(row: rowAvatar, section: 0)
  98. self.tableView.reloadRows(at: [indexPath], with: UITableView.RowAnimation.none)
  99. self.tableView.endUpdates()
  100. }
  101. }