EditGroupViewController.swift 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import UIKit
  2. class EditGroupViewController: UITableViewController {
  3. weak var coordinator: EditGroupCoordinator?
  4. private let chat: DcChat
  5. lazy var groupNameCell: GroupLabelCell = {
  6. let cell = GroupLabelCell(style: .default, reuseIdentifier: nil)
  7. cell.onTextChanged = groupNameEdited(_:)
  8. return cell
  9. }()
  10. lazy var doneButton: UIBarButtonItem = {
  11. let button = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(saveContactButtonPressed))
  12. button.isEnabled = false
  13. return button
  14. }()
  15. lazy var cancelButton: UIBarButtonItem = {
  16. let button = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(cancelButtonPressed))
  17. return button
  18. }()
  19. init(chat: DcChat) {
  20. self.chat = chat
  21. super.init(style: .grouped)
  22. groupNameCell.inputField.text = chat.name
  23. groupNameCell.groupBadge.setName(chat.name)
  24. groupNameCell.groupBadge.setColor(chat.color)
  25. }
  26. required init?(coder aDecoder: NSCoder) {
  27. fatalError("init(coder:) has not been implemented")
  28. }
  29. override func viewDidLoad() {
  30. super.viewDidLoad()
  31. navigationItem.rightBarButtonItem = doneButton
  32. navigationItem.leftBarButtonItem = cancelButton
  33. }
  34. override func viewWillAppear(_ animated: Bool) {
  35. NavBarUtils.setSmallTitle(navigationController: navigationController)
  36. }
  37. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  38. return groupNameCell
  39. }
  40. override func numberOfSections(in tableView: UITableView) -> Int {
  41. return 1
  42. }
  43. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  44. return 1
  45. }
  46. @objc func saveContactButtonPressed() {
  47. let newName = groupNameCell.getGroupName()
  48. dc_set_chat_name(mailboxPointer, UInt32(chat.id), newName)
  49. coordinator?.navigateBack()
  50. }
  51. @objc func cancelButtonPressed() {
  52. coordinator?.navigateBack()
  53. }
  54. private func groupNameEdited(_ newName: String) {
  55. doneButton.isEnabled = true
  56. }
  57. }