Modern Apps git

← Modern-Apps.git

madeveloper: read commit from route, delete VM selection (#nav)

Commit
b189a33d7f7baf57309ed2b8a754f186af05592b
Author
Vayun Mathur <[email protected]>
When
2026-11-16 22:25 UTC (19 days ago)
Parents
ae47e1c
Route audit section 1 flagged the one wart in this otherwise
well-behaved app: MaDevViewModel.selectCommit(name, sha) stored the
tapped commit in the view model immediately before the caller pushed
Route.Commit(repo, branch, sha) - redundant selection state sitting
next to a route that already carries the sha. It did not drive
screen choice; the commit screen already received the sha from its
route.

Changes:
- Delete MaDevViewModel.selectCommit and both call sites in
  Navigation (Repo and Commits entries now just push Route.Commit).
- Delete the underlying domain state with it: RepoBrowserState
  loses selectedCommitSha and the derived selectedCommit, and
  RepoBrowser loses selectCommit; selectBranch no longer clears a
  selection field that no longer exists.
- CommitScreen resolves the commit purely from the route:
  browser.commits.firstOrNull { it.sha == sha }.

Behaviour is identical: selectCommit was always called with the
same sha that was pushed in the route, and selectedCommit was
defined as commits.firstOrNull { it.sha == selectedCommitSha }, so
the removed fallback could only ever return the same commit the
route lookup already finds, or null when it is not in the loaded
pages (in which case the screen shows its "not in the loaded pages"
message, unchanged). The commit screen still loads the repo by
name and resolves by sha from the route.
- RepoBrowserTest: drop the selectCommit test and rename the
  branch-switch test to match what it now asserts.

Verified: :madeveloper:compileDevKotlin and
:madeveloper:testDevUnitTest both pass.

Diff

madeveloper/src/main/java/com/vayunmathur/madeveloper/Navigation.kt
diff --git a/madeveloper/src/main/java/com/vayunmathur/madeveloper/Navigation.kt b/madeveloper/src/main/java/com/vayunmathur/madeveloper/Navigation.ktindex cb6d7dada1..8508e476b6 100644--- a/madeveloper/src/main/java/com/vayunmathur/madeveloper/Navigation.kt+++ b/madeveloper/src/main/java/com/vayunmathur/madeveloper/Navigation.kt@@ -36,7 +36,6 @@ fun Navigation(viewModel: MaDevViewModel) {                 onOpenFile = { branch, path -> backStack.add(Route.File(route.name, path, branch)) },                 onOpenCommits = { branch -> backStack.add(Route.Commits(route.name, branch)) },                 onOpenCommit = { branch, sha ->-                    viewModel.selectCommit(route.name, sha)                     backStack.add(Route.Commit(route.name, branch, sha))                 },                 onBack = { backStack.pop() },@@ -46,7 +45,6 @@ fun Navigation(viewModel: MaDevViewModel) {             CommitsScreen(                 repoName = route.repo, branch = route.branch, viewModel = viewModel,                 onOpenCommit = { sha ->-                    viewModel.selectCommit(route.repo, sha)                     backStack.add(Route.Commit(route.repo, route.branch, sha))                 },                 onBack = { backStack.pop() },
madeveloper/src/main/java/com/vayunmathur/madeveloper/domain/RepoBrowser.kt
diff --git a/madeveloper/src/main/java/com/vayunmathur/madeveloper/domain/RepoBrowser.kt b/madeveloper/src/main/java/com/vayunmathur/madeveloper/domain/RepoBrowser.ktindex 28db4b675a..74c47c56a1 100644--- a/madeveloper/src/main/java/com/vayunmathur/madeveloper/domain/RepoBrowser.kt+++ b/madeveloper/src/main/java/com/vayunmathur/madeveloper/domain/RepoBrowser.kt@@ -2,9 +2,10 @@ package com.vayunmathur.madeveloper.domain  /**  * Pure navigation state for browsing one repository via the git host JSON- * API: the selected branch, the current tree path, the loaded commit page- * and the selected commit. No Android imports, so every transition is- * unit-testable.+ * API: the selected branch, the current tree path and the loaded commit+ * page. The commit shown on the detail screen is identified by+ * `Route.Commit.sha`, not by state here. No Android imports, so every+ * transition is unit-testable.  */ data class RepoBrowserState(     val repoName: String = "",@@ -13,11 +14,7 @@ data class RepoBrowserState(     val path: String = "",     val commits: List<GitCommit> = emptyList(),     val commitsExhausted: Boolean = false,-    val selectedCommitSha: String = "", ) {-    /** The commit currently shown on the commit detail screen, if loaded. */-    val selectedCommit: GitCommit? get() = commits.firstOrNull { it.sha == selectedCommitSha }-     /** Breadcrumb segments of [path] as (label, cumulative path) pairs. */     val breadcrumbs: List<Pair<String, String>>         get() {@@ -45,7 +42,7 @@ object RepoBrowser {     fun selectBranch(state: RepoBrowserState, branch: String): RepoBrowserState =         if (branch == state.branch) state else state.copy(             branch = branch, path = "", commits = emptyList(),-            commitsExhausted = false, selectedCommitSha = "",+            commitsExhausted = false,         )      /** Descends into a tree entry; blobs do not change the tree path. */@@ -70,8 +67,4 @@ object RepoBrowser {             commits = (state.commits + page).distinctBy { it.sha },             commitsExhausted = page.size < pageSize,         )--    /** Selects the commit shown on the detail screen. */-    fun selectCommit(state: RepoBrowserState, sha: String): RepoBrowserState =-        state.copy(selectedCommitSha = sha) }
madeveloper/src/main/java/com/vayunmathur/madeveloper/platform/MaDevViewModel.kt
diff --git a/madeveloper/src/main/java/com/vayunmathur/madeveloper/platform/MaDevViewModel.kt b/madeveloper/src/main/java/com/vayunmathur/madeveloper/platform/MaDevViewModel.ktindex e4c03b39ee..1ae42b271a 100644--- a/madeveloper/src/main/java/com/vayunmathur/madeveloper/platform/MaDevViewModel.kt+++ b/madeveloper/src/main/java/com/vayunmathur/madeveloper/platform/MaDevViewModel.kt@@ -153,12 +153,6 @@ class MaDevViewModel(app: Application) : AndroidViewModel(app) {         }     } -    /** Selects the commit shown on the commit detail screen. */-    fun selectCommit(name: String, sha: String) {-        val current = state.browsers[name] ?: return-        updateBrowser(RepoBrowser.selectCommit(current, sha))-    }-     private fun updateBrowser(browser: RepoBrowserState) {         state = state.copy(browsers = state.browsers + (browser.repoName to browser))     }
madeveloper/src/main/java/com/vayunmathur/madeveloper/ui/CommitScreen.kt
diff --git a/madeveloper/src/main/java/com/vayunmathur/madeveloper/ui/CommitScreen.kt b/madeveloper/src/main/java/com/vayunmathur/madeveloper/ui/CommitScreen.ktindex 4ddc011e46..bc00fa6bb2 100644--- a/madeveloper/src/main/java/com/vayunmathur/madeveloper/ui/CommitScreen.kt+++ b/madeveloper/src/main/java/com/vayunmathur/madeveloper/ui/CommitScreen.kt@@ -28,9 +28,8 @@ import com.vayunmathur.madeveloper.platform.MaDevViewModel fun CommitScreen(repoName: String, sha: String, viewModel: MaDevViewModel, onBack: () -> Unit) {     val context = LocalContext.current     LaunchedEffect(repoName) { viewModel.loadRepo(repoName) }-    val commit = viewModel.browser(repoName)?.let { browser ->-        browser.commits.firstOrNull { it.sha == sha } ?: browser.selectedCommit-    }+    // The commit is identified by the route's sha alone; no VM selection state.+    val commit = viewModel.browser(repoName)?.commits?.firstOrNull { it.sha == sha }     AppScaffold(         title = commit?.shortSha?.ifBlank { sha.take(8) } ?: sha.take(8),         onNavigateBack = onBack, scrollBehavior = appBarScrollBehavior(),
madeveloper/src/test/java/com/vayunmathur/madeveloper/RepoBrowserTest.kt
diff --git a/madeveloper/src/test/java/com/vayunmathur/madeveloper/RepoBrowserTest.kt b/madeveloper/src/test/java/com/vayunmathur/madeveloper/RepoBrowserTest.ktindex 3398d5f672..ea2af48423 100644--- a/madeveloper/src/test/java/com/vayunmathur/madeveloper/RepoBrowserTest.kt+++ b/madeveloper/src/test/java/com/vayunmathur/madeveloper/RepoBrowserTest.kt@@ -25,15 +25,13 @@ class RepoBrowserTest {         assertEquals("", state.path)     } -    @Test fun selectBranchResetsPathCommitsAndSelection() {+    @Test fun selectBranchResetsPathAndCommits() {         var state = RepoBrowser.open(detail)         state = RepoBrowser.navigateTo(state, "src/main")-        state = RepoBrowser.selectCommit(state, "a".repeat(40))         val switched = RepoBrowser.selectBranch(state, "dev")         assertEquals("dev", switched.branch)         assertEquals("", switched.path)         assertTrue(switched.commits.isEmpty())-        assertNull(switched.selectedCommit)         // Re-selecting the same branch is a no-op.         assertEquals(switched, RepoBrowser.selectBranch(switched, "dev"))     }@@ -63,10 +61,4 @@ class RepoBrowserTest {         assertTrue(state.commitsExhausted)         assertNull(state.nextCommitCursor)     }--    @Test fun selectCommitResolvesSelectedCommit() {-        var state = RepoBrowser.open(detail)-        state = RepoBrowser.selectCommit(state, "a".repeat(40))-        assertEquals("head", state.selectedCommit?.subject)-    } }