Fix main-thread decryption block by caching token in StateFlow (#322)

Issue #278 reported that `AuthManager.getToken()` (which reads from
`EncryptedSharedPreferences`) was being called synchronously inside the
`MainNavigation` composable, causing UI thread blocks during navigation
transitions (PERF-10).

This commit resolves the issue by caching the token in a new `tokenFlow`
(`MutableStateFlow`) within `AuthManager`.
- The flow is initialised with the token during `AuthManager.init()`.
- Updates to the token (`setToken`, `setSelectedProfileId`,
  `setProfileToken`) are synchronised with `tokenFlow`.
- `MainNavigation` now correctly observes this state using
  `collectAsState()`, preventing disk-read blocking on recompositions.

Impact: Navigation transitions are now fully non-blocking. No side effects
are expected as state synchronisation maintains parity with persistent storage.

Resolves #278
This commit is contained in:
M57 authored and GitHub committed 2026-06-23 12:19:33 +03:00
1 parent 4d47db263f
commit 3798d0330f
2 files changed
+12 -1

No files matched your search

@@ -326,7 +326,8 @@ private fun appEntryProvider(
@Composable
fun MainNavigation(sessionId: String? = null) {
val hasToken = !AuthManager.getToken().isNullOrBlank()
val token by AuthManager.tokenFlow.collectAsState()
val hasToken = !token.isNullOrBlank()
val startScreen: NavKey = if (hasToken) ChatScreen else ConnectScreen
val backStack = rememberNavBackStack(startScreen)