GroupMembersViewController.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. import UIKit
  2. class NewGroupViewController: GroupMembersViewController {
  3. weak var coordinator: NewGroupCoordinator?
  4. override func viewDidLoad() {
  5. super.viewDidLoad()
  6. title = String.localized("menu_new_group")
  7. let groupCreationNextButton = UIBarButtonItem(title: String.localized("next"),
  8. style: .done,
  9. target: self,
  10. action: #selector(nextButtonPressed))
  11. navigationItem.rightBarButtonItem = groupCreationNextButton
  12. contactIds = Utils.getContactIds()
  13. }
  14. override func viewWillAppear(_ animated: Bool) {
  15. super.viewWillAppear(animated)
  16. }
  17. override func didReceiveMemoryWarning() {
  18. super.didReceiveMemoryWarning()
  19. }
  20. @objc func nextButtonPressed() {
  21. coordinator?.showGroupNameController(contactIdsForGroup: selectedContactIds)
  22. }
  23. }
  24. class AddGroupMembersViewController: GroupMembersViewController {
  25. weak var coordinator: AddGroupMembersCoordinator?
  26. private var chatId: Int?
  27. private let sectionNewContact = 0
  28. private let sectionMemberList = 1
  29. private var contactAddedObserver: NSObjectProtocol?
  30. private lazy var cancelButton: UIBarButtonItem = {
  31. let button = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(cancelButtonPressed))
  32. return button
  33. }()
  34. lazy var doneButton: UIBarButtonItem = {
  35. let button = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(doneButtonPressed))
  36. return button
  37. }()
  38. private lazy var chat: DcChat? = {
  39. if let chatId = chatId {
  40. return DcChat(id: chatId)
  41. }
  42. return nil
  43. }()
  44. private lazy var chatMemberIds: [Int] = {
  45. if let chat = chat {
  46. return chat.contactIds
  47. }
  48. return []
  49. }()
  50. init(chatId: Int) {
  51. super.init()
  52. self.chatId = chatId
  53. numberOfSections = 2
  54. }
  55. required init?(coder _: NSCoder) {
  56. fatalError("init(coder:) has not been implemented")
  57. }
  58. override func viewDidLoad() {
  59. super.viewDidLoad()
  60. super.navigationItem.leftBarButtonItem = cancelButton
  61. super.navigationItem.rightBarButtonItem = doneButton
  62. title = String.localized("group_add_members")
  63. super.contactIds = loadMemberCandidates()
  64. // Do any additional setup after loading the view.
  65. let nc = NotificationCenter.default
  66. contactAddedObserver = nc.addObserver(
  67. forName: dcNotificationContactChanged,
  68. object: nil,
  69. queue: nil
  70. ) { notification in
  71. if let ui = notification.userInfo {
  72. if let contactId = ui["contact_id"] as? Int {
  73. if contactId == 0 {
  74. return
  75. }
  76. self.contactIds = self.loadMemberCandidates()
  77. if self.contactIds.contains(contactId) {
  78. self.selectedContactIds.insert(contactId)
  79. self.tableView.reloadData()
  80. }
  81. }
  82. }
  83. }
  84. }
  85. override func viewWillDisappear(_: Bool) {
  86. if !isMovingFromParent {
  87. // a subview was added to the navigation stack, no action needed
  88. return
  89. }
  90. let nc = NotificationCenter.default
  91. if let observer = self.contactAddedObserver {
  92. nc.removeObserver(observer)
  93. }
  94. }
  95. override func tableView(_: UITableView, numberOfRowsInSection section: Int) -> Int {
  96. switch section {
  97. case sectionNewContact:
  98. return 1
  99. case sectionMemberList:
  100. return getNumberOfRowsForContactList()
  101. default:
  102. return 0
  103. }
  104. }
  105. override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  106. switch indexPath.section {
  107. case sectionNewContact:
  108. return Constants.defaultCellHeight
  109. case sectionMemberList:
  110. return ContactCell.cellHeight
  111. default:
  112. return Constants.defaultCellHeight
  113. }
  114. }
  115. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  116. switch indexPath.section {
  117. case sectionNewContact:
  118. return getNewContactCell()
  119. case sectionMemberList:
  120. return getContactCell(cellForRowAt: indexPath)
  121. default:
  122. return UITableViewCell(style: .default, reuseIdentifier: nil)
  123. }
  124. }
  125. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  126. switch indexPath.section {
  127. case sectionNewContact:
  128. tableView.deselectRow(at: indexPath, animated: true)
  129. coordinator?.showNewContactController()
  130. case sectionMemberList:
  131. didSelectContactCell(at: indexPath)
  132. default:
  133. fatalError("unexpected section selected in GroupMembersViewController")
  134. }
  135. }
  136. func loadMemberCandidates() -> [Int] {
  137. var contactIds = Utils.getContactIds()
  138. let memberSet = Set(chatMemberIds)
  139. contactIds.removeAll(where: { memberSet.contains($0)})
  140. return Array(contactIds)
  141. }
  142. @objc func cancelButtonPressed() {
  143. navigationController?.popViewController(animated: true)
  144. }
  145. @objc func doneButtonPressed() {
  146. guard let chatId = chatId else {
  147. return
  148. }
  149. for contactId in selectedContactIds {
  150. dc_add_contact_to_chat(mailboxPointer, UInt32(chatId), UInt32(contactId))
  151. }
  152. navigationController?.popViewController(animated: true)
  153. }
  154. func getNewContactCell() -> UITableViewCell {
  155. let cell: UITableViewCell
  156. if let c = tableView.dequeueReusableCell(withIdentifier: "actionCell") {
  157. cell = c
  158. } else {
  159. cell = UITableViewCell(style: .default, reuseIdentifier: "actionCell")
  160. }
  161. cell.textLabel?.text = String.localized("menu_new_contact")
  162. cell.textLabel?.textColor = view.tintColor
  163. cell.textLabel?.textAlignment = .center
  164. return cell
  165. }
  166. }
  167. class BlockedContactsViewController: GroupMembersViewController, GroupMemberSelectionDelegate {
  168. override init() {
  169. super.init()
  170. enableCheckmarks = false
  171. }
  172. required init?(coder _: NSCoder) {
  173. fatalError("init(coder:) has not been implemented")
  174. }
  175. override func viewDidLoad() {
  176. super.viewDidLoad()
  177. title = String.localized("pref_blocked_contacts")
  178. contactIds = Utils.getBlockedContactIds()
  179. selectedContactIds = Set(contactIds)
  180. navigationItem.searchController = nil
  181. groupMemberSelectionDelegate = self
  182. }
  183. override func viewWillAppear(_ animated: Bool) {
  184. super.viewWillAppear(animated)
  185. }
  186. func selected(contactId: Int, selected: Bool) {
  187. if !selected {
  188. let dcContact = DcContact(id: contactId)
  189. let title = dcContact.displayName.isEmpty ? dcContact.email : dcContact.displayName
  190. let alert = UIAlertController(title: title, message: String.localized("ask_unblock_contact"), preferredStyle: .actionSheet)
  191. alert.addAction(UIAlertAction(title: String.localized("menu_unblock_contact"), style: .default, handler: { _ in
  192. let contact = DcContact(id: contactId)
  193. contact.unblock()
  194. self.contactIds = Utils.getBlockedContactIds()
  195. self.selectedContactIds = Set(self.contactIds)
  196. self.tableView.reloadData()
  197. }))
  198. alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: { _ in
  199. self.selectedContactIds = Set(self.contactIds)
  200. self.tableView.reloadData()
  201. }))
  202. present(alert, animated: true, completion: nil)
  203. }
  204. }
  205. }
  206. protocol GroupMemberSelectionDelegate: class {
  207. func selected(contactId: Int, selected: Bool)
  208. }
  209. class GroupMembersViewController: UITableViewController, UISearchResultsUpdating {
  210. let contactCellReuseIdentifier = "contactCell"
  211. weak var groupMemberSelectionDelegate: GroupMemberSelectionDelegate?
  212. var enableCheckmarks = true
  213. var numberOfSections = 1
  214. var contactIds: [Int] = [] {
  215. didSet {
  216. tableView.reloadData()
  217. }
  218. }
  219. // contactWithSearchResults.indexesToHightLight empty by default
  220. var contacts: [ContactWithSearchResults] {
  221. return contactIds.map { ContactWithSearchResults(contact: DcContact(id: $0), indexesToHighlight: []) }
  222. }
  223. // used when seachbar is active
  224. var filteredContacts: [ContactWithSearchResults] = []
  225. // searchBar active?
  226. func isFiltering() -> Bool {
  227. return searchController.isActive && !searchBarIsEmpty()
  228. }
  229. private func searchBarIsEmpty() -> Bool {
  230. return searchController.searchBar.text?.isEmpty ?? true
  231. }
  232. private func contactIdByRow(_ row: Int) -> Int {
  233. return isFiltering() ? filteredContacts[row].contact.id : contactIds[row]
  234. }
  235. private func contactSearchResultByRow(_ row: Int) -> ContactWithSearchResults {
  236. return isFiltering() ? filteredContacts[row] : contacts[row]
  237. }
  238. private lazy var searchController: UISearchController = {
  239. let searchController = UISearchController(searchResultsController: nil)
  240. searchController.searchResultsUpdater = self
  241. searchController.obscuresBackgroundDuringPresentation = false
  242. searchController.searchBar.placeholder = String.localized("search")
  243. searchController.hidesNavigationBarDuringPresentation = false
  244. return searchController
  245. }()
  246. var selectedContactIds: Set<Int> = []
  247. init() {
  248. super.init(style: .grouped)
  249. hidesBottomBarWhenPushed = true
  250. }
  251. required init?(coder _: NSCoder) {
  252. fatalError("init(coder:) has not been implemented")
  253. }
  254. override func viewDidLoad() {
  255. tableView.register(ContactCell.self, forCellReuseIdentifier: contactCellReuseIdentifier)
  256. navigationItem.searchController = searchController
  257. if #available(iOS 11.0, *) {
  258. navigationItem.hidesSearchBarWhenScrolling = false
  259. }
  260. definesPresentationContext = true
  261. }
  262. override func numberOfSections(in _: UITableView) -> Int {
  263. return numberOfSections
  264. }
  265. override func tableView(_: UITableView, numberOfRowsInSection _: Int) -> Int {
  266. return getNumberOfRowsForContactList()
  267. }
  268. override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  269. return ContactCell.cellHeight
  270. }
  271. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  272. return getContactCell(cellForRowAt: indexPath)
  273. }
  274. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  275. didSelectContactCell(at: indexPath)
  276. }
  277. func getNumberOfRowsForContactList() -> Int {
  278. return isFiltering() ? filteredContacts.count : contacts.count
  279. }
  280. func getContactCell(cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  281. guard let cell: ContactCell = tableView.dequeueReusableCell(withIdentifier: contactCellReuseIdentifier, for: indexPath) as? ContactCell else {
  282. fatalError("shouldn't happen")
  283. }
  284. let row = indexPath.row
  285. let contact: ContactWithSearchResults = contactSearchResultByRow(row)
  286. updateContactCell(cell: cell, contactWithHighlight: contact)
  287. cell.accessoryType = selectedContactIds.contains(contactIdByRow(row)) && enableCheckmarks ? .checkmark : .none
  288. return cell
  289. }
  290. func didSelectContactCell(at indexPath: IndexPath) {
  291. let row = indexPath.row
  292. if let cell = tableView.cellForRow(at: indexPath) {
  293. tableView.deselectRow(at: indexPath, animated: true)
  294. let contactId = contactIdByRow(row)
  295. if selectedContactIds.contains(contactId) {
  296. selectedContactIds.remove(contactId)
  297. if enableCheckmarks {
  298. cell.accessoryType = .none
  299. }
  300. groupMemberSelectionDelegate?.selected(contactId: contactId, selected: false)
  301. } else {
  302. selectedContactIds.insert(contactId)
  303. if enableCheckmarks {
  304. cell.accessoryType = .checkmark
  305. }
  306. groupMemberSelectionDelegate?.selected(contactId: contactId, selected: true)
  307. }
  308. }
  309. }
  310. func updateSearchResults(for searchController: UISearchController) {
  311. if let searchText = searchController.searchBar.text {
  312. filterContentForSearchText(searchText)
  313. }
  314. }
  315. private func filterContentForSearchText(_ searchText: String, scope _: String = String.localized("pref_show_emails_all")) {
  316. let contactsWithHighlights: [ContactWithSearchResults] = contacts.map { contact in
  317. let indexes = contact.contact.containsExact(searchText: searchText)
  318. return ContactWithSearchResults(contact: contact.contact, indexesToHighlight: indexes)
  319. }
  320. filteredContacts = contactsWithHighlights.filter { !$0.indexesToHighlight.isEmpty }
  321. tableView.reloadData()
  322. }
  323. private func updateContactCell(cell: ContactCell, contactWithHighlight: ContactWithSearchResults) {
  324. let contact = contactWithHighlight.contact
  325. let displayName = contact.displayName
  326. let emailLabelFontSize = cell.emailLabel.font.pointSize
  327. let nameLabelFontSize = cell.nameLabel.font.pointSize
  328. cell.nameLabel.text = displayName
  329. cell.emailLabel.text = contact.email
  330. cell.avatar.setName(displayName)
  331. cell.avatar.setColor(contact.color)
  332. if let profileImage = contact.profileImage {
  333. cell.avatar.setImage(profileImage)
  334. }
  335. cell.setVerified(isVerified: contact.isVerified)
  336. if let emailHighlightedIndexes = contactWithHighlight.indexesToHighlight.filter({ $0.contactDetail == .EMAIL }).first {
  337. // gets here when contact is a result of current search -> highlights relevant indexes
  338. cell.emailLabel.attributedText = contact.email.boldAt(indexes: emailHighlightedIndexes.indexes, fontSize: emailLabelFontSize)
  339. } else {
  340. cell.emailLabel.attributedText = contact.email.boldAt(indexes: [], fontSize: emailLabelFontSize)
  341. }
  342. if let nameHighlightedIndexes = contactWithHighlight.indexesToHighlight.filter({ $0.contactDetail == .NAME }).first {
  343. cell.nameLabel.attributedText = displayName.boldAt(indexes: nameHighlightedIndexes.indexes, fontSize: nameLabelFontSize)
  344. } else {
  345. cell.nameLabel.attributedText = displayName.boldAt(indexes: [], fontSize: nameLabelFontSize)
  346. }
  347. }
  348. }