Created
November 30, 2024 18:07
-
-
Save anitaa1990/72cab98e783669f49cda0d056479d303 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* The main composable function for setting up the app's UI structure. | |
* It defines the scaffold layout, navigation, and other top-level elements for the app. | |
* | |
* @param navController The navigation controller for managing app navigation. | |
* @param snackbarHostState The state for displaying snackbars in the app. | |
*/ | |
@OptIn(ExperimentalMaterial3Api::class) | |
@Composable | |
fun MainApp( | |
navController: NavHostController, | |
snackbarHostState: SnackbarHostState | |
) { | |
// Defines a scroll behavior for the top app bar, enabling it to collapse on scroll. | |
val scrollBehavior = TopAppBarDefaults.exitUntilCollapsedScrollBehavior( | |
rememberTopAppBarState() | |
) | |
// Observes the current back stack entry to determine the navigation state. | |
val currentBackStackEntry by navController.currentBackStackEntryAsState() | |
// Determines if the back button should be shown, based on the navigation stack. | |
val showBackButton by remember(currentBackStackEntry) { | |
derivedStateOf { navController.previousBackStackEntry != null } | |
} | |
// Defines the scaffold structure, which includes the top app bar, snackbar host, and floating action button. | |
Scaffold( | |
snackbarHost = { SnackbarHost(hostState = snackbarHostState) }, // Host for displaying snackbars. | |
topBar = { | |
// The top app bar, which dynamically displays a back button based on the navigation state. | |
MainTopAppBar( | |
navController = navController, | |
showBackButton = showBackButton, | |
scrollBehavior = scrollBehavior | |
) | |
}, | |
modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection) // Ensures nested scrolling works with the top bar. | |
) { innerPadding -> | |
// Sets up the navigation host, which manages the composable destinations in the app. | |
NavHost( | |
navController = navController, | |
startDestination = ROUTE_HOME, // The starting destination of the app. | |
modifier = Modifier | |
.padding(innerPadding) // Ensures padding for the scaffold's content area. | |
.fillMaxSize() // Ensures the navigation content fills the available space. | |
) { | |
// Defines the home screen route. | |
composable(route = ROUTE_HOME) { | |
val noteViewModel = hiltViewModel<NoteViewModel>() // Retrieves the NoteViewModel using Hilt. | |
NotesScreen(viewModel = noteViewModel) // Displays the list of notes. | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment