Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,19 +1,72 @@
package me.lvca.alba.app.ui.designsystem

import androidx.compose.material3.ListItemColors
import androidx.compose.material3.ListItemDefaults
import androidx.compose.material3.ListItemShapes
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.unit.dp
import me.lvca.alba.app.ui.designsystem.theme.AlbaSpacing

/** Shared measures of [AlbaListGroup]'s segments. */
/** Shared measures, shapes and colours of [AlbaListGroup]'s segments, on top of Material's [ListItemDefaults]. */
internal object AlbaListDefaults {
/** The gap between segments, through which the page shows. */
val SegmentGap = 2.dp
/** The gap between segments, through which the page shows: Material's segmented-list gap. */
val SegmentGap = ListItemDefaults.SegmentedGap

/** One padding for every row and segment, on both sides. */
/** One padding for every row and segment, on both sides: Material's list item start and end padding. */
val Padding = AlbaSpacing.lg

/** The inner corners of a segment; the group's outer corners come from [MaterialTheme.shapes.large]. */
val segmentShape: Shape @Composable get() = MaterialTheme.shapes.extraSmall
/** The space Material's list item leaves between its leading content and its text. */
val LeadingGap = 12.dp

/**
* The inner corners of a segment: Material's segmented list item shape. The group's outer corners come from
* [MaterialTheme.shapes.large], Material's segmented-list container shape.
*/
val segmentShape: Shape @Composable get() = ListItemDefaults.shapes().shape

/**
* A segment's shapes: Material's, which keep small corners at rest and round fully while pressed or focused. A
* checked switch row or a selected row keeps the resting shape, as it keeps its colours (see [colors]). The
* [AlbaListGroup] clips the first and last segments' outer corners, as [ListItemDefaults.segmentedShapes] would,
* because a group's rows are any composables (some conditional) and none knows its index.
*/
@Composable
fun segmentShapes(): ListItemShapes = ListItemDefaults.shapes(selectedShape = segmentShape)

/** The shapes of a row outside a group: [shape] in every state, so a row inside a card or a dialog stays still. */
fun plainShapes(shape: Shape): ListItemShapes = ListItemShapes(shape, shape, shape, shape, shape, shape)

/**
* The colours of an Alba row, on Material's segmented list item colours. [containerColor] is the group's
* ([albaGroupColor]) inside an [AlbaListGroup] and transparent elsewhere. The title and leading content take
* [contentColor], LocalContentColor where the row is drawn, so a caller can recolour a row (the error colour for
* Leave, the viewer's ink) by providing a different content colour. The supporting text and trailing content keep
* Material's onSurfaceVariant. A row looks the same when disabled or selected: callers that dim a disabled row do it
* themselves, and a selected row shows its radio button or check.
*/
@Composable
fun colors(containerColor: Color = albaGroupColor(), contentColor: Color = LocalContentColor.current): ListItemColors {
val stock = ListItemDefaults.segmentedColors()
return ListItemDefaults.segmentedColors(
containerColor = containerColor,
contentColor = contentColor,
leadingContentColor = contentColor,
disabledContainerColor = containerColor,
disabledContentColor = contentColor,
disabledLeadingContentColor = contentColor,
disabledTrailingContentColor = stock.trailingContentColor,
disabledOverlineContentColor = stock.overlineContentColor,
disabledSupportingContentColor = stock.supportingContentColor,
selectedContainerColor = containerColor,
selectedContentColor = contentColor,
selectedLeadingContentColor = contentColor,
selectedTrailingContentColor = stock.trailingContentColor,
selectedOverlineContentColor = stock.overlineContentColor,
selectedSupportingContentColor = stock.supportingContentColor,
)
}
}
53 changes: 29 additions & 24 deletions app/src/main/java/me/lvca/alba/app/ui/designsystem/AlbaListGroup.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.selection.toggleable
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.SegmentedListItem
import androidx.compose.material3.Switch
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
Expand All @@ -22,10 +20,13 @@ import androidx.compose.runtime.compositionLocalOf
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.semantics.heading
import androidx.compose.ui.semantics.role
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.unit.dp
import me.lvca.alba.app.ui.designsystem.theme.AlbaSpacing
Expand All @@ -40,9 +41,11 @@ internal fun Modifier.albaListSegment(): Modifier =

/**
* Android 16's grouped list: an optional [title] header in labelLarge and the primary colour, then [content]'s rows,
* each its own rounded segment on surfaceContainer, separated by small gaps instead of dividers. The group rounds its
* outer corners with the theme's large shape; segments keep small inner corners. There are no chevrons: a row that
* opens a page looks like any other row. Use [AlbaListRow], [AlbaSwitchRow], [AlbaFeatureRow] and, for anything else, [AlbaListItem].
* each its own rounded segment on [albaGroupColor], separated by Material's segmented-list gap instead of dividers.
* The rows are Material's segmented list items. The group rounds its outer corners with the theme's large shape (the
* corners [androidx.compose.material3.ListItemDefaults.segmentedShapes] gives a list's ends); segments keep Material's
* small inner corners. There are no chevrons: a row that opens a page looks like any other row. Use [AlbaListRow],
* [AlbaSwitchRow], [AlbaFeatureRow] and, for anything else, [AlbaListItem].
*/
@Composable
internal fun AlbaListGroup(modifier: Modifier = Modifier, title: String? = null, content: @Composable ColumnScope.() -> Unit) {
Expand Down Expand Up @@ -83,9 +86,10 @@ internal fun AlbaListItem(
}

/**
* A setting that is on or off: the whole row toggles, and the stock Material switch shows the state. [modifier] goes on
* the toggleable row, so a test tag there finds the switch's state. [icon] leads the row, in the primary colour, so a
* list of settings scans by glyph.
* A setting that is on or off: Material's toggleable segmented list item with the stock switch at its end. The whole
* row toggles and reads as one switch; the switch itself is not a second target. [modifier] goes on the toggleable row,
* so a test tag there finds the switch's state. [icon] leads the row, in the primary colour, so a list of settings
* scans by glyph. Inside an [AlbaListGroup] it draws itself as a segment.
*/
@Composable
internal fun AlbaSwitchRow(
Expand All @@ -97,25 +101,26 @@ internal fun AlbaSwitchRow(
enabled: Boolean = true,
icon: ImageVector? = null,
) {
Row(
Modifier.albaListSegment().clip(AlbaListDefaults.segmentShape).heightIn(min = 56.dp)
.toggleable(checked, enabled = enabled, role = Role.Switch, onValueChange = onCheckedChange)
.then(modifier).padding(horizontal = AlbaListDefaults.Padding, vertical = AlbaSpacing.md),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(AlbaSpacing.lg),
) {
val grouped = LocalInAlbaListGroup.current
// Material's toggleable item reads as a checkbox; the outer semantics make it the switch it shows.
SegmentedListItem(
checked = checked,
onCheckedChange = onCheckedChange,
shapes = if (grouped) AlbaListDefaults.segmentShapes() else AlbaListDefaults.plainShapes(RectangleShape),
modifier = Modifier.fillMaxWidth().semantics { role = Role.Switch }.then(modifier),
enabled = enabled,
colors = AlbaListDefaults.colors(containerColor = if (grouped) albaGroupColor() else Color.Transparent),
// The glyph sits in the same 40dp box as an [AlbaListRow]'s leading icon, so titles line up across both kinds.
Row(Modifier.weight(1f), verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(AlbaSpacing.md)) {
icon?.let {
leadingContent = icon?.let {
{
Box(Modifier.size(40.dp), contentAlignment = Alignment.Center) {
Icon(it, null, tint = if (enabled) MaterialTheme.colorScheme.primary else MaterialTheme.colorScheme.onSurfaceVariant)
}
}
Column(Modifier.weight(1f), verticalArrangement = Arrangement.spacedBy(AlbaSpacing.xxs)) {
Text(title, style = MaterialTheme.typography.bodyLarge)
subtitle?.let { Text(it, style = MaterialTheme.typography.bodyMedium, color = MaterialTheme.colorScheme.onSurfaceVariant) }
}
}
Switch(checked, onCheckedChange = null, enabled = enabled)
},
supportingContent = subtitle?.let { { Text(it) } },
trailingContent = { Switch(checked, onCheckedChange = null, enabled = enabled) },
) {
Text(title)
}
}
96 changes: 59 additions & 37 deletions app/src/main/java/me/lvca/alba/app/ui/designsystem/AlbaRows.kt
Original file line number Diff line number Diff line change
@@ -1,49 +1,52 @@
package me.lvca.alba.app.ui.designsystem

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.selection.selectable
import androidx.compose.material3.Icon
import androidx.compose.material3.ListItemDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.SegmentedListItem
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.semantics.role
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.isSpecified
import me.lvca.alba.app.ui.designsystem.theme.AlbaIconSize
import me.lvca.alba.app.ui.designsystem.theme.AlbaSpacing

/**
* Alba's list row: an optional [leading] slot, a title with an optional subtitle, and an optional [trailing] slot.
* Pass [selected] for a radio-style row inside a selectableGroup; otherwise the row is a button, or plain when
* [onClick] is null (its trailing slot holding the action). Inside an [AlbaListGroup] it draws itself as a segment.
* Alba's list row, on Material's segmented list item: an optional [leading] slot, a title with an optional subtitle in
* Material's supporting text style, and an optional [trailing] slot. Pass [selected] for a radio-style row inside a
* selectableGroup; otherwise the row is a button, or plain when [onClick] is null (its trailing slot holding the
* action). Inside an [AlbaListGroup] it draws itself as a segment on the group's colour; elsewhere (a card, a dialog, a
* sheet, the viewer) its container is transparent and it keeps [shape].
*
* The title and leading icon take LocalContentColor, so a caller can dim a disabled row or recolour it
* for the viewer by providing a different content colour.
* for the viewer by providing a different content colour. See [AlbaListDefaults.colors].
*/
@Composable
internal fun AlbaListRow(
title: String,
onClick: (() -> Unit)?,
modifier: Modifier = Modifier,
subtitle: String? = null,
/** Goes on the subtitle's text, so a test can find the supporting text alone. */
/** Goes on the subtitle's text, so a test can find the supporting text alone (in the unmerged tree: the row merges it). */
subtitleModifier: Modifier = Modifier,
/** Drawn under the title in place of a text subtitle, such as a status dot and one word. */
supporting: (@Composable () -> Unit)? = null,
Expand All @@ -53,33 +56,42 @@ internal fun AlbaListRow(
/** The box the leading content is centred in, so titles align across rows; null lets it size itself. */
leadingSize: Dp? = 40.dp,
trailing: (@Composable () -> Unit)? = null,
/** Clips the press ripple. Rows inside an [AlbaCard] keep the card's shape and stay rectangular. */
/** The row's shape outside a group. Rows inside an [AlbaCard] keep the card's shape and stay rectangular. */
shape: Shape = RectangleShape,
contentPadding: PaddingValues = PaddingValues(horizontal = AlbaSpacing.lg, vertical = 14.dp),
gap: Dp = AlbaSpacing.md,
minHeight: Dp = 56.dp,
contentPadding: PaddingValues = ListItemDefaults.ContentPadding,
/** The space between the leading content and the text: Material's 12dp, or more. */
gap: Dp = AlbaListDefaults.LeadingGap,
/** Unspecified keeps Material's heights: 56dp for one line, 72dp for two, 88dp for three. */
minHeight: Dp = Dp.Unspecified,
) {
val interaction = when {
onClick == null -> Modifier
selected != null -> Modifier.selectable(selected, enabled = enabled, role = Role.RadioButton, onClick = onClick)
else -> Modifier.clickable(enabled = enabled, role = Role.Button, onClick = onClick)
}
val grouped = LocalInAlbaListGroup.current
Row(modifier.albaListSegment().fillMaxWidth().clip(if (grouped) AlbaListDefaults.segmentShape else shape).then(interaction)
.heightIn(min = minHeight).padding(contentPadding),
verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(gap)) {
if (leading != null) {
if (leadingSize == null) leading()
else Box(Modifier.size(leadingSize), contentAlignment = Alignment.Center) { leading() }
val shapes = if (grouped) AlbaListDefaults.segmentShapes() else AlbaListDefaults.plainShapes(shape)
val colors = AlbaListDefaults.colors(containerColor = if (grouped) albaGroupColor() else Color.Transparent)
val rowModifier = Modifier.fillMaxWidth().then(if (minHeight.isSpecified) Modifier.heightIn(min = minHeight) else Modifier)
.then(modifier)
val extraGap = (gap - AlbaListDefaults.LeadingGap).coerceAtLeast(0.dp)
val leadingContent: (@Composable () -> Unit)? = leading?.let {
{
Box(Modifier.padding(end = extraGap).then(if (leadingSize != null) Modifier.size(leadingSize) else Modifier),
contentAlignment = Alignment.Center) { it() }
}
Column(Modifier.weight(1f), verticalArrangement = Arrangement.spacedBy(AlbaSpacing.xxs)) {
Text(title, style = MaterialTheme.typography.bodyLarge)
subtitle?.let {
Text(it, subtitleModifier, style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant)
}
val supportingContent: (@Composable () -> Unit)? = if (subtitle == null && supporting == null) null else {
{
Column(verticalArrangement = Arrangement.spacedBy(AlbaSpacing.xxs)) {
subtitle?.let { Text(it, subtitleModifier) }
supporting?.invoke()
}
supporting?.invoke()
}
trailing?.invoke()
}
val headline: @Composable () -> Unit = { Text(title) }
when {
onClick == null -> SegmentedListItem(shapes, rowModifier, enabled, leadingContent, trailing, null, supportingContent,
colors = colors, contentPadding = contentPadding, content = headline)
selected != null -> SegmentedListItem(selected, onClick, shapes, rowModifier, enabled, leadingContent, trailing, null,
supportingContent, colors = colors, contentPadding = contentPadding, content = headline)
else -> SegmentedListItem(onClick, shapes, rowModifier.semantics { role = Role.Button }, enabled, leadingContent, trailing,
null, supportingContent, colors = colors, contentPadding = contentPadding, content = headline)
}
}

Expand All @@ -98,15 +110,25 @@ internal fun AlbaChevron(modifier: Modifier = Modifier) {
Icon(AlbaIcons.ChevronRight, null, modifier.testTag(ALBA_CHEVRON_TAG), tint = MaterialTheme.colorScheme.onSurfaceVariant)
}

/** An explanatory row with an accent icon: onboarding and About use these to describe features. */
/**
* An explanatory row with an accent icon: onboarding and About use these to describe features. It is Material's plain
* segmented list item with the title in titleMedium and the icon at the top, beside the first line.
*/
@Composable
internal fun AlbaFeatureRow(icon: ImageVector, title: String, body: String, modifier: Modifier = Modifier) {
Row(modifier.albaListSegment().fillMaxWidth().padding(AlbaSpacing.lg), horizontalArrangement = Arrangement.spacedBy(AlbaSpacing.lg)) {
Icon(icon, null, Modifier.padding(top = AlbaSpacing.xxs).size(AlbaIconSize.standard), tint = MaterialTheme.colorScheme.primary)
Column(Modifier.weight(1f), verticalArrangement = Arrangement.spacedBy(AlbaSpacing.xs)) {
Text(title, style = MaterialTheme.typography.titleMedium)
Text(body, style = MaterialTheme.typography.bodyMedium, color = MaterialTheme.colorScheme.onSurfaceVariant)
}
val grouped = LocalInAlbaListGroup.current
SegmentedListItem(
shapes = if (grouped) AlbaListDefaults.segmentShapes() else AlbaListDefaults.plainShapes(RectangleShape),
modifier = modifier.fillMaxWidth(),
leadingContent = {
Icon(icon, null, Modifier.padding(top = AlbaSpacing.xxs).size(AlbaIconSize.standard), tint = MaterialTheme.colorScheme.primary)
},
supportingContent = { Text(body, Modifier.padding(top = AlbaSpacing.xs)) },
verticalAlignment = Alignment.Top,
colors = AlbaListDefaults.colors(containerColor = if (grouped) albaGroupColor() else Color.Transparent),
contentPadding = PaddingValues(AlbaListDefaults.Padding),
) {
Text(title, style = MaterialTheme.typography.titleMedium)
}
}

Expand Down
Loading