package com.cottondb.android.ui.locations import androidx.compose.foundation.background import androidx.compose.foundation.layout.* import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.text.BasicTextField import androidx.compose.foundation.text.KeyboardActions import androidx.compose.foundation.text.KeyboardOptions import androidx.compose.foundation.verticalScroll import androidx.compose.material3.* import androidx.compose.runtime.* import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.focus.onFocusChanged import androidx.compose.ui.semantics.contentDescription import androidx.compose.ui.semantics.semantics import androidx.compose.ui.text.input.ImeAction import androidx.compose.ui.unit.dp import com.cottondb.android.day.locations.* 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 data class LocationDetailCallbacks( val refresh: () -> Unit, val edit: (LocationField, String) -> Unit, val save: (LocationField) -> Unit, val resolve: (LocationField, Boolean) -> Unit, val setPin: (LocationPin) -> Unit, val setProject: (String, Boolean) -> Unit, val archive: () -> Unit, val back: () -> Unit, ) /** Controlled paper editor; the retained model owns every draft and write outcome. */ @Composable fun LocationDetailScreen(state: LocationDetailState, clock: Clock, zone: ZoneId, callbacks: LocationDetailCallbacks, modifier: Modifier = Modifier) { var menu by remember { mutableStateOf(false) } var addressOpen by rememberSaveable { mutableStateOf(false) } var projectsOpen by rememberSaveable { mutableStateOf(false) } var archiveOpen by rememberSaveable { mutableStateOf(false) } var addressReceipt by rememberSaveable { mutableLongStateOf(state.addressSaved) } LaunchedEffect(state.addressSaved) { if (state.addressSaved != addressReceipt) { addressReceipt = state.addressSaved; addressOpen = false } } val place = state.read?.location Column(modifier.fillMaxSize().background(CottonPalette.groundMuted).safeDrawingPadding()) { Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) { TextButton(onClick = callbacks.back) { Text("Back") } Spacer(Modifier.weight(1f)) Box { if (place != null) TextButton(onClick = { menu = true }, modifier = Modifier.semantics { contentDescription = "More actions" }) { Text("More") } DropdownMenu(expanded = menu, onDismissRequest = { menu = false }) { DropdownMenuItem(text = { Text("Projects…") }, onClick = { menu = false; projectsOpen = true }) DropdownMenuItem(text = { Text("Edit address") }, onClick = { menu = false; addressOpen = true }) DropdownMenuItem(text = { Text("Archive") }, enabled = state.canWrite, onClick = { menu = false; archiveOpen = true }) } } } Column(Modifier.weight(1f).verticalScroll(rememberScrollState()).padding(horizontal = 20.dp), verticalArrangement = Arrangement.spacedBy(22.dp)) { state.readProblem?.let { Text(it, color = CottonPalette.statusError, style = CottonTextStyles.caption) } state.writeProblem?.let { Text(it, color = CottonPalette.statusError, style = CottonTextStyles.caption) } if (state.readProblem != null || state.needsRefresh || state.outcomeUnknown) TextButton(onClick = callbacks.refresh, enabled = !state.writing && !state.loading) { Text("Refresh") } when { place != null -> { TornPaper(place.id, MemoryPaperDye.Cream, Modifier.fillMaxWidth(), edge = PaperEdge.Cut) { Column(Modifier.padding(20.dp), verticalArrangement = Arrangement.spacedBy(12.dp)) { var nameWasFocused by remember { mutableStateOf(false) } BasicTextField( value = state.name.value, onValueChange = { callbacks.edit(LocationField.Name, it) }, readOnly = !state.canEdit, textStyle = CottonTextStyles.editorTitle.copy(fontFamily = CottonFonts.serif, color = CottonPalette.ink), keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done), keyboardActions = KeyboardActions(onDone = { callbacks.save(LocationField.Name) }), modifier = Modifier.fillMaxWidth().semantics { contentDescription = "Place name" }.onFocusChanged { if (nameWasFocused && !it.isFocused) callbacks.save(LocationField.Name) nameWasFocused = it.isFocused }, decorationBox = { inner -> Box { if (state.name.value.isEmpty()) Text("What is this place called?", style = CottonTextStyles.editorTitle.copy(fontFamily = CottonFonts.serif), color = CottonPalette.inkSecondary); inner() } }, ) DraftActions(LocationField.Name, state, callbacks) TextButton(onClick = { addressOpen = true }, modifier = Modifier.semantics { contentDescription = "Edit address" }) { Text(place.address?.trim()?.takeIf(String::isNotEmpty) ?: "add an address", style = CottonTextStyles.preview.copy(fontFamily = CottonFonts.type)) } LocationMiniMap(LocationPresentationFacts.pin(place.id, place.name ?: "", place.lat, place.lng), state.canWrite, callbacks.setPin, Modifier.fillMaxWidth()) place.createdAt?.let { com.cottondb.android.format.RecordDates.label(it, clock, zone) }?.takeIf(String::isNotEmpty)?.let { Text("added $it", Modifier.align(Alignment.End), style = CottonTextStyles.preview.copy(fontFamily = CottonFonts.hand)) } } } TornPaper(place.id + "-notes", MemoryPaperDye.Cream, Modifier.fillMaxWidth(), edge = PaperEdge.Cut) { Column(Modifier.padding(20.dp), verticalArrangement = Arrangement.spacedBy(10.dp)) { Text("Notes", style = CottonTextStyles.screenTitle.copy(fontFamily = CottonFonts.serif)) BasicTextField(value = state.note.value, onValueChange = { callbacks.edit(LocationField.Note, it) }, readOnly = !state.canEdit, textStyle = CottonTextStyles.preview.copy(fontFamily = CottonFonts.type, color = CottonPalette.ink), modifier = Modifier.fillMaxWidth().heightIn(min = 100.dp).semantics { contentDescription = "Place notes" }, decorationBox = { inner -> Box { if (state.note.value.isEmpty()) Text("Why this place matters — what to get, who to ask for…", style = CottonTextStyles.preview.copy(fontFamily = CottonFonts.type), color = CottonPalette.inkSecondary); inner() } }) state.noteStatus?.let { Text(it, Modifier.align(Alignment.End), style = CottonTextStyles.preview.copy(fontFamily = CottonFonts.hand)) } DraftActions(LocationField.Note, state, callbacks) } } } state.read != null -> Text("This place isn't here any more.", style = CottonTextStyles.screenTitle.copy(fontFamily = CottonFonts.serif)) else -> TornPaper("place-loading", MemoryPaperDye.Cream, Modifier.fillMaxWidth().height(280.dp), edge = PaperEdge.Cut) { Text(if (state.loading) "Loading…" else "", Modifier.padding(20.dp)) } } Spacer(Modifier.height(16.dp)) } } if (addressOpen) AlertDialog(onDismissRequest = { if (!state.writing) addressOpen = false }, title = { Text("Address") }, text = { Column(verticalArrangement = Arrangement.spacedBy(12.dp)) { Text("As you'd say it to a driver. Leave it empty to clear it.") OutlinedTextField(value = state.address.value, onValueChange = { callbacks.edit(LocationField.Address, it) }, readOnly = !state.canEdit, label = { Text("Address") }, modifier = Modifier.semantics { contentDescription = "Address" }) if (state.address.conflict) DraftActions(LocationField.Address, state, callbacks) state.writeProblem?.let { Text(it, color = CottonPalette.statusError) } } }, confirmButton = { TextButton(onClick = { if (!state.address.dirty) addressOpen = false else callbacks.save(LocationField.Address) }, enabled = state.canWrite && !state.address.conflict) { Text("Save") } }, dismissButton = { TextButton(onClick = { addressOpen = false }, enabled = !state.writing) { Text("Close") } }) if (projectsOpen) AlertDialog(onDismissRequest = { projectsOpen = false }, title = { Text("Projects") }, text = { Column(Modifier.verticalScroll(rememberScrollState())) { val read = state.read if (read?.projects.isNullOrEmpty()) Text("No projects yet.") read?.projects?.forEach { project -> val linked = read.projectLinks.any { it.projectId == project.id && it.recordKind == "location" && it.recordId == place?.id } Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) { Text(project.name, Modifier.weight(1f), style = CottonTextStyles.body) Checkbox(checked = linked, onCheckedChange = { callbacks.setProject(project.id, it) }, enabled = state.canWrite) } } state.writeProblem?.let { Text(it, color = CottonPalette.statusError) } } }, confirmButton = { TextButton(onClick = { projectsOpen = false }) { Text("Done") } }) if (archiveOpen) AlertDialog(onDismissRequest = { if (!state.writing) archiveOpen = false }, title = { Text("Archive this place?") }, text = { Column { Text("It leaves your places but nothing is deleted — anything linked to it keeps working."); state.writeProblem?.let { Text(it, color = CottonPalette.statusError) } } }, confirmButton = { TextButton(onClick = callbacks.archive, enabled = state.canWrite) { Text("Archive") } }, dismissButton = { TextButton(onClick = { archiveOpen = false }, enabled = !state.writing) { Text("Keep") } }) } @Composable private fun DraftActions(field: LocationField, state: LocationDetailState, callbacks: LocationDetailCallbacks) { val draft = state.draft(field) val label = field.name.lowercase() if (draft.conflict) { Text("This $label changed elsewhere.", color = CottonPalette.statusError, style = CottonTextStyles.caption) Row { TextButton(onClick = { callbacks.resolve(field, true) }, enabled = state.canEdit) { Text("Keep my $label") } TextButton(onClick = { callbacks.resolve(field, false) }, enabled = state.canEdit) { Text("Use latest $label") } } } else if (draft.dirty) TextButton(onClick = { callbacks.save(field) }, enabled = state.canWrite, modifier = Modifier.semantics { contentDescription = "Save place $label" }) { Text("Save") } }