Modern Apps git

← Modern-Apps.git

code: folder picker drill-down as per-level routes (#nav)

Commit
ae47e1c3e7a86f1a69c44487024fc12c70b46e3e
Author
Vayun Mathur <[email protected]>
When
2026-11-16 22:25 UTC (19 days ago)
Parents
f40bdd2
Route audit section 4 flagged FolderBrowserPage: it held the current
directory in a remember-ed mutableStateOf inside a single
Route.FolderBrowser destination, so the listing swapped in place and
Back (or the predictive-back gesture) exited the whole picker from
any depth instead of going up one level.

Judgement on shape: FolderBrowser is a top-level destination in its
own right - an entry in MainActivity Navigation pushed from the
editor - not a picker-internal component hosted inside another
destination. The per-level stack therefore belongs on the app back
stack itself, not in a nested NavBackStack inside a host. This
mirrors the files app, where Route.Directory(path) is one entry per
folder level for exactly this reason.

Changes:
- Route.FolderBrowser changes from a data object to
  data class FolderBrowser(path: String); the editor pushes it with
  the external-storage root, and the entry binds route.path.
- FolderBrowserPage takes the path from its route: tapping a
  directory pushes a new FolderBrowser entry, and the ".." row pops,
  landing on the parent entry. The scaffold back button already
  pops the stack, so all three back affordances now go up one level.
- "Open this folder" still exits the picker entirely: it calls
  viewModel.openFolder(currentDir) and then resets the stack to
  Route.Editor, however deep the drill-down has grown.
- Tests updated for the new page signature; the route-serialization
  test now round-trips FolderBrowser("/sdcard").

User-visible effect: drilling into nested folders and pressing
Back returns to the previous folder instead of closing the picker;
opening a folder behaves exactly as before.

Verified: :code:compileDevKotlin passes. :code:testDevUnitTest has
one failure, Wave2VmActionsTest.createRenameDeleteViaActionsAndSettings
("timeout: deleted"), which is pre-existing: the identical full-suite
run on unmodified origin/main (f40bdd2dd) fails the same test the
same way, and the test passes in isolation on both trees. It
exercises EditorViewModel file actions and does not touch the
folder browser.

Diff

code/src/main/java/com/vayunmathur/code/MainActivity.kt
diff --git a/code/src/main/java/com/vayunmathur/code/MainActivity.kt b/code/src/main/java/com/vayunmathur/code/MainActivity.ktindex 8ea02bc474..51f0c4e622 100644--- a/code/src/main/java/com/vayunmathur/code/MainActivity.kt+++ b/code/src/main/java/com/vayunmathur/code/MainActivity.kt@@ -1,6 +1,7 @@ package com.vayunmathur.code  import android.content.Intent+import android.os.Environment import androidx.activity.viewModels import androidx.compose.runtime.Composable import androidx.compose.ui.res.stringResource@@ -90,8 +91,9 @@ sealed interface Route : NavKey {     @Serializable     data object Search : Route +    /** One directory level of the folder picker; Back pops one level. */     @Serializable-    data object FolderBrowser : Route+    data class FolderBrowser(val path: String) : Route      @Serializable     data object Git : Route@@ -117,7 +119,9 @@ fun Navigation(viewModel: EditorViewModel) {                 viewModel,                 onOpenSettings = { backStack.add(Route.Settings) },                 onOpenSearch = { backStack.add(Route.Search) },-                onOpenFolder = { backStack.add(Route.FolderBrowser) },+                onOpenFolder = {+                    backStack.add(Route.FolderBrowser(Environment.getExternalStorageDirectory().absolutePath))+                },                 onOpenGit = { backStack.add(Route.Git) },                 onOpenTerminal = { backStack.add(Route.Terminal) },                 onOpenPreview = { backStack.add(Route.Preview) },@@ -125,7 +129,7 @@ fun Navigation(viewModel: EditorViewModel) {         }         entry<Route.Settings> { SettingsPage(viewModel, backStack) }         entry<Route.Search> { SearchPage(viewModel, backStack) }-        entry<Route.FolderBrowser> { FolderBrowserPage(viewModel, backStack) }+        entry<Route.FolderBrowser> { route -> FolderBrowserPage(viewModel, backStack, route.path) }         entry<Route.Git> { GitPage(viewModel, backStack) }         entry<Route.Terminal> { TerminalPage(viewModel, backStack) }         entry<Route.Preview> { PreviewPage(viewModel, backStack) }
code/src/main/java/com/vayunmathur/code/ui/FolderBrowserPage.kt
diff --git a/code/src/main/java/com/vayunmathur/code/ui/FolderBrowserPage.kt b/code/src/main/java/com/vayunmathur/code/ui/FolderBrowserPage.ktindex a31efda7ec..51cd444ba7 100644--- a/code/src/main/java/com/vayunmathur/code/ui/FolderBrowserPage.kt+++ b/code/src/main/java/com/vayunmathur/code/ui/FolderBrowserPage.kt@@ -45,12 +45,19 @@ import java.io.File  * In-app folder picker. Android has no "pick a folder โ†’ get a path" system API once we work over  * real [File] paths, so this drills through directories from external storage and lets the user  * open the current one as the project root. Replaces the old SAF `OpenDocumentTree` launcher.+ *+ * Each directory level is its own [Route.FolderBrowser] entry (the files app's+ * `Route.Directory(path)` pattern), so Back โ€” scaffold button, system gesture, and the+ * ".." row โ€” pops exactly one level instead of exiting the picker from any depth.+ * FolderBrowser is a top-level destination pushed from the editor, not a component+ * hosted inside another destination, so the per-level stack is the app back stack+ * itself rather than a nested stack inside a host.  */ @Composable-fun FolderBrowserPage(viewModel: EditorViewModel, backStack: NavBackStack<Route>) {+fun FolderBrowserPage(viewModel: EditorViewModel, backStack: NavBackStack<Route>, path: String) {     val storageRoot = remember { Environment.getExternalStorageDirectory() }-    var currentDir by remember { mutableStateOf(storageRoot) }-    var dirs by remember { mutableStateOf<List<File>>(emptyList()) }+    val currentDir = remember(path) { File(path) }+    var dirs by remember(path) { mutableStateOf<List<File>>(emptyList()) }      LaunchedEffect(currentDir) {         dirs = withContext(Dispatchers.IO) {@@ -71,7 +78,9 @@ fun FolderBrowserPage(viewModel: EditorViewModel, backStack: NavBackStack<Route>             Button(                 onClick = {                     viewModel.openFolder(currentDir)-                    backStack.pop()+                    // Opening a folder exits the picker entirely, however deep+                    // the drill-down stack has grown.+                    backStack.reset(Route.Editor)                 },                 modifier = Modifier.fillMaxWidth().padding(horizontal = 16.dp),             ) { Text(stringResource(R.string.open_this_folder)) }@@ -79,14 +88,17 @@ fun FolderBrowserPage(viewModel: EditorViewModel, backStack: NavBackStack<Route>             HorizontalDivider()              LazyColumn(Modifier.fillMaxSize()) {-                val parent = currentDir.parentFile-                if (parent != null && currentDir.absolutePath != storageRoot.absolutePath) {+                if (currentDir.absolutePath != storageRoot.absolutePath) {                     item(key = "..") {-                        FolderRow(name = "..", onClick = { currentDir = parent })+                        // The parent is the previous stack entry: popping lands on it.+                        FolderRow(name = "..", onClick = { backStack.pop() })                     }                 }                 items(dirs, key = { it.absolutePath }) { dir ->-                    FolderRow(name = dir.name, onClick = { currentDir = dir })+                    FolderRow(+                        name = dir.name,+                        onClick = { backStack.add(Route.FolderBrowser(dir.absolutePath)) },+                    )                 }             }         }
code/src/test/java/com/vayunmathur/code/ui/ExperimentalAndPagesUiTest.kt
diff --git a/code/src/test/java/com/vayunmathur/code/ui/ExperimentalAndPagesUiTest.kt b/code/src/test/java/com/vayunmathur/code/ui/ExperimentalAndPagesUiTest.ktindex de74a3847f..5d141eda22 100644--- a/code/src/test/java/com/vayunmathur/code/ui/ExperimentalAndPagesUiTest.kt+++ b/code/src/test/java/com/vayunmathur/code/ui/ExperimentalAndPagesUiTest.kt@@ -1,5 +1,6 @@ package com.vayunmathur.code.ui +import android.os.Environment import androidx.compose.ui.test.assertIsDisplayed import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onAllNodesWithText@@ -128,7 +129,7 @@ class ExperimentalAndPagesUiTest {      @Test     fun folderBrowserShowsCurrentPathAndOpenButton() {-        rule.setContent { FolderBrowserPage(vm(), backStack()) }+        rule.setContent { FolderBrowserPage(vm(), backStack(), Environment.getExternalStorageDirectory().absolutePath) }         rule.onNodeWithText("Open this folder").assertIsDisplayed()         rule.waitForIdle()     }
code/src/test/java/com/vayunmathur/code/ui/MainAndExpandedUiTest.kt
diff --git a/code/src/test/java/com/vayunmathur/code/ui/MainAndExpandedUiTest.kt b/code/src/test/java/com/vayunmathur/code/ui/MainAndExpandedUiTest.ktindex 82f2829808..7c0cae4b42 100644--- a/code/src/test/java/com/vayunmathur/code/ui/MainAndExpandedUiTest.kt+++ b/code/src/test/java/com/vayunmathur/code/ui/MainAndExpandedUiTest.kt@@ -37,7 +37,7 @@ class MainAndExpandedUiTest {     @Test     fun routeSerializationRoundTrips() {         val routes: List<Route> = listOf(-            Route.Editor, Route.Settings, Route.Search, Route.FolderBrowser,+            Route.Editor, Route.Settings, Route.Search, Route.FolderBrowser("/sdcard"),             Route.Git, Route.Terminal, Route.Preview, Route.Snippets,         )         for (route in routes) {
code/src/test/java/com/vayunmathur/code/ui/MoreInteractionsUiTest.kt
diff --git a/code/src/test/java/com/vayunmathur/code/ui/MoreInteractionsUiTest.kt b/code/src/test/java/com/vayunmathur/code/ui/MoreInteractionsUiTest.ktindex 36d4212ef9..3e5970bae9 100644--- a/code/src/test/java/com/vayunmathur/code/ui/MoreInteractionsUiTest.kt+++ b/code/src/test/java/com/vayunmathur/code/ui/MoreInteractionsUiTest.kt@@ -142,7 +142,7 @@ class MoreInteractionsUiTest {         val probe = File(root, "zzcovprobe${System.nanoTime() % 100000}")         probe.mkdirs()         try {-            rule.setContent { FolderBrowserPage(vm(), NavBackStack(arrayOf(Route.Editor))) }+            rule.setContent { FolderBrowserPage(vm(), NavBackStack(arrayOf(Route.Editor)), Environment.getExternalStorageDirectory().absolutePath) }             rule.waitUntil(10_000) {                 rule.onAllNodesWithText(probe.name).fetchSemanticsNodes().isNotEmpty()             }