AddFriendTableViewController.swift 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. getData { d in
  49. self.data = d
  50. DispatchQueue.main.async {
  51. self.tableView.reloadData()
  52. }
  53. }
  54. }
  55. @objc func cancel(sender: Any) {
  56. navigationController?.dismiss(animated: true, completion: nil)
  57. }
  58. // MARK: - Data source
  59. func getData(completion: @escaping ([User])->()) {
  60. DispatchQueue.global().async {
  61. if let response = Nexilis.writeSync(message: CoreMessage_TMessageBank.getPersonSuggestion(p_last_seq: "0")),
  62. response.isOk() {
  63. let data = response.getBody(key: CoreMessage_TMessageKey.DATA)
  64. guard !data.isEmpty else {
  65. return
  66. }
  67. if let jsonArray = try! JSONSerialization.jsonObject(with: data.data(using: .utf8)!, options: []) as? [[String: String?]] {
  68. var users = jsonArray.map { json in
  69. User(pin: (json[CoreMessage_TMessageKey.F_PIN] ?? "") ?? "",
  70. firstName: (json[CoreMessage_TMessageKey.FIRST_NAME] ?? "") ?? "",
  71. lastName: (json[CoreMessage_TMessageKey.LAST_NAME] ?? "") ?? "",
  72. thumb: (json[CoreMessage_TMessageKey.THUMB_ID] ?? "") ?? "")
  73. }
  74. users = users.filter({ $0.fullName != "USR\($0.pin)" })
  75. completion(users)
  76. }
  77. }
  78. }
  79. }
  80. func getDataSearch(searchText: String, completion: @escaping ([User]) -> ()) {
  81. DispatchQueue.global().async {
  82. if let response = Nexilis.writeSync(message: CoreMessage_TMessageBank.getSearchFriend(search_keyword: searchText, limit: "10")), response.isOk() {
  83. let data = response.getBody(key: CoreMessage_TMessageKey.DATA)
  84. guard !data.isEmpty else {
  85. return
  86. }
  87. if let jsonArray = try! JSONSerialization.jsonObject(with: data.data(using: .utf8)!, options: []) as? [[String: String?]] {
  88. var users = jsonArray.map { json in
  89. User(pin: (json[CoreMessage_TMessageKey.F_PIN] ?? "") ?? "",
  90. firstName: (json[CoreMessage_TMessageKey.FIRST_NAME] ?? "") ?? "",
  91. lastName: (json[CoreMessage_TMessageKey.LAST_NAME] ?? "") ?? "",
  92. thumb: (json[CoreMessage_TMessageKey.THUMB_ID] ?? "") ?? "")
  93. }
  94. users = users.filter({ $0.fullName != "USR\($0.pin)" })
  95. completion(users)
  96. }
  97. }
  98. }
  99. }
  100. // MARK: - Table view data source
  101. override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  102. return "Suggestions"
  103. }
  104. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  105. let user: User
  106. if isFilltering {
  107. user = fillteredData[indexPath.row]
  108. } else {
  109. user = data[indexPath.row]
  110. }
  111. let controller = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "profileView") as! ProfileViewController
  112. controller.flag = .invite
  113. controller.data = user.pin
  114. controller.name = user.fullName
  115. controller.picture = user.thumb
  116. controller.isDismiss = {
  117. self.getData { d in
  118. self.data = d
  119. DispatchQueue.main.async {
  120. self.tableView.reloadData()
  121. }
  122. }
  123. }
  124. navigationController?.show(controller, sender: nil)
  125. }
  126. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  127. if isFilltering {
  128. return fillteredData.count
  129. }
  130. return data.count
  131. }
  132. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  133. let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
  134. var content = cell.defaultContentConfiguration()
  135. let user: User
  136. if isFilltering {
  137. user = fillteredData[indexPath.row]
  138. } else {
  139. user = data[indexPath.row]
  140. }
  141. content.imageProperties.maximumSize = CGSize(width: 40, height: 40)
  142. 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
  143. content.image = image
  144. })
  145. content.text = user.fullName
  146. content.textProperties.font = UIFont.systemFont(ofSize: 14)
  147. cell.contentConfiguration = content
  148. return cell
  149. }
  150. }
  151. // MARK: - Extension
  152. extension AddFriendTableViewController: UISearchControllerDelegate, UISearchBarDelegate, UISearchResultsUpdating {
  153. func updateSearchResults(for searchController: UISearchController) {
  154. filterContentForSearchText(searchController.searchBar.text!)
  155. }
  156. }