FriendsBusinessCardViewController.swift 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. //
  2. // FriendsBusinessCardViewController.swift
  3. // JChat
  4. //
  5. // Created by 邓永豪 on 2017/9/21.
  6. // Copyright © 2017年 HXHG. All rights reserved.
  7. //
  8. import UIKit
  9. class FriendsBusinessCardViewController: UIViewController {
  10. var conversation: JMSGConversation!
  11. override func viewDidLoad() {
  12. super.viewDidLoad()
  13. _init()
  14. }
  15. fileprivate lazy var toolView: UIView = UIView(frame: CGRect(x: 0, y: 64, width: self.view.width, height: 55))
  16. fileprivate lazy var tableView: UITableView = {
  17. let tableView = UITableView(frame: .zero, style: .grouped)
  18. tableView.delegate = self
  19. tableView.dataSource = self
  20. tableView.keyboardDismissMode = .onDrag
  21. tableView.sectionIndexColor = UIColor(netHex: 0x2dd0cf)
  22. tableView.sectionIndexBackgroundColor = .clear
  23. tableView.register(JCContacterCell.self, forCellReuseIdentifier: "JCContacterCell")
  24. tableView.frame = CGRect(x: 0, y: 31 + 64, width: self.view.width, height: self.view.height - 31 - 64)
  25. return tableView
  26. }()
  27. fileprivate lazy var searchView: UISearchBar = UISearchBar.default
  28. fileprivate lazy var users: [JMSGUser] = []
  29. fileprivate lazy var keys: [String] = []
  30. fileprivate lazy var data: Dictionary<String, [JMSGUser]> = Dictionary()
  31. fileprivate lazy var filteredUsersArray: [JMSGUser] = []
  32. fileprivate var searchUser: JMSGUser?
  33. fileprivate var selectUser: JMSGUser!
  34. private lazy var navLeftButton: UIBarButtonItem = UIBarButtonItem(title: "取消", style: .plain, target: self, action: #selector(_clickNavLeftButton))
  35. fileprivate lazy var tipsView: UIView = {
  36. let view = UIView(frame: CGRect(x: 0, y: 64 + 31 + 5, width: self.view.width, height: self.view.height - 31 - 64 - 5))
  37. view.backgroundColor = .white
  38. let tips = UILabel(frame: CGRect(x: 0, y: 0, width: view.width, height: 50))
  39. tips.font = UIFont.systemFont(ofSize: 16)
  40. tips.textColor = UIColor(netHex: 0x999999)
  41. tips.textAlignment = .center
  42. tips.text = "未搜索到用户"
  43. view.addSubview(tips)
  44. view.isHidden = true
  45. return view
  46. }()
  47. private func _init() {
  48. self.title = "发送名片"
  49. view.backgroundColor = .white
  50. automaticallyAdjustsScrollViewInsets = false
  51. view.addSubview(toolView)
  52. view.addSubview(tableView)
  53. view.addSubview(tipsView)
  54. _classify([], isFrist: true)
  55. searchView.frame = CGRect(x: 0, y: 0, width: toolView.width, height: 31)
  56. searchView.delegate = self
  57. toolView.addSubview(searchView)
  58. _setupNavigation()
  59. }
  60. private func _setupNavigation() {
  61. navigationItem.leftBarButtonItem = navLeftButton
  62. }
  63. func _clickNavLeftButton() {
  64. dismiss(animated: true, completion: nil)
  65. }
  66. fileprivate func _classify(_ users: [JMSGUser], isFrist: Bool = false) {
  67. if users.count > 0 {
  68. tipsView.isHidden = true
  69. }
  70. if isFrist {
  71. JMSGConversation.allConversations { (result, error) in
  72. if error == nil {
  73. if let conversations = result as? [JMSGConversation] {
  74. for conv in conversations {
  75. if let user = conv.target as? JMSGUser {
  76. self.users.append(user)
  77. }
  78. }
  79. }
  80. }
  81. }
  82. JMSGFriendManager.getFriendList { (result, error) in
  83. if error == nil {
  84. for item in result as! [JMSGUser] {
  85. if !self.users.contains(item) {
  86. self.users.append(item)
  87. }
  88. }
  89. for item in self.users {
  90. var key = item.displayName().firstCharacter()
  91. if !key.isLetterOrNum() {
  92. key = "#"
  93. }
  94. var array = self.data[key]
  95. if array == nil {
  96. array = [item]
  97. } else {
  98. array?.append(item)
  99. }
  100. if !self.keys.contains(key) {
  101. self.keys.append(key)
  102. }
  103. self.data[key] = array
  104. }
  105. self.filteredUsersArray = self.users
  106. self.keys = self.keys.sortedKeys()
  107. self.tableView.reloadData()
  108. }
  109. }
  110. } else {
  111. filteredUsersArray = users
  112. keys.removeAll()
  113. data.removeAll()
  114. for item in users {
  115. var key = item.displayName().firstCharacter()
  116. if !key.isLetterOrNum() {
  117. key = "#"
  118. }
  119. var array = data[key]
  120. if array == nil {
  121. array = [item]
  122. } else {
  123. array?.append(item)
  124. }
  125. if !keys.contains(key) {
  126. keys.append(key)
  127. }
  128. data[key] = array
  129. }
  130. keys = keys.sortedKeys()
  131. tableView.reloadData()
  132. }
  133. }
  134. fileprivate func filter(_ searchString: String) {
  135. if searchString.isEmpty || searchString == "" {
  136. _classify(users)
  137. return
  138. }
  139. let searchString = searchString.uppercased()
  140. filteredUsersArray = _JCFilterUsers(users: users, string: searchString)
  141. _classify(filteredUsersArray)
  142. }
  143. }
  144. //Mark: -
  145. extension FriendsBusinessCardViewController: UITableViewDelegate, UITableViewDataSource {
  146. func numberOfSections(in tableView: UITableView) -> Int {
  147. if filteredUsersArray.count > 0 {
  148. return keys.count
  149. }
  150. return 0
  151. }
  152. public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  153. return data[keys[section]]!.count
  154. }
  155. func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  156. return keys[section]
  157. }
  158. func sectionIndexTitles(for tableView: UITableView) -> [String]? {
  159. return keys
  160. }
  161. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  162. return 55
  163. }
  164. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  165. if section == 0 {
  166. return 30
  167. }
  168. return 10
  169. }
  170. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  171. return tableView.dequeueReusableCell(withIdentifier: "JCContacterCell", for: indexPath)
  172. }
  173. func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
  174. guard let cell = cell as? JCContacterCell else {
  175. return
  176. }
  177. let user = data[keys[indexPath.section]]?[indexPath.row]
  178. cell.bindDate(user!)
  179. }
  180. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  181. tableView.deselectRow(at: indexPath, animated: true)
  182. selectUser = data[keys[indexPath.section]]?[indexPath.row]
  183. var displayName = ""
  184. if conversation.ex.isGroup {
  185. let group = conversation.target as! JMSGGroup
  186. displayName = group.displayName()
  187. } else {
  188. displayName = conversation.title ?? ""
  189. }
  190. JCAlertView.bulid().setTitle("发送给:\(displayName)")
  191. .setMessage(selectUser.displayName() + "的名片")
  192. .setDelegate(self)
  193. .addCancelButton("取消")
  194. .addButton("确定")
  195. .show()
  196. }
  197. }
  198. extension FriendsBusinessCardViewController: UIAlertViewDelegate {
  199. func alertView(_ alertView: UIAlertView, clickedButtonAt buttonIndex: Int) {
  200. if buttonIndex != 1 {
  201. return
  202. }
  203. let message = JMSGMessage.ex.createBusinessCardMessage(conversation, selectUser.username, selectUser.appKey ?? "")
  204. JMSGMessage.send(message, optionalContent: JMSGOptionalContent.ex.default)
  205. MBProgressHUD_JChat.show(text: "已发送", view: view, 2)
  206. weak var weakSelf = self
  207. let time: TimeInterval = 2
  208. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) {
  209. NotificationCenter.default.post(name: NSNotification.Name(rawValue: kReloadAllMessage), object: nil)
  210. }
  211. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + time) {
  212. weakSelf?.dismiss(animated: true, completion: nil)
  213. }
  214. }
  215. }
  216. extension FriendsBusinessCardViewController: UISearchBarDelegate {
  217. func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
  218. filter(searchText)
  219. }
  220. // func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
  221. // // 搜索非好友
  222. // let searchText = searchBar.text!
  223. // JMSGUser.userInfoArray(withUsernameArray: [searchText]) { (result, error) in
  224. // if error == nil {
  225. // let users = result as! [JMSGUser]
  226. // self.searchUser = users.first
  227. // self._classify([self.searchUser!])
  228. // self.tipsView.isHidden = true
  229. // } else {
  230. // // 未查询到该用户的信息
  231. // self.tipsView.isHidden = false
  232. // }
  233. // }
  234. // }
  235. }