Files
yingmeng-ios-switf/yinmeng-ios/Modules/Room/VC/RoomVC.swift
2024-03-13 20:49:51 +08:00

261 lines
9.9 KiB
Swift

//
// RoomVC.swift
// yinmeng-ios
//
// Created by duoban on 2024/3/6.
//
import NIMSDK
import UIKit
import NSObject_Rx
class RoomVC: BaseViewController, HiddenNavigationBarProtocol {
init(roomUid: String) {
super.init(nibName:nil, bundle:nil)
self.roomUid = roomUid
}
deinit {
TRTCCloud.destroySharedIntance()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
setUILayout()
requestData()
addNIMSDKManager()
addChild(chatListVC)
menuView.clickMenuViewBlcok = {[weak self] in
guard let self = self else { return}
self.view.addSubview(self.bgChatView)
self.bgChatView.snp.makeConstraints { make in
make.edges.equalTo(self.view)
}
let height = ScreenHeight * 2 / 3
self.view.addSubview(self.chatListVC.view)
self.chatListVC.view.layer.cornerRadius = 20
self.chatListVC.view.layer.masksToBounds = true
self.chatListVC.view.frame = CGRect(x: 0, y: ScreenHeight, width: ScreenWidth, height: height)
UIView.animate(withDuration: 0.1) {
self.chatListVC.view.frame = CGRect(x: 0, y:ScreenHeight - height, width: ScreenWidth, height: height)
}
}
topView.didClickTypeBlcok = {[weak self] (type,uid) in
guard let self = self else { return}
if type == 0{
TRTCCloud.sharedInstance().exitRoom()
TRTCCloud.sharedInstance().muteAllRemoteAudio(false)
TRTCCloud.sharedInstance().muteLocalAudio(false)
RoomVCViewModel.roomVM.isMute = true
NIMSDK.shared().chatroomManager.exitChatroom(self.roomInfo?.roomId ?? "")
self.dismiss(animated: true, completion: nil)
return
}
TRTCCloud.sharedInstance().exitRoom()
TRTCCloud.sharedInstance().muteAllRemoteAudio(false)
TRTCCloud.sharedInstance().muteLocalAudio(false)
RoomVCViewModel.roomVM.isMute = true
NIMSDK.shared().chatroomManager.exitChatroom(self.roomInfo?.roomId ?? "")
self.dismiss(animated: true, completion: nil)
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
let vc = RoomVC(roomUid: uid)
let nav = BaseNavigationViewController.init(rootViewController: vc)
nav.modalPresentationStyle = .fullScreen
keyWindow.rootViewController?.present(nav, animated: true, completion: nil)
}
}
}
private func addNIMSDKManager(){
NIMSDK.shared().chatManager.add(self)
NIMSDK.shared().chatroomManager.add(self)
NIMSDK.shared().loginManager.add(self)
NIMSDK.shared().conversationManager.add(self)
}
private func setUILayout(){
view.addSubview(backgroundView)
view.addSubview(topView)
view.addSubview(micSeatView)
view.addSubview(chatScreenView)
view.addSubview(menuView)
backgroundView.snp.makeConstraints { make in
make.edges.equalTo(self.view)
}
topView.snp.makeConstraints { make in
make.leading.trailing.top.equalTo(self.view)
make.height.equalTo(UIDevice.scaleWidth(width: NavHeight))
}
micSeatView.snp.makeConstraints { make in
make.leading.trailing.equalTo(self.view)
make.top.equalTo(self.topView.snp.bottom).offset(UIDevice.scaleWidth(width: 30))
make.height.equalTo(UIDevice.scaleWidth(width: 340))
}
chatScreenView.snp.makeConstraints { make in
make.top.equalTo(micSeatView.snp.bottom).offset(UIDevice.scaleWidth(width: 5))
make.leading.equalTo(UIDevice.scaleWidth(width: 0))
make.trailing.equalTo(-UIDevice.scaleWidth(width: 90))
make.bottom.equalTo(menuView.snp.top).offset(-UIDevice.scaleWidth(width: 5))
}
let bottom = SafeAraeBottomHeight + UIDevice.scaleWidth(width: 8)
menuView.snp.makeConstraints { make in
make.leading.trailing.equalTo(view).inset(UIDevice.scaleWidth(width: 0))
make.bottom.equalTo(-bottom)
make.height.equalTo(UIDevice.scaleWidth(width: 40))
}
}
private func requestData(){
let uid = AuthManager.userUid
RoomVCViewModel.roomVM.getRoomUserInfo(roomUid: roomUid, uid: "\(uid)")
UserViewModel.userVM.selfInfo.subscribe(onNext: {[weak self] result in
if let roomid = self?.roomInfo?.roomId{
self?.chatScreenView.roomId = roomid
RoomVCViewModel.roomVM.NIMSDKEnterRoom(roomId:roomid , user: result)
self?.micSeatView.userData = result
self?.menuView.user = result
}
}).disposed(by: rx.disposeBag)
RoomVCViewModel.roomVM.roomData.subscribe {[weak self] result in
self?.dealWithRoomData(roomData: result)
}.disposed(by: rx.disposeBag)
RoomVCViewModel.roomVM.chatRoomData.subscribe {[weak self] room in
self?.dealWithChatData(chatData: room)
}.disposed(by: rx.disposeBag)
}
private func dealWithChatData(chatData:NIMChatroom){
self.topView.roomData = self.roomInfo
self.micSeatView.roomData = self.roomInfo
self.micSeatView.setTRTC()
self.micSeatView.setMicList()
self.menuView.roomUid = self.roomInfo?.roomId ?? ""
}
private func dealWithRoomData(roomData:RoomDataModel){
let uid = AuthManager.userUid
self.roomInfo = roomData
self.topView.roomData = roomData
if roomData.valid{
UserViewModel.userVM.getUserInfo(uid: uid)
}else{
var text = self.roomInfo?.title
if self.roomInfo?.title.count == 0{
text = self.roomText
}
RoomVCViewModel.roomVM.openRoom(title: text ?? "", type: 3, roomPwd: "", roomDesc: "", backPic: "")
}
}
@objc func dissChatViewAction(){
let height = ScreenHeight * 2 / 3
UIView.animate(withDuration: 0.1) {
self.chatListVC.view.frame = CGRect(x: 0, y: ScreenHeight, width: ScreenWidth, height: height)
}completion: { isCompletion in
self.bgChatView.removeFromSuperview()
self.chatListVC.view.removeFromSuperview()
}
}
var roomUid:String = ""
var roomText:String = ""
var roomInfo:RoomDataModel?
//MARK: -
private lazy var backgroundView:RoomBackgroundView = {
let _backgroundView = RoomBackgroundView(frame: .zero)
return _backgroundView
}()
private lazy var topView:RoomTopView = {
let _topView = RoomTopView(frame: .zero)
return _topView
}()
private lazy var micSeatView:NormalMicSeatSuperView = {
let _micSeatView = NormalMicSeatSuperView(frame: .zero)
return _micSeatView
}()
private lazy var chatScreenView:RoomChatScreenView = {
let _chatScreenView = RoomChatScreenView(frame: .zero)
return _chatScreenView
}()
private lazy var menuView:RoomMenuView = {
let _menuView = RoomMenuView(frame: .zero)
return _menuView
}()
private lazy var bgChatView:UIButton = {
let _bgChatView = UIButton()
_bgChatView.addTarget(self, action: #selector(dissChatViewAction), for: .touchUpInside)
_bgChatView.backgroundColor = UIColor.init(white: 0, alpha: 0.5)
return _bgChatView
}()
private lazy var chatListVC:ChatListVC = {
let _chatListVC = ChatListVC()
return _chatListVC
}()
}
extension RoomVC : NIMChatManagerDelegate, NIMChatroomManagerDelegate, NIMLoginManagerDelegate, NIMConversationManagerDelegate{
func onRecvMessages(_ messages: [NIMMessage]) {
for msg in messages {
guard let session = msg.session else { continue}
if session.sessionType != .chatroom{
continue
}
if session.sessionId != self.roomInfo?.roomId{continue}
if msg.messageType == .notification{
if let notMsg = msg.messageObject as? NIMNotificationObject,let content = notMsg.content as? NIMChatroomNotificationContent{
switch content.eventType {
case .enter:
updateOnlineNum(num: 1)
chatScreenView.getMsgListWithHistory()
break
case .addBlack:
updateOnlineNum(num: -1)
break
case .kicked:
updateOnlineNum(num: -1)
break
case .exit:
updateOnlineNum(num: -1)
break
default:
break
}
}
self.topView.roomData = self.roomInfo
}else if msg.messageType == .text{
chatScreenView.getTextMsg(msg: msg)
}
micSeatView.dealWithNotMessage(msg: msg)
menuView.dealWithNotMessage(msg: msg)
}
}
func send(_ message: NIMMessage, didCompleteWithError error: Error?) {
if let sessionId = message.session?.sessionId,let roomUid = self.roomInfo?.roomId,sessionId != roomUid{
return
}
if message.messageType == .text{
chatScreenView.getTextMsg(msg: message)
}
}
func updateOnlineNum(num:Int){
guard var roomInfo = self.roomInfo else { return }
let onlineNum = roomInfo.onlineNum + num
roomInfo.onlineNum = onlineNum
self.roomInfo = roomInfo
}
}