library/ui: add DetailScaffold, FormComponents, TabbedPagerScaffold, DashboardSection, AddToListDialog; add SettingsSelectRow/SettingsExposedSelectRow
- Commit
ad4bffa10b1e741d4d08799faa732b7a92d22156- Author
- vayun-mathur <[email protected]>
- When
- 2026-08-15 17:33 UTC (3 months ago)
- Parents
7b4046c
Diff
library/ui/src/main/java/com/vayunmathur/library/ui/AddToListDialog.kt
diff --git a/library/ui/src/main/java/com/vayunmathur/library/ui/AddToListDialog.kt b/library/ui/src/main/java/com/vayunmathur/library/ui/AddToListDialog.ktnew file mode 100644index 0000000000..9092ee025a--- /dev/null+++ b/library/ui/src/main/java/com/vayunmathur/library/ui/AddToListDialog.kt@@ -0,0 +1,107 @@+package com.vayunmathur.library.ui++import androidx.compose.foundation.clickable+import androidx.compose.foundation.layout.Column+import androidx.compose.foundation.layout.Row+import androidx.compose.foundation.layout.fillMaxWidth+import androidx.compose.foundation.lazy.LazyColumn+import androidx.compose.foundation.lazy.items+import androidx.compose.runtime.Composable+import androidx.compose.runtime.getValue+import androidx.compose.runtime.mutableStateMapOf+import androidx.compose.runtime.mutableStateOf+import androidx.compose.runtime.remember+import androidx.compose.runtime.setValue+import androidx.compose.ui.Alignment+import androidx.compose.ui.Modifier++/**+ * "Add to …" picker: a titled dialog listing [options] with a checkbox each,+ * plus an optional inline "create new" field, confirmed with an OK button.+ *+ * Music and YouPipe had each written their own add-to-playlist dialog - one an+ * [AlertDialog] with single-select radios and a separate create dialog, the+ * other a hand-built `Dialog`+`Card` with live-toggling checkboxes and an inline+ * field. This is the shared shape: checkboxes staged locally and applied on OK,+ * with the create field built in. It is generic over the item type so anything+ * that means "pick some of these, optionally make a new one" can use it.+ *+ * Selection is staged: [onConfirm] receives the finally-checked set. Callers+ * that track prior membership (a video already in some playlists) diff the+ * result against their own initial state to know what to add and remove.+ *+ * [initiallyChecked] seeds a row's checkbox; staged toggles survive [options]+ * updating (e.g. after a create) rather than resetting.+ */+@Composable+fun <T> AddToListDialog(+ title: String,+ options: List<T>,+ itemLabel: (T) -> String,+ confirmLabel: String,+ dismissLabel: String,+ onConfirm: (Set<T>) -> Unit,+ onDismiss: () -> Unit,+ modifier: Modifier = Modifier,+ initiallyChecked: (T) -> Boolean = { false },+ itemKey: (T) -> Any = { it as Any },+ createLabel: String? = null,+ onCreate: ((String) -> Unit)? = null,+ canCreate: (String) -> Boolean = { it.isNotBlank() },+) {+ // Keyed by itemKey rather than rebuilt from [options], so an item added+ // mid-dialog gets its initial state while existing staged toggles persist.+ val checked = remember { mutableStateMapOf<Any, Boolean>() }+ fun isChecked(item: T) = checked[itemKey(item)] ?: initiallyChecked(item)++ var newName by remember { mutableStateOf("") }++ AlertDialog(+ onDismissRequest = onDismiss,+ modifier = modifier,+ title = { Text(title) },+ text = {+ Column {+ LazyColumn(Modifier.weight(1f, fill = false)) {+ items(options, key = { itemKey(it) }) { item ->+ val itemChecked = isChecked(item)+ ListItem(+ content = { Text(itemLabel(item)) },+ trailingContent = {+ Checkbox(+ checked = itemChecked,+ onCheckedChange = { checked[itemKey(item)] = it },+ )+ },+ modifier = Modifier.clickable { checked[itemKey(item)] = !itemChecked },+ )+ }+ }+ if (createLabel != null && onCreate != null) {+ Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) {+ OutlinedTextField(+ value = newName,+ onValueChange = { newName = it },+ label = { Text(createLabel) },+ singleLine = true,+ modifier = Modifier.weight(1f),+ )+ IconButton(+ onClick = {+ onCreate(newName.trim())+ newName = ""+ },+ enabled = canCreate(newName),+ ) { IconAdd() }+ }+ }+ }+ },+ confirmButton = {+ TextButton(onClick = { onConfirm(options.filter { isChecked(it) }.toSet()) }) {+ Text(confirmLabel)+ }+ },+ dismissButton = { TextButton(onClick = onDismiss) { Text(dismissLabel) } },+ )+}
library/ui/src/main/java/com/vayunmathur/library/ui/DashboardSection.kt
diff --git a/library/ui/src/main/java/com/vayunmathur/library/ui/DashboardSection.kt b/library/ui/src/main/java/com/vayunmathur/library/ui/DashboardSection.ktnew file mode 100644index 0000000000..4a58cc1c92--- /dev/null+++ b/library/ui/src/main/java/com/vayunmathur/library/ui/DashboardSection.kt@@ -0,0 +1,117 @@+package com.vayunmathur.library.ui++import androidx.compose.foundation.layout.Column+import androidx.compose.foundation.layout.ColumnScope+import androidx.compose.foundation.layout.Row+import androidx.compose.foundation.layout.Spacer+import androidx.compose.foundation.layout.fillMaxWidth+import androidx.compose.foundation.layout.padding+import androidx.compose.foundation.layout.size+import androidx.compose.foundation.layout.width+import androidx.compose.foundation.shape.RoundedCornerShape+import androidx.compose.runtime.Composable+import androidx.compose.ui.Alignment+import androidx.compose.ui.Modifier+import androidx.compose.ui.graphics.Color+import androidx.compose.ui.text.font.FontWeight+import androidx.compose.ui.unit.Dp+import androidx.compose.ui.unit.dp++/**+ * The titled card sections a dashboard/home screen is built from.+ *+ * Weather, the app store, the games hub, travel and health had each grown their+ * own `SectionHeader` + rounded `Surface` card combo, drifting on corner radius,+ * container colour and the "See all" affordance. This promotes health's+ * already-factored version to the one shared here.+ */++/**+ * A section heading: a [titleSmall] title, an optional leading icon, and an+ * optional trailing text button (the "See all" affordance).+ *+ * [leadingIcon] receives the [Modifier] and tint to draw with so the caller can+ * pass any of the shared `IconXyz` composables.+ */+@Composable+fun SectionHeader(+ title: String,+ modifier: Modifier = Modifier,+ actionLabel: String? = null,+ onAction: (() -> Unit)? = null,+ leadingIcon: (@Composable (Modifier, Color) -> Unit)? = null,+ accentColor: Color? = null,+) {+ val textColor = accentColor?.copy(alpha = 0.85f) ?: MaterialTheme.colorScheme.onSurfaceVariant+ val iconColor = accentColor ?: MaterialTheme.colorScheme.onSurfaceVariant+ Row(+ modifier = modifier+ .fillMaxWidth()+ .padding(horizontal = Spacing.lg, vertical = Spacing.sm),+ verticalAlignment = Alignment.CenterVertically,+ ) {+ if (leadingIcon != null) {+ leadingIcon(Modifier.size(16.dp), iconColor)+ Spacer(Modifier.width(Spacing.sm))+ }+ Text(+ text = title,+ style = MaterialTheme.typography.titleSmall,+ fontWeight = FontWeight.Bold,+ color = textColor,+ modifier = Modifier.weight(1f),+ )+ if (actionLabel != null && onAction != null) {+ TextButton(onClick = onAction) { Text(actionLabel) }+ }+ }+}++/**+ * A dashboard card: an optional [SectionHeader] over a rounded `Surface`+ * (`surfaceContainerLow`) whose children stack in a [Column].+ *+ * The card is inset from the screen edge by [Spacing.lg]; rows inside supply+ * their own padding (or use [DashboardSectionDivider] between them).+ */+@Composable+fun DashboardSection(+ modifier: Modifier = Modifier,+ title: String? = null,+ actionLabel: String? = null,+ onAction: (() -> Unit)? = null,+ leadingIcon: (@Composable (Modifier, Color) -> Unit)? = null,+ accentColor: Color? = null,+ content: @Composable ColumnScope.() -> Unit,+) {+ Column(modifier = modifier.fillMaxWidth()) {+ if (title != null) {+ SectionHeader(+ title = title,+ actionLabel = actionLabel,+ onAction = onAction,+ leadingIcon = leadingIcon,+ accentColor = accentColor,+ )+ }+ Surface(+ modifier = Modifier+ .fillMaxWidth()+ .padding(horizontal = Spacing.lg),+ shape = RoundedCornerShape(16.dp),+ color = MaterialTheme.colorScheme.surfaceContainerLow,+ ) {+ Column(modifier = Modifier.fillMaxWidth(), content = content)+ }+ }+}++/** Thin inset divider between rows inside a [DashboardSection]. */+@Composable+fun DashboardSectionDivider(insetStart: Dp = 56.dp) {+ HorizontalDivider(+ modifier = Modifier.padding(start = insetStart),+ thickness = 0.5.dp,+ color = MaterialTheme.colorScheme.outlineVariant.copy(alpha = 0.5f),+ )+}
library/ui/src/main/java/com/vayunmathur/library/ui/DetailScaffold.kt
diff --git a/library/ui/src/main/java/com/vayunmathur/library/ui/DetailScaffold.kt b/library/ui/src/main/java/com/vayunmathur/library/ui/DetailScaffold.ktnew file mode 100644index 0000000000..0c90cbad8f--- /dev/null+++ b/library/ui/src/main/java/com/vayunmathur/library/ui/DetailScaffold.kt@@ -0,0 +1,218 @@+package com.vayunmathur.library.ui++import androidx.compose.foundation.layout.Arrangement+import androidx.compose.foundation.layout.Column+import androidx.compose.foundation.layout.ColumnScope+import androidx.compose.foundation.layout.PaddingValues+import androidx.compose.foundation.layout.RowScope+import androidx.compose.foundation.layout.calculateEndPadding+import androidx.compose.foundation.layout.calculateStartPadding+import androidx.compose.foundation.layout.fillMaxSize+import androidx.compose.foundation.layout.padding+import androidx.compose.foundation.lazy.LazyColumn+import androidx.compose.foundation.lazy.LazyListScope+import androidx.compose.foundation.rememberScrollState+import androidx.compose.foundation.verticalScroll+import androidx.compose.runtime.Composable+import androidx.compose.ui.Modifier+import androidx.compose.ui.platform.LocalLayoutDirection+import com.vayunmathur.library.util.NavBackStack+import com.vayunmathur.library.util.NavKey++/**+ * The scaffold for a detail screen - one whose body is a vertically scrolling+ * stack of blocks rather than a flat list (album header + tracks, a config's+ * fields, a contact's cards).+ *+ * [ListPage] covers list screens and [AppScaffold] covers everything with a bar+ * and free-form content; this fills the gap that every detail screen was closing+ * by hand: `AppScaffold { pad -> Column(fillMaxSize().padding(pad)+ * .padding(horizontal = 16.dp).verticalScroll(...), spacedBy(12.dp)) { ... } }`.+ * The horizontal inset ([Spacing.lg]) and the gap between blocks ([Spacing.md])+ * are normalised here so detail screens stop drifting apart.+ *+ * The content lambda runs in a [ColumnScope] that is already scrolling; use+ * [DetailLazyColumn] instead when the body is itself a long list that should+ * lazily compose.+ */+@Composable+fun DetailScaffold(+ title: String,+ modifier: Modifier = Modifier,+ onNavigateBack: (() -> Unit)? = null,+ alignment: AppBarAlignment = AppBarAlignment.Start,+ actions: @Composable RowScope.() -> Unit = {},+ bottomBar: @Composable () -> Unit = {},+ content: @Composable ColumnScope.() -> Unit,+) = AppScaffold(+ title = title,+ modifier = modifier,+ onNavigateBack = onNavigateBack,+ alignment = alignment,+ actions = actions,+ bottomBar = bottomBar,+) { pad ->+ DetailColumn(pad, content)+}++/** [DetailScaffold] with a title slot, for a styled or truncated heading. */+@Composable+fun DetailScaffold(+ title: @Composable () -> Unit,+ modifier: Modifier = Modifier,+ onNavigateBack: (() -> Unit)? = null,+ alignment: AppBarAlignment = AppBarAlignment.Start,+ actions: @Composable RowScope.() -> Unit = {},+ bottomBar: @Composable () -> Unit = {},+ content: @Composable ColumnScope.() -> Unit,+) = AppScaffold(+ title = title,+ modifier = modifier,+ onNavigateBack = onNavigateBack,+ alignment = alignment,+ actions = actions,+ bottomBar = bottomBar,+) { pad ->+ DetailColumn(pad, content)+}++/** [DetailScaffold] for a screen that owns a back stack, wiring the back button to it. */+@Composable+fun <T : NavKey> DetailScaffold(+ title: String,+ backStack: NavBackStack<T>,+ modifier: Modifier = Modifier,+ alignment: AppBarAlignment = AppBarAlignment.Start,+ actions: @Composable RowScope.() -> Unit = {},+ bottomBar: @Composable () -> Unit = {},+ content: @Composable ColumnScope.() -> Unit,+) = DetailScaffold(+ title = title,+ modifier = modifier,+ onNavigateBack = { backStack.pop() },+ alignment = alignment,+ actions = actions,+ bottomBar = bottomBar,+ content = content,+)++/**+ * The detail scaffold whose body is a lazily composed list - album track lists,+ * long grouped detail screens. Same bars and insets as [DetailScaffold], but the+ * scaffold padding is forwarded as the list's `contentPadding` (with the+ * horizontal margin folded in) so items can scroll under the bars.+ */+@Composable+fun DetailLazyColumn(+ title: String,+ modifier: Modifier = Modifier,+ onNavigateBack: (() -> Unit)? = null,+ alignment: AppBarAlignment = AppBarAlignment.Start,+ actions: @Composable RowScope.() -> Unit = {},+ bottomBar: @Composable () -> Unit = {},+ content: LazyListScope.() -> Unit,+) = AppScaffold(+ title = title,+ modifier = modifier,+ onNavigateBack = onNavigateBack,+ alignment = alignment,+ actions = actions,+ bottomBar = bottomBar,+) { pad ->+ DetailList(pad, content)+}++/** [DetailLazyColumn] with a title slot. */+@Composable+fun DetailLazyColumn(+ title: @Composable () -> Unit,+ modifier: Modifier = Modifier,+ onNavigateBack: (() -> Unit)? = null,+ alignment: AppBarAlignment = AppBarAlignment.Start,+ actions: @Composable RowScope.() -> Unit = {},+ bottomBar: @Composable () -> Unit = {},+ content: LazyListScope.() -> Unit,+) = AppScaffold(+ title = title,+ modifier = modifier,+ onNavigateBack = onNavigateBack,+ alignment = alignment,+ actions = actions,+ bottomBar = bottomBar,+) { pad ->+ DetailList(pad, content)+}++/** [DetailLazyColumn] for a screen that owns a back stack. */+@Composable+fun <T : NavKey> DetailLazyColumn(+ title: String,+ backStack: NavBackStack<T>,+ modifier: Modifier = Modifier,+ alignment: AppBarAlignment = AppBarAlignment.Start,+ actions: @Composable RowScope.() -> Unit = {},+ bottomBar: @Composable () -> Unit = {},+ content: LazyListScope.() -> Unit,+) = DetailLazyColumn(+ title = title,+ modifier = modifier,+ onNavigateBack = { backStack.pop() },+ alignment = alignment,+ actions = actions,+ bottomBar = bottomBar,+ content = content,+)++@Composable+private fun DetailColumn(pad: PaddingValues, content: @Composable ColumnScope.() -> Unit) {+ Column(+ modifier = Modifier+ .fillMaxSize()+ .padding(pad)+ .padding(horizontal = Spacing.lg)+ .verticalScroll(rememberScrollState()),+ verticalArrangement = Arrangement.spacedBy(Spacing.md),+ content = content,+ )+}++@Composable+private fun DetailList(pad: PaddingValues, content: LazyListScope.() -> Unit) {+ val direction = LocalLayoutDirection.current+ LazyColumn(+ modifier = Modifier.fillMaxSize(),+ contentPadding = PaddingValues(+ start = pad.calculateStartPadding(direction) + Spacing.lg,+ end = pad.calculateEndPadding(direction) + Spacing.lg,+ top = pad.calculateTopPadding(),+ bottom = pad.calculateBottomPadding(),+ ),+ verticalArrangement = Arrangement.spacedBy(Spacing.md),+ content = content,+ )+}++/**+ * A titled sub-block inside a [DetailScaffold] - an optional [titleSmall]+ * heading over its content. Pass a null [title] for an untitled group.+ */+@Composable+fun DetailSection(+ title: String?,+ modifier: Modifier = Modifier,+ content: @Composable ColumnScope.() -> Unit,+) {+ Column(+ modifier = modifier,+ verticalArrangement = Arrangement.spacedBy(Spacing.sm),+ ) {+ if (title != null) {+ Text(+ title,+ style = MaterialTheme.typography.titleSmall,+ color = MaterialTheme.colorScheme.primary,+ )+ }+ content()+ }+}
library/ui/src/main/java/com/vayunmathur/library/ui/FormComponents.kt
diff --git a/library/ui/src/main/java/com/vayunmathur/library/ui/FormComponents.kt b/library/ui/src/main/java/com/vayunmathur/library/ui/FormComponents.ktnew file mode 100644index 0000000000..11290dd5ab--- /dev/null+++ b/library/ui/src/main/java/com/vayunmathur/library/ui/FormComponents.kt@@ -0,0 +1,97 @@+package com.vayunmathur.library.ui++import androidx.compose.foundation.layout.Arrangement+import androidx.compose.foundation.layout.Column+import androidx.compose.foundation.layout.ColumnScope+import androidx.compose.foundation.layout.fillMaxWidth+import androidx.compose.foundation.layout.padding+import androidx.compose.foundation.text.KeyboardOptions+import androidx.compose.runtime.Composable+import androidx.compose.ui.Modifier+import androidx.compose.ui.text.input.KeyboardType+import androidx.compose.ui.text.input.VisualTransformation++/**+ * The building blocks of a form screen.+ *+ * Editing screens were each assembling the same shapes by hand: a padded+ * [Column] with `Spacer`s of drifting heights between fields, and an+ * `OutlinedTextField(..., modifier = Modifier.fillMaxWidth())` written out in+ * full every time. [FormSection] fixes the outer padding and the gap between+ * fields ([Spacing.lg] / [Spacing.md]); [LabeledTextField] is the full-width+ * field with a label that a form actually wants.+ *+ * Kept thin on purpose - a form is a stack of labelled fields in sections;+ * anything richer should still be written by hand.+ */++/**+ * A titled group of form fields.+ *+ * Pass [title] as null for an untitled leading group. The section owns its+ * outer padding and the vertical gap between children, so callers put fields+ * straight inside without spacers.+ */+@Composable+fun FormSection(+ modifier: Modifier = Modifier,+ title: String? = null,+ content: @Composable ColumnScope.() -> Unit,+) {+ Column(+ modifier = modifier.padding(Spacing.lg),+ verticalArrangement = Arrangement.spacedBy(Spacing.md),+ ) {+ if (title != null) {+ Text(+ title,+ style = MaterialTheme.typography.titleSmall,+ color = MaterialTheme.colorScheme.primary,+ )+ }+ content()+ }+}++/**+ * A labelled text field that fills its width.+ *+ * The default [modifier] is `fillMaxWidth`, which is what every form field+ * wanted and had to spell out; pass a modifier to override it. This collapses+ * the repeated `OutlinedTextField(...) + Spacer(height)` pairs down to one call+ * inside a [FormSection].+ */+@Composable+fun LabeledTextField(+ value: String,+ onValueChange: (String) -> Unit,+ label: String,+ modifier: Modifier = Modifier.fillMaxWidth(),+ enabled: Boolean = true,+ readOnly: Boolean = false,+ singleLine: Boolean = true,+ keyboardType: KeyboardType = KeyboardType.Text,+ visualTransformation: VisualTransformation = VisualTransformation.None,+ isError: Boolean = false,+ supportingText: String? = null,+ placeholder: String? = null,+ leadingIcon: (@Composable () -> Unit)? = null,+ trailingIcon: (@Composable () -> Unit)? = null,+) {+ OutlinedTextField(+ value = value,+ onValueChange = onValueChange,+ modifier = modifier,+ enabled = enabled,+ readOnly = readOnly,+ label = { Text(label) },+ placeholder = placeholder?.let { { Text(it) } },+ leadingIcon = leadingIcon,+ trailingIcon = trailingIcon,+ supportingText = supportingText?.let { { Text(it) } },+ isError = isError,+ visualTransformation = visualTransformation,+ keyboardOptions = KeyboardOptions(keyboardType = keyboardType),+ singleLine = singleLine,+ )+}
library/ui/src/main/java/com/vayunmathur/library/ui/SettingsComponents.kt
diff --git a/library/ui/src/main/java/com/vayunmathur/library/ui/SettingsComponents.kt b/library/ui/src/main/java/com/vayunmathur/library/ui/SettingsComponents.ktindex 15735bd2f5..71fad7ed52 100644--- a/library/ui/src/main/java/com/vayunmathur/library/ui/SettingsComponents.kt+++ b/library/ui/src/main/java/com/vayunmathur/library/ui/SettingsComponents.kt@@ -1,7 +1,9 @@ package com.vayunmathur.library.ui import androidx.compose.foundation.clickable+import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column+import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.padding import androidx.compose.material3.HorizontalDivider import androidx.compose.material3.ListItemDefaults@@ -9,6 +11,10 @@ import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Switch import androidx.compose.material3.Text import androidx.compose.runtime.Composable+import androidx.compose.runtime.getValue+import androidx.compose.runtime.mutableStateOf+import androidx.compose.runtime.remember+import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.unit.dp@@ -131,3 +137,115 @@ fun SettingsDivider(modifier: Modifier = Modifier, inset: Boolean = true) { color = MaterialTheme.colorScheme.outlineVariant.copy(alpha = 0.5f), ) }++/**+ * A settings row that picks one of [options].+ *+ * Reads as a [SettingsRow] whose supporting text is the current selection and+ * whose trailing affordance is a dropdown arrow; tapping the row opens a+ * [DropdownMenu] of the options, the selected one ticked. This is the one row+ * type the settings family was missing, so "pick a value" settings were each+ * hand-rolling a menu. Its shape matches [SettingsRow]/[SettingsSwitchRow] so a+ * screen stays a uniform list of rows.+ */+@Composable+fun <T> SettingsSelectRow(+ title: String,+ selected: T,+ options: List<T>,+ label: (T) -> String,+ onSelect: (T) -> Unit,+ modifier: Modifier = Modifier,+ supportingText: String? = null,+ enabled: Boolean = true,+ leadingContent: @Composable (() -> Unit)? = null,+) {+ var expanded by remember { mutableStateOf(false) }+ Box {+ SettingsRow(+ title = title,+ modifier = modifier,+ supportingText = supportingText ?: label(selected),+ enabled = enabled,+ onClick = { expanded = true },+ leadingContent = leadingContent,+ trailingContent = { IconArrowDropDown() },+ )+ DropdownMenu(expanded = expanded, onDismissRequest = { expanded = false }) {+ options.forEach { option ->+ DropdownMenuItem(+ text = { Text(label(option)) },+ onClick = {+ expanded = false+ onSelect(option)+ },+ trailingIcon = if (option == selected) ({ IconCheck() }) else null,+ )+ }+ }+ }+}++/**+ * The dense, form-style select: a read-only [OutlinedTextField] with an+ * [ExposedDropdownMenuBox], for settings that read like a form field rather than+ * a list row (the camera codec/audio pickers). Prefer [SettingsSelectRow] for+ * ordinary settings lists.+ *+ * [itemSupporting] adds a second line under each option (e.g. a codec's+ * description).+ */+@OptIn(ExperimentalMaterial3Api::class)+@Composable+fun <T> SettingsExposedSelectRow(+ label: String,+ selected: T,+ options: List<T>,+ itemLabel: (T) -> String,+ onSelect: (T) -> Unit,+ modifier: Modifier = Modifier,+ itemSupporting: ((T) -> String?)? = null,+ enabled: Boolean = true,+) {+ var expanded by remember { mutableStateOf(false) }+ ExposedDropdownMenuBox(+ expanded = expanded,+ onExpandedChange = { if (enabled) expanded = it },+ modifier = modifier,+ ) {+ OutlinedTextField(+ value = itemLabel(selected),+ onValueChange = {},+ readOnly = true,+ enabled = enabled,+ label = { Text(label) },+ trailingIcon = { ExposedDropdownMenuDefaults.TrailingIcon(expanded = expanded) },+ modifier = Modifier+ .fillMaxWidth()+ .menuAnchor(ExposedDropdownMenuAnchorType.PrimaryNotEditable),+ )+ ExposedDropdownMenu(expanded = expanded, onDismissRequest = { expanded = false }) {+ options.forEach { option ->+ DropdownMenuItem(+ text = {+ Column {+ Text(itemLabel(option))+ val supporting = itemSupporting?.invoke(option)+ if (supporting != null) {+ Text(+ supporting,+ style = MaterialTheme.typography.bodySmall,+ color = MaterialTheme.colorScheme.onSurfaceVariant,+ )+ }+ }+ },+ onClick = {+ expanded = false+ onSelect(option)+ },+ )+ }+ }+ }+}
library/ui/src/main/java/com/vayunmathur/library/ui/TabbedPagerScaffold.kt
diff --git a/library/ui/src/main/java/com/vayunmathur/library/ui/TabbedPagerScaffold.kt b/library/ui/src/main/java/com/vayunmathur/library/ui/TabbedPagerScaffold.ktnew file mode 100644index 0000000000..4dc8662a89--- /dev/null+++ b/library/ui/src/main/java/com/vayunmathur/library/ui/TabbedPagerScaffold.kt@@ -0,0 +1,144 @@+package com.vayunmathur.library.ui++import androidx.compose.foundation.horizontalScroll+import androidx.compose.foundation.layout.Arrangement+import androidx.compose.foundation.layout.Column+import androidx.compose.foundation.layout.Row+import androidx.compose.foundation.layout.fillMaxWidth+import androidx.compose.foundation.layout.padding+import androidx.compose.foundation.pager.HorizontalPager+import androidx.compose.foundation.pager.PagerState+import androidx.compose.foundation.pager.rememberPagerState+import androidx.compose.foundation.rememberScrollState+import androidx.compose.runtime.Composable+import androidx.compose.runtime.rememberCoroutineScope+import androidx.compose.ui.Modifier+import com.vayunmathur.library.util.BottomNavBar+import com.vayunmathur.library.util.BottomNavBarItem+import kotlinx.coroutines.launch++/** One tab in a [TabbedPagerScaffold]: a label, an optional icon, and its page. */+data class PagerTab(+ val label: String,+ val icon: (@Composable () -> Unit)? = null,+ val content: @Composable () -> Unit,+)++/** How [TabbedPagerScaffold] presents its tabs. */+enum class TabStyle {+ /** A [BottomNavBar] at the bottom - the app-level "main tabs" case (music). */+ BottomNav,++ /** A [PrimaryTabRow] under the top bar - the in-screen tabs case. */+ PrimaryTabRow,+}++/**+ * A tab host that swipes: a set of [tabs] shown in a [HorizontalPager], kept in+ * sync with either a bottom navigation bar or a top tab row.+ *+ * Music and calendar had written out the same plumbing - a+ * [rememberCoroutineScope] used only to `animateScrollToPage`, the pager, and+ * the fiddly business of forwarding just the bottom inset so an inner page's own+ * top bar is not pushed down. That is all this owns; app-specific pagers (a+ * date-anchored calendar, say) stay hand-written.+ *+ * [leadingBottomBar] sits above the tab bar in [TabStyle.BottomNav] (music's+ * now-playing controls) and is the plain bottom bar in [TabStyle.PrimaryTabRow].+ */+@Composable+fun TabbedPagerScaffold(+ tabs: List<PagerTab>,+ modifier: Modifier = Modifier,+ pagerState: PagerState = rememberPagerState(pageCount = { tabs.size }),+ tabStyle: TabStyle = TabStyle.BottomNav,+ topBar: @Composable () -> Unit = {},+ leadingBottomBar: @Composable () -> Unit = {},+) {+ val scope = rememberCoroutineScope()+ val select: (Int) -> Unit = { index ->+ if (pagerState.currentPage != index) scope.launch { pagerState.animateScrollToPage(index) }+ }++ when (tabStyle) {+ TabStyle.BottomNav -> Scaffold(+ modifier = modifier,+ topBar = topBar,+ bottomBar = {+ Column(Modifier.fillMaxWidth()) {+ leadingBottomBar()+ BottomNavBar {+ tabs.forEachIndexed { index, tab ->+ BottomNavBarItem(+ selected = pagerState.currentPage == index,+ onClick = { select(index) },+ icon = tab.icon ?: {},+ label = tab.label,+ )+ }+ }+ }+ },+ ) { padding ->+ // Inner pages own their top bar and consume the top inset, so only+ // the bottom space this scaffold's bars take is forwarded - adding+ // the full padding here would inset the status bar twice.+ HorizontalPager(+ state = pagerState,+ modifier = Modifier.padding(bottom = padding.calculateBottomPadding()),+ ) { page -> tabs[page].content() }+ }++ TabStyle.PrimaryTabRow -> Scaffold(+ modifier = modifier,+ topBar = {+ Column(Modifier.fillMaxWidth()) {+ topBar()+ PrimaryTabRow(selectedTabIndex = pagerState.currentPage) {+ tabs.forEachIndexed { index, tab ->+ Tab(+ selected = pagerState.currentPage == index,+ onClick = { select(index) },+ text = { Text(tab.label) },+ icon = tab.icon,+ )+ }+ }+ }+ },+ bottomBar = leadingBottomBar,+ ) { padding ->+ HorizontalPager(+ state = pagerState,+ modifier = Modifier.padding(padding),+ ) { page -> tabs[page].content() }+ }+ }+}++/**+ * A lightweight, non-swiping tab row of [FilterChip]s - the "product tabs" look+ * (travel). For swipeable page hosting use [TabbedPagerScaffold].+ */+@Composable+fun ChipTabRow(+ tabs: List<String>,+ selected: Int,+ onSelect: (Int) -> Unit,+ modifier: Modifier = Modifier,+) {+ Row(+ modifier = modifier+ .horizontalScroll(rememberScrollState())+ .padding(horizontal = Spacing.lg),+ horizontalArrangement = Arrangement.spacedBy(Spacing.sm),+ ) {+ tabs.forEachIndexed { index, label ->+ FilterChip(+ selected = selected == index,+ onClick = { onSelect(index) },+ label = { Text(label) },+ )+ }+ }+}