diff --git a/ElementX/Sources/FlowCoordinators/RoomFlowCoordinator.swift b/ElementX/Sources/FlowCoordinators/RoomFlowCoordinator.swift index 77d57fc9b..186a1593b 100644 --- a/ElementX/Sources/FlowCoordinators/RoomFlowCoordinator.swift +++ b/ElementX/Sources/FlowCoordinators/RoomFlowCoordinator.swift @@ -282,6 +282,8 @@ class RoomFlowCoordinator: FlowCoordinatorProtocol { roomProxy = proxy } + await roomProxy.subscribeForUpdates() + actionsSubject.send(.presentedRoom(roomID)) self.roomProxy = roomProxy @@ -369,6 +371,7 @@ class RoomFlowCoordinator: FlowCoordinatorProtocol { private func asyncPresentRoomDetails(roomID: String, isRoot: Bool, animated: Bool) async { if isRoot { roomProxy = await userSession.clientProxy.roomForIdentifier(roomID) + await roomProxy?.subscribeForUpdates() } else { await asyncPresentRoom(roomID, animated: animated) } diff --git a/ElementX/Sources/Mocks/Generated/GeneratedMocks.swift b/ElementX/Sources/Mocks/Generated/GeneratedMocks.swift index e38036a96..0b852fa11 100644 --- a/ElementX/Sources/Mocks/Generated/GeneratedMocks.swift +++ b/ElementX/Sources/Mocks/Generated/GeneratedMocks.swift @@ -778,6 +778,18 @@ class RoomProxyMock: RoomProxyProtocol { } var underlyingTimelineProvider: RoomTimelineProviderProtocol! + //MARK: - subscribeForUpdates + + var subscribeForUpdatesCallsCount = 0 + var subscribeForUpdatesCalled: Bool { + return subscribeForUpdatesCallsCount > 0 + } + var subscribeForUpdatesClosure: (() async -> Void)? + + func subscribeForUpdates() async { + subscribeForUpdatesCallsCount += 1 + await subscribeForUpdatesClosure?() + } //MARK: - loadAvatarURLForUserId var loadAvatarURLForUserIdCallsCount = 0 diff --git a/ElementX/Sources/Services/Room/RoomProxy.swift b/ElementX/Sources/Services/Room/RoomProxy.swift index c2a40d52c..c1d7535d4 100644 --- a/ElementX/Sources/Services/Room/RoomProxy.swift +++ b/ElementX/Sources/Services/Room/RoomProxy.swift @@ -72,6 +72,18 @@ class RoomProxy: RoomProxyProtocol { self.room = room self.backgroundTaskService = backgroundTaskService + Task { + await fetchMembers() + await updateMembers() + } + } + + func subscribeForUpdates() async { + guard innerTimelineProvider == nil else { + MXLog.warning("Room already subscribed for updates") + return + } + let settings = RoomSubscription(requiredState: [RequiredState(key: "m.room.name", value: ""), RequiredState(key: "m.room.topic", value: ""), RequiredState(key: "m.room.avatar", value: ""), @@ -79,26 +91,21 @@ class RoomProxy: RoomProxyProtocol { RequiredState(key: "m.room.join_rules", value: "")], timelineLimit: UInt32(SlidingSyncConstants.defaultTimelineLimit)) roomListItem.subscribe(settings: settings) - + let timelineListener = RoomTimelineListener { [weak self] timelineDiffs in self?.updatesSubject.send(timelineDiffs) } - + self.timelineListener = timelineListener let result = await room.addTimelineListener(listener: timelineListener) roomTimelineObservationToken = result.itemsStream - + subscribeToBackpagination() innerTimelineProvider = await RoomTimelineProvider(currentItems: result.items, updatePublisher: updatesPublisher, backPaginationStatePublisher: backPaginationStateSubject.eraseToAnyPublisher()) - - Task { - await fetchMembers() - await updateMembers() - } } lazy var id: String = room.id() diff --git a/ElementX/Sources/Services/Room/RoomProxyProtocol.swift b/ElementX/Sources/Services/Room/RoomProxyProtocol.swift index 79902cb6b..ca5c938d0 100644 --- a/ElementX/Sources/Services/Room/RoomProxyProtocol.swift +++ b/ElementX/Sources/Services/Room/RoomProxyProtocol.swift @@ -77,6 +77,8 @@ protocol RoomProxyProtocol { var updatesPublisher: AnyPublisher<[TimelineDiff], Never> { get } var timelineProvider: RoomTimelineProviderProtocol { get } + + func subscribeForUpdates() async func loadAvatarURLForUserId(_ userId: String) async -> Result