Fix background composable

Change-Id: I548612191b0b99620777c3e2ad0ace56b228d27d
pull/491/head
Alex Vanyo 2 years ago
parent b00ed067a7
commit 2fad9fd1e5

@ -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.DrawableResourceIcon
import com.google.samples.apps.nowinandroid.core.designsystem.icon.Icon.ImageVectorIcon 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.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.R as settingsR
import com.google.samples.apps.nowinandroid.feature.settings.SettingsDialog import com.google.samples.apps.nowinandroid.feature.settings.SettingsDialog
import com.google.samples.apps.nowinandroid.navigation.NiaNavHost import com.google.samples.apps.nowinandroid.navigation.NiaNavHost
@ -86,101 +87,112 @@ fun NiaApp(
windowSizeClass = windowSizeClass windowSizeClass = windowSizeClass
), ),
) { ) {
val background: @Composable (@Composable () -> Unit) -> Unit = val showGradientBackground = appState.currentTopLevelDestination == TopLevelDestination.FOR_YOU
when (appState.currentTopLevelDestination) {
TopLevelDestination.FOR_YOU -> {
content ->
NiaGradientBackground(content = content)
}
else -> { content -> NiaBackground(content = content) }
}
background { NiaBackground {
val snackbarHostState = remember { SnackbarHostState() } 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. // If user is not connected to the internet show a snack bar to inform them.
val notConnectedMessage = stringResource(R.string.not_connected) val notConnectedMessage = stringResource(R.string.not_connected)
LaunchedEffect(isOffline) { LaunchedEffect(isOffline) {
if (isOffline) snackbarHostState.showSnackbar( if (isOffline) snackbarHostState.showSnackbar(
message = notConnectedMessage, message = notConnectedMessage,
duration = Indefinite duration = Indefinite
) )
} }
if (appState.shouldShowSettingsDialog) {
SettingsDialog(
onDismiss = { appState.setShowSettingsDialog(false) }
)
}
Scaffold( if (appState.shouldShowSettingsDialog) {
modifier = Modifier.semantics { SettingsDialog(
testTagsAsResourceId = true onDismiss = { appState.setShowSettingsDialog(false) }
}, )
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")
)
}
} }
) { padding ->
Row( Scaffold(
Modifier modifier = Modifier.semantics {
.fillMaxSize() testTagsAsResourceId = true
.padding(padding) },
.consumedWindowInsets(padding) containerColor = Color.Transparent,
.windowInsetsPadding( contentColor = MaterialTheme.colorScheme.onBackground,
WindowInsets.safeDrawing.only( contentWindowInsets = WindowInsets(0, 0, 0, 0),
WindowInsetsSides.Horizontal 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()) { Column(Modifier.fillMaxSize()) {
// Show the top app bar on top level destinations. // Show the top app bar on top level destinations.
val destination = appState.currentTopLevelDestination val destination = appState.currentTopLevelDestination
if (destination != null) { if (destination != null) {
NiaTopAppBar( NiaTopAppBar(
titleRes = destination.titleTextId, titleRes = destination.titleTextId,
actionIcon = NiaIcons.Settings, actionIcon = NiaIcons.Settings,
actionIconContentDescription = stringResource( actionIconContentDescription = stringResource(
id = settingsR.string.top_app_bar_action_icon_description id = settingsR.string.top_app_bar_action_icon_description
), ),
colors = TopAppBarDefaults.centerAlignedTopAppBarColors( colors = TopAppBarDefaults.centerAlignedTopAppBarColors(
containerColor = Color.Transparent containerColor = Color.Transparent
), ),
onActionClick = { appState.setShowSettingsDialog(true) } onActionClick = { appState.setShowSettingsDialog(true) }
)
}
NiaNavHost(
navController = appState.navController,
onBackClick = appState::onBackClick
) )
} }
NiaNavHost( // TODO: We may want to add padding or spacer when the snackbar is shown so that
navController = appState.navController, // content doesn't display behind it.
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.
} }
} }
} }

Loading…
Cancel
Save