AddFriendTableViewController.swift 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //
  2. // AddFriendTableViewController.swift
  3. // Qmera
  4. //
  5. // Created by Yayan Dwi on 23/09/21.
  6. //
  7. import UIKit
  8. class AddFriendTableViewController: UITableViewController {
  9. var searchController: UISearchController!
  10. var data: [User] = []
  11. var fillteredData: [User] = []
  12. var isSearchBarEmpty: Bool {
  13. return searchController.searchBar.text?.isEmpty ?? true
  14. }
  15. var isFilltering: Bool {
  16. return searchController.isActive && !isSearchBarEmpty
  17. }
  18. var isDismiss: (() -> ())?
  19. func filterContentForSearchText(_ searchText: String) {
  20. fillteredData = data.filter{ $0.fullName.lowercased().contains(searchText.lowercased()) }
  21. getDataSearch(searchText: searchText) { data in
  22. let r = data.filter { $0.fullName.lowercased().contains(searchText.lowercased()) }
  23. self.fillteredData.append(contentsOf: r.filter { !self.fillteredData.contains($0) })
  24. DispatchQueue.main.async {
  25. self.tableView.reloadData()
  26. }
  27. }
  28. tableView.reloadData()
  29. }
  30. override func viewDidDisappear(_ animated: Bool) {
  31. isDismiss?()
  32. }
  33. override func viewDidLoad() {
  34. super.viewDidLoad()
  35. title = "Add Friends".localized()
  36. navigationController?.navigationBar.prefersLargeTitles = true
  37. navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(cancel(sender:)))
  38. searchController = UISearchController(searchResultsController: nil)
  39. searchController.delegate = self
  40. searchController.searchResultsUpdater = self
  41. searchController.searchBar.autocapitalizationType = .none
  42. searchController.searchBar.delegate = self
  43. searchController.obscuresBackgroundDuringPresentation = false
  44. definesPresentationContext = true
  45. navigationItem.searchController = searchController
  46. navigationItem.hidesSearchBarWhenScrolling = true
  47. tableView.tableFooterView = UIView()
  48. self.data.removeAll()
  49. getData { d in
  50. self.data = d
  51. DispatchQueue.main.async {
  52. self.tableView.reloadData()
  53. }
  54. }
  55. }
  56. @objc func cancel(sender: Any) {
  57. navigationController?.dismiss(animated: true, completion: nil)
  58. }
  59. // MARK: - Data source
  60. func getData(completion: @escaping ([User])->()) {
  61. DispatchQueue.global().async {
  62. if let response = Nexilis.writeSync(message: CoreMessage_TMessageBank.getPersonSuggestion(p_last_seq: "0")),
  63. response.isOk() {
  64. let data = response.getBody(key: CoreMessage_TMessageKey.DATA)
  65. guard !data.isEmpty else {
  66. return
  67. }
  68. if let jsonArray = try! JSONSerialization.jsonObject(with: data.data(using: .utf8)!, options: []) as? [[String: String?]] {
  69. var users = jsonArray.map { json in
  70. User(pin: (json[CoreMessage_TMessageKey.F_PIN] ?? "") ?? "",
  71. firstName: (json[CoreMessage_TMessageKey.FIRST_NAME] ?? "") ?? "",
  72. lastName: (json[CoreMessage_TMessageKey.LAST_NAME] ?? "") ?? "",
  73. thumb: (json[CoreMessage_TMessageKey.THUMB_ID] ?? "") ?? "")
  74. }
  75. users = users.filter({ $0.fullName != "USR\($0.pin)" })
  76. completion(users)
  77. }
  78. }
  79. }
  80. }
  81. func getDataSearch(searchText: String, completion: @escaping ([User]) -> ()) {
  82. DispatchQueue.global().async {
  83. if let response = Nexilis.writeSync(message: CoreMessage_TMessageBank.getSearchFriend(search_keyword: searchText, limit: "10")), response.isOk() {
  84. let data = response.getBody(key: CoreMessage_TMessageKey.DATA)
  85. guard !data.isEmpty else {
  86. return
  87. }
  88. if let jsonArray = try! JSONSerialization.jsonObject(with: data.data(using: .utf8)!, options: []) as? [[String: String?]] {
  89. var users = jsonArray.map { json in
  90. User(pin: (json[CoreMessage_TMessageKey.F_PIN] ?? "") ?? "",
  91. firstName: (json[CoreMessage_TMessageKey.FIRST_NAME] ?? "") ?? "",
  92. lastName: (json[CoreMessage_TMessageKey.LAST_NAME] ?? "") ?? "",
  93. thumb: (json[CoreMessage_TMessageKey.THUMB_ID] ?? "") ?? "")
  94. }
  95. users = users.filter({ $0.fullName != "USR\($0.pin)" })
  96. completion(users)
  97. }
  98. }
  99. }
  100. }
  101. // MARK: - Table view data source
  102. override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  103. return "Suggestions"
  104. }
  105. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  106. let user: User
  107. if isFilltering {
  108. user = fillteredData[indexPath.row]
  109. } else {
  110. user = data[indexPath.row]
  111. }
  112. let controller = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "profileView") as! ProfileViewController
  113. controller.flag = .invite
  114. controller.data = user.pin
  115. controller.name = user.fullName
  116. controller.picture = user.thumb
  117. controller.isDismiss = {
  118. self.getData { d in
  119. self.data = d
  120. DispatchQueue.main.async {
  121. self.tableView.reloadData()
  122. }
  123. }
  124. }
  125. navigationController?.show(controller, sender: nil)
  126. }
  127. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  128. if isFilltering {
  129. return fillteredData.count
  130. }
  131. return data.count
  132. }
  133. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  134. let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
  135. var content = cell.defaultContentConfiguration()
  136. let user: User
  137. if isFilltering {
  138. user = fillteredData[indexPath.row]
  139. } else {
  140. user = data[indexPath.row]
  141. }
  142. content.imageProperties.maximumSize = CGSize(width: 40, height: 40)
  143. getImage(name: user.thumb, placeholderImage: UIImage(named: "Profile---Purple", in: Bundle.resourceBundle(for: Nexilis.self), with: nil), isCircle: true, tableView: tableView, indexPath: indexPath, completion: { result, isDownloaded, image in
  144. content.image = image
  145. })
  146. content.text = user.fullName
  147. content.textProperties.font = UIFont.systemFont(ofSize: 14)
  148. cell.contentConfiguration = content
  149. return cell
  150. }
  151. }
  152. // MARK: - Extension
  153. extension AddFriendTableViewController: UISearchControllerDelegate, UISearchBarDelegate, UISearchResultsUpdating {
  154. func updateSearchResults(for searchController: UISearchController) {
  155. filterContentForSearchText(searchController.searchBar.text!)
  156. }
  157. }