From 2fad9fd1e5c07ccfcb1fd7918f983d70b2d26671 Mon Sep 17 00:00:00 2001 From: Alex Vanyo Date: Thu, 15 Dec 2022 15:21:18 -0800 Subject: [PATCH] Fix background composable Change-Id: I548612191b0b99620777c3e2ad0ace56b228d27d --- .../samples/apps/nowinandroid/ui/NiaApp.kt | 178 ++++++++++-------- 1 file changed, 95 insertions(+), 83 deletions(-) diff --git a/app/src/main/java/com/google/samples/apps/nowinandroid/ui/NiaApp.kt b/app/src/main/java/com/google/samples/apps/nowinandroid/ui/NiaApp.kt index 2c76222af..325ff2652 100644 --- a/app/src/main/java/com/google/samples/apps/nowinandroid/ui/NiaApp.kt +++ b/app/src/main/java/com/google/samples/apps/nowinandroid/ui/NiaApp.kt @@ -66,6 +66,7 @@ import com.google.samples.apps.nowinandroid.core.designsystem.component.NiaTopAp import com.google.samples.apps.nowinandroid.core.designsystem.icon.Icon.DrawableResourceIcon import com.google.samples.apps.nowinandroid.core.designsystem.icon.Icon.ImageVectorIcon import com.google.samples.apps.nowinandroid.core.designsystem.icon.NiaIcons +import com.google.samples.apps.nowinandroid.core.designsystem.theme.LocalGradientColors import com.google.samples.apps.nowinandroid.feature.settings.R as settingsR import com.google.samples.apps.nowinandroid.feature.settings.SettingsDialog import com.google.samples.apps.nowinandroid.navigation.NiaNavHost @@ -86,101 +87,112 @@ fun NiaApp( windowSizeClass = windowSizeClass ), ) { - val background: @Composable (@Composable () -> Unit) -> Unit = - when (appState.currentTopLevelDestination) { - TopLevelDestination.FOR_YOU -> { - content -> - NiaGradientBackground(content = content) - } - else -> { content -> NiaBackground(content = content) } - } + val showGradientBackground = appState.currentTopLevelDestination == TopLevelDestination.FOR_YOU - background { - val snackbarHostState = remember { SnackbarHostState() } + NiaBackground { + NiaGradientBackground( + topColor = if (showGradientBackground) { + LocalGradientColors.current.top + } else { + Color.Unspecified + }, + bottomColor = if (showGradientBackground) { + LocalGradientColors.current.bottom + } else { + Color.Unspecified + }, + containerColor = if (showGradientBackground) { + LocalGradientColors.current.container + } else { + Color.Unspecified + }, + ) { + val snackbarHostState = remember { SnackbarHostState() } - val isOffline by appState.isOffline.collectAsStateWithLifecycle() + val isOffline by appState.isOffline.collectAsStateWithLifecycle() - // If user is not connected to the internet show a snack bar to inform them. - val notConnectedMessage = stringResource(R.string.not_connected) - LaunchedEffect(isOffline) { - if (isOffline) snackbarHostState.showSnackbar( - message = notConnectedMessage, - duration = Indefinite - ) - } - - if (appState.shouldShowSettingsDialog) { - SettingsDialog( - onDismiss = { appState.setShowSettingsDialog(false) } - ) - } + // If user is not connected to the internet show a snack bar to inform them. + val notConnectedMessage = stringResource(R.string.not_connected) + LaunchedEffect(isOffline) { + if (isOffline) snackbarHostState.showSnackbar( + message = notConnectedMessage, + duration = Indefinite + ) + } - Scaffold( - modifier = Modifier.semantics { - testTagsAsResourceId = true - }, - containerColor = Color.Transparent, - contentColor = MaterialTheme.colorScheme.onBackground, - contentWindowInsets = WindowInsets(0, 0, 0, 0), - snackbarHost = { SnackbarHost(snackbarHostState) }, - bottomBar = { - if (appState.shouldShowBottomBar) { - NiaBottomBar( - destinations = appState.topLevelDestinations, - onNavigateToDestination = appState::navigateToTopLevelDestination, - currentDestination = appState.currentDestination, - modifier = Modifier.testTag("NiaBottomBar") - ) - } + if (appState.shouldShowSettingsDialog) { + SettingsDialog( + onDismiss = { appState.setShowSettingsDialog(false) } + ) } - ) { padding -> - Row( - Modifier - .fillMaxSize() - .padding(padding) - .consumedWindowInsets(padding) - .windowInsetsPadding( - WindowInsets.safeDrawing.only( - WindowInsetsSides.Horizontal + + Scaffold( + modifier = Modifier.semantics { + testTagsAsResourceId = true + }, + containerColor = Color.Transparent, + contentColor = MaterialTheme.colorScheme.onBackground, + contentWindowInsets = WindowInsets(0, 0, 0, 0), + snackbarHost = { SnackbarHost(snackbarHostState) }, + bottomBar = { + if (appState.shouldShowBottomBar) { + NiaBottomBar( + destinations = appState.topLevelDestinations, + onNavigateToDestination = appState::navigateToTopLevelDestination, + currentDestination = appState.currentDestination, + modifier = Modifier.testTag("NiaBottomBar") ) - ) - ) { - if (appState.shouldShowNavRail) { - NiaNavRail( - destinations = appState.topLevelDestinations, - onNavigateToDestination = appState::navigateToTopLevelDestination, - currentDestination = appState.currentDestination, - modifier = Modifier - .testTag("NiaNavRail") - .safeDrawingPadding() - ) + } } + ) { padding -> + Row( + Modifier + .fillMaxSize() + .padding(padding) + .consumedWindowInsets(padding) + .windowInsetsPadding( + WindowInsets.safeDrawing.only( + WindowInsetsSides.Horizontal + ) + ) + ) { + if (appState.shouldShowNavRail) { + NiaNavRail( + destinations = appState.topLevelDestinations, + onNavigateToDestination = appState::navigateToTopLevelDestination, + currentDestination = appState.currentDestination, + modifier = Modifier + .testTag("NiaNavRail") + .safeDrawingPadding() + ) + } - Column(Modifier.fillMaxSize()) { - // Show the top app bar on top level destinations. - val destination = appState.currentTopLevelDestination - if (destination != null) { - NiaTopAppBar( - titleRes = destination.titleTextId, - actionIcon = NiaIcons.Settings, - actionIconContentDescription = stringResource( - id = settingsR.string.top_app_bar_action_icon_description - ), - colors = TopAppBarDefaults.centerAlignedTopAppBarColors( - containerColor = Color.Transparent - ), - onActionClick = { appState.setShowSettingsDialog(true) } + Column(Modifier.fillMaxSize()) { + // Show the top app bar on top level destinations. + val destination = appState.currentTopLevelDestination + if (destination != null) { + NiaTopAppBar( + titleRes = destination.titleTextId, + actionIcon = NiaIcons.Settings, + actionIconContentDescription = stringResource( + id = settingsR.string.top_app_bar_action_icon_description + ), + colors = TopAppBarDefaults.centerAlignedTopAppBarColors( + containerColor = Color.Transparent + ), + onActionClick = { appState.setShowSettingsDialog(true) } + ) + } + + NiaNavHost( + navController = appState.navController, + onBackClick = appState::onBackClick ) } - NiaNavHost( - navController = appState.navController, - onBackClick = appState::onBackClick - ) + // TODO: We may want to add padding or spacer when the snackbar is shown so that + // content doesn't display behind it. } - - // TODO: We may want to add padding or spacer when the snackbar is shown so that - // content doesn't display behind it. } } }