package com.cottondb.android.ui.locations import androidx.compose.foundation.background 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.Row import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.aspectRatio import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.safeDrawingPadding import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.items import androidx.compose.material3.AlertDialog import androidx.compose.material3.OutlinedTextField import androidx.compose.material3.Text import androidx.compose.material3.TextButton import androidx.compose.runtime.Composable import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.semantics.Role import androidx.compose.ui.semantics.contentDescription import androidx.compose.ui.semantics.semantics import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import com.cottondb.android.day.locations.LocationPresentationFacts import com.cottondb.android.day.locations.LocationRow import com.cottondb.android.day.locations.LocationsState import com.cottondb.android.design.CottonFonts import com.cottondb.android.design.CottonTextStyles import com.cottondb.android.generated.CottonPalette import com.cottondb.android.ui.paper.MemoryPaperDye import com.cottondb.android.ui.paper.PaperEdge import com.cottondb.android.ui.paper.TornPaper import java.time.Clock import 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]. */ @Composable fun 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) } } @Composable private 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") } } } @Composable private 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") } } } @Composable private 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, ) {} } } } @Composable private 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") } } } @Composable private fun LocationsContent( rows: List, 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) } } } @Composable private 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) } } } @Composable private 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()) } } } } @Composable private 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)) } } } } @Composable private 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) } } @Composable private 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") } }, ) }