fc0b65bb6ab2

fix: post-unlock lands on last-viewed entry detail — LastViewedEntryStore (encrypted prefs), EntryDetail records id, LockViewModel.postUnlockDestination resolves last→first→list, NavHost pops Lock; PostUnlockNavTest 5 tests; v2.2.5 (150 tests)

time
2026-06-10 12:17 UTC · 5 months ago
machine
koh2
agent
claude-opus-4-8
session
qa fix sprint v2.2.5
parent
6abb5231210e
6 files app/src/main/java/com/peachpasswords/ui/PeachAppNavHost.kt+2 −2app/src/main/java/com/peachpasswords/ui/lock/LockScreen.kt+4 −3app/src/main/java/com/peachpasswords/ui/lock/LockViewModel.kt+25 −0app/src/main/java/com/peachpasswords/ui/vault/EntryDetailViewModel.kt+8 −0app/src/main/java/com/peachpasswords/vault/LastViewedEntryStore.kt+47 −0app/src/test/java/com/peachpasswords/ui/lock/PostUnlockNavTest.kt+69 −0
app/src/main/java/com/peachpasswords/ui/PeachAppNavHost.kt+2 −2
@@ -102,8 +102,8 @@102102         }103103         composable(PeachNav.Lock.route) {104104             LockScreen(105-                onUnlocked = {106-                    navController.navigate(PeachNav.VaultList.route) {105+                onUnlocked = { dest ->106+                    navController.navigate(dest.route) {107107                         popUpTo(PeachNav.Lock.route) { inclusive = true }108108                     }109109                 },

view file

app/src/main/java/com/peachpasswords/ui/lock/LockScreen.kt+4 −3
@@ -65,7 +65,7 @@6565 6666 @Composable6767 fun LockScreen(68-    onUnlocked: () -> Unit,68+    onUnlocked: (PeachNav) -> Unit,6969     onNavigate: (PeachNav) -> Unit,7070     vm: LockViewModel = hiltViewModel(),7171 ) {@@ -81,8 +81,9 @@8181 8282     LaunchedEffect(state.unlocked) {8383         if (state.unlocked) {84-            // A freshly created vault routes through recovery-word setup before the list.85-            if (state.created) onNavigate(PeachNav.RecoverySetup) else onUnlocked()84+            // A freshly created vault routes through recovery-word setup before the list;85+            // an ordinary unlock lands on the last-viewed entry (brief Fix 3).86+            if (state.created) onNavigate(PeachNav.RecoverySetup) else onUnlocked(vm.postUnlockDestination())8687         }8788     }8889 

view file

app/src/main/java/com/peachpasswords/ui/lock/LockViewModel.kt+25 −0
@@ -5,12 +5,16 @@55 import androidx.fragment.app.FragmentActivity66 import androidx.lifecycle.ViewModel77 import androidx.lifecycle.viewModelScope8+import com.peachpasswords.ui.PeachNav9+import com.peachpasswords.ui.vault.entryTapDestination810 import com.peachpasswords.vault.CreateVaultResult911 import com.peachpasswords.ui.security.scorePassword1012 import com.peachpasswords.vault.DuressAction1113 import com.peachpasswords.vault.DuressConfigStore14+import com.peachpasswords.vault.LastViewedEntryStore1215 import com.peachpasswords.vault.UnlockResult1316 import com.peachpasswords.vault.VaultCrypto17+import com.peachpasswords.vault.VaultEntry1418 import com.peachpasswords.vault.VaultLockTimer1519 import com.peachpasswords.vault.VaultRepository1620 import dagger.hilt.android.lifecycle.HiltViewModel@@ -43,15 +47,36 @@4347 /** CreateVault is blocked below "fair" (zxcvbn-style score 2). */4448 const val MIN_CREATE_STRENGTH = 24549 50+/**51+ * Where to land after a successful unlock (brief Fix 3): the last-viewed entry if52+ * it still exists, else the first active entry, else the vault list when the vault53+ * is empty. [resolve] re-validates the stored id against the live vault so a stale54+ * pointer (deleted entry) falls back rather than opening a missing detail screen.55+ * NOTE entries route to the notes editor via [entryTapDestination], same as a list tap.56+ */57+internal fun postUnlockDestination(58+    lastViewedId: String?,59+    resolve: (String) -> VaultEntry?,60+    firstActive: VaultEntry?,61+): PeachNav {62+    val target = lastViewedId?.let(resolve) ?: firstActive ?: return PeachNav.VaultList63+    return entryTapDestination(target)64+}65+4666 @HiltViewModel4767 class LockViewModel @Inject constructor(4868     private val vault: VaultRepository,4969     private val duress: DuressConfigStore,5070     private val lockTimer: VaultLockTimer,71+    private val lastViewed: LastViewedEntryStore,5172     @ApplicationContext private val appContext: Context,5273 ) : ViewModel() {5374     private val _state = MutableStateFlow(LockUiState(hasVault = vault.hasVault()))5475     val state: StateFlow<LockUiState> = _state.asStateFlow()76+77+    /** Resolve where a just-unlocked session should land (last-viewed entry → first → list). */78+    fun postUnlockDestination(): PeachNav =79+        postUnlockDestination(lastViewed.get(), vault::getEntry, vault.getActiveEntries().firstOrNull())5580 5681     fun unlock(password: String) {5782         if (password.isBlank() || _state.value.loading) return

view file

app/src/main/java/com/peachpasswords/ui/vault/EntryDetailViewModel.kt+8 −0
@@ -5,6 +5,7 @@55 import androidx.lifecycle.SavedStateHandle66 import androidx.lifecycle.ViewModel77 import androidx.lifecycle.viewModelScope8+import com.peachpasswords.vault.LastViewedEntryStore89 import com.peachpasswords.vault.TOTPGenerator910 import com.peachpasswords.vault.VaultEntry1011 import com.peachpasswords.vault.VaultRepository@@ -23,9 +24,16 @@2324 class EntryDetailViewModel @Inject constructor(2425     private val vault: VaultRepository,2526     @ApplicationContext private val appContext: Context,27+    private val lastViewed: LastViewedEntryStore,2628     savedStateHandle: SavedStateHandle,2729 ) : ViewModel() {2830     private val id: String = savedStateHandle.get<String>("id").orEmpty()31+32+    init {33+        // Remember this as the entry to reopen on the next unlock (brief Fix 3).34+        // Only record a real, live entry so a bad deep-link id never becomes the target.35+        if (id.isNotBlank() && vault.getEntry(id) != null) lastViewed.set(id)36+    }2937 3038     val entry: StateFlow<VaultEntry?> =3139         vault.entries.map { list -> list.firstOrNull { it.id == id } }

view file

app/src/main/java/com/peachpasswords/vault/LastViewedEntryStore.kt+47 −0
@@ -0,0 +1,47 @@1+package com.peachpasswords.vault2+3+import android.content.Context4+import androidx.security.crypto.EncryptedSharedPreferences5+import androidx.security.crypto.MasterKey6+import dagger.hilt.android.qualifiers.ApplicationContext7+import javax.inject.Inject8+import javax.inject.Singleton9+10+/**11+ * Remembers the id of the entry the user last opened so a fresh unlock can land12+ * straight back on it (matching the extension, which opens the last entry rather13+ * than the list). Stored in EncryptedSharedPreferences — an entry id is a weak14+ * pointer, but it should not be trivially readable at rest, and it sits in the15+ * same encrypted-prefs family as the duress/license stores.16+ *17+ * The id is only a hint: the post-unlock resolver re-validates it against the18+ * live vault, so a stale id (entry since deleted) silently falls back to the19+ * first entry.20+ */21+@Singleton22+class LastViewedEntryStore @Inject constructor(23+    @ApplicationContext context: Context,24+) {25+    private val masterKey = MasterKey.Builder(context)26+        .setKeyScheme(MasterKey.KeyScheme.AES256_GCM)27+        .build()28+29+    private val prefs = EncryptedSharedPreferences.create(30+        context,31+        PREFS_FILE,32+        masterKey,33+        EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,34+        EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM,35+    )36+37+    fun get(): String? = prefs.getString(KEY_LAST_VIEWED, null)38+39+    fun set(id: String) { prefs.edit().putString(KEY_LAST_VIEWED, id).apply() }40+41+    fun clear() { prefs.edit().remove(KEY_LAST_VIEWED).apply() }42+43+    companion object {44+        private const val PREFS_FILE = "peach_nav_prefs"45+        private const val KEY_LAST_VIEWED = "last_viewed_entry_id"46+    }47+}

view file

app/src/test/java/com/peachpasswords/ui/lock/PostUnlockNavTest.kt+69 −0
@@ -0,0 +1,69 @@1+package com.peachpasswords.ui.lock2+3+import com.peachpasswords.ui.PeachNav4+import com.peachpasswords.vault.VaultEntry5+import org.junit.Assert.assertEquals6+import org.junit.Test7+8+class PostUnlockNavTest {9+10+    private fun login(id: String) =11+        VaultEntry(id = id, title = id, type = "login", createdAt = 0, modifiedAt = 0)12+13+    private fun note(id: String) =14+        VaultEntry(id = id, title = id, type = "note", createdAt = 0, modifiedAt = 0)15+16+    /** A still-present last-viewed id reopens that entry's detail. */17+    @Test fun unlock_navigates_to_last_viewed_entry() {18+        val entries = mapOf("a" to login("a"), "b" to login("b"))19+        val dest = postUnlockDestination(20+            lastViewedId = "b",21+            resolve = entries::get,22+            firstActive = entries["a"],23+        )24+        assertEquals(PeachNav.EntryDetail("b"), dest)25+    }26+27+    /** No history → land on the first active entry. */28+    @Test fun unlock_navigates_to_first_entry_when_no_history() {29+        val first = login("a")30+        val dest = postUnlockDestination(31+            lastViewedId = null,32+            resolve = { null },33+            firstActive = first,34+        )35+        assertEquals(PeachNav.EntryDetail("a"), dest)36+    }37+38+    /** Empty vault → fall back to the list. */39+    @Test fun unlock_navigates_to_vault_list_when_empty() {40+        val dest = postUnlockDestination(41+            lastViewedId = null,42+            resolve = { null },43+            firstActive = null,44+        )45+        assertEquals(PeachNav.VaultList, dest)46+    }47+48+    /** A stale last-viewed id (entry since deleted) falls back to the first entry. */49+    @Test fun unlock_falls_back_when_last_viewed_deleted() {50+        val first = login("a")51+        val dest = postUnlockDestination(52+            lastViewedId = "ghost",53+            resolve = { null }, // "ghost" no longer resolves54+            firstActive = first,55+        )56+        assertEquals(PeachNav.EntryDetail("a"), dest)57+    }58+59+    /** A last-viewed NOTE reopens the notes editor, not the entry detail. */60+    @Test fun unlock_last_viewed_note_routes_to_notes_mode() {61+        val entries = mapOf("n" to note("n"))62+        val dest = postUnlockDestination(63+            lastViewedId = "n",64+            resolve = entries::get,65+            firstActive = entries["n"],66+        )67+        assertEquals(PeachNav.NotesMode("n"), dest)68+    }69+}

view file