Skip to content

Commit

Permalink
Cleaning up user data on logout and passing a weak client delegate wr…
Browse files Browse the repository at this point in the history
…apper to rust.
  • Loading branch information
stefanceriu committed Mar 1, 2022
1 parent e50f025 commit 0d33e1e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
4 changes: 4 additions & 0 deletions ElementX/Sources/AppCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,13 @@ class AuthenticationCoordinator: Coordinator {

func logout() {
keychainController.removeAllTokens()

if let userIdentifier = userSession?.userIdentifier {
deleteBaseDirectoryForUsername(userIdentifier)
}

userSession = nil

delegate?.authenticationCoordinatorDidTearDownUserSession(self)
}

Expand Down Expand Up @@ -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)
}
}
21 changes: 18 additions & 3 deletions ElementX/Sources/Modules/Authentication/UserSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<UserSessionCallback, Never>()

init(client: Client) {
self.client = client

if !client.hasFirstSynced() {
client.startSync(delegate: self)
}
client.setDelegate(delegate: WeakUserSessionWrapper(userSession: self))
client.startSync()
}

var userIdentifier: String {
Expand Down

0 comments on commit 0d33e1e

Please sign in to comment.