LNRoomManager.swift 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. //
  2. // LNRoomManager.swift
  3. // Gami
  4. //
  5. // Created by OneeChan on 2026/3/9.
  6. //
  7. import Foundation
  8. import AtomicXCore
  9. import RTCRoomEngine
  10. protocol LNRoomManagerNotify {
  11. func onUserRoomAbilityChanged()
  12. func onHallEntranceChanged()
  13. }
  14. extension LNRoomManagerNotify {
  15. func onUserRoomAbilityChanged() { }
  16. func onHallEntranceChanged() { }
  17. }
  18. class LNRoomManager {
  19. static let shared = LNRoomManager()
  20. static let RoomNameMinInput = 2
  21. static let RoomNameMaxInput = 20
  22. static let MicNum = 10
  23. static private(set) var hallEntrance = false {
  24. didSet {
  25. if oldValue != hallEntrance {
  26. LNEventDeliver.notifyEvent { ($0 as? LNRoomManagerNotify)?.onHallEntranceChanged() }
  27. }
  28. }
  29. }
  30. private(set) var roomAbility: LNRoomAbilityVO?
  31. private(set) weak var curRoom: LNRoomViewModel?
  32. let liveListStore = LiveListStore.shared
  33. init () {
  34. LNEventDeliver.addObserver(self)
  35. }
  36. }
  37. // MARK: 直播间管理
  38. extension LNRoomManager {
  39. func getCreateRoomAbility(queue: DispatchQueue = .main,
  40. handler: ((LNRoomAbilityVO?) -> Void)?)
  41. {
  42. LNHttpManager.shared.getCreateRoomAbility { info, err in
  43. queue.asyncIfNotGlobal {
  44. if let err {
  45. showToast(err.errorDesc)
  46. handler?(nil)
  47. } else {
  48. self.roomAbility = info
  49. handler?(info)
  50. LNEventDeliver.notifyEvent { ($0 as? LNRoomManagerNotify)?.onUserRoomAbilityChanged() }
  51. }
  52. }
  53. }
  54. }
  55. func createRoom(roomName: String, cover: String, queue: DispatchQueue = .main,
  56. forbidAudio: Bool, handler: @escaping (String?) -> Void)
  57. {
  58. leaveRoom { _ in
  59. LNHttpManager.shared.createRoom(roomTitle: roomName, roomCover: cover, forbidAudio: forbidAudio) { room, err in
  60. queue.asyncIfNotGlobal {
  61. handler(room?.roomId)
  62. LNUserDefaults[.joinedRoomId] = room?.roomId
  63. }
  64. if let err {
  65. showToast(err.errorDesc)
  66. }
  67. }
  68. }
  69. }
  70. func joinRoom(roomId: String, queue: DispatchQueue = .main, handler: @escaping (String?) -> Void) {
  71. liveListStore.joinLive(liveID: roomId) { result in
  72. queue.asyncIfNotGlobal {
  73. switch result {
  74. case .success(let info):
  75. handler(info.liveID)
  76. LNUserDefaults[.joinedRoomId] = roomId
  77. case .failure(let errorInfo):
  78. handler(nil)
  79. showToast(errorInfo.message)
  80. }
  81. }
  82. }
  83. }
  84. func closeRoom(queue: DispatchQueue = .main, handler: @escaping (Bool) -> Void) {
  85. liveListStore.endLive { result in
  86. queue.asyncIfNotGlobal {
  87. switch result {
  88. case .success:
  89. handler(true)
  90. case .failure(let errorInfo):
  91. handler(false)
  92. showToast(errorInfo.message)
  93. }
  94. }
  95. }
  96. }
  97. func leaveRoom(queue: DispatchQueue = .main, handler: @escaping (Bool) -> Void) {
  98. let curLive: String = curRoom?.roomId ?? LNUserDefaults[.joinedRoomId, ""]
  99. if curLive.isEmpty {
  100. handler(true)
  101. } else {
  102. let json = "{\"api\":\"exitRoom\",\"params\":{\"roomId\":\"\(curLive)\",\"syncWaiting\":true,\"roomType\":2}}"
  103. TUIRoomEngine.sharedInstance().callExperimentalAPI(jsonStr: json) { error in
  104. handler(true)
  105. LNUserDefaults[.joinedRoomId] = ""
  106. }
  107. }
  108. }
  109. func updateCurRoom(_ room: LNRoomViewModel) {
  110. curRoom = room
  111. }
  112. }
  113. // MARK: 入口管理
  114. extension LNRoomManager {
  115. func reloadHallEntrance() {
  116. LNHttpManager.shared.getHallEntrance { res, err in
  117. guard let res else { return }
  118. Self.hallEntrance = res.result
  119. }
  120. }
  121. func getRandomRoom(queue: DispatchQueue = .main, handler: @escaping (LNRoomItemVO?) -> Void) {
  122. LNHttpManager.shared.getRandomRoom { res, err in
  123. queue.asyncIfNotGlobal {
  124. handler(res?.room)
  125. }
  126. if let res, res.room?.roomId.isEmpty != false {
  127. Self.hallEntrance = false
  128. }
  129. if let err {
  130. showToast(err.errorDesc)
  131. }
  132. }
  133. }
  134. func searchRoom(keyword: String, next: String?, queue: DispatchQueue = .main,
  135. handler: @escaping (LNSearchRoomResponse?) -> Void) {
  136. LNHttpManager.shared.searchRoom(keyword: keyword, size: 30, next: next ?? "") { res, err in
  137. queue.asyncIfNotGlobal {
  138. handler(res)
  139. }
  140. if let err {
  141. showToast(err.errorDesc)
  142. }
  143. }
  144. }
  145. func getRoomList(next: String?, queue: DispatchQueue = .main, handler: @escaping (LNSearchRoomResponse?) -> Void) {
  146. LNHttpManager.shared.getRoomList(size: 30, next: next ?? "") { res, err in
  147. queue.asyncIfNotGlobal {
  148. handler(res)
  149. }
  150. if let err {
  151. showToast(err.errorDesc)
  152. }
  153. }
  154. }
  155. }
  156. // MARK: 用户
  157. extension LNRoomManager {
  158. func getUserCurRoom(uid: String, queue: DispatchQueue = .main,
  159. handler: @escaping (LNUserCurRoomResponse?) -> Void) {
  160. LNHttpManager.shared.getUserCurRoom(uid: uid) { res, err in
  161. queue.asyncIfNotGlobal {
  162. handler(res)
  163. }
  164. if let err {
  165. showToast(err.errorDesc)
  166. }
  167. }
  168. }
  169. }
  170. extension LNRoomManager: LNAccountManagerNotify, LNProfileManagerNotify {
  171. func onUserLogin() {
  172. getCreateRoomAbility(handler: nil)
  173. }
  174. }