JCSingleSettingViewController.swift 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. //
  2. // JCSingleSettingViewController.swift
  3. // JChat
  4. //
  5. // Created by deng on 2017/4/5.
  6. // Copyright © 2017年 HXHG. All rights reserved.
  7. //
  8. import UIKit
  9. import JMessage
  10. class JCSingleSettingViewController: UIViewController, CustomNavigation {
  11. var user: JMSGUser!
  12. override func viewDidLoad() {
  13. super.viewDidLoad()
  14. _init()
  15. }
  16. private lazy var tableView: UITableView = {
  17. var tableView = UITableView(frame: CGRect(x: 0, y: 0, width: self.view.width, height: self.view.height), style: .grouped)
  18. tableView.separatorStyle = .none
  19. tableView.delegate = self
  20. tableView.dataSource = self
  21. tableView.sectionIndexColor = UIColor(netHex: 0x2dd0cf)
  22. tableView.sectionIndexBackgroundColor = .clear
  23. tableView.register(JCSingleSettingCell.self, forCellReuseIdentifier: "JCSingleSettingCell")
  24. tableView.register(JCButtonCell.self, forCellReuseIdentifier: "JCButtonCell")
  25. tableView.register(JCMineInfoCell.self, forCellReuseIdentifier: "JCMineInfoCell")
  26. return tableView
  27. }()
  28. fileprivate lazy var leftButton = UIButton(frame: CGRect(x: 0, y: 0, width: 60, height: 65 / 3))
  29. //MARK: - private func
  30. private func _init() {
  31. self.title = "聊天设置"
  32. view.backgroundColor = .white
  33. view.addSubview(tableView)
  34. customLeftBarButton(delegate: self)
  35. }
  36. }
  37. extension JCSingleSettingViewController: UITableViewDelegate, UITableViewDataSource {
  38. func numberOfSections(in tableView: UITableView) -> Int {
  39. return 3
  40. }
  41. public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  42. switch section {
  43. case 0:
  44. return 1
  45. case 1:
  46. return 3
  47. case 2:
  48. return 1
  49. default:
  50. return 0
  51. }
  52. }
  53. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  54. switch indexPath.section {
  55. case 0:
  56. return 105
  57. case 1:
  58. return 45
  59. case 2:
  60. return 40
  61. default:
  62. return 45
  63. }
  64. }
  65. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  66. if section == 0 {
  67. return 0.0001
  68. }
  69. return 10
  70. }
  71. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  72. if indexPath.section == 0 {
  73. return tableView.dequeueReusableCell(withIdentifier: "JCSingleSettingCell", for: indexPath)
  74. }
  75. if indexPath.section == 2 {
  76. return tableView.dequeueReusableCell(withIdentifier: "JCButtonCell", for: indexPath)
  77. }
  78. return tableView.dequeueReusableCell(withIdentifier: "JCMineInfoCell", for: indexPath)
  79. }
  80. func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
  81. cell.selectionStyle = .none
  82. if indexPath.section == 2 {
  83. guard let cell = cell as? JCButtonCell else {
  84. return
  85. }
  86. if !user.isFriend {
  87. cell.buttonColor = O2ThemeManager.color(for: "Base.base_color")!
  88. cell.buttonTitle = "添加好友"
  89. cell.delegate = self
  90. } else {
  91. cell.buttonColor = O2ThemeManager.color(for: "Base.base_color")!
  92. cell.buttonTitle = "删除好友"
  93. cell.delegate = self
  94. }
  95. return
  96. }
  97. cell.accessoryType = .disclosureIndicator
  98. if indexPath.section == 0 {
  99. guard let cell = cell as? JCSingleSettingCell else {
  100. return
  101. }
  102. cell.bindData(user)
  103. cell.delegate = self
  104. cell.accessoryType = .none
  105. return
  106. }
  107. guard let cell = cell as? JCMineInfoCell else {
  108. return
  109. }
  110. if indexPath.section == 1 && indexPath.row == 1 {
  111. cell.delegate = self
  112. cell.accessoryType = .none
  113. cell.isShowSwitch = true
  114. }
  115. switch indexPath.row {
  116. case 0:
  117. cell.title = "聊天文件"
  118. case 1:
  119. cell.isSwitchOn = user.isNoDisturb
  120. cell.title = "消息免打扰"
  121. // case 1:
  122. // cell.isSwitchOn = JMessage.isSetGlobalNoDisturb()
  123. // cell.title = "清理缓存"
  124. case 2:
  125. cell.title = "清空聊天记录"
  126. default:
  127. break
  128. }
  129. }
  130. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  131. tableView.deselectRow(at: indexPath, animated: true)
  132. if indexPath.section == 1 {
  133. switch indexPath.row {
  134. case 0:
  135. let vc = FileManagerViewController()
  136. let conv = JMSGConversation.singleConversation(withUsername: user.username)
  137. vc.conversation = conv
  138. navigationController?.pushViewController(vc, animated: true)
  139. case 2:
  140. let actionSheet = UIActionSheet(title: nil, delegate: self, cancelButtonTitle: "取消", destructiveButtonTitle: nil, otherButtonTitles: "清空聊天记录")
  141. actionSheet.tag = 1001
  142. actionSheet.show(in: view)
  143. case 3:
  144. break
  145. default:
  146. break
  147. }
  148. }
  149. }
  150. }
  151. extension JCSingleSettingViewController: UIActionSheetDelegate {
  152. func actionSheet(_ actionSheet: UIActionSheet, clickedButtonAt buttonIndex: Int) {
  153. // if actionSheet.tag == 1001 {
  154. // // SDK 暂无该功能
  155. // }
  156. if actionSheet.tag == 1001 {
  157. if buttonIndex == 1 {
  158. let conv = JMSGConversation.singleConversation(withUsername: user.username)
  159. conv?.deleteAllMessages()
  160. NotificationCenter.default.post(name: Notification.Name(rawValue: kDeleteAllMessage), object: nil)
  161. MBProgressHUD_JChat.show(text: "成功清空", view: view)
  162. }
  163. }
  164. }
  165. }
  166. extension JCSingleSettingViewController: JCMineInfoCellDelegate {
  167. func mineInfoCell(clickSwitchButton button: UISwitch, indexPath: IndexPath?) {
  168. if user.isNoDisturb != button.isOn {
  169. MBProgressHUD_JChat.showMessage(message: "修改中", toView: view)
  170. user.setIsNoDisturb(button.isOn, handler: { (result, error) in
  171. MBProgressHUD_JChat.hide(forView: self.view, animated: true)
  172. if error == nil {
  173. MBProgressHUD_JChat.show(text: "修改成功", view: self.view)
  174. } else {
  175. MBProgressHUD_JChat.show(text: "修改失败", view: self.view)
  176. }
  177. })
  178. }
  179. }
  180. }
  181. extension JCSingleSettingViewController: JCButtonCellDelegate {
  182. func buttonCell(clickButton button: UIButton) {
  183. if user.isFriend {
  184. let alertView = UIAlertView(title: "删除好友", message: "是否确认删除该好友?", delegate: self, cancelButtonTitle: "取消", otherButtonTitles: "删除")
  185. alertView.show()
  186. } else {
  187. let vc = JCAddFriendViewController()
  188. vc.user = user
  189. navigationController?.pushViewController(vc, animated: true)
  190. }
  191. }
  192. }
  193. extension JCSingleSettingViewController: UIAlertViewDelegate {
  194. func alertView(_ alertView: UIAlertView, clickedButtonAt buttonIndex: Int) {
  195. if buttonIndex == 1 {
  196. JMSGFriendManager.removeFriend(withUsername: user.username, appKey: user.appKey, completionHandler: { (result, error) in
  197. if error == nil {
  198. let conv = JMSGConversation.singleConversation(withUsername: self.user.username)
  199. if conv != nil {
  200. JMSGConversation.deleteSingleConversation(withUsername: self.user.username)
  201. }
  202. NotificationCenter.default.post(name: Notification.Name(rawValue: kUpdateFriendList), object: nil)
  203. self.navigationController?.popToRootViewController(animated: true)
  204. } else {
  205. MBProgressHUD_JChat.show(text: "\(String.errorAlert(error! as NSError))", view: self.view)
  206. }
  207. })
  208. }
  209. }
  210. }
  211. extension JCSingleSettingViewController: JCSingleSettingCellDelegate {
  212. func singleSettingCell(clickAddButton button: UIButton) {
  213. let vc = JCUpdateMemberViewController()
  214. vc.isAddMember = false
  215. vc.currentUser = user
  216. navigationController?.pushViewController(vc, animated: true)
  217. }
  218. func singleSettingCell(clickAvatorButton button: UIButton) {
  219. let vc = JCUserInfoViewController()
  220. vc.user = user
  221. vc.isOnConversation = true
  222. navigationController?.pushViewController(vc, animated: true)
  223. }
  224. }
  225. extension JCSingleSettingViewController: UIGestureRecognizerDelegate {
  226. public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
  227. return true
  228. }
  229. }