diff --git a/app/src/main/java/com/stevesoltys/seedvault/repo/SnapshotManager.kt b/app/src/main/java/com/stevesoltys/seedvault/repo/SnapshotManager.kt index 6d3bde26..15b2a931 100644 --- a/app/src/main/java/com/stevesoltys/seedvault/repo/SnapshotManager.kt +++ b/app/src/main/java/com/stevesoltys/seedvault/repo/SnapshotManager.kt @@ -47,6 +47,9 @@ internal class SnapshotManager( * currently available on the backend. */ suspend fun onSnapshotsLoaded(handles: List): List { + // first reset latest snapshot, otherwise we'd hang on to a stale one + // e.g. when switching to new storage without any snapshots + latestSnapshot = null return handles.mapNotNull { snapshotHandle -> val snapshot = try { loadSnapshot(snapshotHandle) diff --git a/app/src/test/java/com/stevesoltys/seedvault/repo/SnapshotManagerTest.kt b/app/src/test/java/com/stevesoltys/seedvault/repo/SnapshotManagerTest.kt index ee712c7f..5104f5a6 100644 --- a/app/src/test/java/com/stevesoltys/seedvault/repo/SnapshotManagerTest.kt +++ b/app/src/test/java/com/stevesoltys/seedvault/repo/SnapshotManagerTest.kt @@ -23,6 +23,7 @@ import org.calyxos.seedvault.core.toHexString import org.junit.jupiter.api.Assertions.assertArrayEquals import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Assertions.assertFalse +import org.junit.jupiter.api.Assertions.assertNull import org.junit.jupiter.api.Assertions.assertTrue import org.junit.jupiter.api.Test import org.junit.jupiter.api.io.TempDir @@ -75,6 +76,10 @@ internal class SnapshotManagerTest : TransportTest() { // snapshot with largest token is latest assertEquals(20, snapshotManager.latestSnapshot?.token) + + // when switching storage and now not having any snapshots, we must clear latestSnapshot + snapshotManager.onSnapshotsLoaded(emptyList()) + assertNull(snapshotManager.latestSnapshot) } @Test