mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-02 12:07:55 +00:00
Add developer option for clearing the application cache (#852)
* Add developer option for clearing the application cache * Tweaks following code review
This commit is contained in:
@@ -19,6 +19,7 @@ import SwiftUI
|
||||
|
||||
enum UserSessionFlowCoordinatorAction {
|
||||
case signOut
|
||||
case clearCache
|
||||
}
|
||||
|
||||
class UserSessionFlowCoordinator: CoordinatorProtocol {
|
||||
@@ -304,6 +305,8 @@ class UserSessionFlowCoordinator: CoordinatorProtocol {
|
||||
case .logout:
|
||||
self.navigationSplitCoordinator.setSheetCoordinator(nil)
|
||||
self.callback?(.signOut)
|
||||
case .clearCache:
|
||||
self.callback?(.clearCache)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@ import MatrixRustSDK
|
||||
class UserSessionStore: UserSessionStoreProtocol {
|
||||
private let keychainController: KeychainControllerProtocol
|
||||
private let backgroundTaskService: BackgroundTaskServiceProtocol
|
||||
private let cachesFolderName = "matrix-sdk-state"
|
||||
|
||||
/// Whether or not there are sessions in the store.
|
||||
var hasSessions: Bool { !keychainController.restorationTokens().isEmpty }
|
||||
@@ -90,6 +91,11 @@ class UserSessionStore: UserSessionStoreProtocol {
|
||||
deleteSessionDirectory(for: userID)
|
||||
}
|
||||
|
||||
func clearCacheFor(userSession: UserSessionProtocol) {
|
||||
let userID = userSession.clientProxy.userID
|
||||
deleteCachesFolder(for: userID)
|
||||
}
|
||||
|
||||
// MARK: - Private
|
||||
|
||||
private func buildUserSessionWithClient(_ clientProxy: ClientProxyProtocol) -> UserSessionProtocol {
|
||||
@@ -140,9 +146,15 @@ class UserSessionStore: UserSessionStoreProtocol {
|
||||
}
|
||||
|
||||
private func deleteSessionDirectory(for userID: String) {
|
||||
// Rust sanitises the user ID replacing invalid characters with an _
|
||||
let sanitisedUserID = userID.replacingOccurrences(of: ":", with: "_")
|
||||
let url = baseDirectory.appendingPathComponent(sanitisedUserID)
|
||||
do {
|
||||
try FileManager.default.removeItem(at: basePath(for: userID))
|
||||
} catch {
|
||||
MXLog.failure("Failed deleting the session data: \(error)")
|
||||
}
|
||||
}
|
||||
|
||||
private func deleteCachesFolder(for userID: String) {
|
||||
let url = basePath(for: userID).appendingPathComponent(cachesFolderName)
|
||||
|
||||
do {
|
||||
try FileManager.default.removeItem(at: url)
|
||||
@@ -150,4 +162,11 @@ class UserSessionStore: UserSessionStoreProtocol {
|
||||
MXLog.failure("Failed deleting the session data: \(error)")
|
||||
}
|
||||
}
|
||||
|
||||
#warning("We should move this and the caches folder path to the rust side")
|
||||
private func basePath(for userID: String) -> URL {
|
||||
// Rust sanitises the user ID replacing invalid characters with an _
|
||||
let sanitisedUserID = userID.replacingOccurrences(of: ":", with: "_")
|
||||
return baseDirectory.appendingPathComponent(sanitisedUserID)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,4 +45,7 @@ protocol UserSessionStoreProtocol {
|
||||
|
||||
/// Logs out of the specified session.
|
||||
func logout(userSession: UserSessionProtocol)
|
||||
|
||||
/// Clears our all the matrix sdk state data for the specified session
|
||||
func clearCacheFor(userSession: UserSessionProtocol)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user