JCGroupSettingViewController.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. //
  2. // JCGroupSettingViewController.swift
  3. // JChat
  4. //
  5. // Created by deng on 2017/4/27.
  6. // Copyright © 2017年 HXHG. All rights reserved.
  7. //
  8. import UIKit
  9. import JMessage
  10. class JCGroupSettingViewController: UIViewController, CustomNavigation {
  11. var group: JMSGGroup!
  12. override func viewDidLoad() {
  13. super.viewDidLoad()
  14. _init()
  15. }
  16. deinit {
  17. NotificationCenter.default.removeObserver(self)
  18. }
  19. private var tableView: UITableView = UITableView(frame: .zero, style: .grouped)
  20. fileprivate var memberCount = 0
  21. fileprivate lazy var users: [JMSGUser] = []
  22. fileprivate var isMyGroup = false
  23. fileprivate var isNeedUpdate = false
  24. //MARK: - private func
  25. private func _init() {
  26. self.title = "群组信息"
  27. view.backgroundColor = .white
  28. users = group.memberArray()
  29. memberCount = users.count
  30. let user = JMSGUser.myInfo()
  31. // && group.ownerAppKey == user.appKey! 这里group.ownerAppKey == "" 目测sdk bug
  32. if group.owner == user.username {
  33. isMyGroup = true
  34. }
  35. tableView.separatorStyle = .none
  36. tableView.delegate = self
  37. tableView.dataSource = self
  38. tableView.sectionIndexColor = UIColor(netHex: 0x2dd0cf)
  39. tableView.sectionIndexBackgroundColor = .clear
  40. tableView.register(JCButtonCell.self, forCellReuseIdentifier: "JCButtonCell")
  41. tableView.register(JCMineInfoCell.self, forCellReuseIdentifier: "JCMineInfoCell")
  42. tableView.register(GroupAvatorCell.self, forCellReuseIdentifier: "GroupAvatorCell")
  43. tableView.frame = CGRect(x: 0, y: 0, width: view.width, height: view.height)
  44. view.addSubview(tableView)
  45. customLeftBarButton(delegate: self)
  46. JMSGGroup.groupInfo(withGroupId: group.gid) { (result, error) in
  47. if error == nil {
  48. guard let group = result as? JMSGGroup else {
  49. return
  50. }
  51. self.group = group
  52. self.isNeedUpdate = true
  53. self._updateGroupInfo()
  54. }
  55. }
  56. NotificationCenter.default.addObserver(self, selector: #selector(_updateGroupInfo), name: NSNotification.Name(rawValue: kUpdateGroupInfo), object: nil)
  57. }
  58. @objc func _updateGroupInfo() {
  59. if !isNeedUpdate {
  60. let conv = JMSGConversation.groupConversation(withGroupId: group.gid)
  61. group = conv?.target as! JMSGGroup
  62. }
  63. if group.memberArray().count != memberCount {
  64. isNeedUpdate = true
  65. memberCount = group.memberArray().count
  66. }
  67. users = group.memberArray()
  68. memberCount = users.count
  69. tableView.reloadData()
  70. }
  71. }
  72. extension JCGroupSettingViewController: UITableViewDelegate, UITableViewDataSource {
  73. func numberOfSections(in tableView: UITableView) -> Int {
  74. return 4
  75. }
  76. public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  77. switch section {
  78. case 0:
  79. return 1
  80. case 1:
  81. return 3
  82. case 2:
  83. // return 5
  84. return 4
  85. case 3:
  86. return 1
  87. default:
  88. return 0
  89. }
  90. }
  91. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  92. switch indexPath.section {
  93. case 0:
  94. if isMyGroup {
  95. if memberCount > 13 {
  96. return 314
  97. }
  98. if memberCount > 8 {
  99. return 260
  100. }
  101. if memberCount > 3 {
  102. return 200
  103. }
  104. return 100
  105. } else {
  106. if memberCount > 14 {
  107. return 314
  108. }
  109. if memberCount > 9 {
  110. return 260
  111. }
  112. if memberCount > 4 {
  113. return 200
  114. }
  115. return 100
  116. }
  117. case 1:
  118. return 45
  119. case 2:
  120. return 40
  121. default:
  122. return 45
  123. }
  124. }
  125. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  126. if section == 0 {
  127. return 0.0001
  128. }
  129. return 10
  130. }
  131. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  132. if indexPath.section == 0 {
  133. var cell = tableView.dequeueReusableCell(withIdentifier: "JCGroupSettingCell") as? JCGroupSettingCell
  134. if isNeedUpdate {
  135. cell = JCGroupSettingCell(style: .default, reuseIdentifier: "JCGroupSettingCell", group: self.group)
  136. isNeedUpdate = false
  137. }
  138. if cell == nil {
  139. cell = JCGroupSettingCell(style: .default, reuseIdentifier: "JCGroupSettingCell", group: self.group)
  140. }
  141. return cell!
  142. }
  143. if indexPath.section == 3 {
  144. return tableView.dequeueReusableCell(withIdentifier: "JCButtonCell", for: indexPath)
  145. }
  146. if indexPath.section == 1 && indexPath.row == 0 {
  147. return tableView.dequeueReusableCell(withIdentifier: "GroupAvatorCell", for: indexPath)
  148. }
  149. return tableView.dequeueReusableCell(withIdentifier: "JCMineInfoCell", for: indexPath)
  150. }
  151. func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
  152. cell.selectionStyle = .none
  153. if indexPath.section == 3 {
  154. guard let cell = cell as? JCButtonCell else {
  155. return
  156. }
  157. cell.buttonColor = UIColor(netHex: 0xEB424D)
  158. cell.buttonTitle = "退出此群"
  159. cell.delegate = self
  160. return
  161. }
  162. cell.accessoryType = .disclosureIndicator
  163. if indexPath.section == 0 {
  164. guard let cell = cell as? JCGroupSettingCell else {
  165. return
  166. }
  167. cell.bindData(self.group)
  168. cell.delegate = self
  169. cell.accessoryType = .none
  170. return
  171. }
  172. if let cell = cell as? GroupAvatorCell {
  173. cell.title = "群头像"
  174. cell.bindData(group)
  175. }
  176. guard let cell = cell as? JCMineInfoCell else {
  177. return
  178. }
  179. if indexPath.section == 2 {
  180. if indexPath.row == 1 {
  181. cell.delegate = self
  182. cell.indexPate = indexPath
  183. cell.accessoryType = .none
  184. cell.isSwitchOn = group.isNoDisturb
  185. cell.isShowSwitch = true
  186. }
  187. if indexPath.row == 2 {
  188. cell.delegate = self
  189. cell.indexPate = indexPath
  190. cell.accessoryType = .none
  191. cell.isSwitchOn = group.isShieldMessage
  192. cell.isShowSwitch = true
  193. }
  194. }
  195. if indexPath.section == 1 {
  196. let conv = JMSGConversation.groupConversation(withGroupId: self.group.gid)
  197. let group = conv?.target as! JMSGGroup
  198. switch indexPath.row {
  199. case 1:
  200. cell.title = "群聊名称"
  201. cell.detail = group.displayName()
  202. case 2:
  203. cell.title = "群描述"
  204. cell.detail = group.desc
  205. default:
  206. break
  207. }
  208. } else {
  209. switch indexPath.row {
  210. case 0:
  211. cell.title = "聊天文件"
  212. case 1:
  213. cell.title = "消息免打扰"
  214. case 2:
  215. cell.title = "消息屏蔽"
  216. // case 2:
  217. // cell.title = "清理缓存"
  218. case 3:
  219. cell.title = "清空聊天记录"
  220. default:
  221. break
  222. }
  223. }
  224. }
  225. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  226. tableView.deselectRow(at: indexPath, animated: true)
  227. if indexPath.section == 1 {
  228. switch indexPath.row {
  229. case 0:
  230. let vc = GroupAvatorViewController()
  231. vc.group = group
  232. navigationController?.pushViewController(vc, animated: true)
  233. case 1:
  234. let vc = JCGroupNameViewController()
  235. vc.group = group
  236. navigationController?.pushViewController(vc, animated: true)
  237. case 2:
  238. let vc = JCGroupDescViewController()
  239. vc.group = group
  240. navigationController?.pushViewController(vc, animated: true)
  241. default:
  242. break
  243. }
  244. }
  245. if indexPath.section == 2 {
  246. switch indexPath.row {
  247. // case 2:
  248. // let actionSheet = UIActionSheet(title: nil, delegate: self, cancelButtonTitle: "取消", destructiveButtonTitle: nil, otherButtonTitles: "清理缓存")
  249. // actionSheet.tag = 1001
  250. // actionSheet.show(in: self.view)
  251. case 0:
  252. let vc = FileManagerViewController()
  253. let conv = JMSGConversation.groupConversation(withGroupId: group.gid)
  254. vc.conversation = conv
  255. navigationController?.pushViewController(vc, animated: true)
  256. case 3:
  257. let actionSheet = UIActionSheet(title: nil, delegate: self, cancelButtonTitle: "取消", destructiveButtonTitle: nil, otherButtonTitles: "清空聊天记录")
  258. actionSheet.tag = 1001
  259. actionSheet.show(in: self.view)
  260. default:
  261. break
  262. }
  263. }
  264. }
  265. }
  266. extension JCGroupSettingViewController: UIAlertViewDelegate {
  267. func alertView(_ alertView: UIAlertView, clickedButtonAt buttonIndex: Int) {
  268. switch buttonIndex {
  269. case 1:
  270. MBProgressHUD_JChat.showMessage(message: "退出中...", toView: self.view)
  271. group.exit({ (result, error) in
  272. MBProgressHUD_JChat.hide(forView: self.view, animated: true)
  273. if error == nil {
  274. self.navigationController?.popToRootViewController(animated: true)
  275. } else {
  276. MBProgressHUD_JChat.show(text: "\(String.errorAlert(error! as NSError))", view: self.view)
  277. }
  278. })
  279. default:
  280. break
  281. }
  282. }
  283. }
  284. extension JCGroupSettingViewController: JCMineInfoCellDelegate {
  285. func mineInfoCell(clickSwitchButton button: UISwitch, indexPath: IndexPath?) {
  286. if indexPath != nil {
  287. switch (indexPath?.row)! {
  288. case 1:
  289. if group.isNoDisturb == button.isOn {
  290. return
  291. }
  292. // 消息免打扰
  293. group.setIsNoDisturb(button.isOn, handler: { (result, error) in
  294. MBProgressHUD_JChat.hide(forView: self.view, animated: true)
  295. if error != nil {
  296. button.isOn = !button.isOn
  297. MBProgressHUD_JChat.show(text: "\(String.errorAlert(error! as NSError))", view: self.view)
  298. }
  299. })
  300. case 2:
  301. if group.isShieldMessage == button.isOn {
  302. return
  303. }
  304. // 消息屏蔽
  305. group.setIsShield(button.isOn, handler: { (result, error) in
  306. MBProgressHUD_JChat.hide(forView: self.view, animated: true)
  307. if error != nil {
  308. button.isOn = !button.isOn
  309. MBProgressHUD_JChat.show(text: "\(String.errorAlert(error! as NSError))", view: self.view)
  310. }
  311. })
  312. default:
  313. break
  314. }
  315. }
  316. }
  317. }
  318. extension JCGroupSettingViewController: JCButtonCellDelegate {
  319. func buttonCell(clickButton button: UIButton) {
  320. let alertView = UIAlertView(title: "退出此群", message: "确定要退出此群?", delegate: self, cancelButtonTitle: "取消", otherButtonTitles: "确定")
  321. alertView.show()
  322. }
  323. }
  324. extension JCGroupSettingViewController: JCGroupSettingCellDelegate {
  325. func clickMoreButton(clickButton button: UIButton) {
  326. let vc = JCGroupMembersViewController()
  327. vc.group = self.group
  328. self.navigationController?.pushViewController(vc, animated: true)
  329. }
  330. //增加群组成员 -- 暂不实现
  331. func clickAddCell(cell: JCGroupSettingCell) {
  332. // let vc = JCUpdateMemberViewController()
  333. // vc.group = group
  334. // self.navigationController?.pushViewController(vc, animated: true)
  335. }
  336. //删除群组成员 -- 暂不实现
  337. func clickRemoveCell(cell: JCGroupSettingCell) {
  338. // let vc = JCRemoveMemberViewController()
  339. // vc.group = group
  340. // self.navigationController?.pushViewController(vc, animated: true)
  341. }
  342. func didSelectCell(cell: JCGroupSettingCell, indexPath: IndexPath) {
  343. let index = indexPath.section * 5 + indexPath.row
  344. let user = users[index]
  345. if user.isEqual(to: JMSGUser.myInfo()) {
  346. // navigationController?.pushViewController(JCMyInfoViewController(), animated: true)
  347. navigationController?.pushViewController(SPersonViewController(), animated: true)
  348. return
  349. }
  350. // let vc = JCUserInfoViewController()
  351. // vc.user = user
  352. // navigationController?.pushViewController(vc, animated: true)
  353. let storyBoard = UIStoryboard(name: "contacts", bundle: nil)
  354. guard let personVC = storyBoard.instantiateViewController(withIdentifier: "ContactPersonInfoV2") as? ContactPersonInfoV2ViewController else {
  355. return
  356. }
  357. let person = PersonV2()
  358. person.id = user.username
  359. personVC.person = person
  360. navigationController?.pushViewController(personVC, animated: true)
  361. }
  362. }
  363. extension JCGroupSettingViewController: UIActionSheetDelegate {
  364. func actionSheet(_ actionSheet: UIActionSheet, clickedButtonAt buttonIndex: Int) {
  365. // if actionSheet.tag == 1001 {
  366. // // SDK 暂无该功能
  367. // }
  368. if actionSheet.tag == 1001 {
  369. if buttonIndex == 1 {
  370. let conv = JMSGConversation.groupConversation(withGroupId: group.gid)
  371. conv?.deleteAllMessages()
  372. NotificationCenter.default.post(name: Notification.Name(rawValue: kDeleteAllMessage), object: nil)
  373. MBProgressHUD_JChat.show(text: "成功清空", view: self.view)
  374. }
  375. }
  376. }
  377. }
  378. extension JCGroupSettingViewController: UIGestureRecognizerDelegate {
  379. public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
  380. return true
  381. }
  382. }