Skip to content

Commit 0d33e1e

Browse files
committed
Cleaning up user data on logout and passing a weak client delegate wrapper to rust.
1 parent e50f025 commit 0d33e1e

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

ElementX/Sources/AppCoordinator.swift

+4
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ class AppCoordinator: AuthenticationCoordinatorDelegate, Coordinator {
6464
}
6565

6666
func authenticationCoordinatorDidTearDownUserSession(_ authenticationCoordinator: AuthenticationCoordinator) {
67+
if let presentedCoordinator = childCoordinators.first {
68+
remove(childCoordinator: presentedCoordinator)
69+
}
70+
6771
mainNavigationController.setViewControllers([splashViewController], animated: false)
6872
authenticationCoordinator.start()
6973
}

ElementX/Sources/Modules/Authentication/AuthenticationCoordinator.swift

+16
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,13 @@ class AuthenticationCoordinator: Coordinator {
7474

7575
func logout() {
7676
keychainController.removeAllTokens()
77+
78+
if let userIdentifier = userSession?.userIdentifier {
79+
deleteBaseDirectoryForUsername(userIdentifier)
80+
}
81+
7782
userSession = nil
83+
7884
delegate?.authenticationCoordinatorDidTearDownUserSession(self)
7985
}
8086

@@ -183,4 +189,14 @@ class AuthenticationCoordinator: Coordinator {
183189

184190
return url.path
185191
}
192+
193+
private func deleteBaseDirectoryForUsername(_ username: String) {
194+
guard var url = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first else {
195+
fatalError("Should always be able to retrieve the caches directory")
196+
}
197+
198+
url = url.appendingPathComponent(username)
199+
200+
try? FileManager.default.removeItem(at: url)
201+
}
186202
}

ElementX/Sources/Modules/Authentication/UserSession.swift

+18-3
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,33 @@ enum UserSessionError: Error {
1818
case failedRetrievingAvatar
1919
}
2020

21+
private class WeakUserSessionWrapper: ClientDelegate {
22+
weak var userSession: UserSession?
23+
24+
init(userSession: UserSession) {
25+
self.userSession = userSession
26+
}
27+
28+
func didReceiveSyncUpdate() {
29+
userSession?.didReceiveSyncUpdate()
30+
}
31+
}
32+
2133
class UserSession: ClientDelegate {
2234

2335
private let client: Client
2436

37+
deinit {
38+
client.setDelegate(delegate: nil)
39+
}
40+
2541
let callbacks = PassthroughSubject<UserSessionCallback, Never>()
2642

2743
init(client: Client) {
2844
self.client = client
2945

30-
if !client.hasFirstSynced() {
31-
client.startSync(delegate: self)
32-
}
46+
client.setDelegate(delegate: WeakUserSessionWrapper(userSession: self))
47+
client.startSync()
3348
}
3449

3550
var userIdentifier: String {

0 commit comments

Comments
 (0)