diff --git a/android/app/src/main/java/net/awan/pipelines/ui/AppViewModel.kt b/android/app/src/main/java/net/awan/pipelines/ui/AppViewModel.kt index fdd28cd5..d35f03d8 100644 --- a/android/app/src/main/java/net/awan/pipelines/ui/AppViewModel.kt +++ b/android/app/src/main/java/net/awan/pipelines/ui/AppViewModel.kt @@ -1108,6 +1108,8 @@ class AppViewModel( fun consumeChatRemovalNotice(noticeId: Long) = chatStore.consumeRemovalNotice(noticeId) + fun beginChatRemovalNotice(noticeId: Long) = chatStore.beginRemovalNotice(noticeId) + fun undoRemoveChat(sessionId: String) = chatStore.undoRemove(sessionId) fun loadChatHistory(sessionId: String) { diff --git a/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatListPane.kt b/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatListPane.kt index c8d10fc5..00502cb6 100644 --- a/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatListPane.kt +++ b/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatListPane.kt @@ -110,21 +110,20 @@ internal fun ChatListPane( val removalNotice = state.removalNotice LaunchedEffect(removalNotice?.id) { removalNotice?.let { notice -> - // A rapid second hide replaces, rather than queues behind, the first - // report. Do not consume until showSnackbar finishes: consuming first - // changes this effect's key and cancels the snackbar coroutine. - snackbarHostState.currentSnackbarData?.dismiss() - if (snackbarHostState.showSnackbar( - message = CHAT_REMOVED_MESSAGE, - actionLabel = "Undo", - duration = SnackbarDuration.Short, - ) == SnackbarResult.ActionPerformed) { - vm.undoRemoveChat(notice.sessionId) + // Claim delivery before showing. Completion remains separate so a + // cancelled effect cannot make this same notice replay on re-entry. + if (vm.beginChatRemovalNotice(notice.id)) { + // A rapid second hide replaces, rather than queues behind, the first. + snackbarHostState.currentSnackbarData?.dismiss() + if (snackbarHostState.showSnackbar( + message = CHAT_REMOVED_MESSAGE, + actionLabel = "Undo", + duration = SnackbarDuration.Short, + ) == SnackbarResult.ActionPerformed) { + vm.undoRemoveChat(notice.sessionId) + } + vm.consumeChatRemovalNotice(notice.id) } - // A completed notice is not replayed after the pane is recreated. If - // another hide replaced it while this coroutine was cancelled, this - // exact-id consume is a no-op and the newer notice remains pending. - vm.consumeChatRemovalNotice(notice.id) } } var renaming by remember { mutableStateOf(null) } diff --git a/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatStore.kt b/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatStore.kt index 71821b0d..6f4e1446 100644 --- a/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatStore.kt +++ b/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatStore.kt @@ -260,6 +260,8 @@ data class ChatState( * hide irreversible, and revisiting the list must not report it again. */ val removalNotice: ChatRemovalNotice? = null, + /** The notice whose snackbar delivery has already started, if any. */ + val removalNoticePresentedId: Long? = null, /** What the operator has read of the list (see [ChatSeenState]). */ val seen: ChatSeenState = ChatSeenState(), /** @@ -562,6 +564,14 @@ class ChatStore( } } + /** Claims a notice before delivery so a cancelled pane cannot replay it. */ + fun beginRemovalNotice(noticeId: Long): Boolean { + val state = _state.value + if (state.removalNotice?.id != noticeId || state.removalNoticePresentedId == noticeId) return false + _state.value = state.copy(removalNoticePresentedId = noticeId) + return true + } + /** Restores [sessionId] only when it is still the current undoable removal. */ fun undoRemove(sessionId: String? = null) { val id = _state.value.lastRemoved ?: return diff --git a/android/app/src/test/java/net/awan/pipelines/ui/chat/ChatRemovalNoticeTest.kt b/android/app/src/test/java/net/awan/pipelines/ui/chat/ChatRemovalNoticeTest.kt index 53bcc261..159264c9 100644 --- a/android/app/src/test/java/net/awan/pipelines/ui/chat/ChatRemovalNoticeTest.kt +++ b/android/app/src/test/java/net/awan/pipelines/ui/chat/ChatRemovalNoticeTest.kt @@ -74,4 +74,18 @@ class ChatRemovalNoticeTest { assertNull(store.value.removalNotice) assertEquals(listOf("b", "c"), store.value.visibleSessions.map { it.id }) } + + @Test + fun presentedNoticeDoesNotReplayWhenLeavingAndReenteringBeforeItExpires() { + val store = store() + store.remove("a") + val notice = store.value.removalNotice!! + + assertTrue(store.beginRemovalNotice(notice.id)) + store.showList() + + assertFalse(store.beginRemovalNotice(notice.id)) + assertEquals(notice, store.value.removalNotice) + assertEquals(listOf("b", "c"), store.value.visibleSessions.map { it.id }) + } } diff --git a/docs/notes/hide-chat-notice.md b/docs/notes/hide-chat-notice.md index a1543356..57594b2e 100644 --- a/docs/notes/hide-chat-notice.md +++ b/docs/notes/hide-chat-notice.md @@ -4,9 +4,9 @@ `android/app/src/main/java/net/awan/pipelines/ui/chat/ChatListPane.kt` now presents a local-hide notice once with Material's short, finite snackbar duration. The `ChatStore` keeps the one-shot -notice separate from the undoable local override, and the pane consumes it only after the snackbar -finishes so changing the effect key cannot cancel its display. A later hide replaces the pending -notice rather than queueing it. Returning to the list, +notice separate from the undoable local override and records delivery separately from snackbar +completion, so leaving and re-entering cannot replay an in-flight notice. A later hide replaces the +pending notice rather than queueing it. Returning to the list, recomposition and a session refresh therefore cannot replay an already-finished notice; Undo still restores only the current local hide and does not delete server state or history.