JCUpdateMemberViewController.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. //
  2. // JCUpdateMemberViewController.swift
  3. // JChat
  4. //
  5. // Created by deng on 2017/5/11.
  6. // Copyright © 2017年 HXHG. All rights reserved.
  7. //
  8. import UIKit
  9. import JMessage
  10. class JCUpdateMemberViewController: UIViewController {
  11. var isAddMember = true
  12. var group: JMSGGroup?
  13. var currentUser: JMSGUser?
  14. override func viewDidLoad() {
  15. super.viewDidLoad()
  16. _init()
  17. }
  18. override func viewDidAppear(_ animated: Bool) {
  19. super.viewDidAppear(animated)
  20. if selectUsers.count == 0 {
  21. confirm.alpha = 0.5
  22. navigationController?.navigationItem.rightBarButtonItem?.isEnabled = false
  23. }
  24. }
  25. private var topOffset: CGFloat {
  26. if isIPhoneX {
  27. return 88
  28. }
  29. return 64
  30. }
  31. fileprivate lazy var toolView: UIView = UIView(frame: CGRect(x: 0, y: self.topOffset, width: self.view.width, height: 55))
  32. fileprivate var tableView: UITableView = UITableView(frame: .zero, style: .grouped)
  33. fileprivate var collectionView: UICollectionView!
  34. fileprivate lazy var searchBar: UISearchBar = UISearchBar.default
  35. fileprivate lazy var confirm = UIButton(frame: CGRect(x: 0, y: 0, width: 120, height: 28))
  36. fileprivate lazy var users: [JMSGUser] = []
  37. fileprivate lazy var keys: [String] = []
  38. fileprivate lazy var data: Dictionary<String, [JMSGUser]> = Dictionary()
  39. fileprivate lazy var filteredUsersArray: [JMSGUser] = []
  40. fileprivate lazy var selectUsers: [JMSGUser] = []
  41. fileprivate var searchUser: JMSGUser?
  42. fileprivate var members: [JMSGUser]?
  43. fileprivate lazy var tipsView: UIView = {
  44. let view = UIView(frame: CGRect(x: 0, y: self.topOffset + 31 + 5, width: self.view.width, height: self.view.height - 31 - self.topOffset - 5))
  45. view.backgroundColor = .white
  46. let tips = UILabel(frame: CGRect(x: 0, y: 0, width: view.width, height: 50))
  47. tips.font = UIFont.systemFont(ofSize: 16)
  48. tips.textColor = UIColor(netHex: 0x999999)
  49. tips.textAlignment = .center
  50. tips.text = "未搜索到用户"
  51. view.addSubview(tips)
  52. view.isHidden = true
  53. return view
  54. }()
  55. private func _init() {
  56. view.backgroundColor = .white
  57. automaticallyAdjustsScrollViewInsets = false
  58. if isAddMember {
  59. self.title = "添加成员"
  60. members = group?.memberArray()
  61. } else {
  62. self.title = "发起群聊"
  63. }
  64. view.addSubview(toolView)
  65. tableView.delegate = self
  66. tableView.dataSource = self
  67. tableView.keyboardDismissMode = .onDrag
  68. tableView.sectionIndexColor = UIColor(netHex: 0x2dd0cf)
  69. tableView.sectionIndexBackgroundColor = .clear
  70. tableView.register(JCSelectMemberCell.self, forCellReuseIdentifier: "JCSelectMemberCell")
  71. tableView.frame = CGRect(x: 0, y: 31 + topOffset, width: view.width, height: view.height - 31 - topOffset)
  72. view.addSubview(tableView)
  73. view.addSubview(tipsView)
  74. _classify([], isFrist: true)
  75. let flowLayout = UICollectionViewFlowLayout()
  76. flowLayout.scrollDirection = .horizontal
  77. flowLayout.minimumInteritemSpacing = 0
  78. flowLayout.minimumLineSpacing = 0
  79. collectionView = UICollectionView(frame: .zero, collectionViewLayout: flowLayout)
  80. collectionView.backgroundColor = .clear
  81. collectionView.delegate = self
  82. collectionView.dataSource = self
  83. collectionView.register(JCUpdateMemberCell.self, forCellWithReuseIdentifier: "JCUpdateMemberCell")
  84. searchBar.frame = CGRect(x: 15, y: 0, width: toolView.width - 30, height: 31)
  85. searchBar.delegate = self
  86. searchBar.placeholder = "可搜索非好友"
  87. toolView.addSubview(searchBar)
  88. toolView.addSubview(collectionView)
  89. _setupNavigation()
  90. }
  91. private func _setupNavigation() {
  92. confirm.addTarget(self, action: #selector(_clickNavRightButton(_:)), for: .touchUpInside)
  93. confirm.setTitle("确定", for: .normal)
  94. confirm.titleLabel?.font = UIFont.systemFont(ofSize: 15)
  95. confirm.contentHorizontalAlignment = .right
  96. let item = UIBarButtonItem(customView: confirm)
  97. navigationItem.rightBarButtonItem = item
  98. }
  99. fileprivate func _classify(_ users: [JMSGUser], isFrist: Bool = false) {
  100. if users.count > 0 {
  101. tipsView.isHidden = true
  102. }
  103. if isFrist {
  104. JMSGFriendManager.getFriendList { (result, error) in
  105. if error == nil {
  106. self.users.removeAll()
  107. self.keys.removeAll()
  108. self.data.removeAll()
  109. for item in result as! [JMSGUser] {
  110. if let currentUser = self.currentUser {
  111. if item.username == currentUser.username {
  112. continue
  113. }
  114. }
  115. self.users.append(item)
  116. var key = item.displayName().firstCharacter()
  117. if !key.isLetterOrNum() {
  118. key = "#"
  119. }
  120. var array = self.data[key]
  121. if array == nil {
  122. array = [item]
  123. } else {
  124. array?.append(item)
  125. }
  126. if !self.keys.contains(key) {
  127. self.keys.append(key)
  128. }
  129. self.data[key] = array
  130. }
  131. self.filteredUsersArray = self.users
  132. self.keys = self.keys.sortedKeys()
  133. self.tableView.reloadData()
  134. self.collectionView.reloadData()
  135. }
  136. }
  137. } else {
  138. filteredUsersArray = users
  139. keys.removeAll()
  140. data.removeAll()
  141. for item in users {
  142. var key = item.displayName().firstCharacter()
  143. if !key.isLetterOrNum() {
  144. key = "#"
  145. }
  146. var array = data[key]
  147. if array == nil {
  148. array = [item]
  149. } else {
  150. array?.append(item)
  151. }
  152. if !keys.contains(key) {
  153. keys.append(key)
  154. }
  155. data[key] = array
  156. }
  157. keys = keys.sortedKeys()
  158. tableView.reloadData()
  159. collectionView.reloadData()
  160. }
  161. }
  162. fileprivate func _removeUser(_ user: JMSGUser) {
  163. selectUsers = selectUsers.filter({ (u) -> Bool in
  164. u.username != user.username || u.appKey != user.appKey
  165. })
  166. }
  167. fileprivate func _reloadCollectionView() {
  168. if selectUsers.count > 0 {
  169. confirm.alpha = 1.0
  170. confirm.setTitle("确定(\(selectUsers.count))", for: .normal)
  171. navigationController?.navigationItem.rightBarButtonItem?.isEnabled = true
  172. } else {
  173. confirm.alpha = 0.5
  174. confirm.setTitle("确定", for: .normal)
  175. navigationController?.navigationItem.rightBarButtonItem?.isEnabled = false
  176. }
  177. switch selectUsers.count {
  178. case 0:
  179. collectionView.frame = .zero
  180. searchBar.frame = CGRect(x: 15, y: 0, width: toolView.width - 30, height: 31)
  181. toolView.frame = CGRect(x: 0, y: topOffset, width: toolView.width, height: 31)
  182. tableView.frame = CGRect(x: tableView.x, y: topOffset + 31, width: tableView.width, height: view.height - topOffset - 31)
  183. tipsView.frame = CGRect(x: 0, y: topOffset + 31 + 5, width: view.width, height: view.height - 31 - topOffset - 5)
  184. case 1:
  185. collectionView.frame = CGRect(x: 10, y: 0, width: 46, height: 55)
  186. searchBar.frame = CGRect(x: 5 + 46, y: 0, width: toolView.width - 5 - 46, height: 55)
  187. toolView.frame = CGRect(x: 0, y: topOffset, width: toolView.width, height: 55)
  188. tableView.frame = CGRect(x: tableView.x, y: topOffset + 55, width: tableView.width, height: view.height - topOffset - 55)
  189. tipsView.frame = CGRect(x: 0, y: topOffset + 31 + 5 + 24, width: view.width, height: view.height - 31 - 40 - 5)
  190. case 2:
  191. collectionView.frame = CGRect(x: 10, y: 0, width: 92, height: 55)
  192. searchBar.frame = CGRect(x: 5 + 46 * 2, y: 0, width: toolView.width - 5 - 46 * 2, height: 55)
  193. case 3:
  194. collectionView.frame = CGRect(x: 10, y: 0, width: 138, height: 55)
  195. searchBar.frame = CGRect(x: 5 + 46 * 3, y: 0, width: toolView.width - 5 - 46 * 3, height: 55)
  196. case 4:
  197. collectionView.frame = CGRect(x: 10, y: 0, width: 184, height: 55)
  198. searchBar.frame = CGRect(x: 5 + 46 * 4, y: 0, width: toolView.width - 5 - 46 * 4, height: 55)
  199. default:
  200. collectionView.frame = CGRect(x: 10, y: 0, width: 230, height: 55)
  201. searchBar.frame = CGRect(x: 5 + 46 * 5, y: 0, width: toolView.width - 5 - 46 * 5, height: 55)
  202. }
  203. collectionView.reloadData()
  204. }
  205. @objc func _clickNavRightButton(_ sender: UIButton) {
  206. if selectUsers.count == 0 {
  207. return
  208. }
  209. var userNames: [String] = []
  210. for item in selectUsers {
  211. userNames.append(item.username)
  212. }
  213. if isAddMember {
  214. MBProgressHUD_JChat.showMessage(message: "添加中...", toView: view)
  215. group?.addMembers(withUsernameArray: userNames, completionHandler: { (result, error) in
  216. MBProgressHUD_JChat.hide(forView: self.view, animated: true)
  217. if error == nil {
  218. NotificationCenter.default.post(name: Notification.Name(rawValue: kUpdateGroupInfo), object: nil)
  219. self.navigationController?.popViewController(animated: true)
  220. } else {
  221. MBProgressHUD_JChat.show(text: "添加失败,请重试", view: self.view)
  222. }
  223. })
  224. } else {
  225. if currentUser != nil {
  226. userNames.insert((currentUser?.username)!, at: 0)
  227. }
  228. MBProgressHUD_JChat.showMessage(message: "创建中...", toView: view)
  229. JMSGGroup.createGroup(withName: nil, desc: nil, memberArray: userNames, completionHandler: { (result, error) in
  230. MBProgressHUD_JChat.hide(forView: self.view, animated: true)
  231. if error == nil {
  232. for vc in (self.navigationController?.viewControllers)! {
  233. if vc is JCConversationListViewController {
  234. self.navigationController?.popToViewController(vc, animated: true)
  235. let group = result as! JMSGGroup
  236. JMSGConversation.createGroupConversation(withGroupId: group.gid, completionHandler: { (result, error) in
  237. let conv = JMSGConversation.groupConversation(withGroupId: group.gid)
  238. let chatVC = JCChatViewController(conversation: conv!)
  239. vc.navigationController?.pushViewController(chatVC, animated: true)
  240. })
  241. }
  242. }
  243. } else {
  244. MBProgressHUD_JChat.show(text: "创建失败,请重试", view: self.view)
  245. }
  246. })
  247. }
  248. }
  249. fileprivate func filter(_ searchString: String) {
  250. if searchString.isEmpty || searchString == "" {
  251. _classify(users)
  252. return
  253. }
  254. let searchString = searchString.uppercased()
  255. filteredUsersArray = _JCFilterUsers(users: users, string: searchString)
  256. _classify(filteredUsersArray)
  257. }
  258. }
  259. //Mark: -
  260. extension JCUpdateMemberViewController: UITableViewDelegate, UITableViewDataSource {
  261. func numberOfSections(in tableView: UITableView) -> Int {
  262. if filteredUsersArray.count > 0 {
  263. return keys.count
  264. }
  265. return 0
  266. }
  267. public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  268. return data[keys[section]]!.count
  269. }
  270. func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  271. return keys[section]
  272. }
  273. func sectionIndexTitles(for tableView: UITableView) -> [String]? {
  274. return keys
  275. }
  276. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  277. return 55
  278. }
  279. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  280. if section == 0 {
  281. return 30
  282. }
  283. return 10
  284. }
  285. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  286. return tableView.dequeueReusableCell(withIdentifier: "JCSelectMemberCell", for: indexPath)
  287. }
  288. func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
  289. guard let cell = cell as? JCSelectMemberCell else {
  290. return
  291. }
  292. let user = data[keys[indexPath.section]]?[indexPath.row]
  293. cell.bindDate(user!)
  294. if let members = members {
  295. if members.contains(where: { (u) -> Bool in
  296. return u.username == user?.username && u.appKey == user?.appKey
  297. }) {
  298. cell.selectIcon = UIImage.loadImage("com_icon_isSelect")
  299. return
  300. }
  301. }
  302. if selectUsers.contains(where: { (u) -> Bool in
  303. return u.username == user?.username && u.appKey == user?.appKey
  304. }) {
  305. cell.selectIcon = UIImage.loadImage("com_icon_select")
  306. } else {
  307. cell.selectIcon = UIImage.loadImage("com_icon_unselect")
  308. }
  309. }
  310. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  311. tableView.deselectRow(at: indexPath, animated: true)
  312. guard let cell = tableView.cellForRow(at: indexPath) as? JCSelectMemberCell else {
  313. return
  314. }
  315. let user = data[keys[indexPath.section]]?[indexPath.row]
  316. if members != nil {
  317. if (members?.contains(where: { (u) -> Bool in
  318. return u.username == user?.username && u.appKey == user?.appKey
  319. }))! {
  320. return
  321. }
  322. }
  323. if selectUsers.contains(where: { (u) -> Bool in
  324. return u.username == user?.username && u.appKey == user?.appKey
  325. }) {
  326. // remove
  327. cell.selectIcon = UIImage.loadImage("com_icon_unselect")
  328. _removeUser(user!)
  329. _reloadCollectionView()
  330. } else {
  331. selectUsers.append(user!)
  332. cell.selectIcon = UIImage.loadImage("com_icon_select")
  333. _reloadCollectionView()
  334. }
  335. if selectUsers.count > 0 {
  336. collectionView.scrollToItem(at: IndexPath(row: selectUsers.count - 1, section: 0), at: .right, animated: false)
  337. }
  338. }
  339. }
  340. extension JCUpdateMemberViewController: UICollectionViewDelegate, UICollectionViewDataSource {
  341. func numberOfSections(in collectionView: UICollectionView) -> Int {
  342. return 1
  343. }
  344. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  345. return selectUsers.count
  346. }
  347. func collectionView(_ collectionView: UICollectionView,
  348. layout collectionViewLayout: UICollectionViewLayout,
  349. sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize {
  350. return CGSize(width: 46, height: 55)
  351. }
  352. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  353. return collectionView.dequeueReusableCell(withReuseIdentifier: "JCUpdateMemberCell", for: indexPath)
  354. }
  355. func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
  356. guard let cell = cell as? JCUpdateMemberCell else {
  357. return
  358. }
  359. cell.backgroundColor = .white
  360. cell.bindDate(user: selectUsers[indexPath.row])
  361. }
  362. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  363. selectUsers.remove(at: indexPath.row)
  364. tableView.reloadData()
  365. _reloadCollectionView()
  366. }
  367. }
  368. extension JCUpdateMemberViewController: UISearchBarDelegate {
  369. func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
  370. filter(searchText)
  371. }
  372. func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
  373. // 搜索非好友
  374. let searchText = searchBar.text!
  375. JMSGUser.userInfoArray(withUsernameArray: [searchText]) { (result, error) in
  376. if error == nil {
  377. let users = result as! [JMSGUser]
  378. self.searchUser = users.first
  379. self._classify([self.searchUser!])
  380. self.tipsView.isHidden = true
  381. } else {
  382. // 未查询到该用户的信息
  383. self.tipsView.isHidden = false
  384. }
  385. }
  386. }
  387. }