MessageMainViewController.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. //
  2. // MessageMainViewController.swift
  3. // RainbowPlanet
  4. //
  5. // Created by 南鑫林 on 2019/11/14.
  6. // Copyright © 2019 RainbowPlanet. All rights reserved.
  7. //
  8. import UIKit
  9. import Lottie
  10. import ESTabBarController_swift
  11. import Kingfisher
  12. import RongIMLib
  13. import SwiftyJSON
  14. import ObjectMapper
  15. import SwiftyMediator
  16. class MessageMainViewController: BaseViewController {
  17. override func didReceiveMemoryWarning() {
  18. super.didReceiveMemoryWarning()
  19. KingfisherManager.shared.cache.clearDiskCache()
  20. KingfisherManager.shared.cache.clearMemoryCache()
  21. }
  22. deinit {
  23. NXLLog("deinit")
  24. if observe != nil {
  25. NotificationCenter.default.removeObserver(observe!)
  26. }
  27. }
  28. weak var observe : NSObjectProtocol?
  29. var messageIndexModel : MessageIndexModel?
  30. var users : Array<Any>?
  31. var conversationModels : Array<ConversationModel>?
  32. var rcConversations : Array<RCConversation>?
  33. override func viewDidLoad() {
  34. super.viewDidLoad()
  35. setupViews()
  36. setupData()
  37. }
  38. override func viewWillAppear(_ animated: Bool) {
  39. super.viewWillAppear(animated)
  40. setConversationList()
  41. }
  42. override func setupViews() {
  43. navigationBar.title = "消息"
  44. view.backgroundColor = kf7f8faColor
  45. view.addSubview(tableView)
  46. view.insertSubview(navigationBar, aboveSubview: tableView)
  47. }
  48. override func setupData() {
  49. observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("MessageMainViewController"), object: nil, queue: OperationQueue.main) {
  50. [weak self] notification in
  51. self?.messageIndexModel = notification.object as? MessageIndexModel
  52. self?.tableView.reloadData()
  53. BaseTabbarViewController.shared.setBadge()
  54. }
  55. observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("RCIMReceiveMessageDelegate"), object: nil, queue: OperationQueue.main) {
  56. [weak self] notification in
  57. self?.setConversationList()
  58. }
  59. tableView.addHeader(withBeginRefresh: true, animation: true) {
  60. [weak self] (page) in
  61. self?.userMemberMessageIndexApi()
  62. self?.setConversationList()
  63. }
  64. observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("MessageModuleTop"), object: nil, queue: OperationQueue.main, using: {
  65. [weak self] (notification) in
  66. if self?.tableView.contentOffset == CGPoint(x: 0, y: 0) {
  67. self?.tableView.mj_header?.beginRefreshing()
  68. }else {
  69. self?.tableView.scrollToTop()
  70. }
  71. })
  72. }
  73. lazy var tableView: UITableView = {
  74. let tableView = UITableView(frame: CGRect(x: 0, y: kNavBarTotalHeight, width: kScreenWidth, height: kScreenHeight - kNavBarTotalHeight), style: UITableView.Style.grouped)
  75. tableView.separatorStyle = .none
  76. tableView.backgroundColor = kf7f8faColor
  77. tableView.dataSource = self
  78. tableView.delegate = self
  79. return tableView
  80. }()
  81. }
  82. extension MessageMainViewController {
  83. /// 用户消息首页
  84. func userMemberMessageIndexApi() {
  85. SwiftMoyaNetWorkServiceUser.shared().userMemberMessageIndexApi(completion: {
  86. [weak self] (messageIndexModel) -> (Void) in
  87. self?.messageIndexModel = messageIndexModel as? MessageIndexModel
  88. self?.tableView.reloadData()
  89. BaseTabbarViewController.shared.setBadge()
  90. MJRefreshManager.mjRefreshManager(tableView: self?.tableView)
  91. }) {
  92. [weak self] loadingStauts in
  93. MJRefreshManager.mjRefreshManagerLoadingStatus(tableView: self?.tableView, loadingStatus: loadingStauts)
  94. }
  95. }
  96. /// 用户消息阅读
  97. func userMemberReadMessageApi(messageShowTypes:String,completion: @escaping () -> Void){
  98. SwiftMoyaNetWorkServiceUser.shared().userMemberReadMessageApi(messageShowTypes: messageShowTypes) { (data) -> (Void) in
  99. completion()
  100. }
  101. }
  102. /// 融云
  103. func setConversationList() {
  104. self.users = RCIMClient.shared()?.getConversationList([RCConversationType.ConversationType_PRIVATE.rawValue])
  105. self.conversationModels = Mapper<ConversationModel>().mapArray(JSONString: self.users?.description ?? "[]")
  106. self.tableView.reloadData()
  107. BaseTabbarViewController.shared.setBadge()
  108. NXLLog(self.users)
  109. }
  110. }
  111. extension MessageMainViewController {
  112. /// push到消息控制器
  113. ///
  114. /// - Parameters:
  115. /// - messageVCType: 类型
  116. /// - messageShowTypes: key
  117. func pushMessageListController(messageVCType:MessageListVCType,messageShowTypes:String) {
  118. let vc = MessageListController()
  119. vc.messageVCType = messageVCType
  120. vc.messageShowTypes = messageShowTypes
  121. self.navigationController?.pushViewController(vc, animated: true)
  122. BaseTabbarViewController.shared.setBadge()
  123. }
  124. }
  125. extension MessageMainViewController: UITableViewDelegate,UITableViewDataSource {
  126. func numberOfSections(in tableView: UITableView) -> Int {
  127. return 3
  128. }
  129. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  130. switch section {
  131. case 0:
  132. return 1
  133. case 1:
  134. return 2
  135. case 2:
  136. return self.conversationModels?.count ?? 0
  137. default:
  138. return 0
  139. }
  140. }
  141. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  142. switch indexPath.section {
  143. case 0:
  144. let cell = MessageHomeOneSectionTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
  145. cell.messageIndexModel = messageIndexModel
  146. return cell
  147. case 1:
  148. let cell = MessageHomeTwoSectionTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
  149. if self.messageIndexModel != nil {
  150. switch indexPath.row {
  151. case 0:// 星球通知
  152. cell.messageModel = (self.messageIndexModel?.notification)!
  153. default:// 星球活动
  154. cell.messageModel = (self.messageIndexModel?.activity)!
  155. }
  156. }
  157. return cell
  158. case 2:
  159. let cell = MessageHomeThreeSectionTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
  160. cell.conversationModel = self.conversationModels?[indexPath.row]
  161. return cell
  162. default:
  163. return UITableViewCell()
  164. }
  165. }
  166. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  167. switch indexPath.section {
  168. case 1:
  169. switch indexPath.row {
  170. case 0:// 星球通知
  171. if self.messageIndexModel?.notification?.isYes == 1 {
  172. userMemberReadMessageApi(messageShowTypes: self.messageIndexModel?.notification?.key ?? "") {
  173. [weak self] in
  174. self?.messageIndexModel?.notification?.isYes = 0
  175. tableView.reloadRows(at: [indexPath], with: UITableView.RowAnimation.none)
  176. let vc = MessagePlanetNotiController()
  177. vc.messageShowTypes = self?.messageIndexModel?.notification?.key ?? ""
  178. self?.navigationController?.pushViewController(vc, animated: true)
  179. let messageIndexModel = MessageIndexModel.shared.object()
  180. messageIndexModel?.notification?.isYes = self?.messageIndexModel?.notification?.isYes
  181. MessageIndexModel.shared.saveObject(model: messageIndexModel!)
  182. BaseTabbarViewController.shared.setBadge()
  183. }
  184. }else {
  185. let vc = MessagePlanetNotiController()
  186. vc.messageShowTypes = self.messageIndexModel?.notification?.key ?? ""
  187. self.navigationController?.pushViewController(vc, animated: true)
  188. BaseTabbarViewController.shared.setBadge()
  189. }
  190. default:// 星球活动
  191. if self.messageIndexModel?.activity?.isYes == 1 {
  192. userMemberReadMessageApi(messageShowTypes: self.messageIndexModel?.activity?.key ?? "") {
  193. [weak self] in
  194. self?.messageIndexModel?.activity?.isYes = 0
  195. tableView.reloadRows(at: [indexPath], with: UITableView.RowAnimation.none)
  196. let vc = MessagePlanetActivityController()
  197. vc.messageShowTypes = self?.messageIndexModel?.activity?.key ?? ""
  198. self?.navigationController?.pushViewController(vc, animated: true)
  199. let messageIndexModel = MessageIndexModel.shared.object()
  200. messageIndexModel?.activity?.isYes = self?.messageIndexModel?.activity?.isYes
  201. MessageIndexModel.shared.saveObject(model: messageIndexModel!)
  202. BaseTabbarViewController.shared.setBadge()
  203. }
  204. }else {
  205. let vc = MessagePlanetActivityController()
  206. vc.messageShowTypes = self.messageIndexModel?.activity?.key ?? ""
  207. self.navigationController?.pushViewController(vc, animated: true)
  208. BaseTabbarViewController.shared.setBadge()
  209. }
  210. }
  211. case 2:
  212. let cell = tableView.cellForRow(at: indexPath) as? MessageHomeThreeSectionTableViewCell
  213. Mediator.push(RongCloudIMRouterModuleType.IMChatPrivate(targetId: cell?.conversationModel?.targetId ?? "0", title: cell?.conversationTitle.text ?? ""))
  214. default:
  215. break
  216. }
  217. }
  218. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  219. switch indexPath.section {
  220. case 0:
  221. return 107
  222. case 1,2,3:
  223. return 70
  224. default:
  225. return 0
  226. }
  227. }
  228. func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
  229. if indexPath.section == 2 {
  230. return true
  231. }
  232. return false
  233. }
  234. func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
  235. return .delete
  236. }
  237. func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
  238. if editingStyle == .delete {
  239. let cell = tableView.cellForRow(at: indexPath) as? MessageHomeThreeSectionTableViewCell
  240. RCIMClient.shared()?.remove(RCConversationType.ConversationType_PRIVATE, targetId: cell?.rcConversation?.targetId)
  241. self.conversationModels?.remove(at: indexPath.row)
  242. tableView.deleteRows(at: [indexPath], with: UITableView.RowAnimation.left)
  243. }
  244. }
  245. func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
  246. return "删除"
  247. }
  248. @available(iOS 11.0, *)
  249. func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
  250. switch indexPath.section {
  251. case 2:
  252. let cell = tableView.cellForRow(at: indexPath) as? MessageHomeThreeSectionTableViewCell
  253. let deleteRowAction = UIContextualAction(style: UIContextualAction.Style.destructive, title: "删除") { [weak self] (contextualAction, view, completionHandler) in
  254. guard let strongSelf = self else { return }
  255. RCIMClient.shared()?.remove(RCConversationType.ConversationType_PRIVATE, targetId: cell?.rcConversation?.targetId)
  256. strongSelf.conversationModels?.remove(at: indexPath.row)
  257. tableView.deleteRows(at: [indexPath], with: UITableView.RowAnimation.left)
  258. }
  259. deleteRowAction.backgroundColor = .red
  260. var topRowtitle : String?
  261. if cell?.rcConversationModel?.isTop ?? false {
  262. topRowtitle = "取消置顶"
  263. } else {
  264. topRowtitle = "置顶"
  265. }
  266. let topRowAction = UIContextualAction(style: UIContextualAction.Style.destructive, title: topRowtitle) { [weak self] (contextualAction, view, completionHandler) in
  267. if cell?.rcConversationModel?.isTop ?? false {
  268. RCIMClient.shared()?.setConversationToTop(RCConversationType.ConversationType_PRIVATE, targetId: cell?.rcConversation?.targetId, isTop: false)
  269. } else {
  270. RCIMClient.shared()?.setConversationToTop(RCConversationType.ConversationType_PRIVATE, targetId: cell?.rcConversation?.targetId, isTop: true)
  271. }
  272. self?.setConversationList()
  273. }
  274. topRowAction.backgroundColor = kThemeColor
  275. let config = UISwipeActionsConfiguration(actions: [deleteRowAction,topRowAction])
  276. return config
  277. default:
  278. return nil
  279. }
  280. }
  281. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  282. if section == 2 {
  283. return 0.000001
  284. }
  285. return 10
  286. }
  287. func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  288. return nil
  289. }
  290. func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  291. return 0.000001
  292. }
  293. func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
  294. return nil
  295. }
  296. }