diff --git a/ElementX/Sources/AppCoordinator.swift b/ElementX/Sources/AppCoordinator.swift index a4c972345..ce848d3a0 100644 --- a/ElementX/Sources/AppCoordinator.swift +++ b/ElementX/Sources/AppCoordinator.swift @@ -64,6 +64,10 @@ class AppCoordinator: AuthenticationCoordinatorDelegate, Coordinator { } func authenticationCoordinatorDidTearDownUserSession(_ authenticationCoordinator: AuthenticationCoordinator) { + if let presentedCoordinator = childCoordinators.first { + remove(childCoordinator: presentedCoordinator) + } + mainNavigationController.setViewControllers([splashViewController], animated: false) authenticationCoordinator.start() } diff --git a/ElementX/Sources/Modules/Authentication/AuthenticationCoordinator.swift b/ElementX/Sources/Modules/Authentication/AuthenticationCoordinator.swift index 0399697f3..3ae3efa77 100644 --- a/ElementX/Sources/Modules/Authentication/AuthenticationCoordinator.swift +++ b/ElementX/Sources/Modules/Authentication/AuthenticationCoordinator.swift @@ -74,7 +74,13 @@ class AuthenticationCoordinator: Coordinator { func logout() { keychainController.removeAllTokens() + + if let userIdentifier = userSession?.userIdentifier { + deleteBaseDirectoryForUsername(userIdentifier) + } + userSession = nil + delegate?.authenticationCoordinatorDidTearDownUserSession(self) } @@ -183,4 +189,14 @@ class AuthenticationCoordinator: Coordinator { return url.path } + + private func deleteBaseDirectoryForUsername(_ username: String) { + guard var url = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first else { + fatalError("Should always be able to retrieve the caches directory") + } + + url = url.appendingPathComponent(username) + + try? FileManager.default.removeItem(at: url) + } } diff --git a/ElementX/Sources/Modules/Authentication/UserSession.swift b/ElementX/Sources/Modules/Authentication/UserSession.swift index 70e2c5727..610bfe237 100644 --- a/ElementX/Sources/Modules/Authentication/UserSession.swift +++ b/ElementX/Sources/Modules/Authentication/UserSession.swift @@ -18,18 +18,33 @@ enum UserSessionError: Error { case failedRetrievingAvatar } +private class WeakUserSessionWrapper: ClientDelegate { + weak var userSession: UserSession? + + init(userSession: UserSession) { + self.userSession = userSession + } + + func didReceiveSyncUpdate() { + userSession?.didReceiveSyncUpdate() + } +} + class UserSession: ClientDelegate { private let client: Client + deinit { + client.setDelegate(delegate: nil) + } + let callbacks = PassthroughSubject() init(client: Client) { self.client = client - if !client.hasFirstSynced() { - client.startSync(delegate: self) - } + client.setDelegate(delegate: WeakUserSessionWrapper(userSession: self)) + client.startSync() } var userIdentifier: String {