MOLiveViewModel.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //
  2. // MOLiveViewModel.swift
  3. // MiMoLive
  4. //
  5. // Created by OneeChan on 2025/9/26.
  6. //
  7. import Foundation
  8. @objc
  9. protocol MOLiveViewModelDelegate {
  10. @objc optional func onLiveViewModelRecvLineInvite(viewModel: MOLiveViewModel, invite: MORtmLivePKLinkInvite)
  11. }
  12. @objcMembers
  13. class MOLiveViewModel: NSObject {
  14. var curJoinedRoom: MOLiveDetail?
  15. var lineViewModel: MOLineViewModel? = nil
  16. let giftViewModel = MOGiftListViewModel()
  17. var isOwner: Bool {
  18. curJoinedRoom?.currentRoom.anchorUser.id == UserDefaults.uid
  19. }
  20. var ownerUid: String? {
  21. curJoinedRoom?.currentRoom.anchorUser.id
  22. }
  23. var curRoomId: String? {
  24. curJoinedRoom?.currentRoom.id
  25. }
  26. override init() {
  27. super.init()
  28. MOEventDeliver.addObserver(self)
  29. }
  30. func onJoingRoom(_ room: MOLiveList) {
  31. reset() // 房间切换,需要重置所有数据
  32. }
  33. func onRoomInfoChanged(_ info: MOStatusInfo) {
  34. curJoinedRoom?.statusInfo = info
  35. }
  36. func onJoinedRoom(_ room: MOLiveDetail) {
  37. self.curJoinedRoom = room
  38. checkLineStatus(room.statusInfo)
  39. giftViewModel.onJoinedRoom(room: room)
  40. if isOwner {
  41. MOLineViewModel.getLineConfig()
  42. }
  43. }
  44. // 后续需要将事件变化挪到本类自身处理
  45. func handleLiveStateChanged(info: MOStatusInfo, showLinkOrPk: Bool) {
  46. if showLinkOrPk {
  47. checkLineStatus(info)
  48. }
  49. }
  50. func isHost(_ uid: String) -> Bool {
  51. guard let curJoinedRoom else { return false }
  52. // 是房主
  53. if ownerUid == uid {
  54. return true
  55. }
  56. // 多人房主播
  57. if let linkMics = curJoinedRoom.statusInfo.roomStatus.linkMics as? [MOLinkMic],
  58. linkMics.first(where: { $0.profile.id == uid }) != nil {
  59. return true
  60. }
  61. // line 对方主播
  62. if let lineViewModel,
  63. lineViewModel.curPeerInfo?.userId == uid {
  64. return true
  65. }
  66. return false
  67. }
  68. }
  69. extension MOLiveViewModel {
  70. // TODO: Line 连线(后续需要迁移到 LineViewModel)
  71. func handleRoomRtm(entity: MORtmEntity) {
  72. guard let jsonEntity = entity.data as? MORtmJosnEntity else { return }
  73. switch (jsonEntity.type) {
  74. case 31:
  75. lineViewModel?.reloadLineRoomInfo()
  76. case 37: //邀请
  77. guard let invite = jsonEntity.pkLinkInvite else { return }
  78. notifyLineInvite(invite: invite)
  79. case 38: //PK房间信息变化
  80. guard let status = jsonEntity.pkV2Status else { return }
  81. lineViewModel?.handleLinePkRtm(status)
  82. case 39: //pk/连线 状态 信息
  83. guard let expand = jsonEntity.pkV2StatusExpand else { return }
  84. lineViewModel?.handleLinePkExpandRtm(expand)
  85. case 40: // toast
  86. guard let toast = jsonEntity.rtmToast else { return }
  87. guard let text = toast.text else { return }
  88. MBProgressHUD.showTipMessage(inWindow: text)
  89. default:
  90. break;
  91. }
  92. }
  93. func checkLineStatus(_ newStatusInfo: MOStatusInfo) {
  94. guard curJoinedRoom != nil else { return }
  95. guard let pkInfo = newStatusInfo.roomStatus.linkPKInfo else { return }
  96. let lineRoomId = pkInfo.pkLinkRoomId
  97. guard lineViewModel?.curLineRoomId != lineRoomId else {
  98. // Line 房间号不变,已经加入了该 Line 房间
  99. return
  100. }
  101. // Line 房间 ID 变化
  102. if let lineViewModel, !lineViewModel.curLineRoomId.isEmpty {
  103. // 需要退出旧 Line 房间
  104. lineViewModel.clear() // 将原来的 Line 房间 清理掉
  105. self.lineViewModel = nil
  106. }
  107. if let lineRoomId, let token = pkInfo.agoraRtcToken,
  108. !lineRoomId.isEmpty, !token.isEmpty {
  109. // 创建新的 Line 房逻辑
  110. self.lineViewModel = MOLineViewModel(lineRoomId: lineRoomId, token: token)
  111. }
  112. }
  113. }
  114. extension MOLiveViewModel {
  115. private func reset() {
  116. lineViewModel?.clear()
  117. lineViewModel = nil
  118. }
  119. }
  120. extension MOLiveViewModel {
  121. func notifyLineInvite(invite: MORtmLivePKLinkInvite) {
  122. MOEventDeliver.notifyEvent { $0.onLiveViewModelRecvLineInvite?(viewModel: self, invite: invite) }
  123. }
  124. }
  125. extension MOLiveViewModel: MOLineViewModelDelegate {
  126. func onLineJoinFailed(viewModel: MOLineViewModel) {
  127. guard viewModel == self.lineViewModel else { return }
  128. self.lineViewModel?.clear()
  129. self.lineViewModel = nil
  130. }
  131. func onLineStateChanged(state: LineState) {
  132. guard MOLiveManager.curLive == self else { return }
  133. if state == .none {
  134. // Line 结束,清理 Line 相关逻辑
  135. self.lineViewModel = nil
  136. }
  137. }
  138. }