Skip to content

Commit

Permalink
fix: Add last main destination to be able to back to the right screen…
Browse files Browse the repository at this point in the history
… when navigating files
  • Loading branch information
tevincent committed Dec 16, 2024
1 parent b0e6e89 commit 77c516b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ fun MainNavHost(
navController: NavHostController,
currentDestination: MainNavigation,
isWindowSmall: Boolean,
onStartDestinationChanged: (MainNavigation) -> Unit,
closeFilesDetails: () -> Unit,
) {
NavHost(
navController = navController,
Expand All @@ -46,6 +48,7 @@ fun MainNavHost(
exitTransition = { if (currentDestination.enableTransition) fadeOut() else ExitTransition.None },
) {
composable<SentDestination> {
onStartDestinationChanged(SentDestination)
TransfersScreenWrapper(
navigateToFilesDetails = { folderUuid ->
navController.navigate(FilesDetailsDestination(folderUuid))
Expand All @@ -54,6 +57,7 @@ fun MainNavHost(
)
}
composable<ReceivedDestination> {
onStartDestinationChanged(ReceivedDestination)
TransfersScreenWrapper(
navigateToFilesDetails = { folderUuid ->
navController.navigate(FilesDetailsDestination(folderUuid))
Expand All @@ -71,10 +75,7 @@ fun MainNavHost(
folderUuid = filesDetailsDestination.folderUuid,
navigateBack = { navController.popBackStack() },
close = {
navController.popBackStack(
ReceivedDestination,
false
)
closeFilesDetails()
},
withFilesSize = false,
withSpaceLeft = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -41,9 +40,13 @@ fun MainScreen() {
val navBackStackEntry by navController.currentBackStackEntryAsState()

val currentDestination by remember(navBackStackEntry) {
derivedStateOf { navBackStackEntry?.toDestination<MainNavigation>() ?: MainNavigation.startDestination }
derivedStateOf {
navBackStackEntry?.toDestination<MainNavigation>() ?: MainNavigation.startDestination
}
}

var lastStartDestination: MainNavigation by remember { mutableStateOf(MainNavigation.startDestination) }

MainScaffold(
navController = navController,
currentDestination = currentDestination,
Expand All @@ -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 {
Expand All @@ -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() {
Expand Down

0 comments on commit 77c516b

Please sign in to comment.