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.

Address PR #161 review: stale rows, delete feedback, labels

Rishabh Bajpai42b2a03dff8f
diff --git a/app/src/androidTest/java/com/gotcha/ChatAssetsNavTest.kt b/app/src/androidTest/java/com/gotcha/ChatAssetsNavTest.kt
index 818f8b5..2196a62 100644
--- a/app/src/androidTest/java/com/gotcha/ChatAssetsNavTest.kt
+++ b/app/src/androidTest/java/com/gotcha/ChatAssetsNavTest.kt
@@ -3,6 +3,7 @@ package com.gotcha
import androidx.compose.ui.test.hasContentDescription
import androidx.compose.ui.test.junit4.createEmptyComposeRule
import androidx.compose.ui.test.longClick
+import androidx.compose.ui.test.onAllNodesWithTag
import androidx.compose.ui.test.onAllNodesWithText
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
@@ -97,6 +98,36 @@ class ChatAssetsNavTest {
composeRule.onNodeWithText("docs").assertExists()
}
+ @Test
+ fun chatMenu_assets_missingFolder_showsEmpty() {
+ mockLlm.start()
+ val context = ApplicationProvider.getApplicationContext<android.content.Context>()
+ TestSeed.seedConfigured(context, baseUrl = mockLlm.baseUrl)
+ seedSessionWithMessage(context, "assets-nav-gone", "Assets gone")
+ GotchaStorage.rootPath = File(context.filesDir, "assets-test-root").absolutePath
+ val chatDir = GotchaStorage.findOrCreateChatDir("assets-nav-gone", "Assets gone")
+ File(chatDir, "vanished").mkdirs()
+ File(chatDir, "vanished/old.txt").writeText("stale")
+ openChatFromDrawer("Assets gone")
+
+ composeRule.onNodeWithTag("chat_options").performClick()
+ composeRule.waitForIdle()
+ composeRule.onNodeWithTag("chat_assets").performClick()
+ composeRule.waitForIdle()
+ composeRule.onNodeWithText("vanished").assertExists()
+
+ // The folder disappears between listing and tap (an agent run or an
+ // external file manager beat us to it): entering must show the empty
+ // state, never the previous listing's actionable rows.
+ File(chatDir, "vanished").deleteRecursively()
+ composeRule.onNodeWithText("vanished").performClick()
+ composeRule.waitUntil(5000) {
+ composeRule.onAllNodesWithTag("chat_assets_empty")
+ .fetchSemanticsNodes().isNotEmpty()
+ }
+ composeRule.onNodeWithText("old.txt").assertDoesNotExist()
+ }
+
private fun seedSessionWithMessage(context: android.content.Context, id: String, title: String) {
kotlinx.coroutines.runBlocking {
com.gotcha.data.ChatHistoryRepository(context).saveSession(
diff --git a/app/src/main/java/com/gotcha/ui/ChatAssetsScreen.kt b/app/src/main/java/com/gotcha/ui/ChatAssetsScreen.kt
index fe6a8eb..6986a87 100644
--- a/app/src/main/java/com/gotcha/ui/ChatAssetsScreen.kt
+++ b/app/src/main/java/com/gotcha/ui/ChatAssetsScreen.kt
@@ -63,6 +63,10 @@ fun ChatAssetsScreen(sessionId: String, onBack: () -> Unit) {
var currentPath by remember(sessionId) { mutableStateOf<String?>(null) }
var loaded by remember { mutableStateOf(false) }
var entries by remember { mutableStateOf(emptyList<ChatAssetEntry>()) }
+ // The path the visible rows belong to. Rows render only for the current
+ // path: navigating (or a folder vanishing mid-scan) never leaves the
+ // previous folder actionable under the new title.
+ var listedPath by remember { mutableStateOf<String?>(null) }
var menuEntry by remember { mutableStateOf<ChatAssetEntry?>(null) }
var deleteEntry by remember { mutableStateOf<ChatAssetEntry?>(null) }
@@ -72,8 +76,16 @@ fun ChatAssetsScreen(sessionId: String, onBack: () -> Unit) {
loaded = true
}
LaunchedEffect(currentPath) {
- val dir = currentPath?.let(::File)?.takeIf { it.isDirectory } ?: return@LaunchedEffect
- entries = withContext(Dispatchers.IO) { chatDirEntries(dir) }
+ val dir = currentPath?.let(::File)?.takeIf { it.isDirectory }
+ // A missing destination publishes an empty list: the old rows must
+ // not survive under a folder that is gone.
+ val fresh = if (dir == null) {
+ emptyList()
+ } else {
+ withContext(Dispatchers.IO) { chatDirEntries(dir) }
+ }
+ listedPath = currentPath
+ entries = fresh
}
val canGoUp = root != null && currentPath != null && currentPath != root?.absolutePath
@@ -108,14 +120,23 @@ fun ChatAssetsScreen(sessionId: String, onBack: () -> Unit) {
},
modifier = Modifier.testTag("chat_assets_back")
) {
- Icon(Icons.AutoMirrored.Filled.ArrowBack, contentDescription = null)
+ Icon(
+ Icons.AutoMirrored.Filled.ArrowBack,
+ contentDescription = stringResource(
+ if (canGoUp) {
+ R.string.chat_assets_up
+ } else {
+ R.string.chat_assets_back
+ }
+ )
+ )
}
}
)
}
) { padding ->
- if (!loaded) {
- // Still resolving the chat folder; it is one lookup, no spinner.
+ if (!loaded || listedPath != currentPath) {
+ // Still resolving, or a scan in flight; chat folders are small.
Unit
} else if (root == null) {
Text(
@@ -252,11 +273,13 @@ fun ChatAssetsScreen(sessionId: String, onBack: () -> Unit) {
val pathAtDelete = currentPath
scope.launch(Dispatchers.IO) {
val target = File(entry.path)
- val deleted = if (target.isDirectory) {
- target.deleteRecursively()
- } else {
- target.delete()
- }
+ val deleted = runCatching {
+ if (target.isDirectory) {
+ target.deleteRecursively()
+ } else {
+ target.delete()
+ }
+ }.getOrDefault(false)
val fresh = pathAtDelete?.let(::File)
?.takeIf { it.isDirectory }
?.let(::chatDirEntries)
@@ -267,15 +290,20 @@ fun ChatAssetsScreen(sessionId: String, onBack: () -> Unit) {
// retrigger. Skip if the user navigated away
// mid-delete rather than show a stale folder.
if (currentPath == pathAtDelete) {
+ listedPath = pathAtDelete
entries = fresh
}
- if (deleted) {
- android.widget.Toast.makeText(
- context,
- context.getString(R.string.chat_assets_deleted),
- android.widget.Toast.LENGTH_SHORT
- ).show()
- }
+ android.widget.Toast.makeText(
+ context,
+ context.getString(
+ if (deleted) {
+ R.string.chat_assets_deleted
+ } else {
+ R.string.chat_assets_delete_failed
+ }
+ ),
+ android.widget.Toast.LENGTH_SHORT
+ ).show()
}
}
},
@@ -300,7 +328,7 @@ private fun openAsset(context: android.content.Context, file: File) {
file
)
val mime = MimeTypeMap.getSingleton()
- .getMimeTypeFromExtension(file.extension) ?: "*/*"
+ .getMimeTypeFromExtension(file.extension.lowercase()) ?: "*/*"
val intent = Intent(Intent.ACTION_VIEW).apply {
setDataAndType(uri, mime)
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
@@ -326,7 +354,7 @@ private fun shareAsset(context: android.content.Context, file: File) {
file
)
val mime = MimeTypeMap.getSingleton()
- .getMimeTypeFromExtension(file.extension) ?: "*/*"
+ .getMimeTypeFromExtension(file.extension.lowercase()) ?: "*/*"
val intent = Intent(Intent.ACTION_SEND).apply {
type = mime
putExtra(Intent.EXTRA_STREAM, uri)
@@ -381,8 +409,15 @@ fun chatDirEntries(dir: File): List<ChatAssetEntry> {
return folders + files
}
-private fun toAssetEntry(file: File): ChatAssetEntry.Asset = ChatAssetEntry.Asset(
- path = file.absolutePath,
- name = file.name,
- detail = "${file.extension.uppercase()} · ${FileResolver.formatSizeStatic(file.length())}"
-)
+private fun toAssetEntry(file: File): ChatAssetEntry.Asset {
+ val ext = file.extension.uppercase()
+ return ChatAssetEntry.Asset(
+ path = file.absolutePath,
+ name = file.name,
+ detail = if (ext.isEmpty()) {
+ FileResolver.formatSizeStatic(file.length())
+ } else {
+ "$ext · ${FileResolver.formatSizeStatic(file.length())}"
+ }
+ )
+}
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index 1a3407b..07d26b4 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -1098,6 +1098,9 @@
<string name="chat_assets_delete_title">Delete?</string>
<string name="chat_assets_delete_confirm">Delete %1$s? This can\'t be undone.</string>
<string name="chat_assets_deleted">Deleted.</string>
+ <string name="chat_assets_delete_failed">Couldn\'t delete that.</string>
+ <string name="chat_assets_back">Back</string>
+ <string name="chat_assets_up">Up one folder</string>
<string name="chat_assets_open_failed">Couldn\'t open that file.</string>
<string name="chat_keep_out_of_notifications">Keep out of notifications</string>
<string name="chat_stop_reading_aloud">Stop reading aloud</string>
diff --git a/app/src/test/java/com/gotcha/ui/ChatAssetSectionsTest.kt b/app/src/test/java/com/gotcha/ui/ChatAssetSectionsTest.kt
index aa82bd5..25b9278 100644
--- a/app/src/test/java/com/gotcha/ui/ChatAssetSectionsTest.kt
+++ b/app/src/test/java/com/gotcha/ui/ChatAssetSectionsTest.kt
@@ -36,18 +36,24 @@ class ChatAssetSectionsTest {
file(File(dir, "Pictures"), "new.png", 3000)
file(dir, "note.md", 2000)
+ // Only immediate children: the PNGs live one level down.
val entries = chatDirEntries(dir)
val folders = entries.filterIsInstance<ChatAssetEntry.Folder>().map { it.name }
assertEquals(listOf("apple", "Pictures", "zebra"), folders)
val files = entries.filterIsInstance<ChatAssetEntry.Asset>()
- assertEquals(listOf("new.png", "old.png", "note.md"), files.map { it.name })
- assertTrue(files[0].detail.startsWith("PNG · "))
+ assertEquals(listOf("note.md"), files.map { it.name })
// Every folder sorts before every file.
assertTrue(
entries.indexOfLast { it is ChatAssetEntry.Folder } <
entries.indexOfFirst { it is ChatAssetEntry.Asset }
)
+
+ // Inside Pictures: newest first, with type and size in the detail.
+ val photos = chatDirEntries(File(dir, "Pictures"))
+ .filterIsInstance<ChatAssetEntry.Asset>()
+ assertEquals(listOf("new.png", "old.png"), photos.map { it.name })
+ assertTrue(photos[0].detail.startsWith("PNG · "))
}
@Test