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 1309d46b..fdd28cd5 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 @@ -1106,7 +1106,9 @@ class AppViewModel( */ fun removeChat(sessionId: String) = chatStore.remove(sessionId) - fun undoRemoveChat() = chatStore.undoRemove() + fun consumeChatRemovalNotice(noticeId: Long) = chatStore.consumeRemovalNotice(noticeId) + + fun undoRemoveChat(sessionId: String) = chatStore.undoRemove(sessionId) fun loadChatHistory(sessionId: String) { if (chatStore.value.thread(sessionId)?.loading == true) return 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 f9e909bf..c8d10fc5 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 @@ -107,15 +107,24 @@ internal fun ChatListPane( // A removal is reported for what it is: a local, undoable change, never a // pretend delete — the server is not asked to remove anything. val snackbarHostState = remember { SnackbarHostState() } - LaunchedEffect(state.lastRemoved) { - if (state.lastRemoved != null && - snackbarHostState.showSnackbar( + 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.Long, - ) == SnackbarResult.ActionPerformed - ) { - vm.undoRemoveChat() + duration = SnackbarDuration.Short, + ) == SnackbarResult.ActionPerformed) { + vm.undoRemoveChat(notice.sessionId) + } + // 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 cb0d241e..71821b0d 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 @@ -254,6 +254,12 @@ data class ChatState( val overrides: ChatSessionOverrides = ChatSessionOverrides(), /** The id of the session most recently removed, while its removal can be undone. */ val lastRemoved: String? = null, + /** + * The one-shot report that a removal was made. It is deliberately separate + * from [lastRemoved]: consuming a rendered notice must not make the local + * hide irreversible, and revisiting the list must not report it again. + */ + val removalNotice: ChatRemovalNotice? = null, /** What the operator has read of the list (see [ChatSeenState]). */ val seen: ChatSeenState = ChatSeenState(), /** @@ -337,6 +343,9 @@ data class ChatState( get() = filteredSessions.filter { it.closed } } +/** A one-shot, in-memory report for the list's local-hide snackbar. */ +data class ChatRemovalNotice(val id: Long, val sessionId: String) + /** * The multi-chat state model: a small, Android-free holder of [ChatState] with * the transitions the chat surface needs. Keeping it free of Compose and of the @@ -378,6 +387,9 @@ class ChatStore( */ private var localMessageSeq = 0 + /** Monotonic identity for one-shot local-hide reports. */ + private var removalNoticeSeq = 0L + // ---- Session list -------------------------------------------------------- fun sessionsLoading() { @@ -533,15 +545,31 @@ class ChatStore( open = _state.value.open.filterNot { it.sessionId == sessionId }, activeId = if (_state.value.activeId == sessionId) null else _state.value.activeId, lastRemoved = sessionId, + // Keep only the newest report. A snackbar host has one visible slot; + // retaining older reports would turn rapid hides into a long queue. + removalNotice = ChatRemovalNotice(++removalNoticeSeq, sessionId), ) } - /** Restores the most recently removed session to the list and forgets it. */ - fun undoRemove() { + /** + * Marks [noticeId] handled after its snackbar finishes. This transient event + * is never persisted, so refreshes, recomposition and re-entry cannot replay + * a finished report. + */ + fun consumeRemovalNotice(noticeId: Long) { + if (_state.value.removalNotice?.id == noticeId) { + _state.value = _state.value.copy(removalNotice = null) + } + } + + /** Restores [sessionId] only when it is still the current undoable removal. */ + fun undoRemove(sessionId: String? = null) { val id = _state.value.lastRemoved ?: return + if (sessionId != null && sessionId != id) return _state.value = _state.value.copy( overrides = _state.value.overrides.restore(id), lastRemoved = null, + removalNotice = null, ) } 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 new file mode 100644 index 00000000..53bcc261 --- /dev/null +++ b/android/app/src/test/java/net/awan/pipelines/ui/chat/ChatRemovalNoticeTest.kt @@ -0,0 +1,77 @@ +package net.awan.pipelines.ui.chat + +import net.awan.pipelines.data.protocol.ChatSession +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertNull +import org.junit.Assert.assertTrue +import org.junit.Test + +/** The transient local-hide report and durable undo state have separate lifetimes. */ +class ChatRemovalNoticeTest { + + private fun store() = ChatStore().also { + it.sessionsLoaded(listOf(ChatSession(id = "a"), ChatSession(id = "b"), ChatSession(id = "c"))) + } + + @Test + fun expirationConsumesTheNoticeButLeavesTheLocalHideInPlace() { + val store = store() + store.remove("a") + val notice = store.value.removalNotice!! + + // This is what the UI does after its finite snackbar expires, without a + // clock or real sleep. Keeping the notice until then avoids cancelling + // showSnackbar by changing its LaunchedEffect key. + store.consumeRemovalNotice(notice.id) + + assertNull(store.value.removalNotice) + assertEquals("a", store.value.lastRemoved) + assertEquals(listOf("b", "c"), store.value.visibleSessions.map { it.id }) + } + + @Test + fun rapidHidesReplaceThePendingNoticeInsteadOfBuildingAQueue() { + val store = store() + store.remove("a") + val first = store.value.removalNotice!! + store.remove("b") + val second = store.value.removalNotice!! + + assertTrue(second.id > first.id) + assertEquals("b", second.sessionId) + store.consumeRemovalNotice(first.id) + assertEquals(second, store.value.removalNotice) + assertEquals(listOf("c"), store.value.visibleSessions.map { it.id }) + } + + @Test + fun undoRestoresOnlyTheRemovalWhoseNoticeWasActioned() { + val store = store() + store.remove("a") + val first = store.value.removalNotice!! + store.remove("b") + val second = store.value.removalNotice!! + + store.undoRemove(first.sessionId) + assertEquals("b", store.value.lastRemoved) + assertFalse("a stays hidden after its replaced snackbar is dismissed", "a" in store.value.visibleSessions.map { it.id }) + + store.undoRemove(second.sessionId) + assertNull(store.value.lastRemoved) + assertNull(store.value.removalNotice) + assertEquals(listOf("b", "c"), store.value.visibleSessions.map { it.id }) + } + + @Test + fun consumedNoticeDoesNotReturnOnListReentryOrSessionRefresh() { + val store = store() + store.remove("a") + store.consumeRemovalNotice(store.value.removalNotice!!.id) + store.showList() + store.sessionsLoaded(listOf(ChatSession(id = "a"), ChatSession(id = "b"), ChatSession(id = "c"))) + + assertNull(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 new file mode 100644 index 00000000..a1543356 --- /dev/null +++ b/docs/notes/hide-chat-notice.md @@ -0,0 +1,21 @@ +# Local hide notice lifetime + +## What this item changed + +`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, +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. + +`ChatRemovalNoticeTest` covers finite-notice consumption, rapid hides, Undo and list re-entry using +the pure store, with no real-time wait. + +## Shared documents this change makes stale + +- `android/README.md`'s chat-session behaviour should say that **Hide on this device** has a + short, finite Undo notice, rapid hides replace rather than queue notices, and an expired or + consumed notice is not replayed when returning to the list. It should name + `ChatRemovalNoticeTest` as unit-test evidence and leave on-device timing to visual review.