package eu.friedcutlet.yokan.ui.screens.reader import androidx.compose.animation.EnterTransition import androidx.compose.animation.ExitTransition import androidx.compose.animation.AnimatedVisibility import androidx.compose.animation.core.CubicBezierEasing import androidx.compose.animation.core.tween import androidx.compose.animation.expandVertically import androidx.compose.animation.shrinkVertically import androidx.compose.foundation.BorderStroke import androidx.compose.foundation.background import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.BoxScope import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxHeight 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.width import androidx.compose.foundation.layout.widthIn import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.items import androidx.compose.foundation.lazy.rememberLazyListState import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material.icons.Icons import androidx.compose.material.icons.automirrored.filled.ArrowBack import androidx.compose.material.icons.automirrored.filled.FormatListBulleted import androidx.compose.material.icons.automirrored.filled.MenuBook import androidx.compose.material.icons.automirrored.filled.NavigateNext import androidx.compose.material.icons.filled.Bookmark import androidx.compose.material.icons.filled.BookmarkBorder import androidx.compose.material.icons.filled.CheckCircleOutline import androidx.compose.material.icons.filled.Close import androidx.compose.material.icons.filled.KeyboardArrowDown import androidx.compose.material.icons.filled.Public import androidx.compose.material.icons.filled.Refresh import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.HorizontalDivider import androidx.compose.material3.Icon import androidx.compose.material3.IconButton import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Surface import androidx.compose.material3.Text import androidx.compose.material.icons.filled.MoreVert import androidx.compose.material3.DropdownMenu import androidx.compose.material3.DropdownMenuItem import org.jetbrains.compose.resources.stringResource import yokan.composeapp.generated.resources.Res import yokan.composeapp.generated.resources.action_back import yokan.composeapp.generated.resources.ui_close import yokan.composeapp.generated.resources.reader_image_actions import yokan.composeapp.generated.resources.reader_refresh_pages import yokan.composeapp.generated.resources.reader_open_web import yokan.composeapp.generated.resources.reader_actions_menu import androidx.compose.material3.TopAppBar import androidx.compose.material3.TopAppBarDefaults import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.runtime.setValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import androidx.compose.ui.graphics.Color import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.dp import androidx.compose.ui.window.Dialog import androidx.compose.ui.window.DialogProperties import eu.friedcutlet.yokan.domain.Chapter import eu.friedcutlet.yokan.ui.status.GlobalStatusAccessButton /** * Reader top app bar — back / title / chapter-list / bookmark / refresh / web actions. * * Title is a two-line label: bold manga name + smaller chapter subtitle. * The bookmark icon swaps between filled/outline mirroring [isBookmarked]. * The refresh and web buttons are hidden for local-archive chapters since they * have no source page list or remote URL. */ @OptIn(ExperimentalMaterial3Api::class) @Composable internal fun ReaderTopBar( mangaTitle: String, chapterTitle: String, isBookmarked: Boolean, showWebAction: Boolean, showRefreshAction: Boolean, onBack: () -> Unit, onChapterList: () -> Unit, onToggleBookmark: () -> Unit, onRefreshPages: () -> Unit, onWeb: () -> Unit, showImageActions: Boolean, onImageActions: () -> Unit, onOpenMenu: () -> Unit, ) { var menuOpen by remember { mutableStateOf(false) } TopAppBar( title = { Column { Text( text = mangaTitle, style = MaterialTheme.typography.titleMedium, fontWeight = FontWeight.SemiBold, maxLines = 1, overflow = TextOverflow.Ellipsis, ) Text( text = chapterTitle, style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant, maxLines = 1, overflow = TextOverflow.Ellipsis, ) } }, navigationIcon = { IconButton(onClick = onBack) { Icon(Icons.AutoMirrored.Filled.ArrowBack, contentDescription = stringResource(Res.string.action_back)) } }, actions = { GlobalStatusAccessButton() IconButton(onClick = onChapterList) { Icon( Icons.AutoMirrored.Filled.FormatListBulleted, contentDescription = "Chapter list", ) } IconButton(onClick = onToggleBookmark) { Icon( imageVector = if (isBookmarked) Icons.Filled.Bookmark else Icons.Filled.BookmarkBorder, contentDescription = if (isBookmarked) "Remove bookmark" else "Bookmark", ) } Box { IconButton(onClick = { onOpenMenu(); menuOpen = true }) { Icon(Icons.Filled.MoreVert, contentDescription = stringResource(Res.string.reader_actions_menu)) } DropdownMenu(expanded = menuOpen, onDismissRequest = { menuOpen = false }) { if (showImageActions) DropdownMenuItem( text = { Text(stringResource(Res.string.reader_image_actions)) }, onClick = { menuOpen = false; onImageActions() }, ) if (showRefreshAction) DropdownMenuItem( text = { Text(stringResource(Res.string.reader_refresh_pages)) }, onClick = { menuOpen = false; onRefreshPages() }, ) if (showWebAction) DropdownMenuItem( text = { Text(stringResource(Res.string.reader_open_web)) }, onClick = { menuOpen = false; onWeb() }, ) } } }, colors = TopAppBarDefaults.topAppBarColors( containerColor = MaterialTheme.colorScheme.surface.copy(alpha = 0.94f), ), ) } @Composable internal fun ReaderChapterListDialog( chapters: List, currentChapterId: Long, onDismiss: () -> Unit, onSelect: (Chapter) -> Unit, ) { val currentIndex = remember(chapters, currentChapterId) { chapters.indexOfFirst { it.id == currentChapterId }.coerceAtLeast(0) } val listState = rememberLazyListState( initialFirstVisibleItemIndex = (currentIndex - 2).coerceAtLeast(0), ) Dialog( onDismissRequest = onDismiss, properties = DialogProperties(usePlatformDefaultWidth = false), ) { Surface( modifier = Modifier.fillMaxWidth(0.85f).fillMaxHeight(0.8f), shape = RoundedCornerShape(20.dp), tonalElevation = 8.dp, ) { Column { Row( modifier = Modifier.fillMaxWidth().padding(start = 20.dp, end = 8.dp, top = 8.dp, bottom = 8.dp), verticalAlignment = Alignment.CenterVertically, ) { Icon( imageVector = Icons.AutoMirrored.Filled.MenuBook, contentDescription = null, tint = MaterialTheme.colorScheme.primary, ) Spacer(Modifier.width(12.dp)) Text( text = "Chapters", style = MaterialTheme.typography.titleLarge, color = MaterialTheme.colorScheme.primary, fontWeight = FontWeight.Bold, modifier = Modifier.weight(1f), ) IconButton(onClick = onDismiss) { Icon(Icons.Filled.Close, contentDescription = stringResource(Res.string.ui_close)) } } HorizontalDivider(color = MaterialTheme.colorScheme.primary.copy(alpha = 0.2f)) LazyColumn( state = listState, modifier = Modifier.fillMaxSize(), contentPadding = androidx.compose.foundation.layout.PaddingValues(12.dp), verticalArrangement = Arrangement.spacedBy(6.dp), ) { items(chapters, key = Chapter::id) { chapter -> val current = chapter.id == currentChapterId Surface( onClick = { if (!current) onSelect(chapter) }, modifier = Modifier.fillMaxWidth(), shape = RoundedCornerShape(12.dp), color = if (current) { MaterialTheme.colorScheme.primary.copy(alpha = 0.15f) } else { Color.Transparent }, border = if (current) { BorderStroke(1.5.dp, MaterialTheme.colorScheme.primary.copy(alpha = 0.4f)) } else { null }, ) { Row( modifier = Modifier.fillMaxWidth().padding(horizontal = 16.dp, vertical = 12.dp), verticalAlignment = Alignment.CenterVertically, ) { Box( Modifier .width(4.dp) .height(48.dp) .clip(RoundedCornerShape(2.dp)) .background( if (chapter.isRead) { MaterialTheme.colorScheme.onSurface.copy(alpha = 0.25f) } else { MaterialTheme.colorScheme.primary }, ), ) Spacer(Modifier.width(12.dp)) Column(Modifier.weight(1f)) { Text( text = chapter.name, style = MaterialTheme.typography.bodyMedium, fontWeight = if (current) FontWeight.Bold else FontWeight.Medium, color = if (chapter.isRead) { MaterialTheme.colorScheme.onSurface.copy(alpha = 0.45f) } else { MaterialTheme.colorScheme.onSurface }, maxLines = 2, overflow = TextOverflow.Ellipsis, ) listOf(chapter.scanlator, chapter.dateUpload) .filter(String::isNotBlank) .joinToString(" · ") .takeIf(String::isNotBlank) ?.let { metadata -> Spacer(Modifier.height(4.dp)) Text( text = metadata, style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant, maxLines = 1, overflow = TextOverflow.Ellipsis, ) } } if (chapter.isBookmarked) { Icon( Icons.Filled.Bookmark, contentDescription = "Bookmarked", tint = MaterialTheme.colorScheme.primary, ) } } } } } } } } } /** * Expands and collapses reader chrome height over the authority's 300 ms ease * curve. The bars stay pinned to their respective screen edges while content * is clipped by the size transition. */ @Composable internal fun BoxScope.ReaderOverlayBars( visible: Boolean, topBar: @Composable () -> Unit, bottomBar: @Composable () -> Unit, ) { val eink = eu.friedcutlet.yokan.ui.theme.LocalEinkMode.current AnimatedVisibility( visible = visible, enter = if (eink) EnterTransition.None else expandVertically( animationSpec = tween(300, easing = ReaderChromeEasing), expandFrom = Alignment.Top, ), exit = if (eink) ExitTransition.None else shrinkVertically( animationSpec = tween(300, easing = ReaderChromeEasing), shrinkTowards = Alignment.Top, ), modifier = Modifier.align(Alignment.TopCenter), ) { topBar() } AnimatedVisibility( visible = visible, enter = if (eink) EnterTransition.None else expandVertically( animationSpec = tween(300, easing = ReaderChromeEasing), expandFrom = Alignment.Bottom, ), exit = if (eink) ExitTransition.None else shrinkVertically( animationSpec = tween(300, easing = ReaderChromeEasing), shrinkTowards = Alignment.Bottom, ), modifier = Modifier.align(Alignment.BottomCenter), ) { bottomBar() } } private val ReaderChromeEasing = CubicBezierEasing(0.25f, 0.1f, 0.25f, 1f) /** * Floating page indicator pill at bottom-center, shown when chrome is hidden. * Mirrors the Flutter behavior — when the user dismisses the bars they still * want to know where they are. Gated by `DbSettings.showPagesNumber` upstream. */ @Composable internal fun BoxScope.ReaderPageIndicatorPill(currentLabel: String, total: Int) { Box( modifier = Modifier .align(Alignment.BottomCenter) .padding(bottom = 18.dp) .clip(RoundedCornerShape(percent = 50)) .background(Color.Black.copy(alpha = 0.55f)) .padding(horizontal = 14.dp, vertical = 6.dp), ) { Text( text = "$currentLabel / $total", color = Color.White, style = MaterialTheme.typography.labelMedium, ) } } @Composable internal fun ReaderChapterTransitionPage( currentChapter: Chapter, nextChapter: Chapter?, mangaTitle: String, vertical: Boolean, isRightToLeft: Boolean, modifier: Modifier = Modifier, ) { Box( modifier = modifier.background(MaterialTheme.colorScheme.surface), contentAlignment = Alignment.Center, ) { if (vertical) { Column( modifier = Modifier.fillMaxWidth().widthIn(max = 480.dp).padding(24.dp), horizontalAlignment = Alignment.CenterHorizontally, ) { TransitionHeading() Spacer(Modifier.height(20.dp)) TransitionChapterCard("Chapter completed", currentChapter.name, primary = false) Spacer(Modifier.height(16.dp)) Icon( imageVector = if (nextChapter == null) { Icons.Filled.CheckCircleOutline } else { Icons.Filled.KeyboardArrowDown }, contentDescription = null, modifier = Modifier.size(32.dp), tint = MaterialTheme.colorScheme.primary, ) Spacer(Modifier.height(16.dp)) if (nextChapter != null) { TransitionChapterCard("Next chapter", nextChapter.name, primary = true) } else { TransitionChapterCard("End of manga", mangaTitle, primary = true) } Spacer(Modifier.height(20.dp)) Text( text = if (nextChapter != null) { "Continue to the next chapter" } else { "Return to the chapter list" }, color = MaterialTheme.colorScheme.onSurfaceVariant, textAlign = TextAlign.Center, style = MaterialTheme.typography.bodyMedium, ) } } else { Column( modifier = Modifier.fillMaxSize().padding(24.dp), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center, ) { TransitionHeading() Spacer(Modifier.height(20.dp)) Row( modifier = Modifier.fillMaxWidth().widthIn(max = 600.dp), verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(12.dp), ) { val currentCard: @Composable () -> Unit = { TransitionChapterCard("Chapter completed", currentChapter.name, primary = false) } val nextCard: @Composable () -> Unit = { if (nextChapter != null) { TransitionChapterCard("Next chapter", nextChapter.name, primary = true) } else { TransitionChapterCard("End of manga", mangaTitle, primary = true) } } if (isRightToLeft) { Box(Modifier.weight(1f)) { nextCard() } Icon(Icons.AutoMirrored.Filled.NavigateNext, contentDescription = null) Box(Modifier.weight(1f)) { currentCard() } } else { Box(Modifier.weight(1f)) { currentCard() } Icon(Icons.AutoMirrored.Filled.NavigateNext, contentDescription = null) Box(Modifier.weight(1f)) { nextCard() } } } Spacer(Modifier.height(16.dp)) Text( text = if (nextChapter != null) { "Continue to the next chapter" } else { "Return to the chapter list" }, color = MaterialTheme.colorScheme.onSurfaceVariant, textAlign = TextAlign.Center, style = MaterialTheme.typography.bodyMedium, ) } } } } @Composable private fun TransitionHeading() { Icon( Icons.AutoMirrored.Filled.MenuBook, contentDescription = null, modifier = Modifier.size(48.dp), tint = MaterialTheme.colorScheme.primary, ) Spacer(Modifier.height(20.dp)) Text( text = "End of chapter", style = MaterialTheme.typography.headlineSmall, fontWeight = FontWeight.Bold, textAlign = TextAlign.Center, ) } @Composable private fun TransitionChapterCard( label: String, chapterName: String, primary: Boolean, ) { Surface( modifier = Modifier.fillMaxWidth(), shape = RoundedCornerShape(12.dp), color = if (primary) { MaterialTheme.colorScheme.primaryContainer } else { MaterialTheme.colorScheme.surfaceVariant }, tonalElevation = if (primary) 2.dp else 0.dp, ) { Column(Modifier.padding(16.dp)) { Text( label, style = MaterialTheme.typography.labelMedium, color = if (primary) { MaterialTheme.colorScheme.onPrimaryContainer.copy(alpha = 0.8f) } else { MaterialTheme.colorScheme.onSurfaceVariant }, ) Spacer(Modifier.height(4.dp)) Text( chapterName, style = MaterialTheme.typography.titleMedium, color = if (primary) { MaterialTheme.colorScheme.onPrimaryContainer } else { MaterialTheme.colorScheme.onSurface }, maxLines = 2, overflow = TextOverflow.Ellipsis, ) } } }