package com.cottondb.android.ui.meetings 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.Row import androidx.compose.foundation.layout.Spacer 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.items import androidx.compose.material3.DropdownMenu import androidx.compose.material3.DropdownMenuItem import androidx.compose.material3.OutlinedTextField 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.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.alpha import androidx.compose.ui.semantics.Role import androidx.compose.ui.semantics.clearAndSetSemantics import androidx.compose.ui.semantics.contentDescription import androidx.compose.ui.semantics.semantics import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp 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 com.cottondb.android.work.meetings.MeetingListRow import com.cottondb.android.work.meetings.MeetingWaveformFacts import com.cottondb.android.work.meetings.MeetingPresentationFacts import java.time.ZoneId /** Controlled `/meetings` list; all reads, navigation, recovery actions, and picker work stay in the host. */ @Composable fun MeetingsScreen( rows: List?, 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)) } } } } } @Composable private 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) } } } } } @Composable private 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") } } } @Composable private fun MeetingUnreadable() { Box(Modifier.fillMaxSize(), contentAlignment = Alignment.Center) { Text("This could not be read.", style = CottonTextStyles.screenTitle.copy(fontFamily = CottonFonts.serif), color = CottonPalette.ink) } } @Composable private 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") } } } @Composable private 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) } } @Composable private 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, ) {} } @Composable private 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)) } } } } } @Composable private 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. */ @Composable private 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), ) } } }