Simplify Adaptive Scaffold

Change-Id: I2775e308de426395d92a2ffd3673f7e4f41f82ca
feature/adaptive_scaffold
Miłosz Moczkowski 1 year ago
parent 2b828655d8
commit 3a030c82ba

@ -16,15 +16,13 @@
package com.google.samples.apps.nowinandroid.core.ui package com.google.samples.apps.nowinandroid.core.ui
import android.app.Activity
import android.content.Context
import android.content.ContextWrapper
import androidx.compose.animation.AnimatedVisibility import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.core.animateFloatAsState import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.tween import androidx.compose.animation.core.tween
import androidx.compose.animation.slideInHorizontally import androidx.compose.animation.slideInHorizontally
import androidx.compose.animation.slideOutHorizontally import androidx.compose.animation.slideOutHorizontally
import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.PaddingValues import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.WindowInsets import androidx.compose.foundation.layout.WindowInsets
@ -58,10 +56,8 @@ import androidx.compose.runtime.movableContentOf
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.testTag import androidx.compose.ui.platform.testTag
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.window.layout.WindowMetricsCalculator
class AdaptiveScaffoldNavigationComponentColors internal constructor( class AdaptiveScaffoldNavigationComponentColors internal constructor(
val detailsPaneContainerColor: Color, val detailsPaneContainerColor: Color,
@ -161,68 +157,63 @@ fun <T> AdaptiveScaffold(
detailsPane: @Composable () -> Unit = {}, detailsPane: @Composable () -> Unit = {},
content: @Composable (padding: PaddingValues) -> Unit, content: @Composable (padding: PaddingValues) -> Unit,
) { ) {
val context = LocalContext.current BoxWithConstraints(modifier = modifier) {
val activity = context.getActivity() ?: throw IllegalStateException("Activity not found") val movableContent = remember(content) {
val metrics = WindowMetricsCalculator.getOrCreate().computeCurrentWindowMetrics(activity) movableContentOf(content)
val widthDp = metrics.bounds.width() / context.resources.displayMetrics.density }
val movableContent = remember(content) { when {
movableContentOf(content) maxWidth >= 1240.dp -> DrawerScaffold(
} navigationItems = navigationItems,
when { navigationItemTitle = navigationItemTitle,
widthDp >= 1240f -> DrawerScaffold( navigationItemIcon = navigationItemIcon,
modifier = modifier, isItemSelected = isItemSelected,
navigationItems = navigationItems, onNavigationItemClick = onNavigationItemClick,
navigationItemTitle = navigationItemTitle, topBar = topBar,
navigationItemIcon = navigationItemIcon, snackbarHost = snackbarHost,
isItemSelected = isItemSelected, colors = colors,
onNavigationItemClick = onNavigationItemClick, contentWindowInsets = contentWindowInsets,
topBar = topBar, isDetailsPaneVisible = isDetailsPaneVisible,
snackbarHost = snackbarHost, detailsPane = detailsPane,
colors = colors, content = movableContent,
contentWindowInsets = contentWindowInsets, )
isDetailsPaneVisible = isDetailsPaneVisible,
detailsPane = detailsPane,
content = movableContent,
)
widthDp >= 600f -> RailScaffold( maxWidth >= 600.dp -> RailScaffold(
modifier = modifier, navigationItems = navigationItems,
navigationItems = navigationItems, navigationItemTitle = navigationItemTitle,
navigationItemTitle = navigationItemTitle, navigationItemIcon = navigationItemIcon,
navigationItemIcon = navigationItemIcon, isItemSelected = isItemSelected,
isItemSelected = isItemSelected, onNavigationItemClick = onNavigationItemClick,
onNavigationItemClick = onNavigationItemClick, topBar = topBar,
topBar = topBar, snackbarHost = snackbarHost,
snackbarHost = snackbarHost, colors = colors,
colors = colors, contentWindowInsets = contentWindowInsets,
contentWindowInsets = contentWindowInsets, isDetailsPaneVisible = isDetailsPaneVisible,
isDetailsPaneVisible = isDetailsPaneVisible, detailsPane = detailsPane,
detailsPane = detailsPane, content = movableContent,
content = movableContent, )
)
else -> BottomBarScaffold( else -> BottomBarScaffold(
modifier = modifier, navigationItems = navigationItems,
navigationItems = navigationItems, navigationItemTitle = navigationItemTitle,
navigationItemTitle = navigationItemTitle, navigationItemIcon = navigationItemIcon,
navigationItemIcon = navigationItemIcon, isItemSelected = isItemSelected,
isItemSelected = isItemSelected, onNavigationItemClick = onNavigationItemClick,
onNavigationItemClick = onNavigationItemClick, topBar = topBar,
topBar = topBar, snackbarHost = snackbarHost,
snackbarHost = snackbarHost, colors = colors,
colors = colors, contentWindowInsets = contentWindowInsets,
contentWindowInsets = contentWindowInsets, isDetailsPaneVisible = isDetailsPaneVisible,
isDetailsPaneVisible = isDetailsPaneVisible, detailsPane = detailsPane,
detailsPane = detailsPane, content = movableContent,
content = movableContent, )
) }
} }
} }
@OptIn(ExperimentalMaterial3Api::class) @OptIn(ExperimentalMaterial3Api::class)
@Composable @Composable
private fun <T> BottomBarScaffold( private fun <T> BottomBarScaffold(
modifier: Modifier, modifier: Modifier = Modifier,
navigationItems: List<T>, navigationItems: List<T>,
navigationItemTitle: @Composable (item: T, isSelected: Boolean) -> Unit, navigationItemTitle: @Composable (item: T, isSelected: Boolean) -> Unit,
navigationItemIcon: @Composable (item: T, isSelected: Boolean) -> Unit, navigationItemIcon: @Composable (item: T, isSelected: Boolean) -> Unit,
@ -285,7 +276,7 @@ private fun <T> BottomBarScaffold(
@OptIn(ExperimentalMaterial3Api::class) @OptIn(ExperimentalMaterial3Api::class)
@Composable @Composable
private fun <T> RailScaffold( private fun <T> RailScaffold(
modifier: Modifier, modifier: Modifier = Modifier,
navigationItems: List<T>, navigationItems: List<T>,
navigationItemTitle: @Composable (item: T, isSelected: Boolean) -> Unit, navigationItemTitle: @Composable (item: T, isSelected: Boolean) -> Unit,
navigationItemIcon: @Composable (item: T, isSelected: Boolean) -> Unit, navigationItemIcon: @Composable (item: T, isSelected: Boolean) -> Unit,
@ -354,7 +345,7 @@ private fun <T> RailScaffold(
@OptIn(ExperimentalMaterial3Api::class) @OptIn(ExperimentalMaterial3Api::class)
@Composable @Composable
private fun <T> DrawerScaffold( private fun <T> DrawerScaffold(
modifier: Modifier, modifier: Modifier = Modifier,
navigationItems: List<T>, navigationItems: List<T>,
navigationItemTitle: @Composable (item: T, isSelected: Boolean) -> Unit, navigationItemTitle: @Composable (item: T, isSelected: Boolean) -> Unit,
navigationItemIcon: @Composable (item: T, isSelected: Boolean) -> Unit, navigationItemIcon: @Composable (item: T, isSelected: Boolean) -> Unit,
@ -427,9 +418,3 @@ private fun <T> DrawerScaffold(
} }
} }
} }
private fun Context.getActivity(): Activity? = when (this) {
is Activity -> this
is ContextWrapper -> baseContext.getActivity()
else -> null
}

Loading…
Cancel
Save