Skip to content

Commit

Permalink
Resolves #105 Improvements to TabBarIntentResolver
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamKobus committed Apr 6, 2022
1 parent ca5cafc commit 05bec92
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ repositories {
}
dependencies {
implementation "com.adamkobus:compose-navigation:0.2.14-SNAPSHOT"
implementation "com.adamkobus:compose-navigation:0.2.15-SNAPSHOT"
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import com.adamkobus.compose.navigation.intent.ResolveResult
*/
@Suppress("ReturnCount")
open class TabBarIntentResolver(
private val tabsMapping: Map<NavIntent, NavGraph>,
private val tabsMapping: Map<String, NavGraph>,
private val tabsRootGraph: NavGraph,
private val popToTabHostIntent: NavIntent? = null,
private val tabStateSavingBehaviour: TabStateSavingBehaviour = SAVE_START_DESTINATION
Expand All @@ -59,7 +59,10 @@ open class TabBarIntentResolver(
}

private fun resolveInternal(intent: NavIntent, navState: NavState): NavAction? {
val mappedGraph = tabsMapping[intent] ?: return null
val mappedGraph = tabsMapping[intent.name] ?: return null
intent.origin?.let {
if (!navState.isCurrent(it)) return null
}
val graphStartDestination = mappedGraph.startDestination()

// no controller with provided tab host was found
Expand All @@ -74,8 +77,16 @@ open class TabBarIntentResolver(
// we're already at the destination that clicking this tab would take us to
if (currentDest == graphStartDestination) return null

// Current destination doesn't belong to tab host at all
if (currentDest.graph !in allGraphs) return null
val navOptions = if (currentDest.graph !in allGraphs) {
intent.popOptions?.copy(launchSingleTop = true)
} else {
navActionOptions {
popUpTo(graphStartDestination)
launchSingleTop = true
}
}
// Current destination doesn't belong to tab host and intent didn't provide nav options
if (navOptions == null) return null

// tab item's starting destination is already in back stack and we can pop back to it
if (navState.isInBackStack(graphStartDestination)) {
Expand All @@ -90,7 +101,10 @@ open class TabBarIntentResolver(
return currentDest goTo mappedGraph withOptions navActionOptions {
popUpTo(tabsRootGraph) {
saveState = tabStateSavingBehaviour == SAVE_ALL ||
(tabStateSavingBehaviour == SAVE_START_DESTINATION && currentDest == currentDest.graph.startDestination())
(
tabStateSavingBehaviour == SAVE_START_DESTINATION &&
currentDest == currentDest.graph.startDestination()
)
}
restoreState = true
launchSingleTop = true
Expand All @@ -117,7 +131,8 @@ open class TabBarIntentResolver(
*/
enum class TabStateSavingBehaviour {
/**
* The state of the graph displayed in the current tab will not be saved. It will start from scratch when user navigates back to it
* The state of the graph displayed in the current tab will not be saved.
* It will start from scratch when user navigates back to it
*/
DONT_SAVE,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import javax.inject.Inject

internal class DevMenuTabHostIntentResolver @Inject constructor() : TabBarIntentResolver(
tabsMapping = mapOf(
DevMenuTabHostIntents.OpenInfo to DevMenuInfoGraph,
DevMenuTabHostIntents.OpenSettings to DevMenuSettingsGraph
DevMenuTabHostIntents.OpenInfo.name to DevMenuInfoGraph,
DevMenuTabHostIntents.OpenSettings.name to DevMenuSettingsGraph
),
tabStateSavingBehaviour = TabStateSavingBehaviour.SAVE_ALL,
tabsRootGraph = DevMenuGraph
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import javax.inject.Inject

class DemoTabBarIntentResolver @Inject constructor() : TabBarIntentResolver(
mapOf(
DemoIntents.OpenDogsBrowser to DogsBrowserGraph,
DemoIntents.OpenCatsBrowser to CatsBrowserGraph
DemoIntents.OpenDogsBrowser.name to DogsBrowserGraph,
DemoIntents.OpenCatsBrowser.name to CatsBrowserGraph
),
AppRootGraph,
tabStateSavingBehaviour = TabStateSavingBehaviour.SAVE_START_DESTINATION
Expand Down
2 changes: 1 addition & 1 deletion gradle/version.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def VERSION_BASE = "0.2.14"
def VERSION_BASE = "0.2.15"

rootProject.readParam("mavenSnapshot", true)

Expand Down

0 comments on commit 05bec92

Please sign in to comment.