package com.cottondb.android.ui.take import androidx.compose.foundation.background import androidx.compose.foundation.clickable import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.text.selection.SelectionContainer import androidx.compose.foundation.verticalScroll import androidx.compose.foundation.layout.Arrangement 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.padding import androidx.compose.foundation.layout.safeDrawingPadding import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.semantics.Role import androidx.compose.ui.semantics.contentDescription import androidx.compose.ui.semantics.semantics import androidx.compose.ui.platform.testTag import androidx.compose.ui.text.AnnotatedString import androidx.compose.ui.text.SpanStyle import androidx.compose.ui.text.buildAnnotatedString import androidx.compose.ui.text.style.TextDecoration import androidx.compose.ui.text.withStyle import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp 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.take.TakeDestination import com.cottondb.android.take.TakePhase import com.cottondb.android.take.TakePresentationFacts import com.cottondb.android.take.TakeState import com.cottondb.android.take.TakeWriteOutcome import com.cottondb.android.ui.paper.MemoryPaperDye import com.cottondb.android.ui.paper.PaperEdge import com.cottondb.android.ui.paper.TornPaper /** Controlled Take surface. Voice, write admission, clipboard, and timing belong to its host. */ @Composable fun TakeScreen( state: TakeState, onBack: () -> Unit, onDiscard: () -> Unit, onFinish: (TakeDestination) -> Unit, onRetry: () -> Unit, onCopy: (String) -> Unit, modifier: Modifier = Modifier, onReturnToKeyboard: (() -> Unit)? = null, ) { val busy = state.phase == TakePhase.Finishing Column( modifier.fillMaxSize().background(CottonPalette.groundMuted).safeDrawingPadding() .padding(horizontal = CottonMetrics.contentMargin), ) { TakeHeader(onBack) Column( Modifier .weight(1f) .fillMaxWidth() .verticalScroll(rememberScrollState()), verticalArrangement = Arrangement.spacedBy(16.dp), ) { when (state.phase) { TakePhase.Initial -> TakeSkeleton() TakePhase.Listening, TakePhase.Finishing -> TakeLiveBody(state) TakePhase.Unavailable -> TakeUnavailable(state, onRetry) TakePhase.Settled -> TakeSettled(state) } state.readProblem?.let { problem -> Text(problem, style = CottonTextStyles.caption, color = CottonPalette.statusWarning) } } TakeFooter(state, busy, onDiscard, onFinish, onRetry, onCopy) if (state.phase == TakePhase.Settled && state.handbackOutcome == com.cottondb.android.take.TakeHandbackOutcome.Published && onReturnToKeyboard != null) { TakeButton("Return to keyboard", true, onReturnToKeyboard, Modifier.fillMaxWidth().padding(bottom = 12.dp)) } } } @Composable private fun TakeHeader(onBack: () -> Unit) = Row( Modifier.fillMaxWidth().padding(vertical = 12.dp), verticalAlignment = Alignment.CenterVertically, ) { Text( "Back", Modifier.clickable(role = Role.Button, onClick = onBack).semantics { contentDescription = "Back" } .padding(vertical = 10.dp, horizontal = 4.dp), style = CottonTextStyles.preview, color = CottonPalette.inkSecondary, ) Text( "Take", Modifier.padding(start = 16.dp), style = CottonTextStyles.pageTitle.copy(fontFamily = CottonFonts.display), color = CottonPalette.ink, ) } @Composable private fun TakeLiveBody(state: TakeState) = Column(verticalArrangement = Arrangement.spacedBy(12.dp)) { if (state.phase == TakePhase.Listening && state.text.isBlank()) { Text( "Listening — just talk. Your vocabulary is helping.", style = CottonTextStyles.caption, color = CottonPalette.inkSecondary, ) } if (state.phase == TakePhase.Listening) { Text( "Listening, ${TakePresentationFacts.formatClock(state.elapsedMillis)}", Modifier.semantics { contentDescription = "Listening for ${TakePresentationFacts.formatClock(state.elapsedMillis)}" }, style = CottonTextStyles.preview.copy(fontFamily = CottonFonts.hand), color = CottonPalette.accent, ) } else { Text("Finishing…", style = CottonTextStyles.preview, color = CottonPalette.inkSecondary) } if (state.text.isNotEmpty()) { Text( transcriptWords(state.text, state.read?.vocabularyTerms.orEmpty(), settled = false), style = CottonTextStyles.body, color = CottonPalette.inkBody, ) } } @Composable private fun TakeUnavailable(state: TakeState, onRetry: () -> Unit) = TornPaper( "take-unavailable", MemoryPaperDye.Cream, Modifier.fillMaxWidth(), edge = PaperEdge.Cut, ) { Column(Modifier.padding(20.dp), verticalArrangement = Arrangement.spacedBy(12.dp)) { Text( state.problem ?: "Couldn't listen just now. If a call is running, finish it first — otherwise check the microphone permission in Settings.", style = CottonTextStyles.body, color = CottonPalette.inkBody, ) TakeButton("Try again", enabled = true, onClick = onRetry) } } @Composable private fun TakeSettled(state: TakeState) = Column(verticalArrangement = Arrangement.spacedBy(14.dp)) { TornPaper("take-slip", MemoryPaperDye.Cream, Modifier.fillMaxWidth(), edge = PaperEdge.Cut) { SelectionContainer(Modifier.testTag("take-settled-text")) { Text( transcriptWords(state.text, state.read?.vocabularyTerms.orEmpty(), settled = true), Modifier.padding(22.dp), style = CottonTextStyles.body.copy( fontFamily = CottonFonts.type, fontSize = 18.sp, lineHeight = 23.sp, ), color = CottonPalette.ink, ) } } state.problem?.let { problem -> Text(problem, style = CottonTextStyles.caption, color = CottonPalette.statusWarning) } (state.handbackOutcome as? com.cottondb.android.take.TakeHandbackOutcome.Failed)?.let { Text(it.message, style = CottonTextStyles.caption, color = CottonPalette.statusWarning) } takeOutcomeLine(state)?.let { line -> Text(line, style = CottonTextStyles.preview.copy(fontFamily = CottonFonts.hand), color = CottonPalette.inkSecondary) } } private fun takeOutcomeLine(state: TakeState): String? = when (val capture = state.captureOutcome) { is TakeWriteOutcome.Created -> "Filed into Cotton." is TakeWriteOutcome.KnownRefusal -> capture.message is TakeWriteOutcome.Unconfirmed -> capture.message is TakeWriteOutcome.NotAdmitted -> capture.message null -> when (val corpus = state.corpusOutcome) { is TakeWriteOutcome.Unconfirmed -> "Ready to copy. The database could not confirm this take." is TakeWriteOutcome.KnownRefusal -> "Ready to copy. ${corpus.message}" else -> "Ready to copy." } } @Composable private fun TakeFooter( state: TakeState, busy: Boolean, onDiscard: () -> Unit, onFinish: (TakeDestination) -> Unit, onRetry: () -> Unit, onCopy: (String) -> Unit, ) = when (state.phase) { TakePhase.Listening, TakePhase.Finishing -> Row( Modifier.fillMaxWidth().padding(vertical = 14.dp), horizontalArrangement = Arrangement.Center, verticalAlignment = Alignment.CenterVertically, ) { TakeButton("Discard", !busy, onDiscard) Spacer(Modifier.padding(horizontal = 5.dp)) TakeButton( if (busy) "Finishing…" else "Done", !busy, { onFinish(TakeDestination.Ready) }, Modifier.weight(1f), primary = true, ) Spacer(Modifier.padding(horizontal = 5.dp)) TakeButton("→ Cotton", !busy, { onFinish(TakeDestination.Cotton) }) } TakePhase.Settled -> Row( Modifier.fillMaxWidth().padding(vertical = 14.dp), horizontalArrangement = Arrangement.Center, ) { TakeButton("Copy", true, { onCopy(state.text) }) Spacer(Modifier.padding(horizontal = 5.dp)) TakeButton("Speak again", true, onRetry, primary = true) } else -> Spacer(Modifier.padding(vertical = 28.dp)) } @Composable private fun TakeButton( label: String, enabled: Boolean, onClick: () -> Unit, modifier: Modifier = Modifier, primary: Boolean = false, ) = Text( label, modifier.background( when { !enabled -> CottonPalette.hairline primary -> CottonPalette.accent else -> CottonPalette.chipGround }, RoundedCornerShape(16.dp), ).clickable(enabled = enabled, role = Role.Button, onClick = onClick) .semantics { contentDescription = label } .padding(horizontal = 16.dp, vertical = 15.dp), textAlign = androidx.compose.ui.text.style.TextAlign.Center, style = CottonTextStyles.preview.copy(fontFamily = CottonFonts.type), color = when { !enabled -> CottonPalette.inkTertiary primary -> CottonPalette.accentContrast else -> CottonPalette.ink }, ) @Composable private fun TakeSkeleton() = TornPaper( "take-loading", MemoryPaperDye.Cream, Modifier.fillMaxWidth().padding(top = 24.dp), edge = PaperEdge.Cut, ) { Spacer(Modifier.padding(vertical = 100.dp)) } private fun transcriptWords(text: String, vocabulary: List, settled: Boolean): AnnotatedString = buildAnnotatedString { TakePresentationFacts.spans(text, vocabulary).forEach { span -> if (span.isVocabulary) { withStyle( SpanStyle( background = if (settled) Color.Unspecified else CottonPalette.chipGround, textDecoration = if (settled) TextDecoration.Underline else null, ), ) { append(span.text) } } else append(span.text) } }