Adopt the new room list service syncIndicatorListener

This commit is contained in:
Stefan Ceriu 2023-09-06 16:10:36 +03:00
parent 62b6cd598f
commit fad608cd34
4 changed files with 54 additions and 25 deletions

View File

@ -598,33 +598,25 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationCoordinatorDelegate,
ServiceLocator.shared.analytics.signpost.beginFirstSync()
userSession.clientProxy.startSync()
let identifier = "StaleDataIndicator"
func showLoadingIndicator() {
ServiceLocator.shared.userIndicatorController.submitIndicator(.init(id: identifier, type: .toast(progress: .indeterminate), title: L10n.commonSyncing, persistent: true))
}
guard clientProxyObserver == nil else {
return
}
// Prevent the syncing indicator from showing over the offline one
if ServiceLocator.shared.networkMonitor.reachabilityPublisher.value == .reachable {
showLoadingIndicator()
}
clientProxyObserver = userSession.clientProxy
.callbacks
.loadingStatePublisher
.removeDuplicates()
.receive(on: DispatchQueue.main)
.sink { action in
switch action {
case .startedUpdating:
showLoadingIndicator()
case .receivedSyncUpdate:
.sink { state in
let toastIdentifier = "StaleDataIndicator"
switch state {
case .loading:
if ServiceLocator.shared.networkMonitor.reachabilityPublisher.value == .reachable {
ServiceLocator.shared.userIndicatorController.submitIndicator(.init(id: toastIdentifier, type: .toast(progress: .indeterminate), title: L10n.commonSyncing, persistent: true))
}
case .notLoading:
ServiceLocator.shared.analytics.signpost.endFirstSync()
ServiceLocator.shared.userIndicatorController.retractIndicatorWithId(identifier)
default:
break
ServiceLocator.shared.userIndicatorController.retractIndicatorWithId(toastIdentifier)
}
}
}

View File

@ -30,6 +30,7 @@ class ClientProxy: ClientProxyProtocol {
private var roomListService: RoomListService?
private var roomListStateUpdateTaskHandle: TaskHandle?
private var roomListStateLoadingStateUpdateTaskHandle: TaskHandle?
private var syncService: SyncService?
private var syncServiceStateUpdateTaskHandle: TaskHandle?
@ -60,6 +61,11 @@ class ClientProxy: ClientProxyProtocol {
let callbacks = PassthroughSubject<ClientProxyCallback, Never>()
private let loadingStateSubject = CurrentValueSubject<ClientProxyLoadingState, Never>(.notLoading)
var loadingStatePublisher: CurrentValuePublisher<ClientProxyLoadingState, Never> {
loadingStateSubject.asCurrentValuePublisher()
}
init(client: ClientProtocol, backgroundTaskService: BackgroundTaskServiceProtocol, appSettings: AppSettings) async {
self.client = client
self.backgroundTaskService = backgroundTaskService
@ -419,6 +425,7 @@ class ClientProxy: ClientProxyProtocol {
syncServiceStateUpdateTaskHandle = createSyncServiceStateObserver(syncService)
roomListStateUpdateTaskHandle = createRoomListServiceObserver(roomListService)
roomListStateLoadingStateUpdateTaskHandle = createRoomListLoadingStateUpdateObserver(roomListService)
} catch {
MXLog.error("Failed building room list service with error: \(error)")
@ -432,10 +439,7 @@ class ClientProxy: ClientProxyProtocol {
MXLog.info("Received sync service update: \(state)")
switch state {
case .running:
// Show the loading spinner when the sync service goes into running
callbacks.send(.startedUpdating)
case .terminated, .idle:
case .running, .terminated, .idle:
break
case .error:
restartSync()
@ -458,6 +462,19 @@ class ClientProxy: ClientProxyProtocol {
})
}
private func createRoomListLoadingStateUpdateObserver(_ roomListService: RoomListService) -> TaskHandle {
roomListService.syncIndicator(listener: RoomListServiceSyncIndicatorListenerProxy { [weak self] state in
guard let self else { return }
switch state {
case .show:
loadingStateSubject.send(.loading)
case .hide:
loadingStateSubject.send(.notLoading)
}
})
}
private func roomTupleForIdentifier(_ identifier: String) -> (RoomListItem?, Room?) {
do {
let roomListItem = try roomListService?.room(roomId: identifier)
@ -509,6 +526,18 @@ private class RoomListStateListenerProxy: RoomListServiceStateListener {
}
}
private class RoomListServiceSyncIndicatorListenerProxy: RoomListServiceSyncIndicatorListener {
private let onUpdateClosure: (RoomListServiceSyncIndicator) -> Void
init(onUpdateClosure: @escaping (RoomListServiceSyncIndicator) -> Void) {
self.onUpdateClosure = onUpdateClosure
}
func onUpdate(syncIndicator: RoomListServiceSyncIndicator) {
onUpdateClosure(syncIndicator)
}
}
private class ClientDelegateWrapper: ClientDelegate {
private let authErrorCallback: (Bool) -> Void
private let tokenRefreshCallback: () -> Void

View File

@ -19,7 +19,6 @@ import Foundation
import MatrixRustSDK
enum ClientProxyCallback {
case startedUpdating
case receivedSyncUpdate
case receivedAuthError(isSoftLogout: Bool)
case updateRestorationToken
@ -33,6 +32,11 @@ enum ClientProxyCallback {
}
}
enum ClientProxyLoadingState {
case loading
case notLoading
}
enum ClientProxyError: Error {
case failedCreatingRoom
case failedRetrievingDirectRoom
@ -67,6 +71,8 @@ struct PusherConfiguration {
protocol ClientProxyProtocol: AnyObject, MediaLoaderProtocol {
var callbacks: PassthroughSubject<ClientProxyCallback, Never> { get }
var loadingStatePublisher: CurrentValuePublisher<ClientProxyLoadingState, Never> { get }
var userID: String { get }
var deviceID: String? { get }

View File

@ -21,6 +21,8 @@ import MatrixRustSDK
class MockClientProxy: ClientProxyProtocol {
let callbacks = PassthroughSubject<ClientProxyCallback, Never>()
let loadingStatePublisher = CurrentValuePublisher<ClientProxyLoadingState, Never>(.notLoading)
let userID: String
let deviceID: String?
let homeserver = ""