mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-03 16:10:20 +00:00
Create sessions through the user session manager
Sign-in now creates and stores its session through the UserSessionManagerProtocol rather than the UserSessionStore, so the AppCoordinator no longer holds the store. - The manager forwards clientSessionDelegate and userSession(for:sessionDirectories:passphrase:) to its store. As with restoring, the new session isn't registered until the AppCoordinator adds it. - AuthenticationService takes the manager, for both sign-in and soft logout. - The store mock's configuration moves to the manager mock.
This commit is contained in:
@@ -15,7 +15,7 @@ class AuthenticationService: AuthenticationServiceProtocol {
|
||||
private var sessionDirectories: SessionDirectories
|
||||
private let passphrase: Data
|
||||
|
||||
private let userSessionStore: UserSessionStoreProtocol
|
||||
private let userSessionManager: UserSessionManagerProtocol
|
||||
private let classicAppManager: ClassicAppManagerProtocol?
|
||||
private let clientFactory: ClientFactoryProtocol
|
||||
private let appSettings: AppSettings
|
||||
@@ -33,7 +33,7 @@ class AuthenticationService: AuthenticationServiceProtocol {
|
||||
|
||||
let classicAppAccount: ClassicAppAccount?
|
||||
|
||||
init(userSessionStore: UserSessionStoreProtocol,
|
||||
init(userSessionManager: UserSessionManagerProtocol,
|
||||
encryptionKeyProvider: EncryptionKeyProviderProtocol,
|
||||
classicAppManager: ClassicAppManagerProtocol?,
|
||||
clientFactory: ClientFactoryProtocol = ClientFactory(),
|
||||
@@ -42,7 +42,7 @@ class AuthenticationService: AuthenticationServiceProtocol {
|
||||
sessionDirectories = .init()
|
||||
passphrase = encryptionKeyProvider.generateKey()
|
||||
|
||||
self.userSessionStore = userSessionStore
|
||||
self.userSessionManager = userSessionManager
|
||||
self.classicAppManager = classicAppManager
|
||||
self.clientFactory = clientFactory
|
||||
self.appSettings = appSettings
|
||||
@@ -270,7 +270,7 @@ class AuthenticationService: AuthenticationServiceProtocol {
|
||||
let client = try await clientFactory.makeAuthenticationClient(serverNameOrBaseURL: serverNameOrBaseURL,
|
||||
sessionDirectories: sessionDirectories,
|
||||
passphrase: passphrase,
|
||||
clientSessionDelegate: userSessionStore.clientSessionDelegate,
|
||||
clientSessionDelegate: userSessionManager.clientSessionDelegate,
|
||||
appSettings: appSettings,
|
||||
appHooks: appHooks)
|
||||
try await appHooks.remoteSettingsHook.initializeCache(using: client, applyingTo: appSettings).get()
|
||||
@@ -284,7 +284,7 @@ class AuthenticationService: AuthenticationServiceProtocol {
|
||||
}
|
||||
|
||||
private func userSession(for client: ClientProtocol) async -> Result<UserSessionProtocol, AuthenticationServiceError> {
|
||||
switch await userSessionStore.userSession(for: client, sessionDirectories: sessionDirectories, passphrase: passphrase) {
|
||||
switch await userSessionManager.userSession(for: client, sessionDirectories: sessionDirectories, passphrase: passphrase) {
|
||||
case .success(let clientProxy):
|
||||
return .success(clientProxy)
|
||||
case .failure:
|
||||
@@ -302,7 +302,7 @@ class AuthenticationService: AuthenticationServiceProtocol {
|
||||
|
||||
do {
|
||||
let client = try await clientFactory.makeInMemoryClient(serverNameOrBaseURL: classicAppAccount.homeserverURL.absoluteString,
|
||||
clientSessionDelegate: userSessionStore.clientSessionDelegate,
|
||||
clientSessionDelegate: userSessionManager.clientSessionDelegate,
|
||||
appSettings: appSettings,
|
||||
appHooks: appHooks)
|
||||
let loginDetails = await client.homeserverLoginDetails()
|
||||
@@ -406,7 +406,7 @@ extension AuthenticationService {
|
||||
}
|
||||
|
||||
static func mock(classicAppManager: ClassicAppManagerProtocol?) -> AuthenticationService {
|
||||
AuthenticationService(userSessionStore: UserSessionStoreMock(.init()),
|
||||
AuthenticationService(userSessionManager: UserSessionManagerMock(.init()),
|
||||
encryptionKeyProvider: EncryptionKeyProvider(),
|
||||
classicAppManager: classicAppManager,
|
||||
clientFactory: ClientFactoryMock(.init()),
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
import Combine
|
||||
import Foundation
|
||||
import MatrixRustSDK
|
||||
import OrderedCollections
|
||||
|
||||
/// Owns the live session of every account signed in on this device, most recently active first.
|
||||
@@ -37,6 +38,10 @@ final class UserSessionManager: UserSessionManagerProtocol {
|
||||
accounts.values.first ?? nil
|
||||
}
|
||||
|
||||
var clientSessionDelegate: ClientSessionDelegate {
|
||||
userSessionStore.clientSessionDelegate
|
||||
}
|
||||
|
||||
init(userSessionStore: UserSessionStoreProtocol, appSettings: AppSettings) {
|
||||
self.userSessionStore = userSessionStore
|
||||
self.appSettings = appSettings
|
||||
@@ -79,6 +84,10 @@ final class UserSessionManager: UserSessionManagerProtocol {
|
||||
return .failure(.failedRestoringSessions)
|
||||
}
|
||||
|
||||
func userSession(for client: ClientProtocol, sessionDirectories: SessionDirectories, passphrase: Data) async -> Result<UserSessionProtocol, UserSessionStoreError> {
|
||||
await userSessionStore.userSession(for: client, sessionDirectories: sessionDirectories, passphrase: passphrase)
|
||||
}
|
||||
|
||||
func add(_ userSession: UserSessionProtocol) {
|
||||
let userID = userSession.clientProxy.userID
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
import Combine
|
||||
import Foundation
|
||||
import MatrixRustSDK
|
||||
|
||||
enum UserSessionManagerError: Error {
|
||||
/// There are no signed in accounts to restore.
|
||||
@@ -23,6 +24,8 @@ protocol UserSessionManagerProtocol: AnyObject {
|
||||
var sessionsPublisher: CurrentValuePublisher<[UserSessionProtocol], Never> { get }
|
||||
/// The session of the account whose UI is on screen.
|
||||
var activeSession: UserSessionProtocol? { get }
|
||||
/// The delegate that stores changes to a client's session, such as refreshed tokens.
|
||||
var clientSessionDelegate: ClientSessionDelegate { get }
|
||||
|
||||
func session(for userID: String) -> UserSessionProtocol?
|
||||
|
||||
@@ -32,6 +35,10 @@ protocol UserSessionManagerProtocol: AnyObject {
|
||||
///
|
||||
/// The session isn't registered: `add` it once it's ready to be used, e.g. after any migrations.
|
||||
func restoreActiveSession() async -> Result<UserSessionProtocol, UserSessionManagerError>
|
||||
/// Creates the session of an account that has just signed in, and stores its credentials.
|
||||
///
|
||||
/// The session isn't registered: `add` it once it's ready to be used.
|
||||
func userSession(for client: ClientProtocol, sessionDirectories: SessionDirectories, passphrase: Data) async -> Result<UserSessionProtocol, UserSessionStoreError>
|
||||
/// Registers a session that's ready to be used, replacing any previous session for its account.
|
||||
/// A new account becomes the active one, a known account keeps its place.
|
||||
func add(_ userSession: UserSessionProtocol)
|
||||
|
||||
Reference in New Issue
Block a user