From 77c516b253b7390bcf9409f07bfafcf35cc19247 Mon Sep 17 00:00:00 2001 From: Vincent TE Date: Tue, 10 Dec 2024 15:16:25 +0100 Subject: [PATCH] fix: Add last main destination to be able to back to the right screen when navigating files --- .../ui/screen/main/MainNavHost.kt | 9 ++--- .../ui/screen/main/MainScreen.kt | 33 +++++++++++++------ 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/main/MainNavHost.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/main/MainNavHost.kt index b47f4646d..dd463b90f 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/main/MainNavHost.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/main/MainNavHost.kt @@ -38,6 +38,8 @@ fun MainNavHost( navController: NavHostController, currentDestination: MainNavigation, isWindowSmall: Boolean, + onStartDestinationChanged: (MainNavigation) -> Unit, + closeFilesDetails: () -> Unit, ) { NavHost( navController = navController, @@ -46,6 +48,7 @@ fun MainNavHost( exitTransition = { if (currentDestination.enableTransition) fadeOut() else ExitTransition.None }, ) { composable { + onStartDestinationChanged(SentDestination) TransfersScreenWrapper( navigateToFilesDetails = { folderUuid -> navController.navigate(FilesDetailsDestination(folderUuid)) @@ -54,6 +57,7 @@ fun MainNavHost( ) } composable { + onStartDestinationChanged(ReceivedDestination) TransfersScreenWrapper( navigateToFilesDetails = { folderUuid -> navController.navigate(FilesDetailsDestination(folderUuid)) @@ -71,10 +75,7 @@ fun MainNavHost( folderUuid = filesDetailsDestination.folderUuid, navigateBack = { navController.popBackStack() }, close = { - navController.popBackStack( - ReceivedDestination, - false - ) + closeFilesDetails() }, withFilesSize = false, withSpaceLeft = false, diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/main/MainScreen.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/main/MainScreen.kt index 8627ca2d0..50b3657a9 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/main/MainScreen.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/main/MainScreen.kt @@ -17,10 +17,9 @@ */ package com.infomaniak.swisstransfer.ui.screen.main -import androidx.compose.runtime.Composable -import androidx.compose.runtime.derivedStateOf -import androidx.compose.runtime.getValue -import androidx.compose.runtime.remember +import android.util.Log +import androidx.compose.runtime.* +import androidx.navigation.NavController import androidx.navigation.compose.currentBackStackEntryAsState import androidx.navigation.compose.rememberNavController import com.infomaniak.swisstransfer.ui.components.BrandTopAppBar @@ -41,9 +40,13 @@ fun MainScreen() { val navBackStackEntry by navController.currentBackStackEntryAsState() val currentDestination by remember(navBackStackEntry) { - derivedStateOf { navBackStackEntry?.toDestination() ?: MainNavigation.startDestination } + derivedStateOf { + navBackStackEntry?.toDestination() ?: MainNavigation.startDestination + } } + var lastStartDestination: MainNavigation by remember { mutableStateOf(MainNavigation.startDestination) } + MainScaffold( navController = navController, currentDestination = currentDestination, @@ -54,10 +57,7 @@ fun MainScreen() { SwissTransferTopAppBar( navigationMenu = TopAppBarButton.backButton { navController.popBackStack() }, actionMenus = arrayOf(TopAppBarButton.closeButton { - navController.popBackStack( - route = MainNavigation.ReceivedDestination, - inclusive = false, - ) + goBackToStartScreen(navController, lastStartDestination) }), ) } else { @@ -66,11 +66,24 @@ fun MainScreen() { } }, content = { - MainNavHost(navController, currentDestination, LocalWindowAdaptiveInfo.current.isWindowSmall()) + MainNavHost( + navController = navController, + currentDestination = currentDestination, + isWindowSmall = LocalWindowAdaptiveInfo.current.isWindowSmall(), + onStartDestinationChanged = { lastStartDestination = it }, + closeFilesDetails = { goBackToStartScreen(navController, lastStartDestination) } + ) }, ) } +private fun goBackToStartScreen(navController: NavController, lastStartDestination: MainNavigation) { + navController.popBackStack( + route = lastStartDestination, + inclusive = false, + ) +} + @PreviewAllWindows @Composable private fun Preview() {