Skip to content

Commit

Permalink
Merge pull request #162 from tiper/bugfix/Issue-134
Browse files Browse the repository at this point in the history
Avoid composing scene multiple times due to BackStackEntry
  • Loading branch information
Tlaster authored Oct 20, 2023
2 parents bc13c2f + 2664fba commit c4422de
Showing 1 changed file with 42 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package moe.tlaster.precompose.navigation

import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import moe.tlaster.precompose.navigation.route.FloatingRoute
import moe.tlaster.precompose.navigation.route.GroupRoute
import moe.tlaster.precompose.navigation.route.Route
Expand All @@ -12,6 +15,24 @@ class RouteBuilder(
) {
private val route = arrayListOf<Route>()

private fun sceneInternal(
route: String,
deepLinks: List<String>,
navTransition: NavTransition?,
swipeProperties: SwipeProperties?,
content: @Composable (State<BackStackEntry>) -> Unit,
) {
addRoute(
SceneRoute(
route = route,
navTransition = navTransition,
deepLinks = deepLinks,
swipeProperties = swipeProperties,
content = { content(remember { mutableStateOf(it) }) },
),
)
}

/**
* Add the scene [Composable] to the [RouteBuilder]
* @param route route for the destination
Expand All @@ -26,14 +47,12 @@ class RouteBuilder(
swipeProperties: SwipeProperties? = null,
content: @Composable (BackStackEntry) -> Unit,
) {
addRoute(
SceneRoute(
route = route,
navTransition = navTransition,
deepLinks = deepLinks,
swipeProperties = swipeProperties,
content = content,
),
sceneInternal(
route = route,
navTransition = navTransition,
deepLinks = deepLinks,
swipeProperties = swipeProperties,
content = { content(it.value) },
)
}

Expand Down Expand Up @@ -74,6 +93,18 @@ class RouteBuilder(
)
}

private fun floatingInternal(
route: String,
content: @Composable (State<BackStackEntry>) -> Unit,
) {
addRoute(
FloatingRoute(
route = route,
content = { content(remember { mutableStateOf(it) }) },
),
)
}

/**
* Add the floating [Composable] to the [RouteBuilder], which will show over the scene
* @param route route for the destination
Expand All @@ -83,11 +114,9 @@ class RouteBuilder(
route: String,
content: @Composable (BackStackEntry) -> Unit,
) {
addRoute(
FloatingRoute(
route = route,
content = content,
),
floatingInternal(
route = route,
content = { content(it.value) },
)
}

Expand Down

0 comments on commit c4422de

Please sign in to comment.