ContactsSearchResultTableViewContoller.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //
  2. // SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  3. // SPDX-License-Identifier: GPL-3.0-or-later
  4. //
  5. import UIKit
  6. @objcMembers class ContactsSearchResultTableViewController: UITableViewController {
  7. var indexes: [String] = []
  8. var contacts: [String: [NCUser]] = [:]
  9. let tableBackgroundView = PlaceholderView(for: .insetGrouped)!
  10. override init(style: UITableView.Style) {
  11. super.init(style: style)
  12. }
  13. required init?(coder: NSCoder) {
  14. fatalError("init(coder:) has not been implemented")
  15. }
  16. override func viewDidLoad() {
  17. super.viewDidLoad()
  18. self.tableView.register(UINib(nibName: kContactsTableCellNibName, bundle: nil), forCellReuseIdentifier: kContactCellIdentifier)
  19. tableBackgroundView.setImage(UIImage(named: "contacts-placeholder"))
  20. tableBackgroundView.placeholderTextView.text = NSLocalizedString("No results found", comment: "")
  21. self.showSearchingUI()
  22. self.tableView.backgroundView = tableBackgroundView
  23. }
  24. // MARK: - UI
  25. func showSearchingUI() {
  26. tableBackgroundView.placeholderView.isHidden = true
  27. tableBackgroundView.loadingView.startAnimating()
  28. tableBackgroundView.loadingView.isHidden = false
  29. }
  30. func hideSearchingUI() {
  31. tableBackgroundView.loadingView.stopAnimating()
  32. tableBackgroundView.loadingView.isHidden = true
  33. }
  34. func setContacts(_ contacts:[String: [NCUser]], indexes:[String]) {
  35. self.contacts = contacts
  36. self.indexes = indexes
  37. self.tableView.reloadData()
  38. }
  39. func setSearchResultContacts(_ contacts:[String: [NCUser]], indexes:[String]) {
  40. self.hideSearchingUI()
  41. self.tableBackgroundView.placeholderView.isHidden = !contacts.isEmpty
  42. self.setContacts(contacts, indexes: indexes)
  43. }
  44. // MARK: - TableView
  45. override func numberOfSections(in tableView: UITableView) -> Int {
  46. return indexes.count
  47. }
  48. override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  49. return indexes[section]
  50. }
  51. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  52. let index = indexes[section]
  53. return contacts[index]?.count ?? 0
  54. }
  55. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  56. let index = indexes[indexPath.section]
  57. let contactsForIndex = contacts[index]
  58. guard let contact = contactsForIndex?[indexPath.row] else {
  59. return UITableViewCell()
  60. }
  61. let contactCell = tableView.dequeueReusableCell(withIdentifier: kContactCellIdentifier) as? ContactsTableViewCell ??
  62. ContactsTableViewCell(style: .default, reuseIdentifier: kContactCellIdentifier)
  63. contactCell.labelTitle.text = contact.name
  64. let contactType = contact.source as String
  65. contactCell.contactImage.setActorAvatar(forId: contact.userId, withType: contactType, withDisplayName: contact.name, withRoomToken: nil)
  66. return contactCell
  67. }
  68. }