| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567 |
- //
- // LNRoomViewModel.swift
- // Gami
- //
- // Created by OneeChan on 2026/3/9.
- //
- import Foundation
- import AtomicXCore
- import Combine
- protocol LNRoomViewModelNotify {
- func onRoomMessageChanged(messages: [LNRoomChatMessageItem])
- func onRoomSeatsChanged()
- func onRoomSpeakingUsersChanged()
- func onRoomSeatApplyChanged()
- func onRoomInfoChanged()
- func onMySeatApplyChanged()
-
- func onRoomClosed()
- }
- extension LNRoomViewModelNotify {
- func onRoomMessageChanged(messages: [LNRoomChatMessageItem]) { }
- func onRoomSeatsChanged() { }
- func onRoomSpeakingUsersChanged() { }
- func onRoomSeatApplyChanged() { }
- func onRoomInfoChanged() { }
- func onMySeatApplyChanged() { }
-
- func onRoomClosed() { }
- }
- enum LNRoomSeatNum: Int, CaseIterable, Comparable {
- case none = -1
- case host = 0
- case guest
- case mic1
- case mic2
- case mic3
- case mic4
- case mic5
- case mic6
- case mic7
- case mic8
-
- static func < (lhs: LNRoomSeatNum, rhs: LNRoomSeatNum) -> Bool {
- lhs.rawValue < rhs.rawValue
- }
-
- var title: String {
- if case .host = self {
- .init(key: "A00328")
- } else if case .guest = self {
- .init(key: "A00329")
- } else {
- .init(key: "A00326", rawValue - 1)
- }
- }
- }
- enum LNApplyingSeatType: Int, Decodable {
- case none
- case guest
- case playmate
- }
- class LNRoomViewModel: NSObject {
- let roomId: String
- private let seatStore: LiveSeatStore
- private let guestStore: CoGuestStore
- private let messageStore: BarrageStore
- private let audienceStore: LiveAudienceStore
-
- private(set) var seatsInfo: [LNRoomSeatItem] = []
- private(set) var roomInfo = LNRoomInfo()
- private(set) var speakingUser: [String] = []
- private(set) var lastmessage: Barrage? = nil
- private(set) var seatApplyCount: Int = 0
- private(set) var waitingForSeat: LNApplyingSeatType = .none {
- didSet {
- if oldValue != waitingForSeat {
- LNEventDeliver.notifyEvent { ($0 as? LNRoomViewModelNotify)?.onMySeatApplyChanged() }
- }
- }
- }
- private let systemHandlerQueue = DispatchQueue(label: "com.gami.room.system.message", attributes: .concurrent)
-
- init(roomId: String) {
- self.roomId = roomId
-
- seatStore = LiveSeatStore.create(liveID: roomId)
- guestStore = CoGuestStore.create(liveID: roomId)
- messageStore = BarrageStore.create(liveID: roomId)
- audienceStore = LiveAudienceStore.create(liveID: roomId)
-
- super.init()
-
- setupSeatObservers()
- setupMessageObservers()
- setupRoomInfoObserver()
- setupAudienceEventObservers()
-
- LNRoomManager.shared.updateCurRoom(self)
- }
-
- func closeRoom() {
- LNRoomManager.shared.closeRoom { success in
- LNEventDeliver.notifyEvent { ($0 as? LNRoomViewModelNotify)?.onRoomClosed() }
- }
- }
-
- func leaveRoom() {
- LNRoomManager.shared.leaveRoom { success in
- LNEventDeliver.notifyEvent { ($0 as? LNRoomViewModelNotify)?.onRoomClosed() }
- }
- }
- }
- // MARK: 麦位管理 - 普通用户
- extension LNRoomViewModel {
- var mySeatInfo: LNRoomSeatItem? {
- seatsInfo.first { $0.uid.isMyUid }
- }
-
- private func setupSeatObservers() {
- seatStore.state.subscribe().receive(on: DispatchQueue.main).sink { [weak self] state in
- guard let self else { return }
- let wasOnMic = mySeatInfo != nil
-
- let seats = state.seatList
- var hasChanged = seats.count != seatsInfo.count
- var newSeats: [LNRoomSeatItem] = []
- for seat in seats {
- let item = seatsInfo.first(where: { $0.index.rawValue == seat.index })
- ?? LNRoomSeatItem(index: LNRoomSeatNum(rawValue: seat.index) ?? .none)
- hasChanged = hasChanged || item.update(seat)
- newSeats.append(item)
- }
- if hasChanged {
- seatsInfo = newSeats
- if !wasOnMic, mySeatInfo != nil {
- // 自己上麦
- waitingForSeat = .none
-
- if roomInfo.forbidAudio {
- DeviceStore.shared.closeLocalMicrophone()
- } else {
- DeviceStore.shared.openLocalMicrophone(completion: nil)
- }
- }
- LNEventDeliver.notifyEvent { ($0 as? LNRoomViewModelNotify)?.onRoomSeatsChanged() }
- }
-
- let speakings = state.speakingUsers.filter { $0.value > 0 }.map { $0.key }.sorted { $1 > $0 }
- if speakings != speakingUser {
- speakingUser = speakings
- LNEventDeliver.notifyEvent { ($0 as? LNRoomViewModelNotify)?.onRoomSpeakingUsersChanged() }
- }
- }.store(in: &cancellables)
- }
-
- // 申请列表
- func getApplyList(type: LNRoomApplySeatType, next: String?, filter: String?,
- handler: @escaping (LNRoomMicApplyListResponse?) -> Void) {
- LNHttpManager.shared.getApplySeatList(roomId: roomId, searchType: type, filter: filter,
- size: 30, next: next ?? "")
- { res, err in
- runOnMain {
- handler(res)
- }
- res?.list.forEach {
- LNProfileManager.shared.updateProfileCache(uid: $0.user.userNo, name: $0.user.nickname, avatar: $0.user.avatar)
- }
- if let err {
- showToast(err.errorDesc)
- }
- }
- }
-
- // 申请列表的技能类别
- func getApplyListCategory(handler: @escaping (LNRoomGameCategoryResponse?) -> Void) {
- LNHttpManager.shared.getApplySeatCategory(roomId: roomId) { res, err in
- runOnMain {
- handler(res)
- }
- if let err {
- showToast(err.errorDesc)
- }
- }
- }
-
- // 申请上麦
- func applySeat(index: LNRoomSeatNum, handler: @escaping (Bool) -> Void) {
- LNPermissionHelper.requestMicrophoneAccess { [weak self] success in
- guard let self else { return }
- guard success else {
- showToast(.init(key: "A00362"))
- return
- }
- LNHttpManager.shared.applySeat(roomId: roomId, index: index.rawValue) { [weak self] err in
- runOnMain {
- handler(err == nil)
- }
- if let err {
- showToast(err.errorDesc)
- } else if let self {
- waitingForSeat = index == .guest ? .guest : index != .host ? .playmate : .none
- }
- }
- }
- }
-
- // 取消上麦申请
- func cancelSeatApply(handler: @escaping (Bool) -> Void) {
- LNHttpManager.shared.cancelApplySeat(roomId: roomId) { [weak self] err in
- runOnMain {
- handler(err == nil)
- }
- if let err {
- showToast(err.errorDesc)
- } else if let self {
- waitingForSeat = .none
- }
- }
- }
-
- // 主动下麦
- func leaveSeat(handler: @escaping (Bool) -> Void) {
- seatStore.leaveSeat { result in
- switch result {
- case .success:
- handler(true)
- case .failure(let err):
- showToast(err.localizedDescription)
- handler(false)
- }
- }
- }
- }
- // MARK: 麦位管理 - 管理员
- extension LNRoomViewModel {
- // 踢人下麦
- func kickUserOffSeat(uid: String, handler: @escaping (Bool) -> Void) {
- seatStore.kickUserOutOfSeat(userID: uid) { result in
- switch result {
- case .success:
- handler(true)
- case .failure(let err):
- showToast(err.localizedDescription)
- handler(false)
- }
- }
- }
-
- func clearApplyList(type: LNRoomApplySeatType, handler: @escaping (Bool) -> Void) {
- LNHttpManager.shared.clearApplySeatList(roomId: roomId, searchType: type) { err in
- runOnMain {
- handler(err == nil)
- }
- if let err {
- showToast(err.errorDesc)
- }
- }
- }
-
- // 接受上麦申请
- func acceptSeatApply(applyId: String, handler: @escaping (Bool) -> Void) {
- LNHttpManager.shared.handleApplySeat(roomId: roomId, applyId: applyId, accept: true) { err in
- runOnMain {
- handler(err == nil)
- }
- if let err {
- showToast(err.errorDesc)
- }
- }
- }
-
- // 拒绝上麦申请
- func rejectSeatApply(applyId: String, handler: @escaping (Bool) -> Void) {
- LNHttpManager.shared.handleApplySeat(roomId: roomId, applyId: applyId, accept: false) { err in
- runOnMain {
- handler(err == nil)
- }
- if let err {
- showToast(err.errorDesc)
- }
- }
- }
-
- // 邀请上麦
- func inviteUserToSeat(uid: String, index: LNRoomSeatNum, handler: @escaping (Bool) -> Void) {
- LNHttpManager.shared.inviteUserToSeat(roomId: roomId, uid: uid, index: index.rawValue) { err in
- runOnMain {
- handler(err == nil)
- }
- if let err {
- showToast(err.errorDesc)
- }
- }
- }
-
- // 关闭麦位
- func lockSeat(num: Int, handler: @escaping (Bool) -> Void) {
- seatStore.lockSeat(seatIndex: num) { result in
- switch result {
- case .success:
- handler(true)
- case .failure(let err):
- showToast(err.localizedDescription)
- handler(false)
- }
- }
- }
-
- // 解锁麦位
- func unlockSeat(num: Int, handler: @escaping (Bool) -> Void) {
- seatStore.unlockSeat(seatIndex: num) { result in
- switch result {
- case .success:
- handler(true)
- case .failure(let err):
- showToast(err.localizedDescription)
- handler(false)
- }
- }
- }
-
- // 用户列表
- func getRoomUserList(next: String?, playmete: Bool, filter: String?,
- handler: @escaping (LNRoomUserListResponse?) -> Void) {
- LNHttpManager.shared.getRoomUserList(
- roomId: roomId, size: 30, next: next ?? "",
- playmete: playmete, filter: filter)
- { res, err in
- runOnMain {
- handler(res)
- }
-
- res?.list.forEach {
- LNProfileManager.shared.updateProfileCache(uid: $0.user.userNo, name: $0.user.nickname, avatar: $0.user.avatar)
- }
-
- if let err {
- showToast(err.errorDesc)
- }
- }
- }
-
- // 申请列表的技能类别
- func getPlaymateCategory(handler: @escaping (LNRoomGameCategoryResponse?) -> Void) {
- LNHttpManager.shared.getRoomPlaymateCategory(roomId: roomId) { res, err in
- runOnMain {
- handler(res)
- }
- if let err {
- showToast(err.errorDesc)
- }
- }
- }
- }
- // MARK: 麦克风管理 - 普通用户
- extension LNRoomViewModel {
- // 关闭自己麦克风
- func muteMySeat() {
- seatStore.muteMicrophone()
- }
-
- // 打开自己麦克风
- func unmuteMySeat(handler: @escaping (Bool) -> Void) {
- let unmute = { [weak self] in
- guard let self else { return }
- seatStore.unmuteMicrophone { result in
- switch result {
- case .success:
- handler(true)
- case .failure(let err):
- showToast(err.localizedDescription)
- handler(false)
- }
- }
- }
- if DeviceStore.shared.state.value.microphoneStatus == .off {
- DeviceStore.shared.openLocalMicrophone { result in
- switch result {
- case .success:
- unmute()
- case .failure(let err):
- showToast(err.localizedDescription)
- handler(false)
- }
- }
- } else {
- unmute()
- }
- }
- }
- // MARK: 麦克风管理 - 管理员
- extension LNRoomViewModel {
- // 禁止某人的麦克风
- func muteSeat(uid: String, handler: @escaping (Bool) -> Void) {
- seatStore.closeRemoteMicrophone(userID: uid) { result in
- switch result {
- case .success:
- handler(true)
- case .failure(let err):
- showToast(err.localizedDescription)
- handler(false)
- }
- }
- }
-
- // 解锁某人麦克风
- func unmuteSeat(uid: String, handler: @escaping (Bool) -> Void) {
- seatStore.openRemoteMicrophone(userID: uid, policy: .unlockOnly) { result in
- switch result {
- case .success:
- handler(true)
- case .failure(let err):
- showToast(err.localizedDescription)
- handler(false)
- }
- }
- }
- }
- // MARK: 公屏
- extension LNRoomViewModel {
- private func setupMessageObservers() {
- messageStore.state.subscribe().receive(on: DispatchQueue.main).sink { [weak self] state in
- guard let self else { return }
- let lastId = lastmessage?.sequence ?? 0
- var newMessage: [Barrage] = []
- if let index = state.messageList.lastIndex(where: { $0.sequence == lastId }) {
- newMessage.append(contentsOf: state.messageList[(index + 1)...])
- } else {
- newMessage.append(contentsOf: state.messageList)
- }
- lastmessage = newMessage.last
-
- var chatMessage: [LNRoomChatMessageItem] = []
- var systemMessage: [LNRoomSystemMessageItem] = []
- newMessage.forEach {
- switch $0.messageType {
- case .text:
- if let item = LNRoomChatMessageItem(info: $0) {
- chatMessage.append(item)
- }
- case .custom:
- if let item = LNRoomSystemMessageItem(info: $0) {
- systemMessage.append(item)
- }
- default:
- break
- }
- }
- if !chatMessage.isEmpty {
- LNEventDeliver.notifyEvent { ($0 as? LNRoomViewModelNotify)?.onRoomMessageChanged(messages: chatMessage) }
- }
- handleSystemMessage(messages: systemMessage)
- }.store(in: &cancellables)
- }
-
- func sendChatMessage(text: String, handler: @escaping (Bool) -> Void) {
- messageStore.sendTextMessage(text: text, extensionInfo: [
- LNRoomChatMessageTypeKey: LNRoomChatMessageType.chat.rawValue
- ]) { result in
- switch result {
- case .success:
- handler(true)
- case .failure(let err):
- showToast(err.localizedDescription)
- handler(false)
- }
- }
- }
- }
- // MARK: 房间信息
- extension LNRoomViewModel {
- private func setupRoomInfoObserver() {
- LNRoomManager.shared.liveListStore.state.subscribe().receive(on: DispatchQueue.main).sink { [weak self] state in
- guard let self else { return }
- if state.currentLive.liveID.isEmpty { return }
-
- if !state.currentLive.roomType.contains(.playmate) {
- showToast(.init(key: "A00389"))
- leaveRoom()
- return
- }
- if roomInfo.update(state.currentLive) {
- LNEventDeliver.notifyEvent { ($0 as? LNRoomViewModelNotify)?.onRoomInfoChanged() }
- }
-
- let oldCount = seatApplyCount
- if state.currentLive.applySeatCount != seatApplyCount {
- seatApplyCount = state.currentLive.applySeatCount
- }
- if oldCount != seatApplyCount {
- LNEventDeliver.notifyEvent { ($0 as? LNRoomViewModelNotify)?.onRoomSeatApplyChanged() }
- }
- }.store(in: &cancellables)
- LNRoomManager.shared.liveListStore.liveListEventPublisher.receive(on: DispatchQueue.main).sink { [weak self] event in
- guard let self else { return }
- switch event {
- case .onLiveEnded(let roomId, _, _):
- if self.roomId == roomId {
- LNEventDeliver.notifyEvent { ($0 as? LNRoomViewModelNotify)?.onRoomClosed() }
- }
- case .onKickedOutOfLive(let roomId, _, _):
- if self.roomId == roomId {
- LNEventDeliver.notifyEvent { ($0 as? LNRoomViewModelNotify)?.onRoomClosed() }
- }
- default: break
- }
- }.store(in: &cancellables)
- }
-
- func updateRoomInfo(name: String, cover: String, forbidAudio: Bool, handler: @escaping (Bool) -> Void) {
- LNHttpManager.shared.updateRoom(roomId: roomId, roomTitle: name,
- roomCover: cover, forbidAudio: forbidAudio) { err in
- runOnMain {
- handler(err == nil)
- }
- if let err {
- showToast(err.errorDesc)
- }
- }
- }
- }
- // MARK: 观众事件
- extension LNRoomViewModel {
- private func setupAudienceEventObservers() {
- audienceStore.liveAudienceEventPublisher.receive(on: DispatchQueue.main).sink { [weak self] event in
- guard let self else { return }
- switch event {
- case .onAudienceJoined(let info):
- messageStore.appendLocalTip(message: Barrage.welcomeMessage(userInfo: info))
- default:
- break
- }
- }.store(in: &cancellables)
- }
- }
- extension LNRoomViewModel {
- private func handleSystemMessage(messages: [LNRoomSystemMessageItem]) {
- systemHandlerQueue.async { [weak self] in
- guard let self else { return }
- messages.forEach { message in
- if message.cmd == .MicClear, let cmd: LNRoomMicClearMessage = message.decodeCmdMessage() {
- if cmd.type == self.waitingForSeat {
- self.waitingForSeat = .none
- }
- }
- }
- }
- }
- }
|