QmeraCallContactViewController.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. //
  2. // QmeraCallContactViewController.swift
  3. // Qmera
  4. //
  5. // Created by Yayan Dwi on 12/10/21.
  6. //
  7. import UIKit
  8. class QmeraCallContactViewController: UITableViewController {
  9. private let searchController: UISearchController = {
  10. let searchController = UISearchController(searchResultsController: nil)
  11. searchController.searchBar.autocapitalizationType = .none
  12. searchController.obscuresBackgroundDuringPresentation = false
  13. searchController.searchBar.updateHeight(height: 32, radius: 20)
  14. searchController.searchBar.searchTextField.attributedPlaceholder = NSAttributedString(string: "Search chats & messages".localized(), attributes: [NSAttributedString.Key.foregroundColor: UIColor.gray, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16)])
  15. return searchController
  16. }()
  17. private var users: [User] = []
  18. private var fillteredUser: [User] = []
  19. private var isSearchBarEmpty: Bool {
  20. return searchController.searchBar.text!.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
  21. }
  22. private var isFilltering: Bool {
  23. return searchController.isActive && !isSearchBarEmpty
  24. }
  25. var isDismiss: ((User) -> ())?
  26. var selectedUser: [User] = []
  27. var isInviteCC = false
  28. var listFriends = false
  29. override func viewDidLoad() {
  30. super.viewDidLoad()
  31. title = "Friends".localized()
  32. if !isInviteCC {
  33. navigationController?.navigationBar.prefersLargeTitles = true
  34. } else {
  35. let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
  36. navigationController?.navigationBar.titleTextAttributes = textAttributes
  37. navigationController?.navigationBar.barTintColor = self.traitCollection.userInterfaceStyle == .dark ? .blackDarkMode : .mainColor
  38. }
  39. tableView.register(UITableViewCell.self, forCellReuseIdentifier: "reuseIdentifier")
  40. definesPresentationContext = true
  41. if !listFriends {
  42. let buttonAddFriend = UIBarButtonItem(image: UIImage(systemName: "plus", withConfiguration: UIImage.SymbolConfiguration(pointSize: 18, weight: .regular, scale: .default)), style: .plain, target: self, action: #selector(addFriend(sender:)))
  43. navigationItem.rightBarButtonItem = buttonAddFriend
  44. }
  45. searchController.delegate = self
  46. searchController.searchResultsUpdater = self
  47. searchController.searchBar.delegate = self
  48. navigationItem.searchController = searchController
  49. navigationItem.hidesSearchBarWhenScrolling = true
  50. getContacts { users in
  51. self.users.append(contentsOf: users)
  52. DispatchQueue.main.async {
  53. self.tableView.reloadData()
  54. }
  55. }
  56. }
  57. @objc func addFriend(sender: UIBarButtonItem) {
  58. let controller = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "addFriendNav") as! UINavigationController
  59. Utils.addBackground(view: controller.view)
  60. if let vc = controller.viewControllers.first as? AddFriendTableViewController {
  61. vc.isDismiss = {
  62. self.users.removeAll()
  63. self.getContacts { users in
  64. self.users.append(contentsOf: users)
  65. DispatchQueue.main.async {
  66. self.tableView.reloadData()
  67. }
  68. }
  69. }
  70. }
  71. self.navigationController?.present(controller, animated: true, completion: nil)
  72. }
  73. private func getContacts(completion: @escaping ([User]) -> ()) {
  74. var contacts: [User] = []
  75. DispatchQueue.global().async {
  76. var query = "SELECT f_pin, first_name, last_name, image_id, user_type, official_account, ex_offmp FROM BUDDY where f_pin <> '\(UserDefaults.standard.string(forKey: "me")!)' and official_account<>'1' order by 2 collate nocase asc"
  77. if self.listFriends {
  78. query = "SELECT f_pin, first_name, last_name, image_id, user_type, official_account, ex_offmp FROM BUDDY where f_pin <> '\(UserDefaults.standard.string(forKey: "me")!)' order by 2 collate nocase asc"
  79. }
  80. Database.shared.database?.inTransaction({ (fmdb, rollback) in
  81. if let cursor = Database.shared.getRecords(fmdb: fmdb, query: query) {
  82. while cursor.next() {
  83. let user = User(pin: cursor.string(forColumnIndex: 0) ?? "",
  84. firstName: cursor.string(forColumnIndex: 1) ?? "",
  85. lastName: cursor.string(forColumnIndex: 2) ?? "",
  86. thumb: cursor.string(forColumnIndex: 3) ?? "",
  87. userType: cursor.string(forColumnIndex: 4) ?? "",
  88. official: cursor.string(forColumnIndex: 5) ?? "",
  89. ex_offmp: cursor.string(forColumnIndex: 6) ?? "")
  90. if (user.firstName + " " + user.lastName).trimmingCharacters(in: .whitespaces) == "USR\(user.pin)" {
  91. continue
  92. }
  93. contacts.append(user)
  94. }
  95. cursor.close()
  96. }
  97. })
  98. var dataContacts = contacts.filter { !self.selectedUser.contains($0) }
  99. if self.listFriends {
  100. dataContacts.sort(by: { Int($0.official!)! > Int($1.official!)! })
  101. }
  102. completion(dataContacts)
  103. }
  104. }
  105. private func filterContentForSearchText(_ searchText: String) {
  106. fillteredUser = users.filter({ d in
  107. let name = d.fullName.trimmingCharacters(in: .whitespaces)
  108. return name.lowercased().contains(searchText.lowercased())
  109. })
  110. tableView.reloadData()
  111. }
  112. // MARK: - Table view data source
  113. override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  114. return 54
  115. }
  116. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  117. let user: User
  118. if isFilltering {
  119. user = fillteredUser[indexPath.row]
  120. } else {
  121. user = users[indexPath.row]
  122. }
  123. if !listFriends {
  124. tableView.deselectRow(at: indexPath, animated: false)
  125. user.isSelected = true
  126. tableView.reloadRows(at: [indexPath], with: .none)
  127. }
  128. if isInviteCC {
  129. if !listFriends {
  130. self.isDismiss?(user)
  131. self.navigationController?.popViewController(animated: true)
  132. } else {
  133. let controller = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "profileView") as! ProfileViewController
  134. controller.data = user.pin
  135. controller.flag = .friend
  136. controller.isBNI = true
  137. controller.isDismiss = {
  138. self.getContacts { users in
  139. self.users.removeAll()
  140. self.users.append(contentsOf: users)
  141. DispatchQueue.main.async {
  142. self.tableView.reloadData()
  143. }
  144. }
  145. }
  146. show(controller, sender: nil)
  147. }
  148. } else {
  149. dismiss(animated: true) {
  150. self.isDismiss?(user)
  151. }
  152. }
  153. }
  154. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  155. if isFilltering {
  156. return fillteredUser.count
  157. }
  158. return users.count
  159. }
  160. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  161. let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
  162. var content = cell.defaultContentConfiguration()
  163. let user: User
  164. if isFilltering {
  165. user = fillteredUser[indexPath.row]
  166. } else {
  167. user = users[indexPath.row]
  168. }
  169. content.imageProperties.maximumSize = CGSize(width: 44, height: 44)
  170. getImage(name: user.thumb, placeholderImage: UIImage(named: "Profile---Purple", in: Bundle.resourceBundle(for: Nexilis.self), with: nil), isCircle: true, tableView: tableView, indexPath: indexPath) { result, isDownloaded, image in
  171. content.image = image
  172. }
  173. if User.isOfficial(official_account: user.official ?? "") || User.isOfficialRegular(official_account: user.official ?? "") {
  174. content.attributedText = self.set(image: UIImage(named: "ic_official_flag", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)!, with: " \(user.fullName)", size: 15, y: -4, colorText: UIColor.officialColor)
  175. } else if User.isVerified(official_account: user.official ?? "") {
  176. content.attributedText = self.set(image: UIImage(named: "ic_verified", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)!, with: " \(user.fullName)", size: 15, y: -4, colorText: UIColor.verifiedColor)
  177. }
  178. else if User.isInternal(userType: user.userType ?? "") {
  179. content.attributedText = self.set(image: UIImage(named: "ic_internal", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)!, with: " \(user.fullName)", size: 15, y: -4, colorText: UIColor.internalColor)
  180. } else if User.isCallCenter(userType: user.userType ?? "") {
  181. let dataCategory = CategoryCC.getDataFromServiceId(service_id: user.ex_offmp!)
  182. if dataCategory != nil {
  183. content.attributedText = self.set(image: UIImage(named: "pb_call_center", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)!, with: " \(user.fullName) (\(dataCategory!.service_name))", size: 15, y: -4, colorText: UIColor.ccColor)
  184. } else {
  185. content.attributedText = self.set(image: UIImage(named: "pb_call_center", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)!, with: " \(user.fullName)", size: 15, y: -4, colorText: UIColor.ccColor)
  186. }
  187. } else {
  188. content.text = user.fullName
  189. }
  190. cell.contentConfiguration = content
  191. cell.accessoryType = user.isSelected ? .checkmark : .none
  192. return cell
  193. }
  194. }
  195. extension QmeraCallContactViewController: UISearchControllerDelegate, UISearchBarDelegate, UISearchResultsUpdating {
  196. func updateSearchResults(for searchController: UISearchController) {
  197. filterContentForSearchText(searchController.searchBar.text!.trimmingCharacters(in: .whitespacesAndNewlines))
  198. }
  199. func set(image: UIImage, with text: String, size: CGFloat, y: CGFloat, colorText: UIColor = UIColor.black) -> NSAttributedString {
  200. let attachment = NSTextAttachment()
  201. attachment.image = image
  202. attachment.bounds = CGRect(x: 0, y: y, width: size, height: size)
  203. let attachmentStr = NSAttributedString(attachment: attachment)
  204. let mutableAttributedString = NSMutableAttributedString()
  205. mutableAttributedString.append(attachmentStr)
  206. let attributedStringColor = [NSAttributedString.Key.foregroundColor : colorText]
  207. let textString = NSAttributedString(string: text, attributes: attributedStringColor)
  208. mutableAttributedString.append(textString)
  209. return mutableAttributedString
  210. }
  211. }