Skip to content

Commit

Permalink
Optimize the push/replace history for navigation.
Browse files Browse the repository at this point in the history
  • Loading branch information
handstandsam committed Feb 9, 2025
1 parent 8cc2f73 commit 372bc7c
Show file tree
Hide file tree
Showing 23 changed files with 89 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,31 @@ import kotlinx.coroutines.flow.MutableStateFlow
*/
class NavRouteRepo(initialRoute: NavRoute) {

private val _navRoute = MutableStateFlow(NavChangeEvent(initialRoute))
private val _navRoute = MutableStateFlow(
NavChangeEvent(
initialRoute,
PushOrReplaceState.PUSH
)
)

val navRoute: Flow<NavChangeEvent> = _navRoute

fun updateNavRoute(navRoute: NavRoute) {
this._navRoute.tryEmit(NavChangeEvent(navRoute))
/**
* Adds new item to browser history stack.
*/
fun pushNavRoute(navRoute: NavRoute) {
this._navRoute.tryEmit(NavChangeEvent(navRoute, PushOrReplaceState.PUSH))
}

/**
* Updates/Replaces the URL in the browser history, but does NOT add a new item to browser history stack.
*/
fun replaceNavRoute(navRoute: NavRoute) {
this._navRoute.tryEmit(NavChangeEvent(navRoute, PushOrReplaceState.REPLACE))
}

class NavChangeEvent(
val navRoute: NavRoute,
val pushOrReplaceState: PushOrReplaceState = PushOrReplaceState.PUSH
val pushOrReplaceState: PushOrReplaceState,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ fun ModulesComposable(
query = query ?: "",
placeholderText = "Module Query...",
) {
navRouteRepo.updateNavRoute(modulesNavRoute.copy(query = it))
navRouteRepo.replaceNavRoute(modulesNavRoute.copy(query = it))
}
ModulesByNameComposable(allModulesWithOwnerMatchingQuery) {
navRouteRepo.updateNavRoute(ModuleDetailNavRoute(it))
navRouteRepo.pushNavRoute(ModuleDetailNavRoute(it))
}
})
BootstrapTabPane(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ fun AllStatsComposable(
val statsOfType = statTotals.statTotals.values.filter { it.metadata.dataType == statDataType }
if (statsOfType.isNotEmpty()) {
H1 { Text("${statDataType.displayName} Stats") }
StatTiles(statsOfType, navRouteRepo::updateNavRoute)
StatTiles(statsOfType, navRouteRepo::pushNavRoute)
}
}

Expand Down Expand Up @@ -129,7 +129,7 @@ fun AllStatsComposable(
onItemClickCallback = { cellValues ->
val statKey = cellValues[0]
val statDataType = StatDataType.fromString(cellValues[2])
navRouteRepo.updateNavRoute(
navRouteRepo.pushNavRoute(
if (statDataType == StatDataType.CODE_REFERENCES) {
CodeReferencesNavRoute(
statKey = statKey,
Expand All @@ -149,7 +149,7 @@ fun AllStatsComposable(
"View All, Grouped By Module",
BootstrapButtonType.SECONDARY,
onClick = {
navRouteRepo.updateNavRoute(
navRouteRepo.pushNavRoute(
StatDetailNavRoute(
pluginIds = listOf(),
statKeys = statInfos.map { it.metadata.key }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ fun AnnotationProcessorsComposable(
types = listOf(String::class),
maxResultsLimitConstant = PagingConstants.MAX_RESULTS
) {
navRouteRepo.updateNavRoute(
navRouteRepo.pushNavRoute(
ModuleDetailNavRoute(
path = it[0]
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ fun ArtifactDetailComposable(
types = listOf(String::class, String::class),
maxResultsLimitConstant = PagingConstants.MAX_RESULTS,
onItemClickCallback = {
navRouteRepo.updateNavRoute(ModuleDetailNavRoute(it[0]))
navRouteRepo.pushNavRoute(ModuleDetailNavRoute(it[0]))
}
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ fun ArtifactsComposable(
navRoute.query ?: "",
"Search For Artifact..."
) {
navRouteRepo.updateNavRoute(
navRouteRepo.pushNavRoute(
ArtifactsNavRoute(it)
)
}
Expand All @@ -140,7 +140,7 @@ fun ArtifactsComposable(
)
),
onClick = { label, value ->
navRouteRepo.updateNavRoute(
navRouteRepo.pushNavRoute(
ArtifactsNavRoute(
query = "$label:"
)
Expand Down Expand Up @@ -169,7 +169,7 @@ fun ArtifactsComposable(
types = artifactsMatchingQuery.map { String::class },
maxResultsLimitConstant = MAX_RESULTS,
onItemClickCallback = {
navRouteRepo.updateNavRoute(
navRouteRepo.pushNavRoute(
ArtifactDetailNavRoute(
group = it[0],
artifact = it[1],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ fun CodeReferencesComposable(
if (currentStatMetadata == null) {
H1 { Text("No stat with key '${codeReferencesNavRoute.statKey}' found") }
BootstrapButton("View All Stats") {
navRouteRepo.updateNavRoute(AllStatsNavRoute())
navRouteRepo.pushNavRoute(AllStatsNavRoute())
}
return
}
Expand All @@ -183,7 +183,7 @@ fun CodeReferencesComposable(
Li {
NavRouteLink(
StatDetailNavRoute(statKeys = listOf(statKey)),
navRouteRepo::updateNavRoute
navRouteRepo::pushNavRoute
) {
Text("View Grouped by Module")
}
Expand All @@ -198,7 +198,7 @@ fun CodeReferencesComposable(
true
}
),
navRouteRepo::updateNavRoute
navRouteRepo::pushNavRoute
) {
if (codeReferencesNavRoute.chart == true) {
Text("Hide Chart")
Expand All @@ -217,7 +217,7 @@ fun CodeReferencesComposable(
true
}
),
navRouteRepo::updateNavRoute,
navRouteRepo::pushNavRoute,
) {
if (codeReferencesNavRoute.treemap == true) {
Text("Hide Treemap")
Expand All @@ -232,7 +232,7 @@ fun CodeReferencesComposable(
statKey = codeReferencesNavRoute.statKey,
owner = codeReferencesNavRoute.owner,
),
navRouteRepo::updateNavRoute
navRouteRepo::pushNavRoute
) {
Text("View Owner Breakdown")
}
Expand Down Expand Up @@ -354,7 +354,7 @@ fun CodeReferencesComposable(
)
}.sortedBy { it.displayText }
) {
navRouteRepo.updateNavRoute(
navRouteRepo.pushNavRoute(
codeReferencesNavRoute.copy(
owner = it?.value,
)
Expand All @@ -377,7 +377,7 @@ fun CodeReferencesComposable(
)
}.sortedBy { it.displayText }
) {
navRouteRepo.updateNavRoute(
navRouteRepo.pushNavRoute(
codeReferencesNavRoute.copy(
module = it?.value
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ fun ConfigurationDetailComposable(
types = listOf(String::class),
maxResultsLimitConstant = MAX_RESULTS,
onItemClickCallback = {
navRouteRepo.updateNavRoute(
navRouteRepo.pushNavRoute(
ModuleDetailNavRoute(
it[0]
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ fun ConfigurationsComposable(
BootstrapRow {
BootstrapColumn(12) {
BootstrapClickableList("Analyzed Configurations", list, MAX_RESULTS) { item ->
navRouteRepo.updateNavRoute(ConfigurationDetailNavRoute(item))
navRouteRepo.pushNavRoute(ConfigurationDetailNavRoute(item))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,19 +139,19 @@ fun ModuleDependencyDiffComposable(
placeholderText = "Module A...",
dataListId = ALL_MODULES_DATALIST_ID,
) {
navRouteRepo.updateNavRoute(navRoute.copy(moduleA = it))
navRouteRepo.replaceNavRoute(navRoute.copy(moduleA = it))
}
BootstrapSearchBox(
query = navRoute.configurationA ?: "",
placeholderText = "Configuration A...",
dataListId = MODULE_A_CONFIGURATIONS_DATALIST,
) {
navRouteRepo.updateNavRoute(navRoute.copy(configurationA = it))
navRouteRepo.replaceNavRoute(navRoute.copy(configurationA = it))
}
}
BootstrapColumn(2) {
BootstrapButton("↔️") {
navRouteRepo.updateNavRoute(
navRouteRepo.replaceNavRoute(
navRoute.copy(
moduleA = navRoute.moduleB,
moduleB = navRoute.moduleA,
Expand All @@ -167,23 +167,23 @@ fun ModuleDependencyDiffComposable(
placeholderText = "Module B...",
dataListId = ALL_MODULES_DATALIST_ID,
) {
navRouteRepo.updateNavRoute(navRoute.copy(moduleB = it))
navRouteRepo.replaceNavRoute(navRoute.copy(moduleB = it))
}
BootstrapSearchBox(
query = navRoute.configurationB ?: "",
placeholderText = "Configuration B...",
dataListId = MODULE_A_CONFIGURATIONS_DATALIST,
) {
navRouteRepo.updateNavRoute(navRoute.copy(configurationB = it))
navRouteRepo.replaceNavRoute(navRoute.copy(configurationB = it))
}
}
}

BootstrapSettingsCheckbox("Include Artifacts", navRoute.includeArtifacts) {
navRouteRepo.updateNavRoute(navRoute.copy(includeArtifacts = it))
navRouteRepo.replaceNavRoute(navRoute.copy(includeArtifacts = it))
}
BootstrapSettingsCheckbox("Show Matching", navRoute.showMatching) {
navRouteRepo.updateNavRoute(navRoute.copy(showMatching = it))
navRouteRepo.replaceNavRoute(navRoute.copy(showMatching = it))
}

Br()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@ fun PluginsComposable(
navRoute.query ?: "",
"Search For Artifact..."
) {
navRouteRepo.updateNavRoute(
navRouteRepo.pushNavRoute(
ArtifactsNavRoute(it)
)
}
BootstrapRow {
BootstrapColumn(6) {
BootstrapClickableList("Plugins", allPluginIds!!, MAX_RESULTS) { gradlePluginId ->
navRouteRepo.updateNavRoute(PluginDetailNavRoute(gradlePluginId))
navRouteRepo.pushNavRoute(PluginDetailNavRoute(gradlePluginId))
}
}
BootstrapColumn(6) {
Expand All @@ -110,7 +110,7 @@ fun PluginsComposable(
)
),
onClick = { label, value ->
navRouteRepo.updateNavRoute(
navRouteRepo.pushNavRoute(
PluginDetailNavRoute(
pluginId = label,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,25 +120,25 @@ fun HomeComposable(
moduleCount,
AllModulesReportPage.navPage,
AllModulesNavRoute(),
) { navRouteRepo.updateNavRoute(it) }
) { navRouteRepo.pushNavRoute(it) }

HomeCountComposable(
artifactCount,
ArtifactsReportPage.navPage,
AllModulesNavRoute()
) { navRouteRepo.updateNavRoute(ArtifactsNavRoute()) }
) { navRouteRepo.pushNavRoute(ArtifactsNavRoute()) }

HomeCountComposable(
pluginIdsCount,
GradlePluginsReportPage.navPage,
AllModulesNavRoute()
) { navRouteRepo.updateNavRoute(GradlePluginsNavRoute(null)) }
) { navRouteRepo.pushNavRoute(GradlePluginsNavRoute(null)) }

HomeCountComposable(
ownersCount,
OwnersReportPage.navPage,
AllModulesNavRoute()
) { navRouteRepo.updateNavRoute(OwnersNavRoute) }
) { navRouteRepo.pushNavRoute(OwnersNavRoute) }

statTotals.statTotals.values.forEach { statTotalAndMetadata ->
BootstrapColumn(3) {
Expand All @@ -159,7 +159,7 @@ fun HomeComposable(
}
A(href = destRoute.toQueryString(), {
onClick {
navRouteRepo.updateNavRoute(destRoute)
navRouteRepo.pushNavRoute(destRoute)
}
}) {
Small {
Expand All @@ -177,7 +177,7 @@ fun HomeComposable(
"View All",
BootstrapButtonType.PRIMARY,
onClick = {
navRouteRepo.updateNavRoute(
navRouteRepo.pushNavRoute(
AllStatsNavRoute()
)
}
Expand Down
Loading

0 comments on commit 372bc7c

Please sign in to comment.