403 lines
17 KiB
Swift
403 lines
17 KiB
Swift
//
|
|
// RoomVC.swift
|
|
// yinmeng-ios
|
|
//
|
|
// Created by yinmeng 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 {
|
|
YinRoomAudioManager.share.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] type in
|
|
guard let self = self else { return}
|
|
if type == .chat{
|
|
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)
|
|
}
|
|
}else{
|
|
let sendGiftVC = SendGiftVC()
|
|
let nav = BaseNavigationViewController.init(rootViewController: sendGiftVC)
|
|
nav.modalPresentationStyle = .overCurrentContext
|
|
let list = micSeatView.getMicUser()
|
|
var userList:[SendGiftUserModel] = []
|
|
for item:[String:UserObject] in list {
|
|
|
|
|
|
if let first = item.keys.first,let user = item[first],let uid = user.uid,uid != AuthManager.userUid{
|
|
let model = SendGiftUserModel()
|
|
model.position = first
|
|
model.nick = user.nick ?? ""
|
|
model.avatar = user.avatar ?? ""
|
|
model.uid = user.uid ?? 0
|
|
userList.append(model)
|
|
|
|
}
|
|
|
|
}
|
|
sendGiftVC.userList = userList
|
|
sendGiftVC.roomUid = self.roomUid
|
|
sendGiftVC.roomId = self.roomInfo?.roomId ?? ""
|
|
sendGiftVC.userData = self.userData
|
|
self.present(nav, animated: true, completion: nil)
|
|
}
|
|
|
|
}
|
|
micSeatView.showUserInfoBlock = {[weak self] user in
|
|
guard let weakSelf = self else { return }
|
|
let userInfoView:HomeVoiceUserInfoView = HomeVoiceUserInfoView.init(frame: .zero)
|
|
userInfoView.clickBtnBlock = {[weak self] (type,uid) in
|
|
guard let weakSelf = self else {
|
|
return
|
|
}
|
|
if type == 0{
|
|
let session = NIMSession(uid, type: .P2P)
|
|
let chatVC = ChatVC.init(session: session)
|
|
weakSelf.navigationController?.pushViewController(chatVC, animated: true)
|
|
}else if (type == 5){
|
|
if let first = user.keys.first,let userModel = user[first],let uid = userModel.uid,uid != AuthManager.userUid{
|
|
var userList:[SendGiftUserModel] = []
|
|
let model = SendGiftUserModel()
|
|
model.position = first
|
|
model.nick = userModel.nick ?? ""
|
|
model.avatar = userModel.avatar ?? ""
|
|
model.uid = userModel.uid ?? 0
|
|
userList.append(model)
|
|
let sendGiftVC = SendGiftVC()
|
|
let nav = BaseNavigationViewController.init(rootViewController: sendGiftVC)
|
|
nav.modalPresentationStyle = .overCurrentContext
|
|
sendGiftVC.userList = userList
|
|
sendGiftVC.roomUid = weakSelf.roomUid
|
|
sendGiftVC.roomId = weakSelf.roomInfo?.roomId ?? ""
|
|
sendGiftVC.userData = weakSelf.userData
|
|
sendGiftVC.isSendPerson = true
|
|
weakSelf.present(nav, animated: true, completion: nil)
|
|
}
|
|
}
|
|
|
|
}
|
|
userInfoView.type = 2
|
|
userInfoView.user = user.values.first
|
|
weakSelf.view.addSubview(userInfoView)
|
|
userInfoView.snp.makeConstraints { make in
|
|
make.edges.equalTo(weakSelf.view)
|
|
}
|
|
}
|
|
topView.didClickTypeBlcok = {[weak self] (type,uid) in
|
|
guard let self = self else { return}
|
|
if type == 0{
|
|
YinRoomAudioManager.share.exitRoom()
|
|
RoomVCViewModel.roomVM.isMute = true
|
|
NIMSDK.shared().chatroomManager.exitChatroom(self.roomInfo?.roomId ?? "")
|
|
self.dismiss(animated: true, completion: nil)
|
|
|
|
}else if type == 1 {
|
|
|
|
YinRoomAudioManager.share.exitRoom()
|
|
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)
|
|
}
|
|
|
|
}else{
|
|
let web = WebViewController(url: uid,isHalf: true)
|
|
self.present(web, 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)
|
|
view.addSubview(roomEffectView)
|
|
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))
|
|
}
|
|
roomEffectView.snp.makeConstraints { make in
|
|
make.edges.equalTo(view)
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
self?.userData = 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){
|
|
topView.roomData = self.roomInfo
|
|
micSeatView.roomData = self.roomInfo
|
|
if let roomInfo = self.roomInfo{
|
|
YinRoomAudioManager.share.dealWithChatData(roomData:roomInfo)
|
|
}
|
|
micSeatView.setMicList()
|
|
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?
|
|
var userData:UserObject?
|
|
//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
|
|
}()
|
|
private lazy var roomEffectView:RoomDynamicEffectView = {
|
|
let _roomEffectView = RoomDynamicEffectView(frame: .zero)
|
|
_roomEffectView.isUserInteractionEnabled = false
|
|
return _roomEffectView
|
|
}()
|
|
|
|
}
|
|
|
|
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
|
|
case .closed:
|
|
print("111")
|
|
break
|
|
|
|
default:
|
|
|
|
break
|
|
}
|
|
}
|
|
self.topView.roomData = self.roomInfo
|
|
}else if msg.messageType == .text{
|
|
chatScreenView.getTextMsg(msg: msg)
|
|
}else if msg.messageType == .custom{
|
|
chatScreenView.dealWithCustomMsg(msg: msg)
|
|
roomEffectView.dealWithCustomMsg(msg: msg)
|
|
micSeatView.dealWithCustomMsg(msg: msg)
|
|
startPlayAnimation(msg: msg)
|
|
}
|
|
micSeatView.dealWithNotMessage(msg: msg)
|
|
menuView.dealWithNotMessage(msg: msg)
|
|
|
|
}
|
|
}
|
|
func startPlayAnimation(msg:NIMMessage){
|
|
if let obj = msg.messageObject as? NIMCustomObject,let customObj = obj.attachment as? ChatCustomMsgModel{
|
|
if customObj.first == 3 || customObj.first == 12{
|
|
if let model = Deserialized<RectiveGiftModel>.toModel(with: customObj.data),let gift = model.gift == nil ? model.giftInfo : model.gift {
|
|
var userList:[String] = []
|
|
if userList.count <= 0 {
|
|
for item in model.targetUsers {
|
|
userList.append("\(item.uid)")
|
|
}
|
|
if userList.count <= 0{
|
|
userList.append(model.targetUid)
|
|
}
|
|
}
|
|
var starPoint = CGPoint(x: ScreenWidth/2, y: NavHeight)
|
|
if let point = micSeatView.getPointWithCell(uid: model.uid){
|
|
if point.x <= 0 || point.y <= 0{
|
|
|
|
}else{
|
|
starPoint = point
|
|
}
|
|
}
|
|
|
|
var curPoint = CGPoint(x: ScreenWidth/2, y: NavHeight)
|
|
for userUid in userList{
|
|
|
|
if let point = micSeatView.getPointWithCell(uid: userUid){
|
|
if point.x <= 0 || point.y <= 0{
|
|
|
|
}else{
|
|
curPoint = point
|
|
}
|
|
}
|
|
|
|
roomEffectView.startPlayAnimation(url: gift.giftUrl, start: starPoint, end: curPoint)
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
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)
|
|
}else if message.messageType == .custom{
|
|
chatScreenView.dealWithCustomMsg(msg: message)
|
|
roomEffectView.dealWithCustomMsg(msg: message)
|
|
startPlayAnimation(msg: message)
|
|
micSeatView.dealWithCustomMsg(msg: message)
|
|
}
|
|
}
|
|
func updateOnlineNum(num:Int){
|
|
guard var roomInfo = self.roomInfo else { return }
|
|
let onlineNum = roomInfo.onlineNum + num
|
|
roomInfo.onlineNum = onlineNum
|
|
self.roomInfo = roomInfo
|
|
}
|
|
}
|