@file:OptIn(androidx.compose.material3.ExperimentalMaterial3Api::class) package com.cottondb.android.ui.day.goals import android.app.DatePickerDialog import androidx.activity.compose.BackHandler 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.PaddingValues import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.imePadding import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.safeDrawingPadding import androidx.compose.foundation.layout.size import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.items import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material3.AlertDialog import androidx.compose.material3.ModalBottomSheet import androidx.compose.material3.OutlinedTextField import androidx.compose.material3.Text import androidx.compose.material3.TextButton import androidx.compose.material3.pulltorefresh.PullToRefreshBox import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.shadow import androidx.compose.ui.focus.onFocusChanged import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.semantics.Role import androidx.compose.ui.semantics.contentDescription import androidx.compose.ui.semantics.semantics import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.dp import com.cottondb.android.day.goals.Goal import com.cottondb.android.day.goals.GoalCreationModel import com.cottondb.android.day.goals.GoalCreationState import com.cottondb.android.day.goals.GoalDetailModel import com.cottondb.android.day.goals.GoalDetailRead import com.cottondb.android.day.goals.GoalDetailState import com.cottondb.android.day.goals.GoalDraft import com.cottondb.android.day.goals.GoalLines import com.cottondb.android.day.goals.GoalMutation import com.cottondb.android.day.goals.GoalStatus import com.cottondb.android.day.goals.GoalsRead import com.cottondb.android.day.goals.GoalsRepository import com.cottondb.android.day.goals.GoalsState import com.cottondb.android.design.CottonFonts import com.cottondb.android.design.CottonMetrics import com.cottondb.android.design.CottonTextStyles import com.cottondb.android.generated.CottonPalette import java.time.Clock import java.time.LocalDate import java.time.format.DateTimeFormatter import java.util.Locale import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.launch /** `/goals`: one ranked paper list. The repository owns all writes and rereads. */ @Composable fun GoalsScreen( repository: GoalsRepository, creation: GoalCreationModel, writerScope: CoroutineScope, clock: Clock, dataRevision: Long, onBack: () -> Unit, onNavigate: (String) -> Unit, ) { val state by repository.state.collectAsState() val scope = rememberCoroutineScope() var composing by rememberSaveable { mutableStateOf(false) } var arranging by rememberSaveable { mutableStateOf(false) } var notice by remember { mutableStateOf(null) } LaunchedEffect(repository, dataRevision) { repository.refresh() } val read = state.readOrNull() val split = read?.let { GoalLines.split(it.goals) } val creationState by creation.state.collectAsState() LaunchedEffect(creationState) { when (creationState) { is GoalCreationState.Created -> composing = false is GoalCreationState.Editing -> notice = (creationState as GoalCreationState.Editing).error is GoalCreationState.Saving -> Unit } } val openComposer = { if (creationState is GoalCreationState.Created) { creation.resetAfterCreated() } composing = true } GoalsGround { Column(Modifier.fillMaxSize()) { GoalsTopBar( title = "Goals", onBack = onBack, onRefresh = { scope.launch { repository.refresh() } }, action = "New goal", onAction = openComposer, ) PullToRefreshBox( isRefreshing = state is GoalsState.Data && (state as GoalsState.Data).refreshing, onRefresh = { scope.launch { repository.refresh() } }, modifier = Modifier.weight(1f), ) { when { state is GoalsState.Loading && read == null -> GoalSkeletons() read == null -> GoalReadFailure((state as GoalsState.Error).message) { scope.launch { repository.refresh() } } split!!.live.isEmpty() && split.behind.isEmpty() -> GoalEmpty( onNew = openComposer, warning = (state as? GoalsState.Error)?.message, ) else -> GoalList( split.live, split.behind, clock, warning = (state as? GoalsState.Error)?.message, onRetry = { scope.launch { repository.refresh() } }, onOpen = { goal -> onNavigate("/goals/goal/${goal.id}") }, onArrange = { arranging = true }, ) } } } } if (composing) { NewGoalSheet( clock = clock, model = creation, writerScope = writerScope, state = creationState, onDismiss = { if (creationState !is GoalCreationState.Saving) composing = false }, ) } if (arranging && split != null) { GoalArrangeSheet( goals = split.live, onDismiss = { arranging = false }, onMove = { from, to -> scope.launch { when (val result = repository.reorder(split.live, from, to)) { is GoalMutation.Failure -> notice = result.message else -> Unit } } }, ) } notice?.let { message -> GoalNotice(message) { notice = null } } } /** `/goals/goal/[id]`. Root owns construction so this screen keeps no session state. */ @Composable fun GoalDetailScreen( model: GoalDetailModel, writerScope: CoroutineScope, clock: Clock, dataRevision: Long, onBack: () -> Unit, onNavigate: (String) -> Unit, ) { val state by model.state.collectAsState() val scope = rememberCoroutineScope() var menu by remember { mutableStateOf(false) } var projects by remember { mutableStateOf(false) } var confirmDelete by remember { mutableStateOf(false) } var notice by remember { mutableStateOf(null) } var leaving by remember { mutableStateOf(false) } val saving by model.mutationBusy.collectAsState() LaunchedEffect(model, dataRevision) { model.refresh() } val read = state.detailOrNull() val draft = state.draftOrEmpty() val goal = read?.goal fun save() { if (goal != null) model.flushPending(writerScope) } fun leave() { if (leaving) return leaving = true scope.launch { val result = model.flushPending(writerScope)?.await() if (result !is GoalMutation.Failure) onBack() else leaving = false } } BackHandler(onBack = ::leave) GoalsGround { Column(Modifier.fillMaxSize()) { GoalsTopBar( title = "Goal", onBack = ::leave, onRefresh = { scope.launch { model.refresh() } }, action = if (goal == null) null else "More", onAction = { menu = true }, ) when { state is GoalDetailState.Loading && read == null -> GoalDetailSkeleton() read == null -> GoalReadFailure((state as GoalDetailState.Error).message) { scope.launch { model.refresh() } } goal == null -> GoalGone() else -> GoalDetailPaper( goal = goal, draft = draft, clock = clock, saving = saving, onDraft = model::setDraft, onSave = ::save, error = (state as? GoalDetailState.Error)?.message, onRetry = { scope.launch { model.refresh() } }, ) } } } if (menu && goal != null) { GoalMoreSheet( goal = goal, onDismiss = { menu = false }, onProjects = { menu = false; projects = true }, onStatus = { status -> if (!saving) { menu = false scope.launch { when (val result = model.status(status, writerScope).await()) { is GoalMutation.Failure -> notice = result.message else -> notice = movedReceipt(status) } } } }, onDelete = { menu = false; confirmDelete = true }, ) } if (projects && read != null && goal != null) { GoalProjectsSheet( read = read, goalId = goal.id, onDismiss = { projects = false }, onToggle = { project, linked -> if (!saving) { scope.launch { val result = if (linked) model.unlink(project.slug, writerScope).await() else model.link(project.slug, writerScope).await() if (result is GoalMutation.Failure) notice = result.message } } }, ) } if (confirmDelete && goal != null) { AlertDialog( onDismissRequest = { confirmDelete = false }, title = { Text("Delete this goal?", fontFamily = CottonFonts.display) }, text = { Text("It goes for good. “Set it aside” keeps it readable in the list instead.") }, confirmButton = { TextButton(onClick = { confirmDelete = false scope.launch { when (val result = model.delete(writerScope).await()) { is GoalMutation.Failure -> notice = result.message else -> { notice = "Deleted"; onBack() } } } }) { Text("Delete", color = CottonPalette.statusError) } }, dismissButton = { TextButton(onClick = { confirmDelete = false }) { Text("Cancel") } }, ) } notice?.let { message -> GoalNotice(message) { notice = null } } } @Composable private fun GoalsGround(content: @Composable () -> Unit) = Box( Modifier.fillMaxSize() .background(CottonPalette.groundMuted) .safeDrawingPadding() .imePadding(), ) { content() } @Composable private fun GoalsTopBar( title: String, onBack: () -> Unit, onRefresh: () -> Unit, action: String?, onAction: () -> Unit, ) = Row( Modifier.fillMaxWidth().padding(horizontal = CottonMetrics.contentMargin, vertical = 14.dp), verticalAlignment = Alignment.CenterVertically, ) { Text( "Back", Modifier.clickable(role = Role.Button, onClick = onBack).padding(8.dp), style = CottonTextStyles.preview, color = CottonPalette.inkSecondary, ) Text( title, Modifier.weight(1f), style = CottonTextStyles.pageTitle.copy(fontFamily = CottonFonts.display), color = CottonPalette.ink, textAlign = TextAlign.Center, ) if (action == null) { Text( "Refresh", Modifier.clickable(role = Role.Button, onClick = onRefresh).padding(8.dp), style = CottonTextStyles.preview, color = CottonPalette.accent, ) } else { Text( action, Modifier.clickable(role = Role.Button, onClick = onAction).padding(8.dp), style = CottonTextStyles.preview, color = CottonPalette.accent, ) } } @Composable private fun GoalList( live: List, behind: List, clock: Clock, warning: String?, onRetry: () -> Unit, onOpen: (Goal) -> Unit, onArrange: () -> Unit, ) = LazyColumn( contentPadding = PaddingValues(CottonMetrics.contentMargin, 20.dp, CottonMetrics.contentMargin, 38.dp), verticalArrangement = Arrangement.spacedBy(16.dp), ) { warning?.let { message -> item { Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) { Text(message, Modifier.weight(1f), style = CottonTextStyles.caption, color = CottonPalette.statusError) Text("Try again", Modifier.clickable(role = Role.Button, onClick = onRetry).padding(6.dp), style = CottonTextStyles.caption, color = CottonPalette.accent) } } } if (live.isNotEmpty()) { item { Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) { Text("In view", Modifier.weight(1f), style = CottonTextStyles.sectionLabel.copy(fontFamily = CottonFonts.display), color = CottonPalette.ink) Text("Arrange", Modifier.clickable(role = Role.Button, onClick = onArrange).padding(6.dp), style = CottonTextStyles.preview, color = CottonPalette.accent) } } items(live, key = Goal::id) { goal -> GoalSheet(goal, clock, onOpen) } } if (behind.isNotEmpty()) { item { Text("Behind you · ${behind.size}", style = CottonTextStyles.sectionLabel.copy(fontFamily = CottonFonts.display), color = CottonPalette.ink) } items(behind, key = Goal::id) { goal -> GoalSheet(goal, clock, onOpen) } } } @Composable private fun GoalSheet(goal: Goal, clock: Clock, onOpen: (Goal) -> Unit) { val caption = GoalLines.caption(goal, LocalDate.now(clock), clock.zone) val stamp = stamp(goal.status) Column( Modifier.fillMaxWidth() .shadow(3.dp, RoundedCornerShape(CottonMetrics.cardRadius)) .background(CottonPalette.groundElevated, RoundedCornerShape(CottonMetrics.cardRadius)) .clickable(role = Role.Button, onClick = { onOpen(goal) }) .semantics { contentDescription = buildString { append(goal.title); caption?.let { append(", ").append(it) }; stamp?.let { append(", $it") } } } .padding(22.dp), verticalArrangement = Arrangement.spacedBy(7.dp), ) { Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.Top) { Text(goal.title, Modifier.weight(1f), style = CottonTextStyles.screenTitle.copy(fontFamily = CottonFonts.display), color = CottonPalette.ink, maxLines = 3, overflow = TextOverflow.Ellipsis) stamp?.let { Text(it, Modifier.padding(start = 12.dp), style = CottonTextStyles.caption.copy(fontFamily = CottonFonts.hand), color = if (goal.status == GoalStatus.Dropped) CottonPalette.statusError else CottonPalette.accent) } } caption?.let { Text(it.lowercase(Locale.ENGLISH), style = CottonTextStyles.caption, color = CottonPalette.inkSecondary) } if (goal.status == GoalStatus.Achieved) GoalProgress() } } @Composable private fun GoalProgress() = Row(verticalAlignment = Alignment.CenterVertically) { Box(Modifier.weight(1f).padding(end = 12.dp).size(height = 3.dp, width = 1.dp).background(CottonPalette.inkSecondary)) Text("100%", style = CottonTextStyles.preview.copy(fontFamily = CottonFonts.hand), color = CottonPalette.inkSecondary) } @Composable private fun GoalDetailPaper( goal: Goal, draft: GoalDraft, clock: Clock, saving: Boolean, onDraft: (GoalDraft) -> Unit, onSave: () -> Unit, error: String?, onRetry: () -> Unit, ) = Column( Modifier.padding(horizontal = CottonMetrics.contentMargin, vertical = 20.dp), verticalArrangement = Arrangement.spacedBy(16.dp), ) { if (error != null) { Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) { Text(error, Modifier.weight(1f), style = CottonTextStyles.caption, color = CottonPalette.statusError) Text("Try again", Modifier.clickable(role = Role.Button, onClick = onRetry).padding(6.dp), style = CottonTextStyles.caption, color = CottonPalette.accent) } } Column( Modifier.fillMaxWidth() .shadow(3.dp, RoundedCornerShape(CottonMetrics.cardRadius)) .background(CottonPalette.groundElevated, RoundedCornerShape(CottonMetrics.cardRadius)) .padding(22.dp), verticalArrangement = Arrangement.spacedBy(12.dp), ) { OutlinedTextField( value = draft.title, onValueChange = { if (it.length <= 200) onDraft(draft.copy(title = it)) }, enabled = !saving, modifier = Modifier.fillMaxWidth().onFocusChanged { if (!it.isFocused) onSave() }.semantics { contentDescription = "The goal" }, placeholder = { Text("What are you going for?") }, textStyle = CottonTextStyles.screenTitle.copy(fontFamily = CottonFonts.display), minLines = 1, maxLines = 3, ) Text(GoalLines.statusLabel(goal.status).lowercase(Locale.ENGLISH), style = CottonTextStyles.preview.copy(fontFamily = CottonFonts.hand), color = CottonPalette.inkSecondary) GoalDateEditor(draft.targetDate, clock, saving) { date -> onDraft(draft.copy(targetDate = date)) onSave() } if (goal.status == GoalStatus.Achieved) GoalProgress() Text("why it matters", style = CottonTextStyles.preview.copy(fontFamily = CottonFonts.hand), color = CottonPalette.inkSecondary) OutlinedTextField( value = draft.why, onValueChange = { if (it.length <= 500) onDraft(draft.copy(why = it)) }, enabled = !saving, modifier = Modifier.fillMaxWidth().onFocusChanged { if (!it.isFocused) onSave() }, placeholder = { Text("Cotton reads this when it decides what is worth your attention.") }, textStyle = CottonTextStyles.body, minLines = 3, maxLines = 8, ) Text( if (saving) "Saving…" else "Save", Modifier.align(Alignment.End).clickable(enabled = !saving && draft.title.isNotBlank(), role = Role.Button, onClick = onSave).padding(8.dp), style = CottonTextStyles.body, color = if (!saving && draft.title.isNotBlank()) CottonPalette.accent else CottonPalette.inkTertiary, ) } } @Composable private fun NewGoalSheet( clock: Clock, model: GoalCreationModel, writerScope: CoroutineScope, state: GoalCreationState, onDismiss: () -> Unit, ) { val draft = when (state) { is GoalCreationState.Editing -> state.draft is GoalCreationState.Saving -> state.draft is GoalCreationState.Created -> GoalDraft() } val saving = state is GoalCreationState.Saving val error = (state as? GoalCreationState.Editing)?.error val titleValid = draft.title.trim().isNotEmpty() ModalBottomSheet(onDismissRequest = onDismiss, containerColor = CottonPalette.ground) { Column( Modifier.fillMaxWidth().padding(horizontal = CottonMetrics.contentMargin, vertical = 14.dp), verticalArrangement = Arrangement.spacedBy(16.dp), ) { OutlinedTextField(value = draft.title, onValueChange = { if (it.length <= 200) model.setDraft(draft.copy(title = it)) }, enabled = !saving, modifier = Modifier.fillMaxWidth(), placeholder = { Text("What are you going for?") }, minLines = 1, maxLines = 3, textStyle = CottonTextStyles.screenTitle.copy(fontFamily = CottonFonts.display)) if (draft.title.isNotEmpty() && !titleValid) Text("A goal needs a title.", style = CottonTextStyles.caption, color = CottonPalette.statusError) Text("Cotton keeps it in view — it never scores you on it.", style = CottonTextStyles.caption, color = CottonPalette.inkSecondary) Text("Why", style = CottonTextStyles.preview.copy(fontFamily = CottonFonts.hand), color = CottonPalette.ink) OutlinedTextField(value = draft.why, onValueChange = { if (it.length <= 500) model.setDraft(draft.copy(why = it)) }, enabled = !saving, modifier = Modifier.fillMaxWidth(), placeholder = { Text("Why it matters (optional)") }, minLines = 2, maxLines = 6) Text("By", style = CottonTextStyles.preview.copy(fontFamily = CottonFonts.hand), color = CottonPalette.ink) GoalDateEditor(draft.targetDate, clock, saving) { model.setDraft(draft.copy(targetDate = it)) } error?.let { Text(it, style = CottonTextStyles.caption, color = CottonPalette.statusError) } Text("Add goal", Modifier.align(Alignment.End).clickable(enabled = !saving && titleValid, role = Role.Button, onClick = { model.submit(writerScope) }).padding(10.dp), style = CottonTextStyles.body, color = if (!saving && titleValid) CottonPalette.accent else CottonPalette.inkTertiary) } } } @Composable private fun GoalDateEditor(value: String, clock: Clock, enabled: Boolean, onChange: (String) -> Unit) { val context = LocalContext.current val today = LocalDate.now(clock) val selected = value.toLocalDateOrNull() ?: today Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) { Text( if (value.isBlank()) "Add a date (optional)" else "by ${selected.format(DateTimeFormatter.ofPattern("d MMMM yyyy", Locale.ENGLISH))}", Modifier.weight(1f).clickable(enabled = enabled, role = Role.Button) { DatePickerDialog(context, { _, year, month, day -> onChange(LocalDate.of(year, month + 1, day).toString()) }, selected.year, selected.monthValue - 1, selected.dayOfMonth).also { dialog -> dialog.datePicker.minDate = today.atStartOfDay(clock.zone).toInstant().toEpochMilli() }.show() }.semantics { contentDescription = if (value.isBlank()) "Add a date" else "Horizon: $value" }, style = CottonTextStyles.body, color = if (value.isBlank()) CottonPalette.inkSecondary else CottonPalette.ink, ) if (value.isNotBlank()) Text("Clear", Modifier.clickable(enabled = enabled, role = Role.Button, onClick = { onChange("") }).padding(8.dp).semantics { contentDescription = "Clear the date" }, style = CottonTextStyles.caption, color = CottonPalette.inkSecondary) } } @Composable private fun GoalMoreSheet(goal: Goal, onDismiss: () -> Unit, onProjects: () -> Unit, onStatus: (GoalStatus) -> Unit, onDelete: () -> Unit) = ModalBottomSheet(onDismissRequest = onDismiss, containerColor = CottonPalette.ground) { Column(Modifier.fillMaxWidth().padding(horizontal = CottonMetrics.contentMargin, vertical = 12.dp), verticalArrangement = Arrangement.spacedBy(4.dp)) { GoalMenuItem("Projects…", onProjects) if (goal.status == GoalStatus.Active || goal.status == GoalStatus.Paused) GoalMenuItem("Reached it", onClick = { onStatus(GoalStatus.Achieved) }) if (goal.status == GoalStatus.Active) GoalMenuItem("Pause this goal", onClick = { onStatus(GoalStatus.Paused) }) if (goal.status != GoalStatus.Active) GoalMenuItem("Make it active again", onClick = { onStatus(GoalStatus.Active) }) if (goal.status != GoalStatus.Dropped) GoalMenuItem("Set it aside", onClick = { onStatus(GoalStatus.Dropped) }) GoalMenuItem("Delete this goal", onDelete, destructive = true) } } @Composable private fun GoalMenuItem(label: String, onClick: () -> Unit, destructive: Boolean = false) = Text(label, Modifier.fillMaxWidth().clickable(role = Role.Button, onClick = onClick).padding(vertical = 14.dp), style = CottonTextStyles.body, color = if (destructive) CottonPalette.statusError else CottonPalette.ink) @Composable private fun GoalProjectsSheet(read: GoalDetailRead, goalId: String, onDismiss: () -> Unit, onToggle: (com.cottondb.android.day.goals.GoalProject, Boolean) -> Unit) = ModalBottomSheet(onDismissRequest = onDismiss, containerColor = CottonPalette.ground) { val linked = read.projectLinks.filter { it.recordId == goalId && it.recordKind == "goal" }.map { it.projectId }.toSet() Column(Modifier.fillMaxWidth().padding(horizontal = CottonMetrics.contentMargin, vertical = 12.dp), verticalArrangement = Arrangement.spacedBy(6.dp)) { Text("Projects", style = CottonTextStyles.screenTitle.copy(fontFamily = CottonFonts.display), color = CottonPalette.ink) read.projects.forEach { project -> val isLinked = project.id in linked Text("${if (isLinked) "✓ " else ""}${project.name}", Modifier.fillMaxWidth().clickable(role = Role.Checkbox, onClick = { onToggle(project, isLinked) }).semantics { contentDescription = "${project.name}, ${if (isLinked) "linked" else "not linked"}" }.padding(vertical = 12.dp), style = CottonTextStyles.body, color = CottonPalette.ink) } } } @Composable private fun GoalArrangeSheet(goals: List, onDismiss: () -> Unit, onMove: (Int, Int) -> Unit) = ModalBottomSheet(onDismissRequest = onDismiss, containerColor = CottonPalette.ground) { Column(Modifier.fillMaxWidth().padding(horizontal = CottonMetrics.contentMargin, vertical = 12.dp), verticalArrangement = Arrangement.spacedBy(8.dp)) { Text("Arrange goals", style = CottonTextStyles.screenTitle.copy(fontFamily = CottonFonts.display), color = CottonPalette.ink) Text("This is your order of what stays in view.", style = CottonTextStyles.caption, color = CottonPalette.inkSecondary) goals.forEachIndexed { index, goal -> Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) { Text(goal.title, Modifier.weight(1f), style = CottonTextStyles.body, color = CottonPalette.ink, maxLines = 1, overflow = TextOverflow.Ellipsis) if (index > 0) Text("Up", Modifier.clickable(role = Role.Button, onClick = { onMove(index, index - 1) }).padding(8.dp), style = CottonTextStyles.caption, color = CottonPalette.accent) if (index < goals.lastIndex) Text("Down", Modifier.clickable(role = Role.Button, onClick = { onMove(index, index + 1) }).padding(8.dp), style = CottonTextStyles.caption, color = CottonPalette.accent) } } } } @Composable private fun GoalSkeletons() = Column(Modifier.padding(CottonMetrics.contentMargin), verticalArrangement = Arrangement.spacedBy(16.dp)) { repeat(3) { Box(Modifier.fillMaxWidth().size(height = 118.dp, width = 1.dp).background(CottonPalette.groundElevated, RoundedCornerShape(CottonMetrics.cardRadius))) } } @Composable private fun GoalDetailSkeleton() = Box(Modifier.fillMaxWidth().padding(CottonMetrics.contentMargin).size(height = 280.dp, width = 1.dp).background(CottonPalette.groundElevated, RoundedCornerShape(CottonMetrics.cardRadius))) @Composable private fun GoalEmpty(onNew: () -> Unit, warning: String?) = Column(Modifier.padding(CottonMetrics.contentMargin), verticalArrangement = Arrangement.spacedBy(10.dp)) { warning?.let { Text(it, style = CottonTextStyles.caption, color = CottonPalette.statusError) }; Text("No goals yet.", style = CottonTextStyles.screenTitle.copy(fontFamily = CottonFonts.display), color = CottonPalette.ink); Text("Say what you are going for — Cotton keeps it in view everywhere else.", style = CottonTextStyles.body, color = CottonPalette.inkBody); Text("New goal", Modifier.clickable(role = Role.Button, onClick = onNew).padding(vertical = 8.dp), style = CottonTextStyles.body, color = CottonPalette.accent) } @Composable private fun GoalGone() = Column(Modifier.padding(CottonMetrics.contentMargin)) { Text("This goal isn't here any more.", style = CottonTextStyles.screenTitle.copy(fontFamily = CottonFonts.display), color = CottonPalette.ink) } @Composable private fun GoalReadFailure(message: String, retry: () -> Unit) = Column(Modifier.padding(CottonMetrics.contentMargin), verticalArrangement = Arrangement.spacedBy(10.dp)) { Text("Goals could not be read.", style = CottonTextStyles.screenTitle.copy(fontFamily = CottonFonts.display), color = CottonPalette.ink); Text(message, style = CottonTextStyles.body, color = CottonPalette.inkBody); Text("Try again", Modifier.clickable(role = Role.Button, onClick = retry).padding(8.dp), style = CottonTextStyles.body, color = CottonPalette.accent) } @Composable private fun GoalNotice(message: String, onDismiss: () -> Unit) = AlertDialog(onDismissRequest = onDismiss, title = { Text("Goals", fontFamily = CottonFonts.display) }, text = { Text(message) }, confirmButton = { TextButton(onClick = onDismiss) { Text("OK") } }) private fun GoalsState.readOrNull(): GoalsRead? = when (this) { is GoalsState.Data -> read; is GoalsState.Error -> last; GoalsState.Loading -> null } private fun GoalDetailState.detailOrNull(): GoalDetailRead? = when (this) { is GoalDetailState.Data -> read; is GoalDetailState.Error -> last; GoalDetailState.Loading -> null } private fun GoalDetailState.draftOrEmpty(): GoalDraft = when (this) { is GoalDetailState.Data -> draft; is GoalDetailState.Error -> draft; GoalDetailState.Loading -> GoalDraft() } private fun stamp(status: GoalStatus): String? = when (status) { GoalStatus.Achieved -> "DONE"; GoalStatus.Dropped -> "DROPPED"; else -> null } private fun movedReceipt(status: GoalStatus): String = when (status) { GoalStatus.Achieved -> "Reached"; GoalStatus.Paused -> "Paused"; GoalStatus.Dropped -> "Set aside"; GoalStatus.Active -> "Back on" } private fun String.toLocalDateOrNull(): LocalDate? = runCatching { LocalDate.parse(this) }.getOrNull()