Skip to content

Commit 16f45c0

Browse files
Adds screenshot tests for snackbar (#1343)
* Adds screenshot tests for snackbar Change-Id: I81566e3301fb17e3f31cbc408e6cc3325eab5a12 * Small tweaks, RFR Change-Id: Ia16db6e6f5bec2714081b5ceaeeba36b2ee26105 * 🤖 Updates screenshots * Update NiaApp.kt Adds modifier to NiaApp
1 parent 88c3eb0 commit 16f45c0

File tree

8 files changed

+342
-89
lines changed

8 files changed

+342
-89
lines changed

app/src/main/kotlin/com/google/samples/apps/nowinandroid/ui/NiaApp.kt

Lines changed: 102 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,6 @@ import com.google.samples.apps.nowinandroid.navigation.NiaNavHost
7575
import com.google.samples.apps.nowinandroid.navigation.TopLevelDestination
7676
import com.google.samples.apps.nowinandroid.feature.settings.R as settingsR
7777

78-
@OptIn(
79-
ExperimentalMaterial3Api::class,
80-
ExperimentalComposeUiApi::class,
81-
)
8278
@Composable
8379
fun NiaApp(appState: NiaAppState) {
8480
val shouldShowGradientBackground =
@@ -108,95 +104,114 @@ fun NiaApp(appState: NiaAppState) {
108104
}
109105
}
110106

111-
if (showSettingsDialog) {
112-
SettingsDialog(
113-
onDismiss = { showSettingsDialog = false },
107+
NiaApp(
108+
appState = appState,
109+
snackbarHostState = snackbarHostState,
110+
showSettingsDialog = showSettingsDialog,
111+
onSettingsDismissed = { showSettingsDialog = false },
112+
onTopAppBarActionClick = { showSettingsDialog = true },
113+
)
114+
}
115+
}
116+
}
117+
118+
@Composable
119+
@OptIn(ExperimentalMaterial3Api::class, ExperimentalComposeUiApi::class)
120+
internal fun NiaApp(
121+
appState: NiaAppState,
122+
snackbarHostState: SnackbarHostState,
123+
showSettingsDialog: Boolean,
124+
onSettingsDismissed: () -> Unit,
125+
onTopAppBarActionClick: () -> Unit,
126+
modifier: Modifier = Modifier,
127+
) {
128+
val unreadDestinations by appState.topLevelDestinationsWithUnreadResources
129+
.collectAsStateWithLifecycle()
130+
131+
if (showSettingsDialog) {
132+
SettingsDialog(
133+
onDismiss = { onSettingsDismissed() },
134+
)
135+
}
136+
Scaffold(
137+
modifier = modifier.semantics {
138+
testTagsAsResourceId = true
139+
},
140+
containerColor = Color.Transparent,
141+
contentColor = MaterialTheme.colorScheme.onBackground,
142+
contentWindowInsets = WindowInsets(0, 0, 0, 0),
143+
snackbarHost = { SnackbarHost(snackbarHostState) },
144+
bottomBar = {
145+
if (appState.shouldShowBottomBar) {
146+
NiaBottomBar(
147+
destinations = appState.topLevelDestinations,
148+
destinationsWithUnreadResources = unreadDestinations,
149+
onNavigateToDestination = appState::navigateToTopLevelDestination,
150+
currentDestination = appState.currentDestination,
151+
modifier = Modifier.testTag("NiaBottomBar"),
152+
)
153+
}
154+
},
155+
) { padding ->
156+
Row(
157+
Modifier
158+
.fillMaxSize()
159+
.padding(padding)
160+
.consumeWindowInsets(padding)
161+
.windowInsetsPadding(
162+
WindowInsets.safeDrawing.only(
163+
WindowInsetsSides.Horizontal,
164+
),
165+
),
166+
) {
167+
if (appState.shouldShowNavRail) {
168+
NiaNavRail(
169+
destinations = appState.topLevelDestinations,
170+
destinationsWithUnreadResources = unreadDestinations,
171+
onNavigateToDestination = appState::navigateToTopLevelDestination,
172+
currentDestination = appState.currentDestination,
173+
modifier = Modifier
174+
.testTag("NiaNavRail")
175+
.safeDrawingPadding(),
114176
)
115177
}
116178

117-
val unreadDestinations by appState.topLevelDestinationsWithUnreadResources.collectAsStateWithLifecycle()
118-
119-
Scaffold(
120-
modifier = Modifier.semantics {
121-
testTagsAsResourceId = true
122-
},
123-
containerColor = Color.Transparent,
124-
contentColor = MaterialTheme.colorScheme.onBackground,
125-
contentWindowInsets = WindowInsets(0, 0, 0, 0),
126-
snackbarHost = { SnackbarHost(snackbarHostState) },
127-
bottomBar = {
128-
if (appState.shouldShowBottomBar) {
129-
NiaBottomBar(
130-
destinations = appState.topLevelDestinations,
131-
destinationsWithUnreadResources = unreadDestinations,
132-
onNavigateToDestination = appState::navigateToTopLevelDestination,
133-
currentDestination = appState.currentDestination,
134-
modifier = Modifier.testTag("NiaBottomBar"),
135-
)
136-
}
137-
},
138-
) { padding ->
139-
Row(
140-
Modifier
141-
.fillMaxSize()
142-
.padding(padding)
143-
.consumeWindowInsets(padding)
144-
.windowInsetsPadding(
145-
WindowInsets.safeDrawing.only(
146-
WindowInsetsSides.Horizontal,
147-
),
179+
Column(Modifier.fillMaxSize()) {
180+
// Show the top app bar on top level destinations.
181+
val destination = appState.currentTopLevelDestination
182+
if (destination != null) {
183+
NiaTopAppBar(
184+
titleRes = destination.titleTextId,
185+
navigationIcon = NiaIcons.Search,
186+
navigationIconContentDescription = stringResource(
187+
id = settingsR.string.feature_settings_top_app_bar_navigation_icon_description,
148188
),
149-
) {
150-
if (appState.shouldShowNavRail) {
151-
NiaNavRail(
152-
destinations = appState.topLevelDestinations,
153-
destinationsWithUnreadResources = unreadDestinations,
154-
onNavigateToDestination = appState::navigateToTopLevelDestination,
155-
currentDestination = appState.currentDestination,
156-
modifier = Modifier
157-
.testTag("NiaNavRail")
158-
.safeDrawingPadding(),
159-
)
160-
}
161-
162-
Column(Modifier.fillMaxSize()) {
163-
// Show the top app bar on top level destinations.
164-
val destination = appState.currentTopLevelDestination
165-
if (destination != null) {
166-
NiaTopAppBar(
167-
titleRes = destination.titleTextId,
168-
navigationIcon = NiaIcons.Search,
169-
navigationIconContentDescription = stringResource(
170-
id = settingsR.string.feature_settings_top_app_bar_navigation_icon_description,
171-
),
172-
actionIcon = NiaIcons.Settings,
173-
actionIconContentDescription = stringResource(
174-
id = settingsR.string.feature_settings_top_app_bar_action_icon_description,
175-
),
176-
colors = TopAppBarDefaults.centerAlignedTopAppBarColors(
177-
containerColor = Color.Transparent,
178-
),
179-
onActionClick = { showSettingsDialog = true },
180-
onNavigationClick = { appState.navigateToSearch() },
181-
)
182-
}
183-
184-
NiaNavHost(
185-
appState = appState,
186-
onShowSnackbar = { message, action ->
187-
snackbarHostState.showSnackbar(
188-
message = message,
189-
actionLabel = action,
190-
duration = Short,
191-
) == ActionPerformed
192-
},
193-
)
194-
}
195-
196-
// TODO: We may want to add padding or spacer when the snackbar is shown so that
197-
// content doesn't display behind it.
189+
actionIcon = NiaIcons.Settings,
190+
actionIconContentDescription = stringResource(
191+
id = settingsR.string.feature_settings_top_app_bar_action_icon_description,
192+
),
193+
colors = TopAppBarDefaults.centerAlignedTopAppBarColors(
194+
containerColor = Color.Transparent,
195+
),
196+
onActionClick = { onTopAppBarActionClick() },
197+
onNavigationClick = { appState.navigateToSearch() },
198+
)
198199
}
200+
201+
NiaNavHost(
202+
appState = appState,
203+
onShowSnackbar = { message, action ->
204+
snackbarHostState.showSnackbar(
205+
message = message,
206+
actionLabel = action,
207+
duration = Short,
208+
) == ActionPerformed
209+
},
210+
)
199211
}
212+
213+
// TODO: We may want to add padding or spacer when the snackbar is shown so that
214+
// content doesn't display behind it.
200215
}
201216
}
202217
}

0 commit comments

Comments
 (0)