280 lines · 10.7 KB · KotlinRaw
package com.cottondb.android.ui.takeimport androidx.compose.foundation.backgroundimport androidx.compose.foundation.clickableimport androidx.compose.foundation.rememberScrollStateimport androidx.compose.foundation.text.selection.SelectionContainerimport androidx.compose.foundation.verticalScrollimport androidx.compose.foundation.layout.Arrangementimport 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.paddingimport androidx.compose.foundation.layout.safeDrawingPaddingimport androidx.compose.foundation.shape.RoundedCornerShapeimport androidx.compose.material3.Textimport androidx.compose.runtime.Composableimport androidx.compose.ui.Alignmentimport androidx.compose.ui.Modifierimport androidx.compose.ui.graphics.Colorimport androidx.compose.ui.semantics.Roleimport androidx.compose.ui.semantics.contentDescriptionimport androidx.compose.ui.semantics.semanticsimport androidx.compose.ui.platform.testTagimport androidx.compose.ui.text.AnnotatedStringimport androidx.compose.ui.text.SpanStyleimport androidx.compose.ui.text.buildAnnotatedStringimport androidx.compose.ui.text.style.TextDecorationimport androidx.compose.ui.text.withStyleimport androidx.compose.ui.unit.dpimport androidx.compose.ui.unit.spimport com.cottondb.android.design.CottonFontsimport com.cottondb.android.design.CottonMetricsimport com.cottondb.android.design.CottonTextStylesimport com.cottondb.android.generated.CottonPaletteimport com.cottondb.android.take.TakeDestinationimport com.cottondb.android.take.TakePhaseimport com.cottondb.android.take.TakePresentationFactsimport com.cottondb.android.take.TakeStateimport com.cottondb.android.take.TakeWriteOutcomeimport com.cottondb.android.ui.paper.MemoryPaperDyeimport com.cottondb.android.ui.paper.PaperEdgeimport com.cottondb.android.ui.paper.TornPaper/** Controlled Take surface. Voice, write admission, clipboard, and timing belong to its host. */@Composablefun 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)) } }}@Composableprivate 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, )}@Composableprivate 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, ) }}@Composableprivate 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) }}@Composableprivate 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." }}@Composableprivate 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))}@Composableprivate 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 },)@Composableprivate 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<String>, 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) }}