From df426bb8dfa0637624e38c0125953ac89574f717 Mon Sep 17 00:00:00 2001 From: Jean-Luc Makiola Date: Mon, 6 Jul 2026 20:34:15 +0200 Subject: [PATCH] fix: scope import ViewModel per-uri so a second import re-parses MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ImportScreen has no nav backstack, so an unkeyed hiltViewModel() resolved to the Activity's ViewModelStore and was retained across imports. Its one-shot `load` guard then showed the *previous* file's parsed state on the next import — trivially reachable now that the in-app Restore button lets you export→restore or restore twice in one session (worst case: the picker still holds file A, so tapping Import writes A's events after you picked B). Keying the VM by the file uri hands each distinct file a fresh VM (fresh Loading state); the same uri (rotation) reuses it and holds the result. Co-Authored-By: Claude Opus 4.8 (1M context) --- .../jeanlucmakiola/calendula/ui/imports/ImportScreen.kt | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/imports/ImportScreen.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/imports/ImportScreen.kt index 42e417d..de3e9c7 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/imports/ImportScreen.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/imports/ImportScreen.kt @@ -75,7 +75,13 @@ fun ImportScreen( onClose: () -> Unit, onOpenSingle: (EventForm) -> Unit, forceMany: Boolean = false, - viewModel: ImportViewModel = hiltViewModel(), + // Key the VM by the file uri. This screen has no nav backstack, so an + // unkeyed hiltViewModel() resolves to the Activity's store and is retained + // across imports — its one-shot `load` guard would then show the *previous* + // file's parsed state on the next import (a second restore, export→restore, + // etc.). Keying per uri hands each distinct file a fresh VM (fresh Loading + // state), while the same uri (rotation) reuses it and holds the result. + viewModel: ImportViewModel = hiltViewModel(key = uri.toString()), ) { LaunchedEffect(uri) { viewModel.load(uri, forceMany) } val state by viewModel.state.collectAsStateWithLifecycle()