85 lines · 4.2 KB · KotlinRaw
package com.cottondb.healthimport androidx.compose.foundation.backgroundimport androidx.compose.foundation.clickableimport androidx.compose.foundation.gestures.snapping.rememberSnapFlingBehaviorimport androidx.compose.foundation.layout.Arrangementimport androidx.compose.foundation.layout.Boximport androidx.compose.foundation.layout.PaddingValuesimport androidx.compose.foundation.layout.Rowimport androidx.compose.foundation.layout.fillMaxWidthimport androidx.compose.foundation.layout.heightimport androidx.compose.foundation.lazy.LazyColumnimport androidx.compose.foundation.lazy.itemsIndexedimport androidx.compose.foundation.lazy.rememberLazyListStateimport androidx.compose.foundation.shape.RoundedCornerShapeimport androidx.compose.runtime.Composableimport androidx.compose.runtime.LaunchedEffectimport androidx.compose.runtime.rememberCoroutineScopeimport androidx.compose.ui.Alignmentimport androidx.compose.ui.Modifierimport androidx.compose.ui.draw.clipimport androidx.compose.ui.semantics.Roleimport com.cottondb.look.LTextimport com.cottondb.look.uiimport kotlinx.coroutines.launchimport java.time.LocalDateimport java.time.format.TextStyleimport java.util.Locale/** * A day on a wheel, as the iPhone asks a date of birth (MATCH-IOS.md): day, month and year columns, each snapping to * the row in the middle band; a row tapped is chosen too, so TalkBack can choose one. The months are the phone's own * names for them, as the iPhone's wheel says its own. No day after today. */@Composablefun DayWheel(day: LocalDate, onDay: (LocalDate) -> Unit, modifier: Modifier = Modifier) { val v = ui val today = LocalDate.now() val years = (FIRST_YEAR..today.year).toList() val months = (1..12).toList() val days = (1..day.lengthOfMonth()).toList() fun pick(year: Int = day.year, month: Int = day.monthValue, of: Int = day.dayOfMonth) { val first = LocalDate.of(year, month, 1) val chosen = first.withDayOfMonth(of.coerceAtMost(first.lengthOfMonth())) onDay(if (chosen.isAfter(today)) today else chosen) } Box(modifier.fillMaxWidth().height(v.space("tap") * SHOWN), contentAlignment = Alignment.Center) { val shape = RoundedCornerShape(v.corner("row")) Box(Modifier.fillMaxWidth().height(v.space("tap")).clip(shape).background(v.c.surface, shape)) Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.spacedBy(v.space("s"))) { Column(days, days.indexOf(day.dayOfMonth), { it.toString() }, Modifier.weight(1f)) { pick(of = it) } Column(months, day.monthValue - 1, { java.time.Month.of(it).getDisplayName(TextStyle.FULL, Locale.getDefault()) }, Modifier.weight(2f)) { pick(month = it) } Column(years, years.indexOf(day.year), { it.toString() }, Modifier.weight(1.4f)) { pick(year = it) } } }}/** One column: its values snapping to the middle row; the one stood on there is chosen. */@Composableprivate fun Column(values: List<Int>, at: Int, said: (Int) -> String, modifier: Modifier, onChosen: (Int) -> Unit) { val v = ui val list = rememberLazyListState(initialFirstVisibleItemIndex = at.coerceAtLeast(0)) val scope = rememberCoroutineScope() LaunchedEffect(list.isScrollInProgress) { if (!list.isScrollInProgress) values.getOrNull(list.firstVisibleItemIndex)?.let { if (it != values.getOrNull(at)) onChosen(it) } } LazyColumn( modifier.height(v.space("tap") * SHOWN), state = list, flingBehavior = rememberSnapFlingBehavior(list), contentPadding = PaddingValues(vertical = v.space("tap") * (SHOWN / 2)), horizontalAlignment = Alignment.CenterHorizontally, ) { itemsIndexed(values) { index, value -> val words = said(value) Box( Modifier.fillMaxWidth().height(v.space("tap")).clickable(role = Role.Button, onClickLabel = words) { scope.launch { list.animateScrollToItem(index) } }, contentAlignment = Alignment.Center, ) { LText(words, "body", if (index == at) v.c.text else v.c.tertiary, lines = 1) } } }}/** Rows the wheel shows at once, the chosen one in the middle. */private const val SHOWN = 5/** The earliest year the wheel offers. */private const val FIRST_YEAR = 1900