From 99de280c5627d9b9720c182075df7dd3cfd90fa0 Mon Sep 17 00:00:00 2001 From: acclorite Date: Thu, 20 Jun 2024 14:00:02 +0300 Subject: [PATCH] Navigation.kt: Remembered shouldShow for Navigator.composable (might be unnoticeable performance improvement). --- .../book_story/presentation/data/Navigation.kt | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/ua/acclorite/book_story/presentation/data/Navigation.kt b/app/src/main/java/ua/acclorite/book_story/presentation/data/Navigation.kt index 68c6ae8f..b1d1a639 100644 --- a/app/src/main/java/ua/acclorite/book_story/presentation/data/Navigation.kt +++ b/app/src/main/java/ua/acclorite/book_story/presentation/data/Navigation.kt @@ -269,11 +269,16 @@ class Navigator @AssistedInject constructor( backExitAnim: ExitTransition = Transitions.BackSlidingTransitionOut, noinline content: @Composable (screen: S) -> Unit ) { - val currentRoute by currentScreen.collectAsState() + val currentRoute = currentScreen.collectAsState() val useBackAnimation by useBackAnimation.collectAsState() + val shouldShow by remember { + derivedStateOf { + currentRoute.value == getRoute() + } + } CustomAnimatedVisibility( - visible = currentRoute == getRoute(), + visible = shouldShow, enter = if (!useBackAnimation) enterAnim else backEnterAnim, exit = if (!useBackAnimation) exitAnim else backExitAnim ) { @@ -309,11 +314,11 @@ class Navigator @AssistedInject constructor( content: @Composable () -> Unit ) { val activity = LocalContext.current as ComponentActivity - val currentScreen by currentScreen.collectAsState() + val currentScreen = currentScreen.collectAsState() val useBackAnimation by useBackAnimation.collectAsState() - val shouldShow by remember(currentScreen) { + val shouldShow by remember { derivedStateOf { - screens.any { it == currentScreen } + screens.any { it == currentScreen.value } } }