Cotton code
270 lines · 12.1 KB · KotlinRaw
package com.cottondb.android.ui.locationsimport androidx.compose.foundation.backgroundimport androidx.compose.foundation.clickableimport androidx.compose.foundation.layout.Arrangementimport androidx.compose.foundation.layout.Boximport androidx.compose.foundation.layout.Columnimport androidx.compose.foundation.layout.Rowimport androidx.compose.foundation.layout.Spacerimport androidx.compose.foundation.layout.aspectRatioimport androidx.compose.foundation.layout.fillMaxSizeimport androidx.compose.foundation.layout.fillMaxWidthimport androidx.compose.foundation.layout.heightimport androidx.compose.foundation.layout.paddingimport androidx.compose.foundation.layout.safeDrawingPaddingimport androidx.compose.foundation.lazy.LazyColumnimport androidx.compose.foundation.lazy.itemsimport androidx.compose.material3.AlertDialogimport androidx.compose.material3.OutlinedTextFieldimport androidx.compose.material3.Textimport androidx.compose.material3.TextButtonimport androidx.compose.runtime.Composableimport androidx.compose.ui.Alignmentimport androidx.compose.ui.Modifierimport androidx.compose.ui.semantics.Roleimport androidx.compose.ui.semantics.contentDescriptionimport androidx.compose.ui.semantics.semanticsimport androidx.compose.ui.text.style.TextOverflowimport androidx.compose.ui.unit.dpimport androidx.compose.ui.unit.spimport com.cottondb.android.day.locations.LocationPresentationFactsimport com.cottondb.android.day.locations.LocationRowimport com.cottondb.android.day.locations.LocationsStateimport com.cottondb.android.design.CottonFontsimport com.cottondb.android.design.CottonTextStylesimport com.cottondb.android.generated.CottonPaletteimport com.cottondb.android.ui.paper.MemoryPaperDyeimport com.cottondb.android.ui.paper.PaperEdgeimport com.cottondb.android.ui.paper.TornPaperimport java.time.Clockimport java.time.ZoneId/** Swift's paper-layout setting without coupling the Day leaf to traffic/photos. */enum class LocationLayout { Postcards, List }/** * Controlled `/locations` presentation. The route owns reads, retained creation, * navigation after a verified receipt, and persistence of [LocationLayout]. */@Composablefun LocationsScreen(    state: LocationsState,    layout: LocationLayout,    clock: Clock,    zone: ZoneId,    onRefresh: () -> Unit,    onCreateOpen: () -> Unit,    onCreateCancel: () -> Unit,    onName: (String) -> Unit,    onAddress: (String) -> Unit,    onCreate: () -> Unit,    onOpen: (String) -> Unit,    onLayout: (LocationLayout) -> Unit,    onBack: () -> Unit = {},    modifier: Modifier = Modifier,) {    Column(modifier.fillMaxSize().background(CottonPalette.groundMuted).safeDrawingPadding()) {        TextButton(onClick = onBack) { Text("Back") }        LocationsHeader(layout, state.rows?.isNotEmpty() == true, onCreateOpen, onLayout)        state.readProblem?.let { problem -> LocationsProblem(problem, onRefresh, !state.loading) }        if (state.needsRefresh || state.outcomeUnknown) TextButton(onClick = onRefresh, enabled = !state.creating && !state.loading) { Text("Refresh") }        state.writeProblem?.let { problem ->            Text(problem, Modifier.padding(horizontal = 20.dp, vertical = 4.dp), style = CottonTextStyles.caption, color = CottonPalette.statusError)        }        when {            state.rows == null && (state.loading || state.readProblem == null) -> LocationsSkeleton(layout)            state.rows == null -> Unit            state.rows?.isEmpty() == true -> LocationsEmpty(onCreateOpen)            else -> LocationsContent(requireNotNull(state.rows), layout, clock, zone, onOpen)        }    }    if (state.composing) {        NewPlaceDialog(state, onCreateCancel, onName, onAddress, onCreate)    }}@Composableprivate fun LocationsHeader(    layout: LocationLayout,    hasRows: Boolean,    onCreateOpen: () -> Unit,    onLayout: (LocationLayout) -> Unit,) {    Row(        Modifier.fillMaxWidth().padding(horizontal = 16.dp, vertical = 8.dp),        verticalAlignment = Alignment.CenterVertically,    ) {        Text("Locations", style = CottonTextStyles.pageTitle.copy(fontFamily = CottonFonts.display), color = CottonPalette.ink)        Spacer(Modifier.weight(1f))        if (hasRows) {            TextButton(                onClick = { onLayout(if (layout == LocationLayout.Postcards) LocationLayout.List else LocationLayout.Postcards) },                modifier = Modifier.semantics { contentDescription = "Change locations layout" },            ) { Text(if (layout == LocationLayout.Postcards) "List" else "Postcards") }        }        TextButton(onClick = onCreateOpen, modifier = Modifier.semantics { contentDescription = "Add a place" }) { Text("Add") }    }}@Composableprivate fun LocationsProblem(problem: String, onRefresh: () -> Unit, canRetry: Boolean) {    Row(Modifier.fillMaxWidth().padding(horizontal = 20.dp, vertical = 4.dp), verticalAlignment = Alignment.CenterVertically) {        Text(problem, Modifier.weight(1f), style = CottonTextStyles.caption, color = CottonPalette.statusError)        TextButton(onClick = onRefresh, enabled = canRetry) { Text("Try again") }    }}@Composableprivate fun LocationsSkeleton(layout: LocationLayout) {    LazyColumn(Modifier.fillMaxSize(), verticalArrangement = Arrangement.spacedBy(16.dp)) {        items((0 until 4).toList()) { index ->            TornPaper(                "location-skeleton-$index",                MemoryPaperDye.Cream,                Modifier.fillMaxWidth().padding(horizontal = 20.dp)                    .height(if (layout == LocationLayout.Postcards) 176.dp else 68.dp),                edge = PaperEdge.Cut,            ) {}        }    }}@Composableprivate fun LocationsEmpty(onCreateOpen: () -> Unit) {    Column(        Modifier.fillMaxSize().padding(32.dp),        verticalArrangement = Arrangement.Center,        horizontalAlignment = Alignment.CenterHorizontally,    ) {        Text("No places yet.", style = CottonTextStyles.screenTitle.copy(fontFamily = CottonFonts.serif), color = CottonPalette.ink)        Text(            "Capture one from Home and it files itself here, or add one now.",            Modifier.padding(top = 8.dp),            style = CottonTextStyles.body.copy(fontFamily = CottonFonts.type),            color = CottonPalette.inkSecondary,        )        TextButton(onClick = onCreateOpen) { Text("Add a place") }    }}@Composableprivate fun LocationsContent(    rows: List<LocationRow>,    layout: LocationLayout,    clock: Clock,    zone: ZoneId,    onOpen: (String) -> Unit,) {    val recent = LocationPresentationFacts.recent(rows, zone = zone)    LazyColumn(        Modifier.fillMaxSize(),        verticalArrangement = Arrangement.spacedBy(if (layout == LocationLayout.Postcards) 18.dp else 10.dp),    ) {        if (recent.isNotEmpty()) {            item("recent-heading") {                Text("Recent", Modifier.padding(start = 20.dp, top = 8.dp), style = CottonTextStyles.sectionLabel.copy(fontFamily = CottonFonts.serif), color = CottonPalette.ink)            }            items(recent, key = { "recent-${it.id}" }) { row -> RecentLocation(row, clock, zone, onOpen) }        }        items(rows, key = LocationRow::id) { row ->            if (layout == LocationLayout.Postcards) Postcard(row, onOpen) else LocationRowCard(row, onOpen)        }    }}@Composableprivate fun RecentLocation(row: LocationRow, clock: Clock, zone: ZoneId, onOpen: (String) -> Unit) {    Row(        Modifier.fillMaxWidth().clickable(role = Role.Button) { onOpen(row.id) }.padding(horizontal = 22.dp, vertical = 5.dp)            .semantics { contentDescription = "Open ${LocationPresentationFacts.displayedName(row)}" },        verticalAlignment = Alignment.CenterVertically,    ) {        Text(LocationPresentationFacts.displayedName(row), Modifier.weight(1f), maxLines = 1, overflow = TextOverflow.Ellipsis, style = CottonTextStyles.preview.copy(fontFamily = CottonFonts.type), color = CottonPalette.ink)        LocationPresentationFacts.whenLabel(row.updatedAt, clock, zone)?.let { label ->            Text(label, style = CottonTextStyles.preview.copy(fontFamily = CottonFonts.hand, fontSize = 16.sp), color = CottonPalette.inkSecondary)        }    }}@Composableprivate fun Postcard(row: LocationRow, onOpen: (String) -> Unit) {    val pin = LocationPresentationFacts.pin(row)    TornPaper(        row.id,        MemoryPaperDye.Cream,        Modifier.fillMaxWidth().padding(horizontal = 20.dp).aspectRatio(1.85f)            .clickable(role = Role.Button) { onOpen(row.id) }            .semantics { contentDescription = "Open ${LocationPresentationFacts.displayedName(row)}" },        edge = PaperEdge.Cut,    ) {        Row(Modifier.fillMaxSize().padding(16.dp), horizontalArrangement = Arrangement.spacedBy(14.dp)) {            LocationWords(row, Modifier.weight(1f), titleSize = 23.sp, maxTitleLines = 2, maxSubtitleLines = 3)            pin?.let { LocationPostcardMap(it, Modifier.weight(1f).fillMaxSize()) }        }    }}@Composableprivate fun LocationRowCard(row: LocationRow, onOpen: (String) -> Unit) {    val pin = LocationPresentationFacts.pin(row)    TornPaper(        row.id,        MemoryPaperDye.Cream,        Modifier.fillMaxWidth().padding(horizontal = 20.dp)            .clickable(role = Role.Button) { onOpen(row.id) }            .semantics { contentDescription = "Open ${LocationPresentationFacts.displayedName(row)}" },        edge = PaperEdge.Cut,    ) {        Row(Modifier.fillMaxWidth().height(72.dp).padding(14.dp), verticalAlignment = Alignment.CenterVertically) {            LocationWords(row, Modifier.weight(1f), titleSize = 18.sp, maxTitleLines = 1, maxSubtitleLines = 1)            pin?.let { LocationPostcardMap(it, Modifier.height(50.dp).fillMaxWidth(.3f)) }        }    }}@Composableprivate fun LocationWords(row: LocationRow, modifier: Modifier, titleSize: androidx.compose.ui.unit.TextUnit, maxTitleLines: Int, maxSubtitleLines: Int) {    Column(modifier, verticalArrangement = Arrangement.spacedBy(6.dp)) {        Text(LocationPresentationFacts.displayedName(row), maxLines = maxTitleLines, overflow = TextOverflow.Ellipsis, style = CottonTextStyles.screenTitle.copy(fontFamily = CottonFonts.serif, fontSize = titleSize), color = CottonPalette.ink)        Text(LocationPresentationFacts.subtitle(row) ?: " ", maxLines = maxSubtitleLines, overflow = TextOverflow.Ellipsis, style = CottonTextStyles.caption.copy(fontFamily = CottonFonts.type), color = CottonPalette.inkSecondary)    }}@Composableprivate fun NewPlaceDialog(    state: LocationsState,    onCancel: () -> Unit,    onName: (String) -> Unit,    onAddress: (String) -> Unit,    onCreate: () -> Unit,) {    AlertDialog(        onDismissRequest = { if (!state.creating) onCancel() },        title = { Text("Add a place", style = CottonTextStyles.screenTitle.copy(fontFamily = CottonFonts.serif)) },        text = {            Column(verticalArrangement = Arrangement.spacedBy(10.dp)) {                OutlinedTextField(                    state.name,                    onName,                    enabled = state.canEdit,                    label = { Text("What is this place called?") },                    modifier = Modifier.fillMaxWidth().semantics { contentDescription = "Place name" },                )                OutlinedTextField(                    state.address,                    onAddress,                    enabled = state.canEdit,                    label = { Text("Address (optional)") },                    minLines = 3,                    maxLines = 4,                    modifier = Modifier.fillMaxWidth().semantics { contentDescription = "Address" },                )                state.writeProblem?.let { Text(it, color = CottonPalette.statusError) }                Text("A name is enough — the note and the pin live on its own screen.", style = CottonTextStyles.caption, color = CottonPalette.inkSecondary)            }        },        confirmButton = { TextButton(onClick = onCreate, enabled = state.canCreate) { Text(if (state.creating) "Adding…" else "Add place") } },        dismissButton = { TextButton(onClick = onCancel, enabled = !state.creating) { Text("Cancel") } },    )}