ConferenceListReducer.swift 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. //
  2. // Conference.swift
  3. // TUIRoomKit
  4. //
  5. // Created by CY zhao on 2024/6/12.
  6. //
  7. import Foundation
  8. let ConferenceListReducer = Reducer<ConferenceListState>(
  9. ReduceOn(ConferenceListActions.updateConferenceList, reduce: { state, action in
  10. let incomingConferences = action.payload.0
  11. let previousConferences = state.scheduledConferences
  12. let combinedSet = Set(incomingConferences + previousConferences)
  13. state.scheduledConferences = Array(combinedSet)
  14. state.fetchScheduledConferencesCursor = action.payload.1
  15. }),
  16. ReduceOn(ConferenceListActions.insertConference, reduce: { state, action in
  17. if !state.scheduledConferences.contains(action.payload) {
  18. state.scheduledConferences.append(action.payload)
  19. }
  20. }),
  21. ReduceOn(ConferenceListActions.removeConference, reduce: { state, action in
  22. let conferenceToRemove = action.payload
  23. let conferences = state.scheduledConferences.map { $0.basicInfo.roomId }
  24. if let index = conferences.firstIndex(of: conferenceToRemove) {
  25. state.scheduledConferences.remove(at: index)
  26. }
  27. }),
  28. ReduceOn(ConferenceListActions.onConferenceUpdated, reduce: { state, action in
  29. let conference = action.payload
  30. if let index = state.scheduledConferences.firstIndex(where: { $0.basicInfo.roomId == conference.basicInfo.roomId }) {
  31. state.scheduledConferences[index] = conference
  32. }
  33. }),
  34. ReduceOn(ConferenceListActions.resetConferenceList, reduce: { state, action in
  35. state.scheduledConferences.removeAll()
  36. })
  37. )