// // MOLiveViewModel.swift // MiMoLive // // Created by OneeChan on 2025/9/26. // import Foundation @objc protocol MOLiveViewModelDelegate { @objc optional func onLiveViewModelRecvLineInvite(viewModel: MOLiveViewModel, invite: MORtmLivePKLinkInvite) } @objcMembers class MOLiveViewModel: NSObject { var curJoinedRoom: MOLiveDetail? var lineViewModel: MOLineViewModel? = nil let giftViewModel = MOGiftListViewModel() var isOwner: Bool { curJoinedRoom?.currentRoom.anchorUser.id == UserDefaults.uid } var ownerUid: String? { curJoinedRoom?.currentRoom.anchorUser.id } var curRoomId: String? { curJoinedRoom?.currentRoom.id } override init() { super.init() MOEventDeliver.addObserver(self) } func onJoingRoom(_ room: MOLiveList) { reset() // 房间切换,需要重置所有数据 } func onRoomInfoChanged(_ info: MOStatusInfo) { curJoinedRoom?.statusInfo = info } func onJoinedRoom(_ room: MOLiveDetail) { self.curJoinedRoom = room checkLineStatus(room.statusInfo) giftViewModel.onJoinedRoom(room: room) if isOwner { MOLineViewModel.getLineConfig() } } // 后续需要将事件变化挪到本类自身处理 func handleLiveStateChanged(info: MOStatusInfo, showLinkOrPk: Bool) { if showLinkOrPk { checkLineStatus(info) } } func isHost(_ uid: String) -> Bool { guard let curJoinedRoom else { return false } // 是房主 if ownerUid == uid { return true } // 多人房主播 if let linkMics = curJoinedRoom.statusInfo.roomStatus.linkMics as? [MOLinkMic], linkMics.first(where: { $0.profile.id == uid }) != nil { return true } // line 对方主播 if let lineViewModel, lineViewModel.curPeerInfo?.userId == uid { return true } return false } } extension MOLiveViewModel { // TODO: Line 连线(后续需要迁移到 LineViewModel) func handleRoomRtm(entity: MORtmEntity) { guard let jsonEntity = entity.data as? MORtmJosnEntity else { return } switch (jsonEntity.type) { case 31: lineViewModel?.reloadLineRoomInfo() case 37: //邀请 guard let invite = jsonEntity.pkLinkInvite else { return } notifyLineInvite(invite: invite) case 38: //PK房间信息变化 guard let status = jsonEntity.pkV2Status else { return } lineViewModel?.handleLinePkRtm(status) case 39: //pk/连线 状态 信息 guard let expand = jsonEntity.pkV2StatusExpand else { return } lineViewModel?.handleLinePkExpandRtm(expand) case 40: // toast guard let toast = jsonEntity.rtmToast else { return } guard let text = toast.text else { return } MBProgressHUD.showTipMessage(inWindow: text) default: break; } } func checkLineStatus(_ newStatusInfo: MOStatusInfo) { guard curJoinedRoom != nil else { return } guard let pkInfo = newStatusInfo.roomStatus.linkPKInfo else { return } let lineRoomId = pkInfo.pkLinkRoomId guard lineViewModel?.curLineRoomId != lineRoomId else { // Line 房间号不变,已经加入了该 Line 房间 return } // Line 房间 ID 变化 if let lineViewModel, !lineViewModel.curLineRoomId.isEmpty { // 需要退出旧 Line 房间 lineViewModel.clear() // 将原来的 Line 房间 清理掉 self.lineViewModel = nil } if let lineRoomId, let token = pkInfo.agoraRtcToken, !lineRoomId.isEmpty, !token.isEmpty { // 创建新的 Line 房逻辑 self.lineViewModel = MOLineViewModel(lineRoomId: lineRoomId, token: token) } } } extension MOLiveViewModel { private func reset() { lineViewModel?.clear() lineViewModel = nil } } extension MOLiveViewModel { func notifyLineInvite(invite: MORtmLivePKLinkInvite) { MOEventDeliver.notifyEvent { $0.onLiveViewModelRecvLineInvite?(viewModel: self, invite: invite) } } } extension MOLiveViewModel: MOLineViewModelDelegate { func onLineJoinFailed(viewModel: MOLineViewModel) { guard viewModel == self.lineViewModel else { return } self.lineViewModel?.clear() self.lineViewModel = nil } func onLineStateChanged(state: LineState) { guard MOLiveManager.curLive == self else { return } if state == .none { // Line 结束,清理 Line 相关逻辑 self.lineViewModel = nil } } }