Skip to content

Commit ded8b10

Browse files
committed
Refactor custom notificationDot modifier
Change-Id: Ife24492a495b111f111a8bd16f21b7ebd469e5aa
1 parent eec9289 commit ded8b10

File tree

1 file changed

+26
-20
lines changed
  • app/src/main/java/com/google/samples/apps/nowinandroid/ui

1 file changed

+26
-20
lines changed

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

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
package com.google.samples.apps.nowinandroid.ui
18+
1819
import androidx.compose.foundation.layout.Column
1920
import androidx.compose.foundation.layout.ExperimentalLayoutApi
2021
import androidx.compose.foundation.layout.Row
@@ -46,6 +47,7 @@ import androidx.compose.runtime.saveable.rememberSaveable
4647
import androidx.compose.runtime.setValue
4748
import androidx.compose.ui.ExperimentalComposeUiApi
4849
import androidx.compose.ui.Modifier
50+
import androidx.compose.ui.composed
4951
import androidx.compose.ui.draw.drawWithContent
5052
import androidx.compose.ui.geometry.Offset
5153
import androidx.compose.ui.graphics.Color
@@ -129,6 +131,8 @@ fun NiaApp(
129131
)
130132
}
131133

134+
val unreadDestinations by appState.topLevelDestinationsWithUnreadResources.collectAsStateWithLifecycle()
135+
132136
Scaffold(
133137
modifier = Modifier.semantics {
134138
testTagsAsResourceId = true
@@ -139,7 +143,6 @@ fun NiaApp(
139143
snackbarHost = { SnackbarHost(snackbarHostState) },
140144
bottomBar = {
141145
if (appState.shouldShowBottomBar) {
142-
val unreadDestinations by appState.topLevelDestinationsWithUnreadResources.collectAsStateWithLifecycle()
143146
NiaBottomBar(
144147
destinations = appState.topLevelDestinations,
145148
destinationsWithUnreadResources = unreadDestinations,
@@ -164,6 +167,7 @@ fun NiaApp(
164167
if (appState.shouldShowNavRail) {
165168
NiaNavRail(
166169
destinations = appState.topLevelDestinations,
170+
destinationsWithUnreadResources = unreadDestinations,
167171
onNavigateToDestination = appState::navigateToTopLevelDestination,
168172
currentDestination = appState.currentDestination,
169173
modifier = Modifier
@@ -208,13 +212,15 @@ fun NiaApp(
208212
@Composable
209213
private fun NiaNavRail(
210214
destinations: List<TopLevelDestination>,
215+
destinationsWithUnreadResources: Set<TopLevelDestination>,
211216
onNavigateToDestination: (TopLevelDestination) -> Unit,
212217
currentDestination: NavDestination?,
213218
modifier: Modifier = Modifier,
214219
) {
215220
NiaNavigationRail(modifier = modifier) {
216221
destinations.forEach { destination ->
217222
val selected = currentDestination.isTopLevelDestinationInHierarchy(destination)
223+
val hasUnread = destinationsWithUnreadResources.contains(destination)
218224
NiaNavigationRailItem(
219225
selected = selected,
220226
onClick = { onNavigateToDestination(destination) },
@@ -237,7 +243,7 @@ private fun NiaNavRail(
237243
}
238244
},
239245
label = { Text(stringResource(destination.iconTextId)) },
240-
246+
modifier = if (hasUnread) Modifier.notificationDot() else Modifier,
241247
)
242248
}
243249
}
@@ -279,30 +285,30 @@ private fun NiaBottomBar(
279285
}
280286
},
281287
label = { Text(stringResource(destination.iconTextId)) },
282-
modifier = if (hasUnread) notificationDot() else Modifier,
288+
modifier = if (hasUnread) Modifier.notificationDot() else Modifier,
283289
)
284290
}
285291
}
286292
}
287293

288-
@Composable
289-
private fun notificationDot(): Modifier {
290-
val tertiaryColor = MaterialTheme.colorScheme.tertiary
291-
return Modifier.drawWithContent {
292-
drawContent()
293-
drawCircle(
294-
tertiaryColor,
295-
radius = 5.dp.toPx(),
296-
// This is based on the dimensions of the NavigationBar's "indicator pill";
297-
// however, its parameters are private, so we must depend on them implicitly
298-
// (NavigationBarTokens.ActiveIndicatorWidth = 64.dp)
299-
center = center + Offset(
300-
64.dp.toPx() * .45f,
301-
32.dp.toPx() * -.45f - 6.dp.toPx(),
302-
),
303-
)
294+
private fun Modifier.notificationDot(): Modifier =
295+
composed {
296+
val tertiaryColor = MaterialTheme.colorScheme.tertiary
297+
drawWithContent {
298+
drawContent()
299+
drawCircle(
300+
tertiaryColor,
301+
radius = 5.dp.toPx(),
302+
// This is based on the dimensions of the NavigationBar's "indicator pill";
303+
// however, its parameters are private, so we must depend on them implicitly
304+
// (NavigationBarTokens.ActiveIndicatorWidth = 64.dp)
305+
center = center + Offset(
306+
64.dp.toPx() * .45f,
307+
32.dp.toPx() * -.45f - 6.dp.toPx(),
308+
),
309+
)
310+
}
304311
}
305-
}
306312

307313
private fun NavDestination?.isTopLevelDestinationInHierarchy(destination: TopLevelDestination) =
308314
this?.hierarchy?.any {

0 commit comments

Comments
 (0)