RaiseHandApplicationNotificationViewModel.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //
  2. // RaiseHandApplicationNotificationViewModel.swift
  3. // TUIRoomKit
  4. //
  5. // Created by janejntang on 2024/5/16.
  6. //
  7. import Foundation
  8. import RTCRoomEngine
  9. protocol RaiseHandApplicationNotificationViewModelResponder: AnyObject {
  10. func showRaiseHandApplicationNotificationView(userId: String, userName: String, count: Int)
  11. func hideRaiseHandApplicationNotificationView()
  12. }
  13. class RaiseHandApplicationNotificationViewModel: NSObject {
  14. var delayDisappearanceTime = 5.0
  15. lazy var userId: String? = {
  16. return inviteSeatList.last?.userId
  17. }()
  18. lazy var userName: String? = {
  19. return inviteSeatList.last?.userName
  20. }()
  21. lazy var applicationCount: Int? = {
  22. return inviteSeatList.count
  23. }()
  24. weak var responder: RaiseHandApplicationNotificationViewModelResponder?
  25. var inviteSeatList: [RequestEntity] {
  26. EngineManager.shared.store.inviteSeatList
  27. }
  28. lazy var isShownRaiseHandApplicationNotificationView: Bool = {
  29. return getShownRequestEntity() != nil
  30. }()
  31. override init() {
  32. super.init()
  33. subscribeEngine()
  34. }
  35. private func subscribeEngine() {
  36. EngineEventCenter.shared.subscribeEngine(event: .onRequestReceived, observer: self)
  37. EngineEventCenter.shared.subscribeEngine(event: .onDeletedTakeSeatRequest, observer: self)
  38. EngineEventCenter.shared.subscribeUIEvent(key: .TUIRoomKitService_RenewSeatList, responder: self)
  39. }
  40. private func unsubscribeEngine() {
  41. EngineEventCenter.shared.unsubscribeEngine(event: .onRequestReceived, observer: self)
  42. EngineEventCenter.shared.unsubscribeEngine(event: .onDeletedTakeSeatRequest, observer: self)
  43. EngineEventCenter.shared.unsubscribeUIEvent(key: .TUIRoomKitService_RenewSeatList, responder: self)
  44. }
  45. private func getShownRequestEntity() -> RequestEntity? {
  46. let currentTime = Date().timeIntervalSince1970
  47. guard let lastItem = inviteSeatList.last else { return nil }
  48. if delayDisappearanceTime > 0, currentTime - lastItem.timestamp > delayDisappearanceTime {
  49. return nil
  50. } else {
  51. return lastItem
  52. }
  53. }
  54. func checkRaiseHandApplicationAction() {
  55. RoomRouter.shared.presentPopUpViewController(viewType: .raiseHandApplicationListViewType, height: 720.scale375Height(), backgroundColor: UIColor(0x22262E))
  56. }
  57. deinit {
  58. unsubscribeEngine()
  59. }
  60. }
  61. extension RaiseHandApplicationNotificationViewModel: RoomEngineEventResponder {
  62. func onEngineEvent(name: EngineEventCenter.RoomEngineEvent, param: [String : Any]?) {
  63. switch name {
  64. case .onRequestReceived:
  65. guard let request = param?["request"] as? TUIRequest else { return }
  66. guard request.requestAction == .takeSeat else { return }
  67. self.userId = request.userId
  68. self.userName = request.userName
  69. responder?.showRaiseHandApplicationNotificationView(userId: request.userId, userName: request.userName, count: inviteSeatList.count)
  70. case .onDeletedTakeSeatRequest:
  71. guard let userId = param?["userId"] as? String else { return }
  72. guard userId == self.userId else { return }
  73. let requestItem = getShownRequestEntity()
  74. self.userId = requestItem?.userId
  75. self.userName = requestItem?.userName
  76. if let requestItem = requestItem {
  77. responder?.showRaiseHandApplicationNotificationView(userId: requestItem.userId, userName: requestItem.userName, count: inviteSeatList.count)
  78. } else {
  79. responder?.hideRaiseHandApplicationNotificationView()
  80. }
  81. default: break
  82. }
  83. }
  84. }
  85. extension RaiseHandApplicationNotificationViewModel: RoomKitUIEventResponder {
  86. func onNotifyUIEvent(key: EngineEventCenter.RoomUIEvent, Object: Any?, info: [AnyHashable : Any]?) {
  87. if key == .TUIRoomKitService_RenewSeatList {
  88. guard let requestItem = getShownRequestEntity() else { return }
  89. responder?.showRaiseHandApplicationNotificationView(userId: requestItem.userId, userName: requestItem.userName, count: inviteSeatList.count)
  90. }
  91. }
  92. }