Cotton code
268 lines · 11.8 KB · KotlinRaw
package com.cottondb.android.ui.meetingsimport androidx.compose.foundation.backgroundimport androidx.compose.foundation.clickableimport androidx.compose.foundation.layout.Arrangementimport androidx.compose.foundation.layout.Boximport androidx.compose.foundation.layout.Columnimport androidx.compose.foundation.layout.Rowimport androidx.compose.foundation.layout.Spacerimport 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.itemsimport androidx.compose.material3.DropdownMenuimport androidx.compose.material3.DropdownMenuItemimport androidx.compose.material3.OutlinedTextFieldimport 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.ui.Alignmentimport androidx.compose.ui.Modifierimport androidx.compose.ui.draw.alphaimport androidx.compose.ui.semantics.Roleimport androidx.compose.ui.semantics.clearAndSetSemanticsimport androidx.compose.ui.semantics.contentDescriptionimport androidx.compose.ui.semantics.semanticsimport androidx.compose.ui.platform.LocalDensityimport androidx.compose.ui.unit.dpimport androidx.compose.ui.unit.spimport com.cottondb.android.design.CottonFontsimport 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 com.cottondb.android.work.meetings.MeetingListRowimport com.cottondb.android.work.meetings.MeetingWaveformFactsimport com.cottondb.android.work.meetings.MeetingPresentationFactsimport java.time.ZoneId/** Controlled `/meetings` list; all reads, navigation, recovery actions, and picker work stay in the host. */@Composablefun MeetingsScreen(    rows: List<MeetingListRow>?,    loading: Boolean,    readProblem: String?,    query: String,    onQuery: (String) -> Unit,    onOpenId: (String) -> Unit,    onRecord: () -> Unit,    onImport: () -> Unit,    onBack: () -> Unit,    onRefresh: () -> Unit,    zone: ZoneId,    modifier: Modifier = Modifier,    recovery: (@Composable () -> Unit)? = null,) {    var searching by remember { mutableStateOf(query.isNotBlank()) }    var menuOpen by remember { mutableStateOf(false) }    val bites = searching && query.isNotBlank()    val shown = if (bites) rows?.filter { MeetingPresentationFacts.matches(it, query, zone) }.orEmpty() else rows.orEmpty()    Column(modifier.fillMaxSize().background(CottonPalette.groundMuted).safeDrawingPadding()) {        MeetingsHeader(            searching = searching,            query = query,            onQuery = onQuery,            onBack = onBack,            onOpenSearch = { searching = true },            onCloseSearch = { searching = false; onQuery("") },            menuOpen = menuOpen,            onMenuOpen = { menuOpen = true },            onMenuDismiss = { menuOpen = false },            onRecord = { menuOpen = false; onRecord() },            onImport = { menuOpen = false; onImport() },        )        LazyColumn(Modifier.fillMaxSize(), verticalArrangement = Arrangement.spacedBy(14.dp)) {            recovery?.let { slot -> item("recovery") { Column(Modifier.padding(horizontal = 20.dp)) { slot() } } }            readProblem?.let { problem -> item("problem") { MeetingProblem(problem, onRefresh, enabled = !loading) } }            when {                rows == null && loading -> items((0 until 4).toList()) { MeetingSkeletonRow(it) }                rows == null -> item("unreadable") { MeetingUnreadable() }                rows.isEmpty() -> item("empty") { MeetingsEmpty(onRecord) }                bites && shown.isEmpty() -> item("no-matches") { NoMeetingMatches() }                else -> items(shown, key = MeetingListRow::id) { row -> MeetingRow(row, zone, onOpenId, Modifier.padding(horizontal = 20.dp).alpha(if (searching && !bites) .4f else 1f)) }            }        }    }}@Composableprivate fun MeetingsHeader(    searching: Boolean,    query: String,    onQuery: (String) -> Unit,    onBack: () -> Unit,    onOpenSearch: () -> Unit,    onCloseSearch: () -> Unit,    menuOpen: Boolean,    onMenuOpen: () -> Unit,    onMenuDismiss: () -> Unit,    onRecord: () -> Unit,    onImport: () -> Unit,) {    Row(        Modifier.fillMaxWidth().padding(horizontal = 16.dp, vertical = 8.dp),        verticalAlignment = Alignment.CenterVertically,    ) {        if (searching) {            OutlinedTextField(                value = query,                onValueChange = onQuery,                singleLine = true,                placeholder = { Text("Search meetings") },                modifier = Modifier.weight(1f).semantics { contentDescription = "Search meetings" },            )            TextButton(onClick = onCloseSearch) { Text("Close") }        } else {            TextButton(onClick = onBack, modifier = Modifier.semantics { contentDescription = "Back" }) { Text("Back") }            Text("Meetings", style = CottonTextStyles.pageTitle.copy(fontFamily = CottonFonts.display), color = CottonPalette.ink)            Spacer(Modifier.weight(1f))            TextButton(onClick = onOpenSearch, modifier = Modifier.semantics { contentDescription = "Search meetings" }) { Text("Search") }            Box {                TextButton(onClick = onMenuOpen, modifier = Modifier.semantics { contentDescription = "Add a meeting" }) { Text("Add") }                DropdownMenu(expanded = menuOpen, onDismissRequest = onMenuDismiss) {                    DropdownMenuItem(text = { Text("Record a meeting") }, onClick = onRecord)                    DropdownMenuItem(text = { Text("Import a recording") }, onClick = onImport)                }            }        }    }}@Composableprivate fun MeetingProblem(problem: String, onRefresh: () -> Unit, enabled: 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 = enabled) { Text("Try again") }    }}@Composableprivate fun MeetingUnreadable() {    Box(Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {        Text("This could not be read.", style = CottonTextStyles.screenTitle.copy(fontFamily = CottonFonts.serif), color = CottonPalette.ink)    }}@Composableprivate fun MeetingsEmpty(onRecord: () -> Unit) {    Column(        Modifier.fillMaxSize().padding(32.dp),        verticalArrangement = Arrangement.Center,        horizontalAlignment = Alignment.CenterHorizontally,    ) {        Text("No meetings yet.", style = CottonTextStyles.screenTitle.copy(fontFamily = CottonFonts.serif), color = CottonPalette.ink)        Text(            "Cotton writes a recording down, marks who spoke, and reads it back to you. The recording is kept, so a better transcript can always be made of it later.",            Modifier.padding(top = 8.dp),            style = CottonTextStyles.body.copy(fontFamily = CottonFonts.type),            color = CottonPalette.inkSecondary,        )        TextButton(onClick = onRecord, modifier = Modifier.semantics { contentDescription = "Record a meeting" }) { Text("Record a meeting") }    }}@Composableprivate fun NoMeetingMatches() {    Box(Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {        Text("No meeting is named, summarised or spoken in by that.", style = CottonTextStyles.body.copy(fontFamily = CottonFonts.type), color = CottonPalette.inkSecondary)    }}@Composableprivate fun MeetingSkeletonRow(index: Int) {    TornPaper(        id = "meeting-blank-$index",        dye = MemoryPaperDye.Cream,        modifier = Modifier.fillMaxWidth().padding(horizontal = 20.dp).height(118.dp),        edge = PaperEdge.Cut,    ) {}}@Composableprivate fun MeetingRow(row: MeetingListRow, zone: ZoneId, onOpenId: (String) -> Unit, modifier: Modifier) {    val hand = MeetingPresentationFacts.handStatus(row.status, row.waitingToUpload)    val failed = row.status == "failed"    TornPaper(        id = row.id,        dye = MemoryPaperDye.Cream,        modifier = modifier.fillMaxWidth().clickable(role = Role.Button) { onOpenId(row.id) }            .semantics { contentDescription = "Open ${MeetingPresentationFacts.title(row, zone)}" },        edge = PaperEdge.Cut,    ) {        Column(Modifier.padding(18.dp)) {            Text(                MeetingPresentationFacts.title(row, zone),                maxLines = 2,                style = CottonTextStyles.screenTitle.copy(fontFamily = CottonFonts.serif, fontSize = 20.sp),                color = CottonPalette.ink,            )            MeetingPresentationFacts.paperLine(row, zone)?.let { line ->                Text(line, Modifier.padding(top = 4.dp), style = CottonTextStyles.caption.copy(fontFamily = CottonFonts.type), color = CottonPalette.inkSecondary)            }            Row(Modifier.fillMaxWidth().height(34.dp).padding(top = 12.dp), verticalAlignment = Alignment.CenterVertically) {                PlayDisc(enabled = hand == null)                Spacer(Modifier.width(12.dp))                if (hand != null) {                    Text(hand, maxLines = 1, style = CottonTextStyles.preview.copy(fontFamily = CottonFonts.hand, fontSize = 18.sp), color = if (failed) CottonPalette.statusError else CottonPalette.inkSecondary)                } else {                    DecorativeMeetingWaveform(row.id, Modifier.weight(1f).height(28.dp))                }            }        }    }}@Composableprivate fun PlayDisc(enabled: Boolean) {    androidx.compose.foundation.Canvas(        Modifier.height(32.dp).width(32.dp)            .clearAndSetSemantics {},    ) {        val opacity = if (enabled) 1f else .4f        drawCircle(CottonPalette.inkSecondary.copy(alpha = .16f * opacity), radius = size.minDimension / 2f, center = androidx.compose.ui.geometry.Offset(size.width / 2f, size.height / 2f))        val triangle = androidx.compose.ui.graphics.Path().apply {            moveTo(size.width * .43f, size.height * .33f)            lineTo(size.width * .43f, size.height * .67f)            lineTo(size.width * .69f, size.height * .50f)            close()        }        drawPath(triangle, CottonPalette.ink, alpha = opacity)    }}/** Swift MeetingWaveform rendered from the shared deterministic, non-audio facts. */@Composableprivate fun DecorativeMeetingWaveform(id: String, modifier: Modifier = Modifier) {    val density = LocalDensity.current    androidx.compose.foundation.Canvas(modifier.clearAndSetSemantics {}) {        val pitch = with(density) { 3.dp.toPx() }        val bars = MeetingWaveformFacts.bars(            MeetingWaveformFacts.samples(id),            MeetingWaveformFacts.barCount(size.width.toDouble(), pitch.toDouble()),        )        if (bars.isEmpty()) return@Canvas        val width = size.width / bars.size        bars.forEachIndexed { index, value ->            val height = size.height * value.toFloat()            drawRect(                color = CottonPalette.inkSecondary,                topLeft = androidx.compose.ui.geometry.Offset(index * width + width * .275f, (size.height - height) / 2f),                size = androidx.compose.ui.geometry.Size(width * .45f, height),            )        }    }}