Package-level declarations
Session lifecycle management
Session Manager
The session package contains session management functionality. A session is the lifecycle that all libraries follow and on which their entire operation depends. Most functions will throw SessionIsNotInitializedException if the session is inactive. The interface that interacts with the session is SessionManager.
// Build core manager.
val coreContext: CoreContext = CoreManager.build(applicationContext)
val session: SessionManager = coreContext.sessionManager
// Starts session every time when launch your app.
session.start(
reset = false, // if true, then performs the session.stop() procedure at the beginning
baseUrl = "https://my.domain.com/",
accessToken = "Backend access token",
profileId = "user profile id",
userAgent = "my user agent"
)
// Then use features functions with features or core libraries.
// ...
// Stop session after logout
session.stop()During work, you can check the session state using utility methods.
val coreContext: CoreContext
val session: SessionManager = coreContext.sessionManager
// false if session is not active
session.isSessionActive
// throw SessionIsNotInitializedException if session does not have selected state
session.chekState(SessionState.ACTIVE)
// throw SessionIsNotInitializedException if session is not initialized
session.checkSessionInitialized()Lifecycle
A session has its own lifecycle, which consists of several stages described in the SessionState enumeration:
SessionState.NOT_INITIALIZED - Initial state.
SessionState.STARTS - Session initialization process.
SessionState.ACTIVE - The session is initialized, all components are ready for operation.
SessionState.STOPS - The session is stopping.
SessionState.INACTIVE - The session is stopped. The session is inactive.
You can track the session lifecycle using stateFlow.
val coreContext: CoreContext
val session: SessionManager = coreContext.sessionManager
lifecycleScope.launch {
session.stateFlow.collect { state -> // state: SessionState
// Handle it...
}
}You can also create your own handler and pass it directly to SessionManager. This is how all libraries associate with sessions.
val coreContext: CoreContext
val session: SessionManager = coreContext.sessionManager
val sessionHandler = object : SessionUnit {
override suspend fun onState(state: SessionState) {
// Handle it...
}
}
val isAdded = session.registerUnit(sessionHandler)
// isAdded == false if handler already addedSession Credentials
В процессе работы пользователю может понадобится доступ к данным сессии. Набор данных выражен интерфейсом SessionCredentials Cуществует ряд утилитарных методов для доступа к этим данных.
val coreContext: CoreContext
val session: SessionManager = coreContext.sessionManager
val credentials: SessionCredentials = session.getCredentialsOrThrow()
// SessionCredentials fields
val baseUrl: String = session.getBaseUrlOrThrow()
val accessToken: String = session.getAccessTokenOrThrow()
val userId: Long = session.getUserIdOrThrow()
val userAgent: String = session.getUserAgentOrThrow()Types
Exception that occurs when a session is not initialized