package com.cottondb.android.ui.activity import androidx.compose.foundation.background import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.ColumnScope 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.layout.width import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.grid.GridCells import androidx.compose.foundation.lazy.grid.GridItemSpan import androidx.compose.foundation.lazy.grid.LazyVerticalGrid import androidx.compose.foundation.lazy.grid.items import androidx.compose.foundation.lazy.items import androidx.compose.material3.AlertDialog import androidx.compose.material3.Button import androidx.compose.material3.HorizontalDivider import androidx.compose.material3.Text import androidx.compose.material3.TextButton import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.alpha 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 androidx.compose.ui.unit.sp import com.cottondb.android.activity.BinPresentationFacts import com.cottondb.android.activitybin.BinEntry import com.cottondb.android.activitybin.BinRead 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 com.cottondb.android.ui.paper.MemoryPaperDye import com.cottondb.android.ui.paper.PaperEdge import com.cottondb.android.ui.paper.TornPaper import java.math.BigInteger import java.time.Clock /** * Controlled `/bin` surface. The parent owns its read, restore and empty mutations; this screen * holds only the destructive confirmation and delegates the admitted action through callbacks. */ @Composable fun BinScreen( read: BinRead?, loading: Boolean, readProblem: String?, writeProblem: String?, canWrite: Boolean, isGrid: Boolean, onLayoutChange: (Boolean) -> Unit, onRetry: () -> Unit, onRestore: (BinEntry) -> Unit, onEmpty: () -> Unit, onBack: () -> Unit, modifier: Modifier = Modifier, clock: Clock = Clock.systemDefaultZone(), ) { var confirmingEmpty by rememberSaveable { mutableStateOf(false) } val mountedClock = remember { Clock.fixed(clock.instant(), clock.zone) } val sections = read?.sections().orEmpty() val total = read?.let { BinPresentationFacts.total(it.counts) } val hasRows = sections.isNotEmpty() val writesAllowed = canWrite Column( modifier .fillMaxSize() .background(CottonPalette.groundMuted) .safeDrawingPadding() .padding(horizontal = CottonMetrics.contentMargin), ) { BinHeader( showLayoutToggle = total != null && total > BigInteger.ONE, isGrid = isGrid, onLayoutChange = onLayoutChange, onBack = onBack, ) if (readProblem != null && read != null) BinProblem(readProblem, onRetry) if (writeProblem != null) BinWriteProblem(writeProblem) when { read == null && loading -> BinSkeleton(isGrid) read == null && readProblem != null -> BinUnreadable(onRetry) read == null -> BinSkeleton(isGrid) !hasRows -> BinEmpty() else -> { BinCollection(sections, isGrid, mountedClock, writesAllowed, onRestore) BinEmptyAction(writesAllowed) { confirmingEmpty = true } } } } if (confirmingEmpty && total != null) { AlertDialog( onDismissRequest = { confirmingEmpty = false }, title = { Text("Empty the bin?") }, text = { Text( "This erases Cotton's copy of all $total — it cannot be undone. " + "Your mail provider keeps its own bin, so mail you binned is still there until they clear it.", ) }, dismissButton = { TextButton(onClick = { confirmingEmpty = false }) { Text("Cancel") } }, confirmButton = { Button( onClick = { if (writesAllowed) { confirmingEmpty = false onEmpty() } }, enabled = writesAllowed, modifier = Modifier.semantics { contentDescription = "Confirm empty bin" }, ) { Text("Empty") } }, ) } } @Composable private fun BinHeader( showLayoutToggle: Boolean, isGrid: Boolean, onLayoutChange: (Boolean) -> Unit, onBack: () -> Unit, ) { Row( Modifier.fillMaxWidth().padding(vertical = 12.dp), verticalAlignment = Alignment.CenterVertically, ) { TextButton(onClick = onBack, modifier = Modifier.semantics { contentDescription = "Back" }) { Text("Back") } Text( "Bin", Modifier.weight(1f), style = CottonTextStyles.screenTitle.copy(fontFamily = CottonFonts.serif), color = CottonPalette.ink, textAlign = TextAlign.Center, ) if (showLayoutToggle) { TextButton( onClick = { onLayoutChange(!isGrid) }, modifier = Modifier.semantics { contentDescription = "Change bin layout" }, ) { Text(if (isGrid) "List" else "Grid") } } else { Spacer(Modifier.width(48.dp)) } } } @Composable private fun ColumnScope.BinCollection( sections: List, isGrid: Boolean, clock: Clock, writesAllowed: Boolean, onRestore: (BinEntry) -> Unit, ) { if (isGrid) { LazyVerticalGrid( columns = GridCells.Fixed(2), modifier = Modifier.fillMaxWidth().weight(1f).semantics { contentDescription = "Bin entries" }, horizontalArrangement = Arrangement.spacedBy(12.dp), verticalArrangement = Arrangement.spacedBy(14.dp), ) { sections.forEach { section -> item(span = { GridItemSpan(maxLineSpan) }) { BinSectionHeading(section) } items(BinPresentationFacts.items(section), key = BinPresentationFacts.Item::id) { item -> BinCard(item, isGrid = true, clock = clock, writesAllowed = writesAllowed) { onRestore(item.entry.toBinEntry()) } } } } } else { LazyColumn( modifier = Modifier.fillMaxWidth().weight(1f).semantics { contentDescription = "Bin entries" }, verticalArrangement = Arrangement.spacedBy(10.dp), ) { sections.forEach { section -> item { BinSectionHeading(section) } items(BinPresentationFacts.items(section), key = BinPresentationFacts.Item::id) { item -> if (BinPresentationFacts.paperObject(item.kind, false) == BinPresentationFacts.PaperObject.Card) { BinCard(item, isGrid = false, clock = clock, writesAllowed = writesAllowed) { onRestore(item.entry.toBinEntry()) } } else { BinStrip(item, clock, writesAllowed) { onRestore(item.entry.toBinEntry()) } } } } } } } @Composable private fun BinSectionHeading(section: BinPresentationFacts.Section) = Row( Modifier.fillMaxWidth().padding(top = 18.dp, bottom = 2.dp).semantics { contentDescription = "${section.label}, ${BinPresentationFacts.kindCount(section.count)}" }, horizontalArrangement = Arrangement.SpaceBetween, ) { Text(section.label, style = CottonTextStyles.sectionLabel, color = CottonPalette.ink) Text(BinPresentationFacts.kindCount(section.count), style = CottonTextStyles.caption, color = CottonPalette.inkSecondary) } @Composable private fun BinCard( item: BinPresentationFacts.Item, isGrid: Boolean, clock: Clock, writesAllowed: Boolean, onRestore: () -> Unit, ) { val entry = item.entry val title = entry.title.orEmpty() Column(horizontalAlignment = Alignment.End) { TornPaper( item.id, MemoryPaperDye.Cream, Modifier .fillMaxWidth() .then(if (isGrid) Modifier.aspectRatio(.78f) else Modifier) .alpha(.6f) .semantics { contentDescription = "$title, ${BinPresentationFacts.kindWord(item.kind)}, ${BinPresentationFacts.rowMeta(entry.trashedAt, clock, clock.zone)}" }, edge = PaperEdge.Cut, ) { Column(Modifier.fillMaxSize().padding(14.dp), verticalArrangement = Arrangement.SpaceBetween) { Column { Text(title, style = CottonTextStyles.rowTitle.copy(fontFamily = CottonFonts.serif), color = CottonPalette.ink, maxLines = if (isGrid) 3 else 2, overflow = TextOverflow.Ellipsis) Text(BinPresentationFacts.kindWord(item.kind), Modifier.padding(top = 4.dp), style = CottonTextStyles.caption, color = CottonPalette.inkSecondary) } Text(BinPresentationFacts.rowMeta(entry.trashedAt, clock, clock.zone), style = CottonTextStyles.caption, color = CottonPalette.inkSecondary) } } RestoreButton(title, writesAllowed, onRestore) } } @Composable private fun BinStrip(item: BinPresentationFacts.Item, clock: Clock, writesAllowed: Boolean, onRestore: () -> Unit) { val entry = item.entry val title = entry.title.orEmpty() Column(horizontalAlignment = Alignment.End) { TornPaper( item.id, MemoryPaperDye.Cream, Modifier.fillMaxWidth().alpha(.6f).semantics { contentDescription = "$title, ${BinPresentationFacts.kindWord(item.kind)}, ${BinPresentationFacts.rowMeta(entry.trashedAt, clock, clock.zone)}" }, edge = PaperEdge.Cut, ) { Row(Modifier.fillMaxWidth().padding(14.dp), verticalAlignment = Alignment.CenterVertically) { Text(BinPresentationFacts.initial(title), Modifier.width(32.dp), style = CottonTextStyles.rowTitle, color = CottonPalette.inkSecondary) Column(Modifier.weight(1f)) { Text(title, style = CottonTextStyles.body, color = CottonPalette.ink, maxLines = 2, overflow = TextOverflow.Ellipsis) Text("${BinPresentationFacts.kindWord(item.kind)} · ${BinPresentationFacts.rowMeta(entry.trashedAt, clock, clock.zone)}", style = CottonTextStyles.caption, color = CottonPalette.inkSecondary) } } } RestoreButton(title, writesAllowed, onRestore) } } @Composable private fun RestoreButton(title: String, writesAllowed: Boolean, onRestore: () -> Unit) { TextButton( onClick = { if (writesAllowed) onRestore() }, enabled = writesAllowed, modifier = Modifier.semantics { contentDescription = "Restore $title" }, ) { Text("Restore") } } @Composable private fun BinEmptyAction(writesAllowed: Boolean, onEmpty: () -> Unit) = Column( Modifier.fillMaxWidth().padding(top = 16.dp, bottom = 12.dp), horizontalAlignment = Alignment.CenterHorizontally, ) { TextButton( onClick = { if (writesAllowed) onEmpty() }, enabled = writesAllowed, modifier = Modifier.semantics { contentDescription = "Empty the bin" }, ) { Text("Empty the bin", color = CottonPalette.statusError) } Text( "Your mail provider keeps its own bin, usually for about a month.", style = CottonTextStyles.caption.copy(fontFamily = CottonFonts.hand, fontSize = 17.sp), color = CottonPalette.inkSecondary, textAlign = TextAlign.Center, ) } @Composable private fun ColumnScope.BinSkeleton(isGrid: Boolean) { if (isGrid) { LazyVerticalGrid(GridCells.Fixed(2), Modifier.fillMaxWidth().weight(1f), horizontalArrangement = Arrangement.spacedBy(12.dp), verticalArrangement = Arrangement.spacedBy(14.dp)) { items(4) { index -> TornPaper("bin-skeleton-$index", MemoryPaperDye.Cream, Modifier.fillMaxWidth().aspectRatio(.78f), edge = PaperEdge.Cut) {} } } } else { LazyColumn(Modifier.fillMaxWidth().weight(1f), verticalArrangement = Arrangement.spacedBy(10.dp)) { items(3) { index -> TornPaper("bin-skeleton-$index", MemoryPaperDye.Cream, Modifier.fillMaxWidth().height(66.dp), edge = PaperEdge.Cut) {} } } } } @Composable private fun BinUnreadable(onRetry: () -> Unit) = Column(Modifier.fillMaxWidth().padding(top = 40.dp), horizontalAlignment = Alignment.CenterHorizontally) { TornPaper("bin-unreadable", MemoryPaperDye.Cream, Modifier.fillMaxWidth(), edge = PaperEdge.Cut) { Column(Modifier.padding(20.dp)) { Text("Couldn't read the bin", style = CottonTextStyles.body, color = CottonPalette.ink) Text("What you binned is on your database; this device could not read it just now.", Modifier.padding(top = 6.dp), style = CottonTextStyles.caption, color = CottonPalette.inkSecondary) } } TextButton(onClick = onRetry, modifier = Modifier.semantics { contentDescription = "Try again" }) { Text("Try again") } } @Composable private fun BinProblem(problem: String, onRetry: () -> Unit) = Row(Modifier.fillMaxWidth().padding(bottom = 8.dp), verticalAlignment = Alignment.CenterVertically) { Text(problem, Modifier.weight(1f), style = CottonTextStyles.caption, color = CottonPalette.statusError) TextButton(onClick = onRetry, modifier = Modifier.semantics { contentDescription = "Try again" }) { Text("Try again") } } @Composable private fun BinWriteProblem(problem: String) { Text(problem, Modifier.padding(bottom = 8.dp), style = CottonTextStyles.caption, color = CottonPalette.statusError) } @Composable private fun BinEmpty() = Column(Modifier.fillMaxWidth().padding(top = 40.dp), horizontalAlignment = Alignment.CenterHorizontally) { TornPaper("bin-empty", MemoryPaperDye.Cream, Modifier.fillMaxWidth(), edge = PaperEdge.Cut) { Column(Modifier.padding(20.dp)) { Text("The bin is empty", style = CottonTextStyles.body, color = CottonPalette.ink) Text("Anything you bin waits here until you empty it — nothing is erased on its own.", Modifier.padding(top = 6.dp), style = CottonTextStyles.caption, color = CottonPalette.inkSecondary) } } } private fun BinRead.sections(): List = BinPresentationFacts.sections( entries.map { BinPresentationFacts.Entry(it.id, it.kind, it.title, it.trashedAt) }, counts, ) private fun BinPresentationFacts.Entry.toBinEntry(): BinEntry = BinEntry( kind = kind.orEmpty(), id = id.orEmpty(), title = title.orEmpty(), trashedAt = trashedAt.orEmpty(), )