JCRemoveMemberViewController.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. //
  2. // JCRemoveMemberViewController.swift
  3. // JChat
  4. //
  5. // Created by deng on 2017/5/16.
  6. // Copyright © 2017年 HXHG. All rights reserved.
  7. //
  8. import UIKit
  9. import JMessage
  10. class JCRemoveMemberViewController: UIViewController {
  11. var group: JMSGGroup!
  12. override func viewDidLoad() {
  13. super.viewDidLoad()
  14. _init()
  15. }
  16. fileprivate lazy var toolView: UIView = UIView(frame: CGRect(x: 0, y: 64, width: self.view.width, height: 55))
  17. fileprivate var tableView: UITableView = UITableView(frame: .zero, style: .grouped)
  18. fileprivate var collectionView: UICollectionView!
  19. fileprivate lazy var searchView: UISearchBar = UISearchBar.default
  20. fileprivate lazy var delButton = UIButton(frame: CGRect(x: 0, y: 0, width: 120, height: 28))
  21. fileprivate lazy var users: [JMSGUser] = []
  22. fileprivate lazy var keys: [String] = []
  23. fileprivate lazy var data: Dictionary<String, [JMSGUser]> = Dictionary()
  24. fileprivate lazy var filteredUsersArray: [JMSGUser] = []
  25. fileprivate lazy var selectUsers: [JMSGUser] = []
  26. private func _init() {
  27. self.title = "删除成员"
  28. view.backgroundColor = .white
  29. automaticallyAdjustsScrollViewInsets = false
  30. view.addSubview(toolView)
  31. tableView.delegate = self
  32. tableView.dataSource = self
  33. tableView.keyboardDismissMode = .onDrag
  34. tableView.sectionIndexColor = UIColor(netHex: 0x2dd0cf)
  35. tableView.sectionIndexBackgroundColor = .clear
  36. tableView.register(JCSelectMemberCell.self, forCellReuseIdentifier: "JCSelectMemberCell")
  37. tableView.frame = CGRect(x: 0, y: 31 + 64, width: view.width, height: view.height - 55 - 64)
  38. view.addSubview(tableView)
  39. let flowLayout = UICollectionViewFlowLayout()
  40. flowLayout.scrollDirection = .horizontal
  41. flowLayout.minimumInteritemSpacing = 0
  42. flowLayout.minimumLineSpacing = 0
  43. collectionView = UICollectionView(frame: .zero, collectionViewLayout: flowLayout)
  44. collectionView.backgroundColor = .clear
  45. collectionView.delegate = self
  46. collectionView.dataSource = self
  47. collectionView.register(JCUpdateMemberCell.self, forCellWithReuseIdentifier: "JCUpdateMemberCell")
  48. searchView.frame = CGRect(x: 15, y: 0, width: toolView.width - 30, height: 31)
  49. searchView.delegate = self
  50. users = group.memberArray()
  51. if group.owner == JMSGUser.myInfo().username {
  52. users = users.filter({ (u) -> Bool in
  53. u.username != JMSGUser.myInfo().username || u.appKey != JMSGUser.myInfo().appKey
  54. })
  55. }
  56. filteredUsersArray = users
  57. _classify(users)
  58. toolView.addSubview(searchView)
  59. toolView.addSubview(collectionView)
  60. _setupNavigation()
  61. }
  62. private func _setupNavigation() {
  63. delButton.addTarget(self, action: #selector(_clickNavRightButton(_:)), for: .touchUpInside)
  64. delButton.setTitle("删除", for: .normal)
  65. delButton.titleLabel?.font = UIFont.systemFont(ofSize: 15)
  66. delButton.contentHorizontalAlignment = .right
  67. let item = UIBarButtonItem(customView: delButton)
  68. navigationItem.rightBarButtonItem = item
  69. navigationController?.navigationItem.rightBarButtonItem?.isEnabled = false
  70. }
  71. private func _classify(_ users: [JMSGUser]) {
  72. filteredUsersArray = users
  73. keys.removeAll()
  74. data.removeAll()
  75. for item in users {
  76. let key = item.displayName().firstCharacter()
  77. var array = data[key]
  78. if array == nil {
  79. array = [item]
  80. } else {
  81. array?.append(item)
  82. }
  83. if !keys.contains(key) {
  84. keys.append(key)
  85. }
  86. data[key] = array
  87. }
  88. keys = keys.sorted(by: { (str1, str2) -> Bool in
  89. return str1 < str2
  90. })
  91. tableView.reloadData()
  92. collectionView.reloadData()
  93. }
  94. fileprivate func _reloadCollectionView() {
  95. if selectUsers.count > 0 {
  96. delButton.alpha = 1.0
  97. delButton.setTitle("删除(\(selectUsers.count))", for: .normal)
  98. navigationController?.navigationItem.rightBarButtonItem?.isEnabled = true
  99. } else {
  100. delButton.alpha = 0.5
  101. delButton.setTitle("删除", for: .normal)
  102. navigationController?.navigationItem.rightBarButtonItem?.isEnabled = false
  103. }
  104. switch selectUsers.count {
  105. case 0:
  106. collectionView.frame = .zero
  107. searchView.frame = CGRect(x: 15, y: 0, width: toolView.width - 30, height: 31)
  108. toolView.frame = CGRect(x: 0, y: 64, width: toolView.width, height: 31)
  109. tableView.frame = CGRect(x: tableView.x, y: 64 + 31, width: tableView.width, height: view.height - 64 - 31)
  110. case 1:
  111. collectionView.frame = CGRect(x: 10, y: 0, width: 46, height: 55)
  112. searchView.frame = CGRect(x: 5 + 46, y: 0, width: toolView.width - 5 - 46, height: 55)
  113. toolView.frame = CGRect(x: 0, y: 64, width: toolView.width, height: 55)
  114. tableView.frame = CGRect(x: tableView.x, y: 64 + 55, width: tableView.width, height: view.height - 64 - 55)
  115. case 2:
  116. collectionView.frame = CGRect(x: 10, y: 0, width: 92, height: 55)
  117. searchView.frame = CGRect(x: 5 + 46 * 2, y: 0, width: toolView.width - 5 - 46 * 2, height: 55)
  118. case 3:
  119. collectionView.frame = CGRect(x: 10, y: 0, width: 138, height: 55)
  120. searchView.frame = CGRect(x: 5 + 46 * 3, y: 0, width: toolView.width - 5 - 46 * 3, height: 55)
  121. case 4:
  122. collectionView.frame = CGRect(x: 10, y: 0, width: 184, height: 55)
  123. searchView.frame = CGRect(x: 5 + 46 * 4, y: 0, width: toolView.width - 5 - 46 * 4, height: 55)
  124. default:
  125. collectionView.frame = CGRect(x: 10, y: 0, width: 230, height: 55)
  126. searchView.frame = CGRect(x: 5 + 46 * 5, y: 0, width: toolView.width - 5 - 46 * 5, height: 55)
  127. }
  128. collectionView.reloadData()
  129. }
  130. func _clickNavRightButton(_ sender: UIButton) {
  131. var userNames: [String] = []
  132. for item in selectUsers {
  133. userNames.append(item.username)
  134. }
  135. MBProgressHUD_JChat.showMessage(message: "删除中...", toView: view)
  136. group.removeMembers(withUsernameArray: userNames) { (result, error) in
  137. MBProgressHUD_JChat.hide(forView: self.view, animated: true)
  138. if error == nil {
  139. NotificationCenter.default.post(name: Notification.Name(rawValue: kUpdateGroupInfo), object: nil)
  140. self.navigationController?.popViewController(animated: true)
  141. } else {
  142. MBProgressHUD_JChat.show(text: "删除失败,请重试", view: self.view)
  143. }
  144. }
  145. }
  146. fileprivate func filter(_ searchString: String) {
  147. if searchString.isEmpty || searchString == "" {
  148. _classify(users)
  149. return
  150. }
  151. filteredUsersArray = _JCFilterUsers(users: users, string: searchString)
  152. _classify(filteredUsersArray)
  153. }
  154. }
  155. //Mark: - UITableViewDelegate & UITableViewDataSource
  156. extension JCRemoveMemberViewController: UITableViewDelegate, UITableViewDataSource {
  157. func numberOfSections(in tableView: UITableView) -> Int {
  158. if filteredUsersArray.count > 0 {
  159. return keys.count
  160. }
  161. return 0
  162. }
  163. public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  164. return data[keys[section]]!.count
  165. }
  166. func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  167. return keys[section]
  168. }
  169. func sectionIndexTitles(for tableView: UITableView) -> [String]? {
  170. return keys
  171. }
  172. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  173. return 55
  174. }
  175. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  176. if section == 0 {
  177. return 30
  178. }
  179. return 10
  180. }
  181. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  182. return tableView.dequeueReusableCell(withIdentifier: "JCSelectMemberCell", for: indexPath)
  183. }
  184. func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
  185. guard let cell = cell as? JCSelectMemberCell else {
  186. return
  187. }
  188. let user = data[keys[indexPath.section]]?[indexPath.row]
  189. cell.bindDate(user!)
  190. if selectUsers.contains(where: { (u) -> Bool in
  191. return u.username == user?.username && u.appKey == user?.appKey
  192. }) {
  193. cell.selectIcon = UIImage.loadImage("com_icon_select")
  194. } else {
  195. cell.selectIcon = UIImage.loadImage("com_icon_unselect")
  196. }
  197. }
  198. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  199. tableView.deselectRow(at: indexPath, animated: true)
  200. guard let cell = tableView.cellForRow(at: indexPath) as? JCSelectMemberCell else {
  201. return
  202. }
  203. guard let user = data[keys[indexPath.section]]?[indexPath.row] else {
  204. return
  205. }
  206. if selectUsers.contains(where: { (u) -> Bool in
  207. return u.username == user.username && u.appKey == user.appKey
  208. }) {
  209. // remove
  210. cell.selectIcon = UIImage.loadImage("com_icon_unselect")
  211. selectUsers = selectUsers.filter({ (u) -> Bool in
  212. u.username != user.username || u.appKey != user.appKey
  213. })
  214. _reloadCollectionView()
  215. } else {
  216. selectUsers.append(user)
  217. cell.selectIcon = UIImage.loadImage("com_icon_select")
  218. _reloadCollectionView()
  219. }
  220. if selectUsers.count > 0 {
  221. collectionView.scrollToItem(at: IndexPath(row: selectUsers.count - 1, section: 0), at: .right, animated: false)
  222. }
  223. }
  224. }
  225. extension JCRemoveMemberViewController: UICollectionViewDelegate, UICollectionViewDataSource {
  226. func numberOfSections(in collectionView: UICollectionView) -> Int {
  227. return 1
  228. }
  229. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  230. return selectUsers.count
  231. }
  232. func collectionView(_ collectionView: UICollectionView,
  233. layout collectionViewLayout: UICollectionViewLayout,
  234. sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize {
  235. return CGSize(width: 46, height: 55)
  236. }
  237. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  238. return collectionView.dequeueReusableCell(withReuseIdentifier: "JCUpdateMemberCell", for: indexPath)
  239. }
  240. func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
  241. guard let cell = cell as? JCUpdateMemberCell else {
  242. return
  243. }
  244. cell.backgroundColor = .white
  245. cell.bindDate(user: selectUsers[indexPath.row])
  246. }
  247. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  248. selectUsers.remove(at: indexPath.row)
  249. tableView.reloadData()
  250. _reloadCollectionView()
  251. }
  252. }
  253. extension JCRemoveMemberViewController: UISearchBarDelegate {
  254. func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
  255. filter(searchText)
  256. }
  257. }