AddGroupMembersViewController.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import UIKit
  2. import DcCore
  3. class AddGroupMembersViewController: GroupMembersViewController {
  4. var onMembersSelected: ((Set<Int>) -> Void)?
  5. lazy var isVerifiedGroup: Bool = false
  6. lazy var isNewGroup: Bool = {
  7. return chat == nil
  8. }()
  9. private lazy var sections: [AddGroupMemberSections] = {
  10. if isVerifiedGroup {
  11. return [.memberList]
  12. } else {
  13. return [.newContact, .memberList]
  14. }
  15. }()
  16. enum AddGroupMemberSections {
  17. case newContact
  18. case memberList
  19. }
  20. private lazy var chatMemberIds: [Int] = {
  21. if let chat = chat {
  22. return chat.contactIds
  23. }
  24. return []
  25. }()
  26. private lazy var chat: DcChat? = {
  27. if let chatId = self.chatId {
  28. return dcContext.getChat(chatId: chatId)
  29. }
  30. return nil
  31. }()
  32. private var chatId: Int?
  33. private lazy var newContactCell: ActionCell = {
  34. let cell = ActionCell()
  35. cell.actionColor = SystemColor.blue.uiColor
  36. cell.actionTitle = String.localized("menu_new_contact")
  37. return cell
  38. }()
  39. private lazy var cancelButton: UIBarButtonItem = {
  40. let button = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(cancelButtonPressed))
  41. return button
  42. }()
  43. lazy var doneButton: UIBarButtonItem = {
  44. let button = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(doneButtonPressed))
  45. return button
  46. }()
  47. // add members of new group, no chat object yet
  48. init(preselected: Set<Int>, isVerified: Bool) {
  49. super.init()
  50. isVerifiedGroup = isVerified
  51. numberOfSections = sections.count
  52. selectedContactIds = preselected
  53. }
  54. // add members of existing group
  55. init(chatId: Int) {
  56. self.chatId = chatId
  57. super.init()
  58. isVerifiedGroup = chat?.isProtected ?? false
  59. numberOfSections = sections.count
  60. selectedContactIds = Set(dcContext.getChat(chatId: chatId).contactIds)
  61. }
  62. required init?(coder _: NSCoder) {
  63. fatalError("init(coder:) has not been implemented")
  64. }
  65. // MARK: - lifecycle
  66. override func viewDidLoad() {
  67. super.viewDidLoad()
  68. title = String.localized("group_add_members")
  69. navigationItem.rightBarButtonItem = doneButton
  70. navigationItem.leftBarButtonItem = cancelButton
  71. contactIds = loadMemberCandidates()
  72. }
  73. @objc func cancelButtonPressed() {
  74. navigationController?.popViewController(animated: true)
  75. }
  76. @objc func doneButtonPressed() {
  77. if let onMembersSelected = onMembersSelected {
  78. if isNewGroup {
  79. selectedContactIds.insert(Int(DC_CONTACT_ID_SELF))
  80. }
  81. onMembersSelected(selectedContactIds)
  82. }
  83. navigationController?.popViewController(animated: true)
  84. }
  85. override func tableView(_: UITableView, numberOfRowsInSection section: Int) -> Int {
  86. let sectionType = sections[section]
  87. switch sectionType {
  88. case .newContact:
  89. return 1
  90. case .memberList:
  91. return numberOfRowsForContactList
  92. }
  93. }
  94. override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  95. let sectionType = sections[indexPath.section]
  96. return sectionType == .memberList ? ContactCell.cellHeight : UITableView.automaticDimension
  97. }
  98. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  99. let sectionType = sections[indexPath.section]
  100. switch sectionType {
  101. case .newContact:
  102. return newContactCell
  103. case .memberList:
  104. return updateContactCell(for: indexPath)
  105. }
  106. }
  107. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  108. let sectionType = sections[indexPath.section]
  109. switch sectionType {
  110. case .newContact:
  111. tableView.deselectRow(at: indexPath, animated: false)
  112. showNewContactController()
  113. case .memberList:
  114. didSelectContactCell(at: indexPath)
  115. }
  116. }
  117. func loadMemberCandidates() -> [Int] {
  118. var flags: Int32 = 0
  119. if isVerifiedGroup {
  120. flags |= DC_GCL_VERIFIED_ONLY
  121. }
  122. return dcContext.getContacts(flags: flags)
  123. }
  124. private func showNewContactController() {
  125. let newContactController = NewContactController(dcContext: dcContext, searchResult: searchText)
  126. newContactController.createChatOnSave = false
  127. newContactController.onContactSaved = { [weak self] (contactId: Int) -> Void in
  128. guard let self = self else { return }
  129. self.contactIds = self.loadMemberCandidates()
  130. if self.contactIds.contains(contactId) {
  131. self.selectedContactIds.insert(contactId)
  132. self.tableView.reloadData()
  133. }
  134. }
  135. navigationController?.pushViewController(newContactController, animated: true)
  136. }
  137. // MARK: - search
  138. override open func filterContactIds(flags: Int32, queryString: String) -> [Int] {
  139. let flags = self.isVerifiedGroup ? DC_GCL_VERIFIED_ONLY : DC_GCL_ADD_SELF
  140. return dcContext.getContacts(flags: flags, queryString: queryString)
  141. }
  142. }