JCContactsViewController.swift 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. //
  2. // JCContactsViewController.swift
  3. // JChat
  4. //
  5. // Created by deng on 2017/2/16.
  6. // Copyright © 2017年 HXHG. All rights reserved.
  7. //
  8. import UIKit
  9. import YHPopupView
  10. class JCContactsViewController: UIViewController {
  11. public required init() {
  12. super.init(nibName: nil, bundle: nil)
  13. NotificationCenter.default.addObserver(self, selector: #selector(_updateBadge), name: NSNotification.Name(rawValue: kUpdateVerification), object: nil)
  14. }
  15. required init?(coder aDecoder: NSCoder) {
  16. fatalError("init(coder:) has not been implemented")
  17. }
  18. //MARK: - life cycle
  19. override func viewDidLoad() {
  20. super.viewDidLoad()
  21. _init()
  22. }
  23. override func viewWillAppear(_ animated: Bool) {
  24. super.viewWillAppear(animated)
  25. _updateBadge()
  26. }
  27. deinit {
  28. NotificationCenter.default.removeObserver(self)
  29. }
  30. private lazy var addButton = UIButton(frame: CGRect(x: 0, y: 0, width: 36, height: 36))
  31. fileprivate lazy var contacterView: UITableView = {
  32. var contacterView = UITableView(frame: .zero, style: .grouped)
  33. contacterView.delegate = self
  34. contacterView.dataSource = self
  35. contacterView.separatorStyle = .none
  36. contacterView.sectionIndexColor = UIColor(netHex: 0x2dd0cf)
  37. contacterView.sectionIndexBackgroundColor = .clear
  38. return contacterView
  39. }()
  40. private lazy var searchController: JCSearchController = JCSearchController(searchResultsController: JCNavigationController(rootViewController: JCSearchResultViewController()))
  41. private lazy var searchView: UIView = UIView(frame: CGRect(x: 0, y: 0, width: self.view.width, height: 31))
  42. fileprivate var badgeCount = 0
  43. fileprivate lazy var tagArray = ["验证消息", "群组"]
  44. fileprivate lazy var users: [JMSGUser] = []
  45. fileprivate lazy var keys: [String] = []
  46. fileprivate lazy var data: Dictionary<String, [JMSGUser]> = Dictionary()
  47. //MARK: - private func
  48. private func _init() {
  49. self.title = "通讯录"
  50. view.backgroundColor = UIColor(netHex: 0xe8edf3)
  51. if #available(iOS 10.0, *) {
  52. navigationController?.tabBarItem.badgeColor = UIColor(netHex: 0xEB424C)
  53. }
  54. let nav = searchController.searchResultsController as! JCNavigationController
  55. let vc = nav.topViewController as! JCSearchResultViewController
  56. searchController.delegate = self
  57. searchController.searchResultsUpdater = vc
  58. searchView.addSubview(searchController.searchBar)
  59. contacterView.tableHeaderView = searchView
  60. contacterView.register(JCContacterCell.self, forCellReuseIdentifier: "JCContacterCell")
  61. contacterView.frame = CGRect(x: 0, y: 0, width: view.width, height: view.height)
  62. view.addSubview(contacterView)
  63. _setupNavigation()
  64. _getFriends()
  65. _updateBadge()
  66. NotificationCenter.default.addObserver(self, selector: #selector(_updateUserInfo), name: NSNotification.Name(rawValue: kUpdateFriendInfo), object: nil)
  67. NotificationCenter.default.addObserver(self, selector: #selector(_getFriends), name: NSNotification.Name(rawValue: kUpdateFriendList), object: nil)
  68. }
  69. private func _setupNavigation() {
  70. addButton.addTarget(self, action: #selector(_clickNavRightButton(_:)), for: .touchUpInside)
  71. addButton.setImage(UIImage.loadImage("com_icon_friend_add"), for: .normal)
  72. let item = UIBarButtonItem(customView: addButton)
  73. navigationItem.rightBarButtonItem = item
  74. }
  75. @objc func _updateUserInfo() {
  76. _classify(users)
  77. contacterView.reloadData()
  78. }
  79. func _classify(_ users: [JMSGUser]) {
  80. self.users = users
  81. keys.removeAll()
  82. data.removeAll()
  83. for item in users {
  84. var key = item.displayName().firstCharacter()
  85. if !key.isLetterOrNum() {
  86. key = "#"
  87. }
  88. var array = data[key]
  89. if array == nil {
  90. array = [item]
  91. } else {
  92. array?.append(item)
  93. }
  94. if !keys.contains(key) {
  95. keys.append(key)
  96. }
  97. data[key] = array
  98. }
  99. keys = keys.sortedKeys()
  100. }
  101. @objc func _getFriends() {
  102. JMSGFriendManager.getFriendList { (result, error) in
  103. if let users = result as? [JMSGUser] {
  104. self._classify(users)
  105. self.contacterView.reloadData()
  106. }
  107. }
  108. }
  109. //MARK: - click func
  110. @objc func _clickNavRightButton(_ sender: UIButton) {
  111. navigationController?.pushViewController(JCSearchFriendViewController(), animated: true)
  112. }
  113. @objc func _updateBadge() {
  114. if UserDefaults.standard.object(forKey: kUnreadInvitationCount) != nil {
  115. badgeCount = UserDefaults.standard.object(forKey: kUnreadInvitationCount) as! Int
  116. } else {
  117. badgeCount = 0
  118. }
  119. if badgeCount > 99 {
  120. navigationController?.tabBarItem.badgeValue = "99+"
  121. } else {
  122. navigationController?.tabBarItem.badgeValue = badgeCount == 0 ? nil : "\(badgeCount)"
  123. }
  124. contacterView.reloadRows(at: [IndexPath(row: 0, section: 0)], with: .none)
  125. }
  126. }
  127. //Mark: -
  128. extension JCContactsViewController: UITableViewDelegate, UITableViewDataSource {
  129. func numberOfSections(in tableView: UITableView) -> Int {
  130. if users.count > 0 {
  131. return keys.count + 1
  132. }
  133. return 1
  134. }
  135. public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  136. if section == 0 {
  137. return tagArray.count
  138. }
  139. return data[keys[section - 1]]!.count
  140. }
  141. func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  142. if section == 0 {
  143. return ""
  144. }
  145. return keys[section - 1]
  146. }
  147. func sectionIndexTitles(for tableView: UITableView) -> [String]? {
  148. return keys
  149. }
  150. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  151. return 55
  152. }
  153. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  154. if section == 0 {
  155. return 5
  156. }
  157. return 10
  158. }
  159. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  160. return tableView.dequeueReusableCell(withIdentifier: "JCContacterCell", for: indexPath)
  161. }
  162. func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
  163. guard let cell = cell as? JCContacterCell else {
  164. return
  165. }
  166. if indexPath.section == 0 {
  167. switch indexPath.row {
  168. case 0:
  169. cell.title = "验证消息"
  170. cell.icon = UIImage.loadImage("com_icon_friend_tip")
  171. cell.isShowBadge = badgeCount > 0 ? true : false
  172. case 1:
  173. cell.title = "群组"
  174. cell.icon = UIImage.loadImage("com_icon_group_36")
  175. cell.isShowBadge = false
  176. default:
  177. break
  178. }
  179. return
  180. }
  181. let user = data[keys[indexPath.section - 1]]?[indexPath.row]
  182. cell.isShowBadge = false
  183. cell.bindDate(user!)
  184. }
  185. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  186. tableView.deselectRow(at: indexPath, animated: true)
  187. if indexPath.section == 0 {
  188. switch indexPath.row {
  189. case 0:
  190. navigationController?.pushViewController(JCIdentityVerificationViewController(), animated: true)
  191. case 1:
  192. navigationController?.pushViewController(JCGroupListViewController(), animated: true)
  193. default:
  194. break
  195. }
  196. return
  197. }
  198. let vc = JCUserInfoViewController()
  199. let user = data[keys[indexPath.section - 1]]?[indexPath.row]
  200. vc.user = user
  201. navigationController?.pushViewController(vc, animated: true)
  202. }
  203. }
  204. extension JCContactsViewController: UISearchControllerDelegate {
  205. func willPresentSearchController(_ searchController: UISearchController) {
  206. contacterView.isHidden = true
  207. navigationController?.tabBarController?.tabBar.isHidden = true
  208. }
  209. func willDismissSearchController(_ searchController: UISearchController) {
  210. contacterView.isHidden = false
  211. let nav = searchController.searchResultsController as! JCNavigationController
  212. nav.isNavigationBarHidden = true
  213. nav.popToRootViewController(animated: false)
  214. navigationController?.tabBarController?.tabBar.isHidden = false
  215. }
  216. }