package eu.friedcutlet.yokan.ui.screens.detail import androidx.compose.animation.animateContentSize import androidx.compose.animation.core.CubicBezierEasing import androidx.compose.animation.core.tween import androidx.compose.foundation.ExperimentalFoundationApi import androidx.compose.foundation.background import androidx.compose.foundation.clickable import androidx.compose.foundation.combinedClickable 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.WindowInsets import androidx.compose.foundation.layout.asPaddingValues 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.size import androidx.compose.foundation.layout.statusBars import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.text.selection.SelectionContainer import androidx.compose.material.icons.Icons import androidx.compose.material.icons.filled.ArrowDropDown import androidx.compose.material.icons.filled.ExpandLess import androidx.compose.material.icons.filled.ExpandMore import androidx.compose.material.icons.filled.Info import androidx.compose.material3.DropdownMenu import androidx.compose.material3.DropdownMenuItem import androidx.compose.material3.Icon import androidx.compose.material3.IconButton import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Text 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.draw.clip import androidx.compose.ui.graphics.Brush import androidx.compose.ui.graphics.Color import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.dp import eu.friedcutlet.yokan.domain.Manga import eu.friedcutlet.yokan.ui.components.CoverImage @Composable internal fun DetailBackdropHeader( manga: Manga, showSource: Boolean, onTitleSelected: (String) -> Unit, onCoverClick: () -> Unit, ) { val coverModel: Any? = manga.customCoverImage ?: manga.customCoverFromTracker?.takeIf { it.isNotBlank() } ?: manga.imageUrl?.takeIf { it.isNotBlank() } val titleOptions = manga.alternativeTitles.ifEmpty { listOf(manga.name) } var titleMenuOpen by remember(manga.id, titleOptions) { mutableStateOf(false) } // The header underlaps the transparent app bar (content skips the top // scaffold padding), so grow it by status-bar inset + 64.dp app-bar height: // the backdrop fills from y=0 while the bottom-aligned content keeps the // same 300.dp of space clear of the app bar. val appBarOffset = WindowInsets.statusBars.asPaddingValues().calculateTopPadding() + 64.dp Box(modifier = Modifier.fillMaxWidth().height(300.dp + appBarOffset)) { // Mangayomi keeps the cover barely visible beneath a scaffold-colored scrim. if (coverModel != null) { CoverImage( model = coverModel, contentDescription = null, contentScale = ContentScale.Crop, modifier = Modifier .fillMaxSize() .alpha(0.16f), ) } // Scrim gradient so foreground text remains readable Box( modifier = Modifier .fillMaxSize() .background( Brush.verticalGradient( colors = listOf( Color.Transparent, eu.friedcutlet.yokan.ui.theme.LocalYokanScaffoldColor.current.copy(alpha = 0.55f), eu.friedcutlet.yokan.ui.theme.LocalYokanScaffoldColor.current, ), ), ), ) // Bottom row: thumbnail + title block Row( modifier = Modifier .align(Alignment.BottomStart) .fillMaxWidth() .padding(horizontal = 13.dp, vertical = 20.dp), verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(13.dp), ) { CoverImage( model = coverModel, contentDescription = "${manga.name} cover", contentScale = ContentScale.Crop, fallbackInitials = manga.name.initials(), modifier = Modifier .size(width = 98.dp, height = 150.dp) .clip(RoundedCornerShape(5.dp)) .background(MaterialTheme.colorScheme.primary.copy(alpha = 0.16f)) .clickable(onClickLabel = "View full-size cover", onClick = onCoverClick), ) Column(modifier = Modifier.weight(1f), verticalArrangement = Arrangement.spacedBy(4.dp)) { Box { Row( modifier = Modifier .fillMaxWidth() .clickable(enabled = titleOptions.size > 1) { titleMenuOpen = true }, verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(4.dp), ) { SelectionContainer(Modifier.weight(1f, fill = false)) { Text( manga.name, style = MaterialTheme.typography.titleLarge, fontWeight = FontWeight.Normal, maxLines = 3, overflow = TextOverflow.Ellipsis, ) } if (titleOptions.size > 1) { Icon(Icons.Filled.ArrowDropDown, contentDescription = "Select title") } } TitleOptionsMenu( expanded = titleMenuOpen, titleOptions = titleOptions, onDismiss = { titleMenuOpen = false }, onSelect = { titleMenuOpen = false onTitleSelected(it) }, ) } Text( text = manga.author?.takeIf { it.isNotBlank() } ?: "Unknown", style = MaterialTheme.typography.bodyMedium, color = MaterialTheme.colorScheme.onSurfaceVariant, maxLines = 1, overflow = TextOverflow.Ellipsis, ) Row( verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(4.dp), ) { Icon( Icons.Filled.Info, contentDescription = null, modifier = Modifier.size(14.dp), tint = MaterialTheme.colorScheme.onSurfaceVariant, ) Text( text = if (showSource) { "${manga.status.label} • ${manga.source} (${manga.lang.uppercase()})" } else { manga.status.label }, style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant, maxLines = 2, overflow = TextOverflow.Ellipsis, ) } } } } } @Composable internal fun DetailSideHeader( manga: Manga, showSource: Boolean, onTitleSelected: (String) -> Unit, onCoverClick: () -> Unit, ) { val coverModel: Any? = manga.customCoverImage ?: manga.customCoverFromTracker?.takeIf { it.isNotBlank() } ?: manga.imageUrl?.takeIf { it.isNotBlank() } val titleOptions = manga.alternativeTitles.ifEmpty { listOf(manga.name) } var titleMenuOpen by remember(manga.id, titleOptions) { mutableStateOf(false) } Row( modifier = Modifier .fillMaxWidth() .padding(horizontal = 13.dp, vertical = 20.dp), verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(13.dp), ) { CoverImage( model = coverModel, contentDescription = "${manga.name} cover", contentScale = ContentScale.Crop, fallbackInitials = manga.name.initials(), modifier = Modifier .size(width = 98.dp, height = 150.dp) .clip(RoundedCornerShape(5.dp)) .background(MaterialTheme.colorScheme.primary.copy(alpha = 0.16f)) .clickable(onClickLabel = "View full-size cover", onClick = onCoverClick), ) Column( modifier = Modifier.weight(1f), verticalArrangement = Arrangement.spacedBy(6.dp), ) { Box { Row( modifier = Modifier .fillMaxWidth() .clickable(enabled = titleOptions.size > 1) { titleMenuOpen = true }, verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(4.dp), ) { SelectionContainer(Modifier.weight(1f, fill = false)) { Text( manga.name, style = MaterialTheme.typography.titleLarge, fontWeight = FontWeight.Normal, maxLines = 4, overflow = TextOverflow.Ellipsis, ) } if (titleOptions.size > 1) { Icon(Icons.Filled.ArrowDropDown, contentDescription = "Select title") } } TitleOptionsMenu( expanded = titleMenuOpen, titleOptions = titleOptions, onDismiss = { titleMenuOpen = false }, onSelect = { titleMenuOpen = false onTitleSelected(it) }, ) } Text( text = manga.author?.takeIf { it.isNotBlank() } ?: "Unknown", style = MaterialTheme.typography.bodyMedium, color = MaterialTheme.colorScheme.onSurfaceVariant, maxLines = 1, overflow = TextOverflow.Ellipsis, ) Row( verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(4.dp), ) { Icon( Icons.Filled.Info, contentDescription = null, modifier = Modifier.size(14.dp), tint = MaterialTheme.colorScheme.onSurfaceVariant, ) Text( text = if (showSource) { "${manga.status.label} • ${manga.source} (${manga.lang.uppercase()})" } else { manga.status.label }, style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant, maxLines = 2, overflow = TextOverflow.Ellipsis, ) } } } } @Composable private fun TitleOptionsMenu( expanded: Boolean, titleOptions: List, onDismiss: () -> Unit, onSelect: (String) -> Unit, ) { DropdownMenu(expanded = expanded, onDismissRequest = onDismiss) { titleOptions.forEach { title -> DropdownMenuItem( text = { Text( title, maxLines = 2, overflow = TextOverflow.Ellipsis, ) }, onClick = { onSelect(title) }, ) } } } @OptIn(ExperimentalFoundationApi::class) @Composable internal fun DetailDescription(text: String, expanded: Boolean, onToggle: () -> Unit) { var collapsedOverflows by remember(text) { mutableStateOf(false) } Column( modifier = Modifier .fillMaxWidth() .animateContentSize( animationSpec = tween( durationMillis = 500, easing = CubicBezierEasing(0.18f, 1f, 0.04f, 1f), ), ) .padding(horizontal = 14.dp, vertical = 8.dp), ) { SelectionContainer { Text( text = text.trim(), style = MaterialTheme.typography.bodyMedium, maxLines = if (expanded) Int.MAX_VALUE else 3, overflow = TextOverflow.Ellipsis, onTextLayout = { layout -> if (!expanded) collapsedOverflows = layout.hasVisualOverflow }, modifier = Modifier .fillMaxWidth() .combinedClickable(onClick = { if (collapsedOverflows || expanded) onToggle() }), ) } if (collapsedOverflows || expanded) { IconButton( onClick = onToggle, modifier = Modifier.align(Alignment.CenterHorizontally).size(32.dp), ) { Icon( imageVector = if (expanded) Icons.Filled.ExpandLess else Icons.Filled.ExpandMore, contentDescription = if (expanded) "Collapse description" else "Expand description", ) } } } } private fun String.initials(): String = split(" ") .filter { it.isNotBlank() } .take(2) .joinToString("") { it.first().uppercase() } .ifBlank { "?" }