123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340 |
- //
- // MessageMainViewController.swift
- // RainbowPlanet
- //
- // Created by 南鑫林 on 2019/11/14.
- // Copyright © 2019 RainbowPlanet. All rights reserved.
- //
- import UIKit
- import Lottie
- import ESTabBarController_swift
- import Kingfisher
- import RongIMLib
- import SwiftyJSON
- import ObjectMapper
- import SwiftyMediator
- class MessageMainViewController: BaseViewController {
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- KingfisherManager.shared.cache.clearDiskCache()
- KingfisherManager.shared.cache.clearMemoryCache()
- }
- deinit {
- NXLLog("deinit")
- if observe != nil {
- NotificationCenter.default.removeObserver(observe!)
- }
- }
- weak var observe : NSObjectProtocol?
-
- var messageIndexModel : MessageIndexModel?
- var users : Array<Any>?
- var conversationModels : Array<ConversationModel>?
- var rcConversations : Array<RCConversation>?
-
-
- override func viewDidLoad() {
- super.viewDidLoad()
- setupViews()
- setupData()
- }
-
- override func viewWillAppear(_ animated: Bool) {
- super.viewWillAppear(animated)
- setConversationList()
- }
-
- override func setupViews() {
- navigationBar.title = "消息"
- view.backgroundColor = kf7f8faColor
- view.addSubview(tableView)
- view.insertSubview(navigationBar, aboveSubview: tableView)
- }
-
- override func setupData() {
-
- observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("MessageMainViewController"), object: nil, queue: OperationQueue.main) {
- [weak self] notification in
- self?.messageIndexModel = notification.object as? MessageIndexModel
- self?.tableView.reloadData()
- BaseTabbarViewController.shared.setBadge()
- }
-
- observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("RCIMReceiveMessageDelegate"), object: nil, queue: OperationQueue.main) {
- [weak self] notification in
- self?.setConversationList()
- }
-
- tableView.addHeader(withBeginRefresh: true, animation: true) {
- [weak self] (page) in
- self?.userMemberMessageIndexApi()
- self?.setConversationList()
- }
-
- observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("MessageModuleTop"), object: nil, queue: OperationQueue.main, using: {
- [weak self] (notification) in
- if self?.tableView.contentOffset == CGPoint(x: 0, y: 0) {
- self?.tableView.mj_header?.beginRefreshing()
- }else {
- self?.tableView.scrollToTop()
- }
- })
- }
-
- lazy var tableView: UITableView = {
- let tableView = UITableView(frame: CGRect(x: 0, y: kNavBarTotalHeight, width: kScreenWidth, height: kScreenHeight - kNavBarTotalHeight), style: UITableView.Style.grouped)
- tableView.separatorStyle = .none
- tableView.backgroundColor = kf7f8faColor
- tableView.dataSource = self
- tableView.delegate = self
- return tableView
- }()
-
- }
- extension MessageMainViewController {
-
- /// 用户消息首页
- func userMemberMessageIndexApi() {
- SwiftMoyaNetWorkServiceUser.shared().userMemberMessageIndexApi(completion: {
- [weak self] (messageIndexModel) -> (Void) in
- self?.messageIndexModel = messageIndexModel as? MessageIndexModel
- self?.tableView.reloadData()
- BaseTabbarViewController.shared.setBadge()
- MJRefreshManager.mjRefreshManager(tableView: self?.tableView)
- }) {
- [weak self] loadingStauts in
- MJRefreshManager.mjRefreshManagerLoadingStatus(tableView: self?.tableView, loadingStatus: loadingStauts)
- }
- }
-
- /// 用户消息阅读
- func userMemberReadMessageApi(messageShowTypes:String,completion: @escaping () -> Void){
- SwiftMoyaNetWorkServiceUser.shared().userMemberReadMessageApi(messageShowTypes: messageShowTypes) { (data) -> (Void) in
- completion()
- }
- }
-
- /// 融云
- func setConversationList() {
- self.users = RCIMClient.shared()?.getConversationList([RCConversationType.ConversationType_PRIVATE.rawValue])
- self.conversationModels = Mapper<ConversationModel>().mapArray(JSONString: self.users?.description ?? "[]")
- self.tableView.reloadData()
- BaseTabbarViewController.shared.setBadge()
- NXLLog(self.users)
- }
-
- }
- extension MessageMainViewController {
-
- /// push到消息控制器
- ///
- /// - Parameters:
- /// - messageVCType: 类型
- /// - messageShowTypes: key
- func pushMessageListController(messageVCType:MessageListVCType,messageShowTypes:String) {
- let vc = MessageListController()
- vc.messageVCType = messageVCType
- vc.messageShowTypes = messageShowTypes
- self.navigationController?.pushViewController(vc, animated: true)
- BaseTabbarViewController.shared.setBadge()
- }
- }
- extension MessageMainViewController: UITableViewDelegate,UITableViewDataSource {
- func numberOfSections(in tableView: UITableView) -> Int {
- return 3
- }
-
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- switch section {
- case 0:
- return 1
- case 1:
- return 2
- case 2:
- return self.conversationModels?.count ?? 0
- default:
- return 0
- }
- }
-
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- switch indexPath.section {
- case 0:
- let cell = MessageHomeOneSectionTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
- cell.messageIndexModel = messageIndexModel
- return cell
- case 1:
- let cell = MessageHomeTwoSectionTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
- if self.messageIndexModel != nil {
- switch indexPath.row {
- case 0:// 星球通知
- cell.messageModel = (self.messageIndexModel?.notification)!
- default:// 星球活动
- cell.messageModel = (self.messageIndexModel?.activity)!
- }
- }
- return cell
- case 2:
- let cell = MessageHomeThreeSectionTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
- cell.conversationModel = self.conversationModels?[indexPath.row]
- return cell
- default:
- return UITableViewCell()
- }
- }
-
- func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
- switch indexPath.section {
- case 1:
- switch indexPath.row {
- case 0:// 星球通知
- if self.messageIndexModel?.notification?.isYes == 1 {
- userMemberReadMessageApi(messageShowTypes: self.messageIndexModel?.notification?.key ?? "") {
- [weak self] in
- self?.messageIndexModel?.notification?.isYes = 0
- tableView.reloadRows(at: [indexPath], with: UITableView.RowAnimation.none)
- let vc = MessagePlanetNotiController()
- vc.messageShowTypes = self?.messageIndexModel?.notification?.key ?? ""
- self?.navigationController?.pushViewController(vc, animated: true)
- let messageIndexModel = MessageIndexModel.shared.object()
- messageIndexModel?.notification?.isYes = self?.messageIndexModel?.notification?.isYes
- MessageIndexModel.shared.saveObject(model: messageIndexModel!)
- BaseTabbarViewController.shared.setBadge()
- }
- }else {
- let vc = MessagePlanetNotiController()
- vc.messageShowTypes = self.messageIndexModel?.notification?.key ?? ""
- self.navigationController?.pushViewController(vc, animated: true)
- BaseTabbarViewController.shared.setBadge()
- }
- default:// 星球活动
- if self.messageIndexModel?.activity?.isYes == 1 {
- userMemberReadMessageApi(messageShowTypes: self.messageIndexModel?.activity?.key ?? "") {
- [weak self] in
- self?.messageIndexModel?.activity?.isYes = 0
- tableView.reloadRows(at: [indexPath], with: UITableView.RowAnimation.none)
- let vc = MessagePlanetActivityController()
- vc.messageShowTypes = self?.messageIndexModel?.activity?.key ?? ""
- self?.navigationController?.pushViewController(vc, animated: true)
-
- let messageIndexModel = MessageIndexModel.shared.object()
- messageIndexModel?.activity?.isYes = self?.messageIndexModel?.activity?.isYes
- MessageIndexModel.shared.saveObject(model: messageIndexModel!)
- BaseTabbarViewController.shared.setBadge()
- }
- }else {
- let vc = MessagePlanetActivityController()
- vc.messageShowTypes = self.messageIndexModel?.activity?.key ?? ""
- self.navigationController?.pushViewController(vc, animated: true)
- BaseTabbarViewController.shared.setBadge()
- }
- }
- case 2:
- let cell = tableView.cellForRow(at: indexPath) as? MessageHomeThreeSectionTableViewCell
- Mediator.push(RongCloudIMRouterModuleType.IMChatPrivate(targetId: cell?.conversationModel?.targetId ?? "0", title: cell?.conversationTitle.text ?? ""))
-
- default:
- break
- }
- }
-
- func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
- switch indexPath.section {
- case 0:
- return 107
- case 1,2,3:
- return 70
- default:
- return 0
- }
- }
-
- func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
- if indexPath.section == 2 {
- return true
- }
- return false
- }
-
- func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
- return .delete
- }
-
- func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
- if editingStyle == .delete {
- let cell = tableView.cellForRow(at: indexPath) as? MessageHomeThreeSectionTableViewCell
- RCIMClient.shared()?.remove(RCConversationType.ConversationType_PRIVATE, targetId: cell?.rcConversation?.targetId)
- self.conversationModels?.remove(at: indexPath.row)
- tableView.deleteRows(at: [indexPath], with: UITableView.RowAnimation.left)
- }
- }
-
- func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
- return "删除"
- }
-
- @available(iOS 11.0, *)
- func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
- switch indexPath.section {
- case 2:
- let cell = tableView.cellForRow(at: indexPath) as? MessageHomeThreeSectionTableViewCell
- let deleteRowAction = UIContextualAction(style: UIContextualAction.Style.destructive, title: "删除") { [weak self] (contextualAction, view, completionHandler) in
- guard let strongSelf = self else { return }
- RCIMClient.shared()?.remove(RCConversationType.ConversationType_PRIVATE, targetId: cell?.rcConversation?.targetId)
- strongSelf.conversationModels?.remove(at: indexPath.row)
- tableView.deleteRows(at: [indexPath], with: UITableView.RowAnimation.left)
- }
- deleteRowAction.backgroundColor = .red
-
- var topRowtitle : String?
- if cell?.rcConversationModel?.isTop ?? false {
- topRowtitle = "取消置顶"
- } else {
- topRowtitle = "置顶"
- }
- let topRowAction = UIContextualAction(style: UIContextualAction.Style.destructive, title: topRowtitle) { [weak self] (contextualAction, view, completionHandler) in
- if cell?.rcConversationModel?.isTop ?? false {
- RCIMClient.shared()?.setConversationToTop(RCConversationType.ConversationType_PRIVATE, targetId: cell?.rcConversation?.targetId, isTop: false)
- } else {
- RCIMClient.shared()?.setConversationToTop(RCConversationType.ConversationType_PRIVATE, targetId: cell?.rcConversation?.targetId, isTop: true)
- }
- self?.setConversationList()
- }
- topRowAction.backgroundColor = kThemeColor
- let config = UISwipeActionsConfiguration(actions: [deleteRowAction,topRowAction])
- return config
- default:
- return nil
- }
-
- }
-
- func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
- if section == 2 {
- return 0.000001
- }
- return 10
- }
-
- func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
- return nil
- }
-
- func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
- return 0.000001
-
- }
-
- func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
- return nil
- }
-
- }
|