MOGiftListViewModel.swift 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. //
  2. // MOGiftListViewModel.swift
  3. // MiMoLive
  4. //
  5. // Created by OneeChan on 2025/9/18.
  6. //
  7. import Foundation
  8. @objc
  9. protocol MOGiftListViewModelDelegate {
  10. @objc optional func onGiftListChanged(category: Int, list: [MOGiftlist])
  11. @objc optional func onUserBalanceChangedBySendGift(current: Int)
  12. @objc optional func onUserLevelChangedBySendGift(level: Int, curExp: Int, nextExp: Int)
  13. @objc optional func onSendGiftSuccess(category: Int, num: Int, gift: MOGiftlist, info: MOGiftInfo)
  14. @objc optional func onSendBagSuccess(category: Int, num: Int, gift: MOGiftlist, info: MOGiftInfo, newItem: MOGiftlist?)
  15. }
  16. @objcMembers
  17. class MOGiftListViewModel: NSObject {
  18. private static let kPageSize = 60
  19. private let observers: NSHashTable<MOGiftListViewModelDelegate> = NSHashTable.weakObjects()
  20. @objc
  21. enum GiftCategory: Int {
  22. case hot = 1
  23. case lucky = 2
  24. case fans = 3
  25. case vip = 4
  26. case lv = 5
  27. case funny = 6
  28. case luxury = 7
  29. case sl = 8
  30. }
  31. private var categories: [MOGiftCategoryList] = [
  32. MOGiftCategoryList.initWith("Hot", code: Double(GiftCategory.hot.rawValue)),
  33. MOGiftCategoryList.initWith("SL", code: Double(GiftCategory.sl.rawValue)),
  34. MOGiftCategoryList.initWith("Lucky", code: Double(GiftCategory.lucky.rawValue)),
  35. MOGiftCategoryList.initWith("Fans", code: Double(GiftCategory.fans.rawValue)),
  36. MOGiftCategoryList.initWith("Vip", code: Double(GiftCategory.vip.rawValue)),
  37. MOGiftCategoryList.initWith("Lv", code: Double(GiftCategory.lv.rawValue)),
  38. MOGiftCategoryList.initWith("Funny", code: Double(GiftCategory.funny.rawValue)),
  39. MOGiftCategoryList.initWith("Luxury", code: Double(GiftCategory.luxury.rawValue))
  40. ]
  41. private var curRoom: MOLiveDetail? = nil
  42. var curRoomId: String? {
  43. curRoom?.currentRoom.id
  44. }
  45. private var giftCache: [Int: [MOGiftlist]] = [:]
  46. private var giftNextTags: [Int: String] = [:]
  47. override init() {
  48. super.init()
  49. MOEventDeliver.addObserver(self)
  50. }
  51. }
  52. // MARK: Category 类别
  53. extension MOGiftListViewModel {
  54. var categoryTitles: [String] {
  55. categories.map { $0.name }
  56. }
  57. func category(at: Int) -> MOGiftCategoryList? {
  58. guard at < categories.count else { return nil }
  59. return categories[at]
  60. }
  61. func checkGiftCategory() {
  62. let category = MOSvgaSourceManage.shareManager().giftCategoryArr
  63. if category.count == 0 {
  64. MOSvgaSourceManage.shareManager().toGetGiftCategoryListDataAndNeedShowError(true)
  65. } else {
  66. categories = category.compactMap({ item in
  67. guard let item = item as? MOGiftCategoryList else { return nil }
  68. return item
  69. })
  70. }
  71. preloadGiftList()
  72. }
  73. }
  74. // MARK: Bag 包裹
  75. extension MOGiftListViewModel {
  76. func sendBag(toUid: String, category: Int, gift: MOGiftlist, num: Int, completion: ((Bool) -> Void)? = nil) {
  77. guard !toUid.isEmpty else {
  78. MBProgressHUD.showTipMessage(inWindow: NSLocalizedString("mimo_2_live_gift_send_0_tip", comment: ""))
  79. completion?(false)
  80. return
  81. }
  82. guard let roomId = curRoomId,
  83. !roomId.isEmpty,
  84. let giftInfo = gift.giftInfo,
  85. let giftId = giftInfo.id,
  86. !giftId.isEmpty,
  87. let notifyGift = gift.copy() as? MOGiftlist
  88. else {
  89. completion?(false)
  90. return
  91. }
  92. let skinCode: Double = if let skin = gift.giftInfo.selectSkin {
  93. skin.code
  94. } else {
  95. 0
  96. }
  97. let skin = gift.giftInfo.selectSkin?.copy() as? MOEffect
  98. //没有礼物类别的情况 - 比如心愿单送礼
  99. let dict: [String : Any] = ["roomId": roomId,
  100. "userId": toUid,
  101. "giftId": giftId,
  102. "skinCode": skinCode,
  103. "count": num]
  104. MOHttpManager.shared().toGivingPackGift(withParams: dict) { [weak self] data, error in
  105. guard let self, self.curRoomId == roomId else { return }
  106. guard let data = data as? [AnyHashable: Any] else {
  107. completion?(false)
  108. return
  109. }
  110. if !data.isCodeSuccess {
  111. if data.code != 20007 {
  112. data.showNetError()
  113. }
  114. completion?(false)
  115. return
  116. }
  117. guard let resData = data.data else {
  118. completion?(false)
  119. return
  120. }
  121. var newItem: MOGiftlist? = nil
  122. if let bagData = resData["packGiftInfo"] as? [AnyHashable: Any],
  123. let bag = MOGiftlist.modelObject(with: bagData),
  124. !bag.giftInfo.id.isEmpty {
  125. bag.isBag = gift.isBag
  126. newItem = bag
  127. }
  128. notifyUserLevelChanged(resData["userLevel"])
  129. notifyGift.isBag = true
  130. let rebateDiamond = Double(resData.intValue(for: "rebateDiamond"))
  131. notifyGift.giftInfo.rebateMultiple = Double(resData.intValue(for: "rebateMultiple"))
  132. notifyGift.giftInfo.rebateDiamond = rebateDiamond
  133. notifyGift.endGiftNum = num
  134. notifyGift.giftInfo.selectSkin = skin
  135. notifyGift.giftInfo.antiDrillCritGift = giftInfo.antiDrillCritGift
  136. if giftInfo.antiDrillCritGift {
  137. notifyGift.giftInfo.hitDiamond = Int(rebateDiamond)
  138. }
  139. notifySendBagSuccess(category, num, notifyGift, giftInfo, newItem)
  140. completion?(true)
  141. }
  142. }
  143. func sendBag(toUids: [String], category: Int, gift: MOGiftlist, num: Int, completion: ((Bool) -> Void)? = nil) {
  144. guard !toUids.isEmpty else {
  145. MBProgressHUD.showTipMessage(inWindow: NSLocalizedString("mimo_2_live_gift_send_0_tip", comment: ""))
  146. completion?(false)
  147. return
  148. }
  149. guard let roomId = curRoomId,
  150. !roomId.isEmpty,
  151. let giftInfo = gift.giftInfo,
  152. let giftId = giftInfo.id,
  153. !giftId.isEmpty,
  154. let notifyGift = gift.copy() as? MOGiftlist
  155. else {
  156. completion?(false)
  157. return
  158. }
  159. let skinCode: Double = if let skin = gift.giftInfo.selectSkin {
  160. skin.code
  161. } else {
  162. 0
  163. }
  164. let skin = gift.giftInfo.selectSkin?.copy() as? MOEffect
  165. //没有礼物类别的情况 - 比如心愿单送礼
  166. let dict: [String : Any] = ["roomId": roomId,
  167. "userIds": toUids,
  168. "giftId": giftId,
  169. "skinCode": skinCode,
  170. "count": num]
  171. MOHttpManager.shared().toSendMoreOneGiftAboutPack(withParams: dict) { [weak self] data, error in
  172. guard let self, self.curRoomId == roomId else { return }
  173. guard let data = data as? [AnyHashable: Any] else {
  174. completion?(false)
  175. return
  176. }
  177. if !data.isCodeSuccess {
  178. if data.code != 20007 {
  179. data.showNetError()
  180. }
  181. completion?(false)
  182. return
  183. }
  184. guard let resData = data.data,
  185. let resultData = MOGiftResultData.modelObject(with: resData)
  186. else {
  187. completion?(false)
  188. return
  189. }
  190. var newItem: MOGiftlist? = nil
  191. if let bagData = resData["packGiftInfo"] as? [AnyHashable: Any],
  192. let bag = MOGiftlist.modelObject(with: bagData),
  193. !bag.giftInfo.id.isEmpty {
  194. bag.isBag = gift.isBag
  195. newItem = bag
  196. }
  197. notifyUserLevelChanged(resData["userLevel"])
  198. var rebateDiamond: Double = 0
  199. var rebateMultiple: Double = 0
  200. for item in resultData.giftRebates {
  201. if let rebate = item as? MOGiftRebates {
  202. rebateMultiple += rebate.multiple
  203. rebateDiamond += rebate.diamond
  204. }
  205. }
  206. notifyGift.giftInfo.rebateMultiple = rebateMultiple
  207. notifyGift.giftInfo.rebateDiamond = rebateDiamond
  208. notifyGift.giftInfo.num = Double(num)
  209. notifyGift.endGiftNum = num
  210. notifyGift.giftInfo.multipleUserList = toUids
  211. notifyGift.giftInfo.selectSkin = skin
  212. notifyGift.giftInfo.antiDrillCritGift = giftInfo.antiDrillCritGift
  213. if giftInfo.antiDrillCritGift {
  214. notifyGift.giftInfo.hitDiamond = Int(rebateDiamond)
  215. }
  216. notifySendBagSuccess(category, num * toUids.count, notifyGift, giftInfo, newItem)
  217. completion?(true)
  218. }
  219. }
  220. }
  221. // MARK: Gift 礼物
  222. extension MOGiftListViewModel {
  223. func sendGift(toUid: String, category: Int, gift: MOGiftlist, num: Int, completion: ((Bool) -> Void)? = nil) {
  224. guard !toUid.isEmpty else {
  225. MBProgressHUD.showTipMessage(inWindow: NSLocalizedString("mimo_2_live_gift_send_0_tip", comment: ""))
  226. completion?(false)
  227. return
  228. }
  229. guard let roomId = curRoomId,
  230. !roomId.isEmpty,
  231. let giftInfo = gift.giftInfo,
  232. let giftId = giftInfo.id,
  233. !giftId.isEmpty,
  234. let notifyGift = gift.copy() as? MOGiftlist
  235. else {
  236. completion?(false)
  237. return
  238. }
  239. let skinCode: Double = if let skin = gift.giftInfo.selectSkin {
  240. skin.code
  241. } else {
  242. 0
  243. }
  244. let skin = gift.giftInfo.selectSkin?.copy() as? MOEffect
  245. //没有礼物类别的情况 - 比如心愿单送礼
  246. let dict: [String : Any] = ["roomId": roomId,
  247. "userId": toUid,
  248. "giftId": giftId,
  249. "skinCode": skinCode,
  250. "count": num]
  251. MOHttpManager.shared().toGivingGiftAboutLive(withParams: dict) { [weak self] data, error in
  252. guard let self, self.curRoomId == roomId else { return }
  253. guard let data = data as? [AnyHashable: Any] else {
  254. completion?(false)
  255. return
  256. }
  257. if !data.isCodeSuccess {
  258. if data.code != 20007 {
  259. data.showNetError()
  260. }
  261. completion?(false)
  262. return
  263. }
  264. guard let resData = data.data else {
  265. completion?(false)
  266. return
  267. }
  268. // MOLogV(@"givingGift: %@",[[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:data options:0 error:nil] encoding:NSUTF8StringEncoding]);
  269. let currentDiamond = resData.intValue(for: "currentDiamond")
  270. notifyUserBalanceChanged(currentDiamond)
  271. notifyUserLevelChanged(resData["userLevel"])
  272. let rebateDiamond = Double(resData.intValue(for: "rebateDiamond"))
  273. notifyGift.giftInfo.rebateMultiple = Double(resData.intValue(for: "rebateMultiple"))
  274. notifyGift.giftInfo.rebateDiamond = rebateDiamond
  275. notifyGift.giftInfo.num = Double(num)
  276. notifyGift.endGiftNum = num
  277. notifyGift.giftInfo.selectSkin = skin
  278. if let blindboxLotteryDict = resData["blindboxLotterys"] {
  279. notifyGift.giftInfo.blindboxLottery = MOBlindboxLottery.model(withJSON: blindboxLotteryDict)
  280. } else {
  281. notifyGift.giftInfo.blindboxLottery = nil
  282. }
  283. notifyGift.giftInfo.antiDrillCritGift = giftInfo.antiDrillCritGift
  284. if giftInfo.antiDrillCritGift {
  285. notifyGift.giftInfo.hitDiamond = Int(rebateDiamond)
  286. }
  287. notifySendGiftSuccess(category, num, notifyGift, giftInfo)
  288. completion?(true)
  289. }
  290. }
  291. func sendGift(toUids: [String], category: Int, gift: MOGiftlist, num: Int, completion: ((Bool) -> Void)? = nil) {
  292. guard !toUids.isEmpty else {
  293. MBProgressHUD.showTipMessage(inWindow: NSLocalizedString("mimo_2_live_gift_send_0_tip", comment: ""))
  294. completion?(false)
  295. return
  296. }
  297. guard let roomId = curRoomId,
  298. !roomId.isEmpty,
  299. let giftInfo = gift.giftInfo,
  300. let giftId = giftInfo.id,
  301. !giftId.isEmpty,
  302. let notifyGift = gift.copy() as? MOGiftlist
  303. else {
  304. completion?(false)
  305. return
  306. }
  307. let skinCode: Double = if let skin = gift.giftInfo.selectSkin {
  308. skin.code
  309. } else {
  310. 0
  311. }
  312. let skin = gift.giftInfo.selectSkin?.copy() as? MOEffect
  313. //没有礼物类别的情况 - 比如心愿单送礼
  314. let dict: [String : Any] = ["roomId": roomId,
  315. "userIds": toUids,
  316. "giftId": giftId,
  317. "skinCode": skinCode,
  318. "count": num]
  319. MOHttpManager.shared().toSendMoreOneGift(withParams: dict) { [weak self] data, error in
  320. guard let self, self.curRoomId == roomId else { return }
  321. guard let data = data as? [AnyHashable: Any] else {
  322. completion?(false)
  323. return
  324. }
  325. if !data.isCodeSuccess {
  326. if data.code != 20007 {
  327. data.showNetError()
  328. }
  329. completion?(false)
  330. return
  331. }
  332. guard let resData = data.data,
  333. let resultData = MOGiftResultData.modelObject(with: resData)
  334. else {
  335. completion?(false)
  336. return
  337. }
  338. notifyUserBalanceChanged(Int(resultData.currentDiamond))
  339. notifyUserLevelChanged(resData["userLevel"])
  340. var rebateDiamond: Double = 0
  341. var rebateMultiple: Double = 0
  342. for item in resultData.giftRebates {
  343. if let rebate = item as? MOGiftRebates {
  344. rebateMultiple += rebate.multiple
  345. rebateDiamond += rebate.diamond
  346. }
  347. }
  348. notifyGift.giftInfo.rebateMultiple = rebateMultiple
  349. notifyGift.giftInfo.rebateDiamond = rebateDiamond
  350. notifyGift.giftInfo.num = Double(num)
  351. notifyGift.endGiftNum = num
  352. notifyGift.giftInfo.multipleUserList = toUids
  353. notifyGift.giftInfo.selectSkin = skin
  354. if let blindboxLotteryDict = data["blindboxLotterys"] {
  355. notifyGift.giftInfo.blindboxLottery = MOBlindboxLottery.model(withJSON: blindboxLotteryDict)
  356. } else {
  357. notifyGift.giftInfo.blindboxLottery = nil
  358. }
  359. notifyGift.giftInfo.antiDrillCritGift = giftInfo.antiDrillCritGift
  360. if giftInfo.antiDrillCritGift {
  361. notifyGift.giftInfo.hitDiamond = Int(rebateDiamond)
  362. }
  363. notifySendGiftSuccess(category, num, notifyGift, giftInfo)
  364. completion?(true)
  365. }
  366. }
  367. func preloadGiftList() {
  368. categories.forEach { item in
  369. loadGiftList(category: Int(item.code), reload: true, completion: nil)
  370. }
  371. }
  372. func isGiftListHasNext(_ category: Int) -> Bool {
  373. guard let tag = giftNextTags[category] else { return true }
  374. return tag.count > 0
  375. }
  376. func giftListFor(_ category: Int) -> [MOGiftlist] {
  377. giftCache[category] ?? []
  378. }
  379. func loadGiftList(category: Int, reload: Bool = false, completion:((Bool) -> Void)?) {
  380. guard let roomId = curRoomId else { return }
  381. let next = reload ? "" : giftNextTags[category] ?? ""
  382. let page: [String : Any] = ["size": Self.kPageSize, "next": next]
  383. let dic: [String: Any] = ["category": category, "roomId": roomId, "page": page]
  384. MOHttpManager.shared().toGetLiveGiftList(withParams: dic) { [weak self] data, error in
  385. guard let self, self.curRoomId == roomId else { return }
  386. guard let data = data as? [AnyHashable: Any] else {
  387. completion?(false)
  388. return
  389. }
  390. if !data.isCodeSuccess {
  391. data.showNetError()
  392. completion?(false)
  393. return
  394. }
  395. guard let resData = data.data else {
  396. completion?(false)
  397. return
  398. }
  399. guard let baseData = MOGiftListBaseData.modelObject(with: resData),
  400. let list = baseData.giftlist as? [MOGiftlist] else {
  401. completion?(false)
  402. return
  403. }
  404. var cache = giftCache[category] ?? []
  405. var isChanged = false
  406. if reload {
  407. // 刷新的时候,判断列表是否变化,减少列表没必要的刷新
  408. isChanged = cache != list
  409. if isChanged {
  410. cache.removeAll()
  411. cache.append(contentsOf: list)
  412. }
  413. } else {
  414. isChanged = true
  415. cache.append(contentsOf: list)
  416. }
  417. if isChanged {
  418. giftCache[category] = cache
  419. }
  420. giftNextTags[category] = list.count < Self.kPageSize ? "" : baseData.next
  421. completion?(true)
  422. if isChanged {
  423. notifyGiftListChanged(category)
  424. }
  425. }
  426. }
  427. }
  428. // MARK: Observers 监听
  429. extension MOGiftListViewModel {
  430. func addObserver(_ observer: MOGiftListViewModelDelegate) {
  431. guard !observers.contains(observer) else { return }
  432. observers.add(observer)
  433. }
  434. private func notifyGiftListChanged(_ category: Int) {
  435. let list = giftCache[category] ?? []
  436. observers.allObjects.forEach { $0.onGiftListChanged?(category: category, list: list) }
  437. }
  438. private func notifyUserBalanceChanged(_ cur: Int) {
  439. guard cur != -1 else { return }
  440. // 更新用户余额
  441. let userInfo = MODataCache.sharedYYCache().object(forKey: kMineUserInfo) as? MOMeDataInfo
  442. userInfo?.userWallet.diamond = Double(cur)
  443. MODataCache.sharedYYCache().setObject(userInfo, forKey: kMineUserInfo)
  444. observers.allObjects.forEach { $0.onUserBalanceChangedBySendGift?(current: cur) }
  445. }
  446. private func notifyUserLevelChanged(_ data: Any?) {
  447. guard let levelDic = data as? [AnyHashable: Any] else { return }
  448. //等级 - 经验
  449. let level = levelDic.intValue(for: "leel")
  450. let curExp = levelDic.intValue(for: "currExp")
  451. let nextExp = levelDic.intValue(for: "nextExp")
  452. observers.allObjects.forEach { $0.onUserLevelChangedBySendGift?(level: level, curExp: curExp, nextExp: nextExp) }
  453. }
  454. private func notifySendGiftSuccess(_ category: Int, _ num: Int, _ gift: MOGiftlist, _ info: MOGiftInfo) {
  455. observers.allObjects.forEach { $0.onSendGiftSuccess?(category: category, num: num, gift: gift, info: info) }
  456. }
  457. private func notifySendBagSuccess(_ category: Int, _ num: Int, _ gift: MOGiftlist, _ info: MOGiftInfo, _ newItem: MOGiftlist?) {
  458. observers.allObjects.forEach { $0.onSendBagSuccess?(category: category, num: num, gift: gift, info: info, newItem: newItem) }
  459. }
  460. }
  461. extension MOGiftListViewModel/*: RoomMainEvent*/ {
  462. func onJoinedRoom(room: MOLiveDetail) {
  463. guard room.currentRoom.id != curRoomId else { return }
  464. curRoom = room
  465. giftCache.removeAll()
  466. giftNextTags.removeAll()
  467. checkGiftCategory()
  468. }
  469. }