Skip to content

Your phone, on your command. Gotcha is world's first fully on-device AI copilot for Android that turns natural language into real device actions — safely.

fix(notifications): opening a chat marks its inbox entries read (#116)

Durganshu8486c0e85950
diff --git a/app/src/main/java/com/gotcha/agent/ChatRunner.kt b/app/src/main/java/com/gotcha/agent/ChatRunner.kt
index 44045bf..2521035 100644
--- a/app/src/main/java/com/gotcha/agent/ChatRunner.kt
+++ b/app/src/main/java/com/gotcha/agent/ChatRunner.kt
@@ -250,8 +250,9 @@ class ChatRunner(private val app: Application) : AgentEvents {
/**
* Bumped when the runner adds an inbox entry of its own (an interrupted
- * run, issue #105), so the bell re-reads its unread count: the Activity's
- * read in onResume can come before the entry exists.
+ * run, issue #105) or marks a chat's entries read (issue #116), so the bell
+ * re-reads its unread count: the Activity's read in onResume can come
+ * before the change is written.
*/
private val _inboxChanges = MutableStateFlow(0)
val inboxChanges: StateFlow<Int> = _inboxChanges.asStateFlow()
@@ -629,6 +630,14 @@ class ChatRunner(private val app: Application) : AgentEvents {
}
}
+ /** The user has seen chat [sessionId]: its inbox entries are read (issue #116). */
+ fun markChatRead(sessionId: String) {
+ scope.launch {
+ val changed = withContext(Dispatchers.IO) { localNotificationStore.markChatRead(sessionId) }
+ if (changed) _inboxChanges.update { it + 1 }
+ }
+ }
+
/**
* The chat screen is gone for good (its ViewModel was cleared) while the run
* may go on. A runtime-permission dialog needs that screen, so an ask still
diff --git a/app/src/main/java/com/gotcha/agent/ChatViewModel.kt b/app/src/main/java/com/gotcha/agent/ChatViewModel.kt
index 6f7126e..d26c227 100644
--- a/app/src/main/java/com/gotcha/agent/ChatViewModel.kt
+++ b/app/src/main/java/com/gotcha/agent/ChatViewModel.kt
@@ -747,8 +747,14 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), C
fun setForeground(foreground: Boolean, recreating: Boolean = false) {
runner.setForeground(foreground, recreating)
// Back in the app on a chat whose task finished: that chat's
- // notification has done its job.
- if (foreground) _uiState.value.activeSessionId?.let(completionNotifier::cancel)
+ // notification and inbox entries have done their job.
+ if (foreground) _uiState.value.activeSessionId?.let(::markChatSeen)
+ }
+
+ /** The user has seen [sessionId]: its notification and inbox entries have done their job (issue #116). */
+ private fun markChatSeen(sessionId: String) {
+ completionNotifier.cancel(sessionId)
+ runner.markChatRead(sessionId)
}
/** Speak the given text aloud using the configured TTS provider. */
@@ -1020,7 +1026,7 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), C
}
fun openSession(id: String?) {
- id?.let(completionNotifier::cancel)
+ id?.let(::markChatSeen)
lastInputWasVoice = false
runner.clearVoiceFlag()
viewModelScope.launch {
diff --git a/app/src/main/java/com/gotcha/notifications/LocalNotificationStore.kt b/app/src/main/java/com/gotcha/notifications/LocalNotificationStore.kt
index 555357f..e7430cc 100644
--- a/app/src/main/java/com/gotcha/notifications/LocalNotificationStore.kt
+++ b/app/src/main/java/com/gotcha/notifications/LocalNotificationStore.kt
@@ -95,6 +95,17 @@ class LocalNotificationStore internal constructor(private val prefs: SharedPrefe
encode(KEY_ENTRIES, entrySerializer, all.map { if (it.id == id) it.copy(read = true) else it })
}
+ /**
+ * Marks every entry for chat [sessionId] read, once the user has seen that
+ * chat (issue #116). Entries stay listed. Returns true when any changed.
+ */
+ fun markChatRead(sessionId: String): Boolean {
+ val all = entries()
+ if (all.none { it.sessionId == sessionId && !it.read }) return false
+ encode(KEY_ENTRIES, entrySerializer, all.map { if (it.sessionId == sessionId) it.copy(read = true) else it })
+ return true
+ }
+
fun markAllRead() {
val all = entries()
if (all.all { it.read }) return
diff --git a/app/src/test/java/com/gotcha/agent/ChatViewModelCompletionNotificationTest.kt b/app/src/test/java/com/gotcha/agent/ChatViewModelCompletionNotificationTest.kt
index edd9f9a..91bbc87 100644
--- a/app/src/test/java/com/gotcha/agent/ChatViewModelCompletionNotificationTest.kt
+++ b/app/src/test/java/com/gotcha/agent/ChatViewModelCompletionNotificationTest.kt
@@ -9,6 +9,7 @@ import com.gotcha.data.LlmProvider
import com.gotcha.data.Settings
import com.gotcha.data.SettingsRepository
import com.gotcha.notifications.ChatCompletionNotifier
+import com.gotcha.notifications.LocalNotificationStore
import com.gotcha.service.ChatRunService
import com.gotcha.service.RunningChat
import com.gotcha.testsupport.FakeAndroidKeyStore
@@ -164,6 +165,55 @@ class ChatViewModelCompletionNotificationTest {
assertTrue(posted().isEmpty())
}
+ private val inbox by lazy { LocalNotificationStore(application) }
+
+ /** Marking a chat read is written off the main thread; waits for it. */
+ private fun waitForInbox(unread: Int) {
+ val deadline = System.currentTimeMillis() + 5_000
+ while (System.currentTimeMillis() < deadline && inbox.unreadCount() != unread) {
+ ShadowLooper.idleMainLooper()
+ Thread.sleep(10)
+ }
+ ShadowLooper.idleMainLooper()
+ }
+
+ @Test
+ fun `returning to the chat marks its inbox entry read and keeps it`() {
+ start()
+ viewModel.sendMessage("Hello")
+ viewModel.setForeground(false)
+ waitForRunToFinish()
+ assertEquals(1, inbox.unreadCount())
+ val changesBefore = viewModel.inboxChanges.value
+
+ viewModel.setForeground(true)
+ waitForInbox(unread = 0)
+
+ assertEquals(0, inbox.unreadCount())
+ assertTrue(inbox.entries().single().read)
+ assertTrue(viewModel.inboxChanges.value > changesBefore)
+ }
+
+ @Test
+ fun `opening the chat from elsewhere marks its inbox entry read`() {
+ start()
+ viewModel.sendMessage("Hello")
+ val runChat = viewModel.uiState.value.activeSessionId!!
+ viewModel.setForeground(false)
+ waitForRunToFinish()
+ // Back in the app on another chat: the finished one is still unseen.
+ viewModel.openSession(null)
+ viewModel.setForeground(true)
+ waitForInbox(unread = 0)
+ assertEquals(1, inbox.unreadCount())
+
+ viewModel.openSession(runChat)
+ waitForInbox(unread = 0)
+
+ assertEquals(0, inbox.unreadCount())
+ assertEquals(1, inbox.entries().size)
+ }
+
@Test
fun `hint promises a notification when one can be posted`() {
start()
diff --git a/app/src/test/java/com/gotcha/notifications/LocalNotificationStoreTest.kt b/app/src/test/java/com/gotcha/notifications/LocalNotificationStoreTest.kt
index 8a53306..209fc4f 100644
--- a/app/src/test/java/com/gotcha/notifications/LocalNotificationStoreTest.kt
+++ b/app/src/test/java/com/gotcha/notifications/LocalNotificationStoreTest.kt
@@ -49,6 +49,21 @@ class LocalNotificationStoreTest {
assertEquals(0, store.unreadCount())
}
+ @Test
+ fun `marking a chat read touches only that chat and keeps its entries`() {
+ store.addEntry(NotificationCategory.TASK_FINISHED, "a1", "b", NotificationTarget.Chat("a"), now = now)
+ store.addEntry(NotificationCategory.TASK_FINISHED, "a2", "b", NotificationTarget.Chat("a"), now = now + 1)
+ store.addEntry(NotificationCategory.TASK_FINISHED, "b", "b", NotificationTarget.Chat("b"), now = now + 2)
+ store.addEntry(NotificationCategory.SERVER, "home", "b", NotificationTarget.Home, now = now + 3)
+
+ assertTrue(store.markChatRead("a"))
+
+ assertEquals(4, store.entries().size)
+ assertEquals(setOf("b", "home"), store.entries().filterNot { it.read }.map { it.title }.toSet())
+ assertFalse(store.markChatRead("a"))
+ assertFalse(store.markChatRead("unknown"))
+ }
+
@Test
fun `entries older than 30 days drop out`() {
store.addEntry(NotificationCategory.SERVER, "old", "b", NotificationTarget.Home, now = now - 31 * DAY_MS)