355 lines · 15.4 KB · KotlinRaw
package com.cottondb.android.ui.activityimport androidx.compose.foundation.backgroundimport androidx.compose.foundation.layout.Arrangementimport androidx.compose.foundation.layout.Columnimport androidx.compose.foundation.layout.ColumnScopeimport 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.layout.widthimport androidx.compose.foundation.lazy.LazyColumnimport androidx.compose.foundation.lazy.grid.GridCellsimport androidx.compose.foundation.lazy.grid.GridItemSpanimport androidx.compose.foundation.lazy.grid.LazyVerticalGridimport androidx.compose.foundation.lazy.grid.itemsimport androidx.compose.foundation.lazy.itemsimport androidx.compose.material3.AlertDialogimport androidx.compose.material3.Buttonimport androidx.compose.material3.HorizontalDividerimport androidx.compose.material3.Textimport androidx.compose.material3.TextButtonimport androidx.compose.runtime.Composableimport androidx.compose.runtime.getValueimport androidx.compose.runtime.mutableStateOfimport androidx.compose.runtime.rememberimport androidx.compose.runtime.setValueimport androidx.compose.runtime.saveable.rememberSaveableimport androidx.compose.ui.Alignmentimport androidx.compose.ui.Modifierimport androidx.compose.ui.draw.alphaimport androidx.compose.ui.semantics.contentDescriptionimport androidx.compose.ui.semantics.semanticsimport androidx.compose.ui.text.style.TextAlignimport androidx.compose.ui.text.style.TextOverflowimport androidx.compose.ui.unit.dpimport androidx.compose.ui.unit.spimport com.cottondb.android.activity.BinPresentationFactsimport com.cottondb.android.activitybin.BinEntryimport com.cottondb.android.activitybin.BinReadimport com.cottondb.android.design.CottonFontsimport com.cottondb.android.design.CottonMetricsimport 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.math.BigIntegerimport 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. */@Composablefun 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") } }, ) }}@Composableprivate 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)) } }}@Composableprivate fun ColumnScope.BinCollection( sections: List<BinPresentationFacts.Section>, 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()) } } } } } }}@Composableprivate 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)}@Composableprivate 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) }}@Composableprivate 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) }}@Composableprivate fun RestoreButton(title: String, writesAllowed: Boolean, onRestore: () -> Unit) { TextButton( onClick = { if (writesAllowed) onRestore() }, enabled = writesAllowed, modifier = Modifier.semantics { contentDescription = "Restore $title" }, ) { Text("Restore") }}@Composableprivate 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, )}@Composableprivate 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) {} } } }}@Composableprivate 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") }}@Composableprivate 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") }}@Composableprivate fun BinWriteProblem(problem: String) { Text(problem, Modifier.padding(bottom = 8.dp), style = CottonTextStyles.caption, color = CottonPalette.statusError)}@Composableprivate 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.Section> = 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(),)