feat: Implement forced logout on app restart and prioritize last logged public key in UnlockScreen

This commit is contained in:
k1ngsterr1
2026-01-09 03:56:48 +05:00
parent eab7cf35fd
commit b6786cea05
4 changed files with 25 additions and 7 deletions

View File

@@ -53,7 +53,9 @@ class MainActivity : ComponentActivity() {
var accountInfoList by remember { mutableStateOf<List<AccountInfo>>(emptyList()) } var accountInfoList by remember { mutableStateOf<List<AccountInfo>>(emptyList()) }
// Check for existing accounts and build AccountInfo list // Check for existing accounts and build AccountInfo list
// Also force logout so user always sees unlock screen on app restart
LaunchedEffect(Unit) { LaunchedEffect(Unit) {
accountManager.logout() // Always start logged out
val accounts = accountManager.getAllAccounts() val accounts = accountManager.getAllAccounts()
hasExistingAccount = accounts.isNotEmpty() hasExistingAccount = accounts.isNotEmpty()
accountInfoList = accounts.map { account -> accountInfoList = accounts.map { account ->

View File

@@ -19,6 +19,7 @@ class AccountManager(private val context: Context) {
private val CURRENT_PUBLIC_KEY = stringPreferencesKey("current_public_key") private val CURRENT_PUBLIC_KEY = stringPreferencesKey("current_public_key")
private val ACCOUNTS_JSON = stringPreferencesKey("accounts_json") private val ACCOUNTS_JSON = stringPreferencesKey("accounts_json")
private val IS_LOGGED_IN = booleanPreferencesKey("is_logged_in") private val IS_LOGGED_IN = booleanPreferencesKey("is_logged_in")
private val LAST_LOGGED_PUBLIC_KEY = stringPreferencesKey("last_logged_public_key")
} }
val currentPublicKey: Flow<String?> = context.accountDataStore.data.map { preferences -> val currentPublicKey: Flow<String?> = context.accountDataStore.data.map { preferences ->
@@ -33,6 +34,14 @@ class AccountManager(private val context: Context) {
preferences[ACCOUNTS_JSON] preferences[ACCOUNTS_JSON]
} }
val lastLoggedPublicKey: Flow<String?> = context.accountDataStore.data.map { preferences ->
preferences[LAST_LOGGED_PUBLIC_KEY]
}
suspend fun getLastLoggedPublicKey(): String? {
return context.accountDataStore.data.first()[LAST_LOGGED_PUBLIC_KEY]
}
suspend fun saveAccount(account: EncryptedAccount) { suspend fun saveAccount(account: EncryptedAccount) {
context.accountDataStore.edit { preferences -> context.accountDataStore.edit { preferences ->
val existingJson = preferences[ACCOUNTS_JSON] val existingJson = preferences[ACCOUNTS_JSON]
@@ -66,6 +75,7 @@ class AccountManager(private val context: Context) {
context.accountDataStore.edit { preferences -> context.accountDataStore.edit { preferences ->
preferences[CURRENT_PUBLIC_KEY] = publicKey preferences[CURRENT_PUBLIC_KEY] = publicKey
preferences[IS_LOGGED_IN] = true preferences[IS_LOGGED_IN] = true
preferences[LAST_LOGGED_PUBLIC_KEY] = publicKey
} }
} }

View File

@@ -93,9 +93,16 @@ fun UnlockScreen(
) )
} }
// Select account // Select account - prioritize: selectedAccountId > lastLoggedPublicKey > currentPublicKey > first account
val targetPublicKey = selectedAccountId ?: accountManager.currentPublicKey.first() val lastLoggedKey = accountManager.getLastLoggedPublicKey()
val currentKey = accountManager.currentPublicKey.first()
val targetPublicKey = selectedAccountId ?: lastLoggedKey ?: currentKey
Log.d("UnlockScreen", "selectedAccountId=$selectedAccountId, lastLoggedKey=$lastLoggedKey, currentKey=$currentKey, targetPublicKey=$targetPublicKey")
Log.d("UnlockScreen", "accounts=${accounts.map { it.publicKey }}")
selectedAccount = accounts.find { it.publicKey == targetPublicKey } ?: accounts.firstOrNull() selectedAccount = accounts.find { it.publicKey == targetPublicKey } ?: accounts.firstOrNull()
Log.d("UnlockScreen", "selectedAccount=${selectedAccount?.publicKey}")
} }
// Filter accounts by search // Filter accounts by search

View File

@@ -159,7 +159,7 @@ fun ChatsListScreen(
val drawerState = rememberDrawerState(initialValue = DrawerValue.Closed) val drawerState = rememberDrawerState(initialValue = DrawerValue.Closed)
val scope = rememberCoroutineScope() val scope = rememberCoroutineScope()
// Update status bar and navigation bar colors to match background // Update status bar and completely hide navigation bar
LaunchedEffect(isDarkTheme) { LaunchedEffect(isDarkTheme) {
if (!view.isInEditMode) { if (!view.isInEditMode) {
val window = (view.context as android.app.Activity).window val window = (view.context as android.app.Activity).window
@@ -169,10 +169,9 @@ fun ChatsListScreen(
insetsController.isAppearanceLightStatusBars = !isDarkTheme insetsController.isAppearanceLightStatusBars = !isDarkTheme
window.statusBarColor = android.graphics.Color.TRANSPARENT window.statusBarColor = android.graphics.Color.TRANSPARENT
// Navigation bar - match background color // Completely hide navigation bar
val navBarColor = if (isDarkTheme) 0xFF1A1A1A.toInt() else 0xFFFFFFFF.toInt() insetsController.hide(androidx.core.view.WindowInsetsCompat.Type.navigationBars())
window.navigationBarColor = navBarColor insetsController.systemBarsBehavior = androidx.core.view.WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
insetsController.isAppearanceLightNavigationBars = !isDarkTheme
} }
} }