361 lines · 13.2 KB · KotlinRaw
package com.cottondb.android.ui.briefingimport androidx.compose.foundation.Imageimport androidx.compose.foundation.backgroundimport androidx.compose.foundation.clickableimport androidx.compose.foundation.horizontalScrollimport androidx.compose.foundation.layout.Arrangementimport androidx.compose.foundation.layout.Boximport androidx.compose.foundation.layout.Columnimport androidx.compose.foundation.layout.PaddingValuesimport androidx.compose.foundation.layout.Rowimport androidx.compose.foundation.layout.fillMaxSizeimport androidx.compose.foundation.layout.fillMaxWidthimport androidx.compose.foundation.layout.heightimport androidx.compose.foundation.layout.heightInimport androidx.compose.foundation.layout.paddingimport androidx.compose.foundation.layout.safeDrawingPaddingimport androidx.compose.foundation.layout.sizeimport androidx.compose.foundation.layout.widthimport androidx.compose.foundation.lazy.LazyColumnimport androidx.compose.foundation.rememberScrollStateimport androidx.compose.foundation.text.selection.SelectionContainerimport androidx.compose.material3.HorizontalDividerimport androidx.compose.material3.Textimport androidx.compose.material3.TextButtonimport androidx.compose.runtime.Composableimport androidx.compose.ui.Alignmentimport androidx.compose.ui.Modifierimport androidx.compose.ui.draw.alphaimport androidx.compose.ui.layout.ContentScaleimport androidx.compose.ui.res.painterResourceimport androidx.compose.ui.semantics.selectedimport androidx.compose.ui.semantics.Roleimport 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.Rimport com.cottondb.android.briefing.BriefingBlockimport com.cottondb.android.briefing.BriefingBlocksimport com.cottondb.android.briefing.BriefingProjectimport com.cottondb.android.briefing.BriefingReadimport com.cottondb.android.design.CottonFontsimport com.cottondb.android.design.CottonMetricsimport com.cottondb.android.design.CottonTextStylesimport com.cottondb.android.generated.CottonPaletteimport com.cottondb.android.home.HomeProjectColourimport com.cottondb.android.home.projectColoursimport com.cottondb.android.ui.paper.MemoryPaperDyeimport com.cottondb.android.ui.paper.PaperEdgeimport com.cottondb.android.ui.paper.TornPaper/** A controlled, read-only rendering of the native `brief` result. */@Composablefun BriefingScreen( read: BriefingRead?, lockedProject: String?, currentProject: String?, loading: Boolean, readProblem: String?, onRetry: () -> Unit, onSelectProject: (String) -> Unit, onBack: () -> Unit, modifier: Modifier = Modifier,) { Column( modifier = modifier.fillMaxSize().background(CottonPalette.groundMuted).safeDrawingPadding(), ) { BriefingHeader(read, lockedProject, onBack) when { readProblem != null && read == null -> BriefingFailure(readProblem, onRetry) read == null -> BriefingSkeleton() else -> { if (readProblem != null) BriefingCachedFailure(readProblem, onRetry) BriefingContent(read, lockedProject, currentProject, onSelectProject) } } }}@Composableprivate fun BriefingHeader(read: BriefingRead?, lockedProject: String?, onBack: () -> Unit) { val lockedName = lockedProject?.let { slug -> read?.projects?.firstOrNull { it.slug == slug }?.name ?: slug } Row( Modifier.fillMaxWidth().padding(horizontal = 8.dp), verticalAlignment = Alignment.CenterVertically, ) { TextButton(onClick = onBack, modifier = Modifier.semantics { contentDescription = "Back" }) { Text("Back") } Column(Modifier.padding(top = 12.dp)) { Text( "Briefing", style = CottonTextStyles.screenTitle.copy(fontFamily = CottonFonts.serif), color = CottonPalette.ink, ) lockedName?.let { Text(it, style = CottonTextStyles.caption, color = CottonPalette.inkSecondary) } } }}@Composableprivate fun BriefingFailure(problem: String, onRetry: () -> Unit) { Column(Modifier.padding(CottonMetrics.contentMargin)) { Text( "The briefing could not be read.", style = CottonTextStyles.body, color = CottonPalette.statusError, ) Text(problem, style = CottonTextStyles.caption, color = CottonPalette.inkSecondary) TextButton(onClick = onRetry, modifier = Modifier.semantics { contentDescription = "Try again" }) { Text("Try again") } }}@Composableprivate fun BriefingCachedFailure(problem: String, onRetry: () -> Unit) { Row(Modifier.fillMaxWidth().padding(horizontal = CottonMetrics.contentMargin)) { 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 BriefingContent( read: BriefingRead, lockedProject: String?, currentProject: String?, onSelectProject: (String) -> Unit,) { val effectiveProject = effectiveProject(read, lockedProject, currentProject) LazyColumn( contentPadding = PaddingValues(CottonMetrics.contentMargin), verticalArrangement = Arrangement.spacedBy(14.dp), ) { if (lockedProject == null) { item { ProjectRail(read.projects, effectiveProject, onSelectProject) } } item { BriefingMeter(read) } item { TornPaper( id = "briefing-${effectiveProject ?: "all"}", dye = MemoryPaperDye.Cream, modifier = Modifier.fillMaxWidth().semantics { contentDescription = "Briefing sheet" }, edge = PaperEdge.Cut, ) { SelectionContainer { Column( Modifier.fillMaxWidth() .heightIn(min = 420.dp) .padding(horizontal = 22.dp, vertical = 30.dp), ) { BriefingBlocks.parse(read.briefing.orEmpty()).forEachIndexed { index, block -> BriefingBlockText(block, first = index == 0) } } } } } }}private fun effectiveProject( read: BriefingRead, lockedProject: String?, currentProject: String?,): String? = lockedProject ?: currentProject ?: read.briefedProjects.firstOrNull() ?: read.projects.firstOrNull()?.slug@Composableprivate fun ProjectRail( projects: List<BriefingProject>, currentProject: String?, onSelectProject: (String) -> Unit,) { if (projects.isEmpty()) return val colours = projectColours(projects.map(BriefingProject::slug)) Row( modifier = Modifier.horizontalScroll(rememberScrollState()).padding(vertical = 6.dp), horizontalArrangement = Arrangement.spacedBy(16.dp), ) { projects.forEach { project -> val colour = colours.getValue(project.slug) val isSelected = project.slug == currentProject Column( horizontalAlignment = Alignment.CenterHorizontally, modifier = Modifier.width(72.dp) .clickable(role = Role.Button, onClick = { onSelectProject(project.slug) }) .semantics { contentDescription = "Briefing project ${project.name}"; selected = isSelected }, ) { Box(contentAlignment = Alignment.Center) { Image( painter = painterResource(briefingCapResource(colour)), contentDescription = null, modifier = Modifier.size(72.dp), contentScale = ContentScale.Fit, ) Text( project.name, Modifier.width(58.dp), style = CottonTextStyles.preview.copy( fontFamily = CottonFonts.cap, fontSize = 15.sp, lineHeight = 20.sp, ), color = if (briefingCapHasDarkInk(colour)) CottonPalette.ink else CottonPalette.ground, textAlign = TextAlign.Center, maxLines = 3, overflow = TextOverflow.Ellipsis, ) } Text( project.name, style = CottonTextStyles.caption.copy(fontSize = 12.sp, lineHeight = 15.sp), color = if (isSelected) CottonPalette.ink else CottonPalette.inkSecondary, maxLines = 1, overflow = TextOverflow.Ellipsis, ) HorizontalDivider( Modifier.width(40.dp).padding(top = 2.dp).alpha(if (isSelected) 1f else 0f), thickness = 1.2.dp, color = CottonPalette.ink, ) } } }}@Composableprivate fun BriefingMeter(read: BriefingRead) { Column(Modifier.fillMaxWidth(), horizontalAlignment = Alignment.End) { Text( BriefingBlocks.meter(read.tokensEstimate, read.budgetTokens, read.overBudget), style = CottonTextStyles.preview.copy(fontFamily = CottonFonts.hand, fontSize = 16.sp), color = if (read.overBudget) CottonPalette.statusError else CottonPalette.inkSecondary, ) BriefingBlocks.machine(read.machineScoped)?.let { Text(it, style = CottonTextStyles.caption, color = CottonPalette.inkSecondary) } }}@Composableprivate fun BriefingBlockText(block: BriefingBlock, first: Boolean) { val gap = BriefingBlocks.gapBefore(block, first).dp when (block) { is BriefingBlock.Heading -> Text( block.text, Modifier.padding(top = gap), style = CottonTextStyles.screenTitle.copy( fontFamily = CottonFonts.serif, fontSize = 22.sp, lineHeight = 27.sp, ), color = CottonPalette.ink, ) is BriefingBlock.Section -> Text( block.text, Modifier.padding(top = gap), style = CottonTextStyles.rowTitle.copy( fontFamily = CottonFonts.hand, fontSize = 19.sp, lineHeight = 23.sp, ), color = CottonPalette.ink, ) is BriefingBlock.Item -> Row( Modifier.padding(top = gap), verticalAlignment = Alignment.Top, ) { Text( "•", Modifier.padding(end = 8.dp), style = briefingBodyStyle(), color = CottonPalette.inkSecondary, ) Text( block.text, Modifier.weight(1f), style = briefingBodyStyle(), color = CottonPalette.ink, ) } is BriefingBlock.Line -> Text( block.text, Modifier.padding(top = gap), style = briefingBodyStyle(), color = CottonPalette.ink, ) BriefingBlock.Rule -> HorizontalDivider( Modifier.fillMaxWidth() .padding(top = gap) .height(3.dp) .semantics { contentDescription = "Briefing rule" }, thickness = 0.8.dp, color = CottonPalette.inkSecondary, ) }}private fun briefingBodyStyle() = CottonTextStyles.caption.copy( fontFamily = CottonFonts.type, fontSize = 13.sp, lineHeight = 17.sp,)@Composableprivate fun BriefingSkeleton() { TornPaper( id = "briefing-skeleton", dye = MemoryPaperDye.Cream, modifier = Modifier.fillMaxWidth() .padding(CottonMetrics.contentMargin) .height(420.dp) .alpha(.5f), edge = PaperEdge.Cut, ) {}}private fun briefingCapHasDarkInk(colour: HomeProjectColour): Boolean = colour in setOf( HomeProjectColour.CREAM, HomeProjectColour.YELLOW, HomeProjectColour.SILVER, HomeProjectColour.SKY, HomeProjectColour.PINK,)private fun briefingCapResource(colour: HomeProjectColour): Int = when (colour) { HomeProjectColour.CREAM -> R.drawable.cap_cream HomeProjectColour.RED -> R.drawable.cap_red HomeProjectColour.NAVY -> R.drawable.cap_navy HomeProjectColour.YELLOW -> R.drawable.cap_yellow HomeProjectColour.GREEN -> R.drawable.cap_green HomeProjectColour.SILVER -> R.drawable.cap_silver HomeProjectColour.ORANGE -> R.drawable.cap_orange HomeProjectColour.BURGUNDY -> R.drawable.cap_burgundy HomeProjectColour.SKY -> R.drawable.cap_sky HomeProjectColour.PINK -> R.drawable.cap_pink HomeProjectColour.BLACK -> R.drawable.cap_black HomeProjectColour.TEAL -> R.drawable.cap_teal}