fix(android): don't wipe viewer lists on rotation — reset only on genuine session change #1538

Merged
chris merged 1 commit from fix/android-viewer-rotation-empty into main 2026-11-16 20:14:14 +01:00
Owner

Symptom

Rotating the screen while a photo was open in the full-bleed viewer showed the empty state ("Der er ikke noget at vise lige nu.", R.string.viewer_empty) instead of the photo. The same wipe hit the search, album-detail, map and people viewers, and the open tab's list state in general.

Root-cause chain

  1. MainActivity has no configChanges → rotation = Activity recreation, the composition restarts.
  2. Six app-scoped shell ViewModels (search, albumDetail, personDetail, map, timeline, addToAlbum — hoisted in MemoriesApp.kt since ISSUE-121) survive recreation under the Activity's ViewModelStore…
  3. …but each site's LaunchedEffect(session) { vm.reset() } re-launches in the fresh composition even though session is unchanged: authRepository.session is a StateFlow (app-scoped in AppContainer) that replays its current value to the new collector.
  4. So every rotation ran all six reset() calls, and TimelineViewModel.reset() emptied _uiState to TimelineUiState() exactly while the viewer destinations rendered items = timelineState.items — tripping MediaViewerScreen's empty-list guard (the ISSUE-121 belt-and-braces check).
  5. The reset re-run also contradicted reset()'s documented intent: "forget everything about the PREVIOUS login", not the current one.

Fix shape

All six sites now call a small private helper:

@Composable
private fun ResetOnSessionChange(session: StoredSession?, onReset: () -> Unit) {
    var last by remember { mutableStateOf(session) }
    LaunchedEffect(session) {
        if (session != last) {
            last = session
            onReset()
        }
    }
}

The remember is deliberately not rememberSaveable: the last-seen-session anchor must NOT survive recreation, so a fresh composition re-seeds it from the current session and the comparison stays quiet — while a genuine session identity change (login null→s1, logout s1→null, re-login s1→s2; login always mints a fresh token and StoredSession is a data class, so structural inequality is exact) still trips the reset, preserving the ISSUE-121 requirement that one member's data never lingers under another member's login. Process death also re-seeds — correct, because the ViewModels are fresh anyway.

Untouched on purpose:

  • the session-navigation LaunchedEffect(session) (harmless on recreation: currentRoute is restored, not LOGIN);
  • MediaViewerScreen's empty-list guard (stays as the ISSUE-121 safety net);
  • timeline refresh-on-re-entry (comes from ensureLoaded()'s own reentry throttle, independent of reset).

Validation

  • ANDROID_HOME=~/Android/Sdk rtk ./gradlew :app:compileDebugKotlin (from apps/android/) → BUILD SUCCESSFUL; no new warnings from the touched file.
  • ANDROID_HOME=~/Android/Sdk rtk ./gradlew :app:testDebugUnitTest → BUILD SUCCESSFUL; aggregated XML results: 75 suites / 935 tests / 0 skipped / 0 failures / 0 errors — matches the pre-change baseline exactly.
  • No automated regression test added: the bug lives at the composition-lifecycle level (Activity recreation) and the test tree has no Robolectric activity-recreation/compose-recomposition precedent — scaffolding one would be heroic, so per repo practice manual QA covers it:
    1. Open a photo in the viewer → rotate the device → the photo stays (before: empty state).
    2. Log out → log in as another member → timeline/search/album/map lists are reset (no data lingering across accounts).
    3. Search, open a result, rotate → results (not the empty state) are shown.

Bookkeeping

  • KNOWN_ISSUES.md: new entry ISSUE-237 (high / open / area:timeline) in the Active section, referencing this branch for the resolve step after merge.
  • apps/android/CHANGELOG.md: new ## [0.10.26] - 2026-11-16 Fixed entry (user-facing wording).
  • apps/android/app/build.gradle.kts: versionName = "0.10.25" → "0.10.26" (versionCode derives from versionName; per release policy the bump rides along because the changelog entry exists).
## Symptom Rotating the screen while a photo was open in the full-bleed viewer showed the empty state ("Der er ikke noget at vise lige nu.", `R.string.viewer_empty`) instead of the photo. The same wipe hit the search, album-detail, map and people viewers, and the open tab's list state in general. ## Root-cause chain 1. `MainActivity` has no `configChanges` → rotation = Activity recreation, the composition restarts. 2. Six app-scoped shell ViewModels (search, albumDetail, personDetail, map, timeline, addToAlbum — hoisted in `MemoriesApp.kt` since ISSUE-121) **survive** recreation under the Activity's `ViewModelStore`… 3. …but each site's `LaunchedEffect(session) { vm.reset() }` re-launches in the fresh composition even though `session` is unchanged: `authRepository.session` is a `StateFlow` (app-scoped in `AppContainer`) that replays its current value to the new collector. 4. So every rotation ran all six `reset()` calls, and `TimelineViewModel.reset()` emptied `_uiState` to `TimelineUiState()` exactly while the viewer destinations rendered `items = timelineState.items` — tripping `MediaViewerScreen`'s empty-list guard (the ISSUE-121 belt-and-braces check). 5. The reset re-run also contradicted `reset()`'s documented intent: "forget everything about the PREVIOUS login", not the current one. ## Fix shape All six sites now call a small private helper: ```kotlin @Composable private fun ResetOnSessionChange(session: StoredSession?, onReset: () -> Unit) { var last by remember { mutableStateOf(session) } LaunchedEffect(session) { if (session != last) { last = session onReset() } } } ``` The `remember` is deliberately **not** `rememberSaveable`: the last-seen-session anchor must NOT survive recreation, so a fresh composition re-seeds it from the *current* session and the comparison stays quiet — while a genuine session identity change (login `null→s1`, logout `s1→null`, re-login `s1→s2`; login always mints a fresh token and `StoredSession` is a data class, so structural inequality is exact) still trips the reset, preserving the ISSUE-121 requirement that one member's data never lingers under another member's login. Process death also re-seeds — correct, because the ViewModels are fresh anyway. Untouched on purpose: - the session-navigation `LaunchedEffect(session)` (harmless on recreation: `currentRoute` is restored, not LOGIN); - `MediaViewerScreen`'s empty-list guard (stays as the ISSUE-121 safety net); - timeline refresh-on-re-entry (comes from `ensureLoaded()`'s own reentry throttle, independent of reset). ## Validation - `ANDROID_HOME=~/Android/Sdk rtk ./gradlew :app:compileDebugKotlin` (from `apps/android/`) → **BUILD SUCCESSFUL**; no new warnings from the touched file. - `ANDROID_HOME=~/Android/Sdk rtk ./gradlew :app:testDebugUnitTest` → **BUILD SUCCESSFUL**; aggregated XML results: **75 suites / 935 tests / 0 skipped / 0 failures / 0 errors** — matches the pre-change baseline exactly. - No automated regression test added: the bug lives at the composition-lifecycle level (Activity recreation) and the test tree has no Robolectric activity-recreation/compose-recomposition precedent — scaffolding one would be heroic, so per repo practice manual QA covers it: 1. Open a photo in the viewer → rotate the device → **the photo stays** (before: empty state). 2. Log out → log in as another member → timeline/search/album/map lists are **reset** (no data lingering across accounts). 3. Search, open a result, rotate → results (not the empty state) are shown. ## Bookkeeping - `KNOWN_ISSUES.md`: new entry **ISSUE-237** (`high` / `open` / `area:timeline`) in the Active section, referencing this branch for the resolve step after merge. - `apps/android/CHANGELOG.md`: new `## [0.10.26] - 2026-11-16` Fixed entry (user-facing wording). - `apps/android/app/build.gradle.kts`: `versionName = "0.10.25"` → `"0.10.26"` (versionCode derives from versionName; per release policy the bump rides along because the changelog entry exists).
fix(android): don't wipe viewer lists on rotation — reset only on genuine session change
All checks were successful
Wiki Sync / Sync wiki from docs (push) Has been skipped
Build and Deploy / Detect changed services and test scopes (push) Successful in 3s
Build and Deploy / Smoke API and media worker (push) Has been skipped
Build and Deploy / Smoke web (push) Has been skipped
Build and Deploy / Smoke ML worker (push) Has been skipped
Build and Deploy / Smoke people worker (push) Has been skipped
Build and Deploy / Build postgres image (push) Has been skipped
Build and Deploy / Build Android CI image (push) Has been skipped
Build and Deploy / Build plants worker image (push) Has been skipped
Build and Deploy / Build converter worker image (push) Has been skipped
Build and Deploy / Deploy postgres (push) Has been skipped
Build and Deploy / Build API image (push) Has been skipped
Build and Deploy / Build media worker image (push) Has been skipped
Build and Deploy / Build ML worker image (push) Has been skipped
Build and Deploy / Build people worker image (push) Has been skipped
Build and Deploy / Build web image (push) Has been skipped
Build and Deploy / Deploy API (push) Has been skipped
Build and Deploy / Deploy web (push) Has been skipped
Build and Deploy / Deploy media worker (push) Has been skipped
Build and Deploy / Deploy ML worker (push) Has been skipped
Build and Deploy / Deploy plants worker (push) Has been skipped
Build and Deploy / Deploy people worker (push) Has been skipped
Build and Deploy / Deploy converter worker (push) Has been skipped
Build and Deploy / Deploy review instance (push) Has been skipped
Build and Deploy / Clean Docker build artifacts (push) Has been skipped
Build and Deploy / Run scoped PR tests (push) Successful in 5s
Build and Deploy / Android unit tests and debug APK (push) Successful in 6m44s
3f99168660
Rotating the screen while a photo was open showed the viewer empty state
instead of the photo. MainActivity has no configChanges, so rotation
recreates the Activity; the six app-scoped shell ViewModels survive that
recreation, but each `LaunchedEffect(session) { vm.reset() }` re-launches
in the fresh composition because authRepository.session is a StateFlow
that replays its current value — so every rotation ran all six resets and
TimelineViewModel.reset() emptied the list the open viewer was rendering
(R.string.viewer_empty). The reset's documented intent is to forget the
PREVIOUS login, which a re-run on recreation contradicts.

All six sites now go through ResetOnSessionChange(session), a wrapper
whose plain (deliberately NOT rememberSaveable) remembered last-session
state makes the reset fire only on a genuine session identity change:
recreation re-seeds the anchor quietly, while login (null→s1), logout
(s1→null) and re-login (s1→s2) still reset so one member's data never
lingers under another member's login. StoredSession is a data class with
freshly minted tokens, so structural inequality is exact.

The session-navigation LaunchedEffect is untouched (harmless on
recreation: currentRoute is restored), the viewer empty-list guard stays
as the ISSUE-121 belt-and-braces check, and timeline refresh-on-re-entry
keeps coming from ensureLoaded()'s own reentry throttle.

KNOWN_ISSUES: ISSUE-237. Changelog + versionName 0.10.26.

Validation: :app:compileDebugKotlin green; :app:testDebugUnitTest green
(75 suites / 935 tests / 0 failures, baseline unchanged). Manual QA
scenario: open photo → rotate → photo stays; login/logout → lists reset.
chris merged commit f1e78a140e into main 2026-11-16 20:14:14 +01:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
chris/memories!1538
No description provided.