AddFriendTableViewController.swift 7.4 KB

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