| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385 |
- //
- // LNRoomViewModel.swift
- // Gami
- //
- // Created by OneeChan on 2026/3/9.
- //
- import Foundation
- import AtomicXCore
- import Combine
- protocol LNRoomViewModelNotify {
- func onRoomMessageChanged()
- func onRoomSeatsChanged()
- func onRoomSpeakingUsersChanged()
- func onRoomSeatApplyChanged()
- func onRoomInfoChanged()
-
- func onRoomClosed()
- }
- extension LNRoomViewModelNotify {
- func onRoomMessageChanged() { }
- func onRoomSeatsChanged() { }
- func onRoomSpeakingUsersChanged() { }
- func onRoomSeatApplyChanged() { }
- func onRoomInfoChanged() { }
-
- func onRoomClosed() { }
- }
- class LNRoomViewModel: NSObject {
- let roomId: String
- private let seatStore: LiveSeatStore
- private let guestStore: CoGuestStore
- private let messageStore: BarrageStore
- private(set) var seatsInfo: [LNRoomSeatItem] = []
- private(set) var roomInfo = LNRoomInfo()
- private(set) var speakingUser: [String] = []
-
- private let maxMessageCount = 300
- private let messageRefreshInterval: TimeInterval = 1
- private var messageRefreshTask: String?
-
- init(roomId: String) {
- self.roomId = roomId
-
- seatStore = LiveSeatStore.create(liveID: roomId)
- guestStore = CoGuestStore.create(liveID: roomId)
- messageStore = BarrageStore.create(liveID: roomId)
-
- super.init()
-
- setupSeatObservers()
- setupApplyObservers()
- setupMessageObservers()
- setupRoomInfoObserver()
- }
-
- func closeRoom() {
- LNRoomManager.shared.closeRoom { success in
- LNEventDeliver.notifyEvent { ($0 as? LNRoomViewModelNotify)?.onRoomClosed() }
- }
- }
- }
- // MARK: 麦位管理 - 普通用户
- extension LNRoomViewModel {
- var isOnMic: Bool {
- seatsInfo.contains { $0.uid == myUid }
- }
-
- private func setupSeatObservers() {
- seatStore.state.subscribe().receive(on: DispatchQueue.main).sink { [weak self] state in
- guard let self else { return }
- let seats = state.seatList
- var hasChanged = seats.count != seatsInfo.count
- var newSeats: [LNRoomSeatItem] = []
- for seat in seats {
- let item = seatsInfo.first(where: { $0.index == seat.index }) ?? LNRoomSeatItem(index: seat.index)
- hasChanged = hasChanged || item.update(seat)
- newSeats.append(item)
- }
- if hasChanged {
- seatsInfo = newSeats
- 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 applySeat(handler: @escaping (Bool) -> Void) {
- guestStore.applyForSeat(timeout: 0, extraInfo: nil) { result in
- switch result {
- case .success:
- handler(true)
- case .failure(let err):
- showToast(err.localizedDescription)
- handler(false)
- }
- }
- }
-
- // 取消上麦申请
- func cancelSeatApply(handler: @escaping (Bool) -> Void) {
- guestStore.cancelApplication { result in
- switch result {
- case .success:
- handler(true)
- case .failure(let err):
- showToast(err.localizedDescription)
- handler(false)
- }
- }
- }
-
- // 主动下麦
- func offSeat(handler: @escaping (Bool) -> Void) {
- guestStore.disConnect { result in
- switch result {
- case .success:
- handler(true)
- case .failure(let err):
- showToast(err.localizedDescription)
- handler(false)
- }
- }
- }
-
- // 接受邀请
- func acceptSeatInvite(uid: String, handler: @escaping (Bool) -> Void) {
- guestStore.acceptInvitation(inviterID: uid) { result in
- switch result {
- case .success:
- handler(true)
- case .failure(let err):
- showToast(err.localizedDescription)
- handler(false)
- }
- }
- }
-
- // 拒绝邀请
- func rejectSeatInvite(uid: String, handler: @escaping (Bool) -> Void) {
- guestStore.rejectInvitation(inviterID: uid) { result in
- switch result {
- case .success:
- handler(true)
- case .failure(let err):
- showToast(err.localizedDescription)
- handler(false)
- }
- }
- }
- }
- // MARK: 麦位管理 - 管理员
- extension LNRoomViewModel {
- var curSeatApplications: [LNRoomSeatApplyItem] {
- guestStore.state.value.applicants.map {
- LNRoomSeatApplyItem(userNo: $0.userID, avatar: $0.avatarURL,
- name: $0.userName, gender: .unknow,
- time: 0, category: "")
- }
- }
-
- private func setupApplyObservers() {
- guestStore.state.subscribe().receive(on: DispatchQueue.main).sink { [weak self] state in
- guard let self else { return }
- LNEventDeliver.notifyEvent { ($0 as? LNRoomViewModelNotify)?.onRoomSeatApplyChanged() }
- }.store(in: &cancellables)
- }
-
- // 踢人下麦
- 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 acceptSeatApply(uid: String, handler: @escaping (Bool) -> Void) {
- guestStore.acceptApplication(userID: uid) { result in
- switch result {
- case .success:
- handler(true)
- case .failure(let err):
- showToast(err.localizedDescription)
- handler(false)
- }
- }
- }
-
- // 拒绝上麦申请
- func rejectSeatApply(uid: String, handler: @escaping (Bool) -> Void) {
- guestStore.rejectApplication(userID: uid) { result in
- switch result {
- case .success:
- handler(true)
- case .failure(let err):
- showToast(err.localizedDescription)
- handler(false)
- }
- }
- }
-
- // 邀请上麦
- func inviteUserToSeat(uid: String, handler: @escaping (Bool) -> Void) {
- guestStore.inviteToSeat(userID: uid, timeout: 0, extraInfo: nil) { result in
- switch result {
- case .success:
- handler(true)
- case .failure(let err):
- showToast(err.localizedDescription)
- handler(false)
- }
- }
- }
-
- // 关闭麦位
- 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)
- }
- }
- }
- }
- // MARK: 麦克风管理 - 普通用户
- extension LNRoomViewModel {
- // 关闭自己麦克风
- func muteMySeat() {
- seatStore.muteMicrophone()
- }
-
- // 打开自己麦克风
- func unmuteMySeat(handler: @escaping (Bool) -> Void) {
- seatStore.unmuteMicrophone { result in
- switch result {
- case .success:
- handler(true)
- case .failure(let err):
- showToast(err.localizedDescription)
- handler(false)
- }
- }
- }
- }
- // 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: 公屏
- private extension BarrageType {
- var toMessageType: LNRoomMessageItemType {
- switch self {
- case .text:
- .chat
- case .custom:
- .system
- default:
- .unknown
- }
- }
- }
- extension LNRoomViewModel {
- var curMessage: [LNRoomMessageItem] {
- messageStore.state.value.messageList.suffix(maxMessageCount).map {
- LNRoomMessageItem(type: $0.messageType.toMessageType, sender: $0.sender.userID, text: $0.textContent)
- }
- }
-
- private func setupMessageObservers() {
- messageStore.state.subscribe().receive(on: DispatchQueue.main).sink { [weak self] state in
- guard let self else { return }
- guard messageRefreshTask == nil else {
- return
- }
- messageRefreshTask = LNDelayTask.perform(delay: self.messageRefreshInterval, task: { [weak self] in
- guard let self else { return }
-
- messageRefreshTask = nil
- LNEventDeliver.notifyEvent { ($0 as? LNRoomViewModelNotify)?.onRoomMessageChanged() }
- })
- }.store(in: &cancellables)
- }
-
- func sendMessage(text: String, handler: @escaping (Bool) -> Void) {
- messageStore.sendTextMessage(text: text, extensionInfo: nil) { 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 roomInfo.update(state.currentLive) {
- LNEventDeliver.notifyEvent { ($0 as? LNRoomViewModelNotify)?.onRoomInfoChanged() }
- }
- }.store(in: &cancellables)
- }
-
- func updateRoomInfo(name: String, cover: String, handler: @escaping (Bool) -> Void) {
- var info = LiveInfo()
- info.liveID = roomInfo.liveID
- info.liveName = name
- info.coverURL = cover
- LNRoomManager.shared.liveListStore.updateLiveInfo(info, modifyFlag: [.liveName, .coverURL]) { result in
- switch result {
- case .success:
- handler(true)
- case .failure(let err):
- showToast(err.localizedDescription)
- handler(false)
- }
- }
- }
- }
|