mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-02 12:07:55 +00:00
refactored the ClientProxy to:
- throw an error during the init - to have its init directly instantiate the sync service and the providers Update ElementX/Sources/Services/UserSession/UserSessionStore.swift Co-authored-by: Doug <6060466+pixlwave@users.noreply.github.com> renamed
This commit is contained in:
@@ -20,13 +20,13 @@ class ClientProxy: ClientProxyProtocol {
|
||||
private let mediaLoader: MediaLoaderProtocol
|
||||
private let clientQueue: DispatchQueue
|
||||
|
||||
private var roomListService: RoomListService?
|
||||
private var roomListService: RoomListService
|
||||
// periphery: ignore - only for retain
|
||||
private var roomListStateUpdateTaskHandle: TaskHandle?
|
||||
// periphery: ignore - only for retain
|
||||
private var roomListStateLoadingStateUpdateTaskHandle: TaskHandle?
|
||||
|
||||
private var syncService: SyncService?
|
||||
private var syncService: SyncService
|
||||
// periphery: ignore - only for retain
|
||||
private var syncServiceStateUpdateTaskHandle: TaskHandle?
|
||||
|
||||
@@ -43,10 +43,10 @@ class ClientProxy: ClientProxyProtocol {
|
||||
|
||||
// These following summary providers both operate on the same allRooms() list but
|
||||
// can apply their own filtering and pagination
|
||||
private(set) var roomSummaryProvider: RoomSummaryProviderProtocol?
|
||||
private(set) var alternateRoomSummaryProvider: RoomSummaryProviderProtocol?
|
||||
private(set) var roomSummaryProvider: RoomSummaryProviderProtocol
|
||||
private(set) var alternateRoomSummaryProvider: RoomSummaryProviderProtocol
|
||||
|
||||
private(set) var staticRoomSummaryProvider: StaticRoomSummaryProviderProtocol?
|
||||
private(set) var staticRoomSummaryProvider: StaticRoomSummaryProviderProtocol
|
||||
|
||||
let notificationSettings: NotificationSettingsProxyProtocol
|
||||
|
||||
@@ -139,7 +139,7 @@ class ClientProxy: ClientProxyProtocol {
|
||||
init(client: ClientProtocol,
|
||||
needsSlidingSyncMigration: Bool,
|
||||
networkMonitor: NetworkMonitorProtocol,
|
||||
appSettings: AppSettings) async {
|
||||
appSettings: AppSettings) async throws {
|
||||
self.client = client
|
||||
self.networkMonitor = networkMonitor
|
||||
self.appSettings = appSettings
|
||||
@@ -153,7 +153,22 @@ class ClientProxy: ClientProxyProtocol {
|
||||
secureBackupController = SecureBackupController(encryption: client.encryption())
|
||||
|
||||
self.needsSlidingSyncMigration = needsSlidingSyncMigration
|
||||
|
||||
|
||||
let configuredAppService = try await ClientProxyServices(client: client,
|
||||
actionsSubject: actionsSubject,
|
||||
notificationSettings: notificationSettings,
|
||||
appSettings: appSettings)
|
||||
|
||||
syncService = configuredAppService.syncService
|
||||
roomListService = configuredAppService.roomListService
|
||||
roomSummaryProvider = configuredAppService.roomSummaryProvider
|
||||
alternateRoomSummaryProvider = configuredAppService.alternateRoomSummaryProvider
|
||||
staticRoomSummaryProvider = configuredAppService.staticRoomSummaryProvider
|
||||
|
||||
syncServiceStateUpdateTaskHandle = createSyncServiceStateObserver(syncService)
|
||||
roomListStateUpdateTaskHandle = createRoomListServiceObserver(roomListService)
|
||||
roomListStateLoadingStateUpdateTaskHandle = createRoomListLoadingStateUpdateObserver(roomListService)
|
||||
|
||||
delegateHandle = client.setDelegate(delegate: ClientDelegateWrapper { [weak self] isSoftLogout in
|
||||
self?.hasEncounteredAuthError = true
|
||||
self?.actionsSubject.send(.receivedAuthError(isSoftLogout: isSoftLogout))
|
||||
@@ -168,8 +183,6 @@ class ClientProxy: ClientProxyProtocol {
|
||||
}
|
||||
}
|
||||
.store(in: &cancellables)
|
||||
|
||||
await configureAppService()
|
||||
|
||||
loadUserAvatarURLFromCache()
|
||||
|
||||
@@ -281,7 +294,7 @@ class ClientProxy: ClientProxyProtocol {
|
||||
MXLog.info("Starting sync")
|
||||
|
||||
Task {
|
||||
await syncService?.start()
|
||||
await syncService.start()
|
||||
|
||||
// If we are using OIDC we want to cache the account management URL in volatile memory on the SDK side.
|
||||
// To avoid the cache being invalidated while the app is backgrounded, we cache at every sync start.
|
||||
@@ -322,12 +335,6 @@ class ClientProxy: ClientProxyProtocol {
|
||||
restartTask = nil
|
||||
}
|
||||
|
||||
guard let syncService else {
|
||||
MXLog.warning("No sync service to stop.")
|
||||
completion?()
|
||||
return
|
||||
}
|
||||
|
||||
// Capture the sync service strongly as this method is called on deinit and so the
|
||||
// existence of self when the Task executes is questionable and would sometimes crash.
|
||||
// Note: This isn't strictly necessary now given the unwrap above, but leaving the code as
|
||||
@@ -493,12 +500,6 @@ class ClientProxy: ClientProxyProtocol {
|
||||
return room
|
||||
}
|
||||
|
||||
// Else wait for the visible rooms list to go into fully loaded
|
||||
guard let roomSummaryProvider else {
|
||||
MXLog.error("Rooms summary provider not setup yet")
|
||||
return nil
|
||||
}
|
||||
|
||||
if !roomSummaryProvider.statePublisher.value.isLoaded {
|
||||
_ = await roomSummaryProvider.statePublisher.values.first { $0.isLoaded }
|
||||
}
|
||||
@@ -524,33 +525,11 @@ class ClientProxy: ClientProxyProtocol {
|
||||
}
|
||||
|
||||
func roomSummaryForIdentifier(_ identifier: String) -> RoomSummary? {
|
||||
// the alternate room summary provider is not impacted by filtering
|
||||
guard let provider = staticRoomSummaryProvider else {
|
||||
MXLog.verbose("Missing room summary provider")
|
||||
return nil
|
||||
}
|
||||
|
||||
guard let roomSummary = provider.roomListPublisher.value.first(where: { $0.id == identifier }) else {
|
||||
MXLog.verbose("Missing room summary, count: \(provider.roomListPublisher.value.count)")
|
||||
return nil
|
||||
}
|
||||
|
||||
return roomSummary
|
||||
staticRoomSummaryProvider.roomListPublisher.value.first(where: { $0.id == identifier })
|
||||
}
|
||||
|
||||
func roomSummaryForAlias(_ alias: String) -> RoomSummary? {
|
||||
// the alternate room summary provider is not impacted by filtering
|
||||
guard let provider = staticRoomSummaryProvider else {
|
||||
MXLog.verbose("Missing room summary provider")
|
||||
return nil
|
||||
}
|
||||
|
||||
guard let roomSummary = provider.roomListPublisher.value.first(where: { $0.canonicalAlias == alias || $0.alternativeAliases.contains(alias) }) else {
|
||||
MXLog.verbose("Missing room summary, count: \(provider.roomListPublisher.value.count)")
|
||||
return nil
|
||||
}
|
||||
|
||||
return roomSummary
|
||||
staticRoomSummaryProvider.roomListPublisher.value.first(where: { $0.canonicalAlias == alias || $0.alternativeAliases.contains(alias) })
|
||||
}
|
||||
|
||||
func loadUserDisplayName() async -> Result<Void, ClientProxyError> {
|
||||
@@ -839,63 +818,7 @@ class ClientProxy: ClientProxyProtocol {
|
||||
self.userAvatarURLSubject.value = urlString.flatMap(URL.init)
|
||||
}
|
||||
}
|
||||
|
||||
private func configureAppService() async {
|
||||
guard syncService == nil else {
|
||||
fatalError("This shouldn't be called more than once")
|
||||
}
|
||||
|
||||
do {
|
||||
let syncService = try await client
|
||||
.syncService()
|
||||
.withCrossProcessLock()
|
||||
.withUtdHook(delegate: ClientDecryptionErrorDelegate(actionsSubject: actionsSubject))
|
||||
.finish()
|
||||
|
||||
let roomListService = syncService.roomListService()
|
||||
|
||||
let roomMessageEventStringBuilder = RoomMessageEventStringBuilder(attributedStringBuilder: AttributedStringBuilder(cacheKey: "roomList",
|
||||
mentionBuilder: PlainMentionBuilder()), destination: .roomList)
|
||||
let eventStringBuilder = RoomEventStringBuilder(stateEventStringBuilder: RoomStateEventStringBuilder(userID: userID, shouldDisambiguateDisplayNames: false),
|
||||
messageEventStringBuilder: roomMessageEventStringBuilder,
|
||||
shouldDisambiguateDisplayNames: false,
|
||||
shouldPrefixSenderName: true)
|
||||
|
||||
roomSummaryProvider = RoomSummaryProvider(roomListService: roomListService,
|
||||
eventStringBuilder: eventStringBuilder,
|
||||
name: "AllRooms",
|
||||
shouldUpdateVisibleRange: true,
|
||||
notificationSettings: notificationSettings,
|
||||
appSettings: appSettings)
|
||||
try await roomSummaryProvider?.setRoomList(roomListService.allRooms())
|
||||
|
||||
alternateRoomSummaryProvider = RoomSummaryProvider(roomListService: roomListService,
|
||||
eventStringBuilder: eventStringBuilder,
|
||||
name: "AlternateAllRooms",
|
||||
notificationSettings: notificationSettings,
|
||||
appSettings: appSettings)
|
||||
try await alternateRoomSummaryProvider?.setRoomList(roomListService.allRooms())
|
||||
|
||||
staticRoomSummaryProvider = RoomSummaryProvider(roomListService: roomListService,
|
||||
eventStringBuilder: eventStringBuilder,
|
||||
name: "StaticAllRooms",
|
||||
roomListPageSize: .max,
|
||||
notificationSettings: notificationSettings,
|
||||
appSettings: appSettings)
|
||||
try await staticRoomSummaryProvider?.setRoomList(roomListService.allRooms())
|
||||
|
||||
self.syncService = syncService
|
||||
self.roomListService = roomListService
|
||||
|
||||
syncServiceStateUpdateTaskHandle = createSyncServiceStateObserver(syncService)
|
||||
roomListStateUpdateTaskHandle = createRoomListServiceObserver(roomListService)
|
||||
roomListStateLoadingStateUpdateTaskHandle = createRoomListLoadingStateUpdateObserver(roomListService)
|
||||
|
||||
} catch {
|
||||
MXLog.error("Failed building room list service with error: \(error)")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private func createSyncServiceStateObserver(_ syncService: SyncService) -> TaskHandle {
|
||||
syncService.state(listener: SyncServiceStateObserverProxy { [weak self] state in
|
||||
guard let self else { return }
|
||||
@@ -967,11 +890,6 @@ class ClientProxy: ClientProxyProtocol {
|
||||
}()
|
||||
|
||||
private func buildRoomForIdentifier(_ roomID: String) async -> RoomProxyType? {
|
||||
guard let roomListService else {
|
||||
MXLog.error("Failed retrieving room: \(roomID), room list service not set up")
|
||||
return nil
|
||||
}
|
||||
|
||||
do {
|
||||
let roomListItem = try roomListService.room(roomId: roomID)
|
||||
|
||||
@@ -1205,3 +1123,57 @@ private class SendQueueRoomErrorListenerProxy: SendQueueRoomErrorListener {
|
||||
onErrorClosure(roomId, error)
|
||||
}
|
||||
}
|
||||
|
||||
private struct ClientProxyServices {
|
||||
let syncService: SyncService
|
||||
let roomListService: RoomListService
|
||||
let roomSummaryProvider: RoomSummaryProviderProtocol
|
||||
let alternateRoomSummaryProvider: RoomSummaryProviderProtocol
|
||||
let staticRoomSummaryProvider: StaticRoomSummaryProviderProtocol
|
||||
|
||||
init(client: ClientProtocol,
|
||||
actionsSubject: PassthroughSubject<ClientProxyAction, Never>,
|
||||
notificationSettings: NotificationSettingsProxyProtocol,
|
||||
appSettings: AppSettings) async throws {
|
||||
let syncService = try await client
|
||||
.syncService()
|
||||
.withCrossProcessLock()
|
||||
.withUtdHook(delegate: ClientDecryptionErrorDelegate(actionsSubject: actionsSubject))
|
||||
.finish()
|
||||
|
||||
let roomListService = syncService.roomListService()
|
||||
|
||||
let roomMessageEventStringBuilder = RoomMessageEventStringBuilder(attributedStringBuilder: AttributedStringBuilder(cacheKey: "roomList",
|
||||
mentionBuilder: PlainMentionBuilder()), destination: .roomList)
|
||||
let eventStringBuilder = try RoomEventStringBuilder(stateEventStringBuilder: RoomStateEventStringBuilder(userID: client.userId(), shouldDisambiguateDisplayNames: false),
|
||||
messageEventStringBuilder: roomMessageEventStringBuilder,
|
||||
shouldDisambiguateDisplayNames: false,
|
||||
shouldPrefixSenderName: true)
|
||||
|
||||
roomSummaryProvider = RoomSummaryProvider(roomListService: roomListService,
|
||||
eventStringBuilder: eventStringBuilder,
|
||||
name: "AllRooms",
|
||||
shouldUpdateVisibleRange: true,
|
||||
notificationSettings: notificationSettings,
|
||||
appSettings: appSettings)
|
||||
try await roomSummaryProvider.setRoomList(roomListService.allRooms())
|
||||
|
||||
alternateRoomSummaryProvider = RoomSummaryProvider(roomListService: roomListService,
|
||||
eventStringBuilder: eventStringBuilder,
|
||||
name: "AlternateAllRooms",
|
||||
notificationSettings: notificationSettings,
|
||||
appSettings: appSettings)
|
||||
try await alternateRoomSummaryProvider.setRoomList(roomListService.allRooms())
|
||||
|
||||
staticRoomSummaryProvider = RoomSummaryProvider(roomListService: roomListService,
|
||||
eventStringBuilder: eventStringBuilder,
|
||||
name: "StaticAllRooms",
|
||||
roomListPageSize: .max,
|
||||
notificationSettings: notificationSettings,
|
||||
appSettings: appSettings)
|
||||
try await staticRoomSummaryProvider.setRoomList(roomListService.allRooms())
|
||||
|
||||
self.syncService = syncService
|
||||
self.roomListService = roomListService
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,15 +94,15 @@ protocol ClientProxyProtocol: AnyObject, MediaLoaderProtocol {
|
||||
|
||||
var pusherNotificationClientIdentifier: String? { get }
|
||||
|
||||
var roomSummaryProvider: RoomSummaryProviderProtocol? { get }
|
||||
var roomSummaryProvider: RoomSummaryProviderProtocol { get }
|
||||
|
||||
/// Used for listing rooms that shouldn't be affected by the main `roomSummaryProvider` filtering
|
||||
/// But can still be filtered by queries, since this may be shared across multiple views, remember to reset
|
||||
/// The filtering state when you are done with it
|
||||
var alternateRoomSummaryProvider: RoomSummaryProviderProtocol? { get }
|
||||
var alternateRoomSummaryProvider: RoomSummaryProviderProtocol { get }
|
||||
|
||||
/// Used for listing rooms, can't be filtered nor its state observed
|
||||
var staticRoomSummaryProvider: StaticRoomSummaryProviderProtocol? { get }
|
||||
var staticRoomSummaryProvider: StaticRoomSummaryProviderProtocol { get }
|
||||
|
||||
var roomsToAwait: Set<String> { get set }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user