Files
element-x-ios/ElementX/Sources/Services/UserSession/UserSessionManagerProtocol.swift
T
Mauro Romito 5792a8ba5c 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.
2026-10-09 15:21:27 +02:00

50 lines
2.3 KiB
Swift

//
// Copyright 2026 Element Creations Ltd.
//
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
// Please see LICENSE files in the repository root for full details.
//
import Combine
import Foundation
import MatrixRustSDK
enum UserSessionManagerError: Error {
/// There are no signed in accounts to restore.
case noAccounts
/// None of the signed in accounts could be restored, so they have all been removed.
case failedRestoringSessions
}
// sourcery: AutoMockable
protocol UserSessionManagerProtocol: AnyObject {
/// Every signed in account, most recently active first. Known before any session is live, so use it to count accounts.
var userIDs: [String] { get }
/// The live sessions, in the same order as `userIDs`.
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?
// MARK: - Lifecycle
/// Restores the most recently active account, falling back to the next one when it fails (the store deletes failed accounts).
///
/// 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)
/// Deletes an account's credentials and data, and forgets its session.
func remove(userID: String)
/// Deletes every account's credentials and data.
func reset()
}