Skip to content

Commit

Permalink
Small code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafael Costa committed Oct 30, 2021
1 parent 72741de commit 2ccbfd6
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import com.google.accompanist.navigation.material.ModalBottomSheetLayout
import com.google.accompanist.navigation.material.rememberBottomSheetNavigator
import com.ramcosta.composedestinations.Destination
import com.ramcosta.composedestinations.NavGraphs
import com.ramcosta.composedestinations.findDestination
import com.ramcosta.composedestinations.navDestination

@OptIn(ExperimentalMaterialNavigationApi::class, ExperimentalMaterialApi::class)
@Composable
Expand All @@ -29,12 +29,8 @@ fun DestinationsSampleScaffold(
drawerContent: @Composable ColumnScope.(Destination) -> Unit,
content: @Composable (PaddingValues) -> Unit
) {

val currentBackStackEntryAsState by navController.currentBackStackEntryAsState()

val destination = currentBackStackEntryAsState?.destination?.route
?.let { NavGraphs.root.findDestination(it) as Destination }
?: NavGraphs.root.startDestination
val destination = currentBackStackEntryAsState?.navDestination ?: NavGraphs.root.startDestination

val bottomSheetNavigator = rememberBottomSheetNavigator()
navController.navigatorProvider += bottomSheetNavigator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import com.ramcosta.composedestinations.GoToProfileConfirmationDestination
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.navigation.DestinationsNavigator
import com.ramcosta.samples.destinationstodosample.destinations.commons.DrawerController
import com.ramcosta.samples.destinationstodosample.destinations.styles.GreetingTransitions
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import com.ramcosta.samples.destinationstodosample.R
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ramcosta.samples.destinationstodosample.destinations.styles
package com.ramcosta.samples.destinationstodosample.destinations.greeting

import androidx.compose.animation.*
import androidx.compose.animation.core.tween
Expand All @@ -12,7 +12,14 @@ object GreetingTransitions : AnimatedDestinationStyle {
target: Destination?
): EnterTransition? {

return popEnterTransition(initial, target)
return when (initial) {
ProfileScreenDestination ->
slideInHorizontally(
initialOffsetX = { 1000 },
animationSpec = tween(700)
)
else -> null
}
}

override fun AnimatedContentScope<String>.exitTransition(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import androidx.compose.ui.unit.dp
import com.ramcosta.composedestinations.annotation.DeepLink
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.annotation.FULL_ROUTE_PLACEHOLDER
import com.ramcosta.samples.destinationstodosample.destinations.styles.ProfileTransitions

val DEFAULT_GROUP : String? = null
const val DEFAULT_ID = 2L
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ramcosta.samples.destinationstodosample.destinations.styles
package com.ramcosta.samples.destinationstodosample.destinations.profile

import androidx.compose.animation.*
import androidx.compose.animation.core.tween
Expand Down Expand Up @@ -26,9 +26,17 @@ object ProfileTransitions : AnimatedDestinationStyle {

override fun AnimatedContentScope<String>.exitTransition(
initial: Destination?,
target: Destination?
target: Destination?,
): ExitTransition? {
return popExitTransition(initial, target)

return when (target) {
GreetingScreenDestination ->
slideOutHorizontally(
targetOffsetX = { -1000 },
animationSpec = tween(700)
)
else -> null
}
}

override fun AnimatedContentScope<String>.popEnterTransition(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,36 +73,32 @@ $END_ACCOMPANIST_MATERIAL_IMPORTS
*
* @see [$NAV_HOST_METHOD_QUALIFIED_NAME]
*
* @param navGraph [$GENERATED_NAV_GRAPHS_OBJECT] with the destinations to add to NavHost. By default,
* it will be the 'root' one which is the one with all annotated destinations
* that don't specify other 'navGraph'
* @param modifier [Modifier]
* @param startDestination the start destination to use
* @param navController [NavHostController]
* @param modifier [Modifier]
* @param dependenciesContainerBuilder lambda invoked when a destination gets navigated to. It allows
* the caller to contribute certain dependencies that the destination can use.
*/
$EXPERIMENTAL_API_PLACEHOLDER@Composable
fun $DESTINATIONS_NAV_HOST(
navGraph: $GENERATED_NAV_GRAPH = ${GENERATED_NAV_GRAPHS_OBJECT}.root,
startDestination: $GENERATED_DESTINATION = navGraph.startDestination,$ANIMATION_DEFAULT_PARAMS_PLACEHOLDER
navController: NavHostController = rememberDestinationsNavController(),
modifier: Modifier = Modifier,
startDestination: $GENERATED_DESTINATION = ${GENERATED_NAV_GRAPHS_OBJECT}.root.startDestination,$ANIMATION_DEFAULT_PARAMS_PLACEHOLDER
navController: NavHostController = rememberDestinationsNavController(),
dependenciesContainerBuilder: @Composable DependenciesContainerBuilder.(NavBackStackEntry) -> Unit = {}
) {
$NAV_HOST_METHOD_NAME(
navController = navController,
startDestination = startDestination.route,
modifier = modifier,
route = navGraph.route,$ANIMATED_NAV_HOST_CALL_PARAMETERS_START
route = ${GENERATED_NAV_GRAPHS_OBJECT}.root.route,$ANIMATED_NAV_HOST_CALL_PARAMETERS_START
contentAlignment = defaultAnimationParams.contentAlignment,
enterTransition = defaultAnimationParams.enterTransition?.run { { i, t -> enter(i.navDestination, t.navDestination) } },
exitTransition = defaultAnimationParams.exitTransition?.run{ {i, t -> exit(i.navDestination, t.navDestination) } },
popEnterTransition = defaultAnimationParams.popEnterTransition?.run{ {i, t -> enter(i.navDestination, t.navDestination) } },
popExitTransition = defaultAnimationParams.popExitTransition?.run{ {i, t -> exit(i.navDestination, t.navDestination) } },$ANIMATED_NAV_HOST_CALL_PARAMETERS_END
) {
addNavGraphDestinations(
navGraphSpec = navGraph,
navGraphSpec = ${GENERATED_NAV_GRAPHS_OBJECT}.root,
addNavigation = addNavigation(),
addComposable = addComposable(navController, dependenciesContainerBuilder)
)
Expand All @@ -128,8 +124,7 @@ ${EXPERIMENTAL_API_PLACEHOLDER}private fun addComposable(
): NavGraphBuilder.($CORE_DESTINATION_SPEC) -> Unit {
return { destination ->
destination as $GENERATED_DESTINATION
val destinationStyle = destination.style
when (destinationStyle) {
when (val destinationStyle = destination.style) {
is DestinationStyle.Default -> {
addComposable(
destination,
Expand Down Expand Up @@ -181,12 +176,11 @@ ${EXPERIMENTAL_API_PLACEHOLDER}private fun NavGraphBuilder.addComposable(
) { navBackStackEntry ->
destination.Content(
navController,
navBackStackEntry,
{$ANIMATED_VISIBILITY_TO_CONTENT_START
dependency<$ANIMATED_VISIBILITY_SCOPE_SIMPLE_NAME>(this@composable)$ANIMATED_VISIBILITY_TO_CONTENT_END
dependenciesContainerBuilder(navBackStackEntry)
}
)
navBackStackEntry
) {$ANIMATED_VISIBILITY_TO_CONTENT_START
dependency<$ANIMATED_VISIBILITY_SCOPE_SIMPLE_NAME>(this@composable)$ANIMATED_VISIBILITY_TO_CONTENT_END
dependenciesContainerBuilder(navBackStackEntry)
}
}
}
Expand All @@ -201,12 +195,11 @@ private fun NavGraphBuilder.addDialogComposable(
destination.arguments,
destination.deepLinks,
dialogStyle.properties
) {
) { navBackStackEntry ->
destination.Content(
navController = navController,
navBackStackEntry = it,
dependenciesContainerBuilder = { dependenciesContainerBuilder(it) }
)
navController,
navBackStackEntry
) { dependenciesContainerBuilder(navBackStackEntry) }
}
}
Expand Down Expand Up @@ -240,12 +233,11 @@ private fun NavGraphBuilder.addAnimatedComposable(
) { navBackStackEntry ->
destination.Content(
navController,
navBackStackEntry,
{
dependency<$ANIMATED_VISIBILITY_SCOPE_SIMPLE_NAME>(this@composable)
dependenciesContainerBuilder(navBackStackEntry)
}
)
navBackStackEntry
) {
dependency<$ANIMATED_VISIBILITY_SCOPE_SIMPLE_NAME>(this@composable)
dependenciesContainerBuilder(navBackStackEntry)
}
}
}
$END_ACCOMPANIST_NAVIGATION
Expand All @@ -264,12 +256,11 @@ private fun NavGraphBuilder.addBottomSheetComposable(
) { navBackStackEntry ->
destination.Content(
navController,
navBackStackEntry,
{
dependency<$COLUMN_SCOPE_SIMPLE_NAME>(this@bottomSheet)
dependenciesContainerBuilder(navBackStackEntry)
}
)
navBackStackEntry
) {
dependency<$COLUMN_SCOPE_SIMPLE_NAME>(this@bottomSheet)
dependenciesContainerBuilder(navBackStackEntry)
}
}
}
$END_ACCOMPANIST_MATERIAL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,9 @@ class SingleDestinationWriter(

code += "navArgument(\"${it.name}\") {\n\t\t\t"
code += "type = ${it.toNavTypeCode()}\n\t\t\t"
code += "nullable = ${it.type.isNullable}\n\t\t"
if (it.type.isNullable) {
code += "nullable = true\n\t\t"
}
code += navArgDefaultCode(it.defaultValue)
code += "}"

Expand Down

0 comments on commit 2ccbfd6

Please sign in to comment.