Fixes cache cleaning behavior after sdk change

This commit is contained in:
Stefan Ceriu
2023-05-12 18:24:20 +03:00
committed by Stefan Ceriu
parent 1981270840
commit a2b38f7656
3 changed files with 18 additions and 18 deletions
@@ -458,20 +458,21 @@ class AppCoordinator: AppCoordinatorProtocol {
private func clearCache() { private func clearCache() {
showLoadingIndicator() showLoadingIndicator()
defer {
hideLoadingIndicator()
}
navigationRootCoordinator.setRootCoordinator(SplashScreenCoordinator()) navigationRootCoordinator.setRootCoordinator(SplashScreenCoordinator())
userSession.clientProxy.stopSync() userSession.clientProxy.stopSync()
userSessionFlowCoordinator?.stop() userSessionFlowCoordinator?.stop()
userSessionStore.clearCacheFor(userSession: userSession) let userID = userSession.userID
tearDownUserSession() tearDownUserSession()
stateMachine.processEvent(.startWithExistingSession) // Allow for everything to deallocate properly
Task {
try await Task.sleep(for: .seconds(2))
userSessionStore.clearCache(for: userID)
stateMachine.processEvent(.startWithExistingSession)
hideLoadingIndicator()
}
} }
} }
@@ -21,7 +21,7 @@ import MatrixRustSDK
class UserSessionStore: UserSessionStoreProtocol { class UserSessionStore: UserSessionStoreProtocol {
private let keychainController: KeychainControllerProtocol private let keychainController: KeychainControllerProtocol
private let backgroundTaskService: BackgroundTaskServiceProtocol private let backgroundTaskService: BackgroundTaskServiceProtocol
private let cachesFolderName = "matrix-sdk-state" private let matrixSDKStateKey = "matrix-sdk-state"
/// Whether or not there are sessions in the store. /// Whether or not there are sessions in the store.
var hasSessions: Bool { !keychainController.restorationTokens().isEmpty } var hasSessions: Bool { !keychainController.restorationTokens().isEmpty }
@@ -91,9 +91,8 @@ class UserSessionStore: UserSessionStoreProtocol {
deleteSessionDirectory(for: userID) deleteSessionDirectory(for: userID)
} }
func clearCacheFor(userSession: UserSessionProtocol) { func clearCache(for userID: String) {
let userID = userSession.clientProxy.userID deleteCaches(for: userID)
deleteCachesFolder(for: userID)
} }
// MARK: - Private // MARK: - Private
@@ -153,17 +152,17 @@ class UserSessionStore: UserSessionStoreProtocol {
} }
} }
private func deleteCachesFolder(for userID: String) { private func deleteCaches(for userID: String) {
let url = basePath(for: userID).appendingPathComponent(cachesFolderName)
do { do {
try FileManager.default.removeItem(at: url) for url in try FileManager.default.contentsOfDirectory(at: basePath(for: userID), includingPropertiesForKeys: nil) where url.path.contains(matrixSDKStateKey) {
try FileManager.default.removeItem(at: url)
}
} catch { } catch {
MXLog.failure("Failed deleting the session data: \(error)") MXLog.failure("Failed deleting the session data: \(error)")
} }
} }
#warning("We should move this and the caches folder path to the rust side") #warning("We should move this and the caches cleanup to the rust side")
private func basePath(for userID: String) -> URL { private func basePath(for userID: String) -> URL {
// Rust sanitises the user ID replacing invalid characters with an _ // Rust sanitises the user ID replacing invalid characters with an _
let sanitisedUserID = userID.replacingOccurrences(of: ":", with: "_") let sanitisedUserID = userID.replacingOccurrences(of: ":", with: "_")
@@ -47,5 +47,5 @@ protocol UserSessionStoreProtocol {
func logout(userSession: UserSessionProtocol) func logout(userSession: UserSessionProtocol)
/// Clears our all the matrix sdk state data for the specified session /// Clears our all the matrix sdk state data for the specified session
func clearCacheFor(userSession: UserSessionProtocol) func clearCache(for userID: String)
} }