SourceRelatedScreen.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/discovery/SourceRelatedScreen.kt
1package eu.friedcutlet.yokan.ui.screens.discovery23import androidx.compose.foundation.layout.*4import androidx.compose.foundation.lazy.items5import androidx.compose.material.icons.Icons6import androidx.compose.material.icons.filled.AccountTree7import androidx.compose.material.icons.filled.Refresh8import androidx.compose.material3.*9import androidx.compose.runtime.*10import androidx.compose.ui.Alignment11import androidx.compose.ui.Modifier12import androidx.compose.ui.draw.clip13import androidx.compose.ui.semantics.heading14import androidx.compose.ui.semantics.semantics15import androidx.compose.ui.text.font.FontWeight16import androidx.compose.ui.text.style.TextOverflow17import androidx.compose.ui.unit.dp18import androidx.lifecycle.compose.collectAsStateWithLifecycle19import androidx.lifecycle.viewmodel.compose.viewModel20import eu.friedcutlet.yokan.app.YokanRuntime21import eu.friedcutlet.yokan.services.extensions.RemoteManga22import eu.friedcutlet.yokan.ui.components.CoverImage23import eu.friedcutlet.yokan.ui.components.EmptyState24import eu.friedcutlet.yokan.ui.components.RouteScaffold25import eu.friedcutlet.yokan.ui.components.hoverHighlight26import eu.friedcutlet.yokan.ui.navigation.Navigator27import eu.friedcutlet.yokan.ui.navigation.Route28import org.jetbrains.compose.resources.stringResource29import yokan.composeapp.generated.resources.*3031@Composable32internal fun SourceRelatedRoute(route: Route.SourceRelated, nav: Navigator) {33 val vm = viewModel { SourceRelatedViewModel(route.mangaId, YokanRuntime.repository,34 YokanRuntime.database, YokanRuntime.services.extensionRuntimeFactory) }35 val state by vm.state.collectAsStateWithLifecycle()36 LaunchedEffect(state.openedTitleId) {37 state.openedTitleId?.let { id -> vm.opened(); nav.navigate(Route.Detail(id)) }38 }39 SourceRelatedScreen(state, onOpen = vm::open, onRefresh = vm::refresh, onBack = nav::back)40}4142@Composable43internal fun SourceRelatedScreen(44 state: SourceRelatedState,45 onOpen: (RemoteManga) -> Unit,46 onRefresh: () -> Unit,47 onBack: () -> Unit,48) {49 RouteScaffold(stringResource(Res.string.source_related_entries), onBack, actions = {50 IconButton(onClick = onRefresh, enabled = !state.loading && !state.opening) {51 Icon(Icons.Filled.Refresh, stringResource(Res.string.source_related_reload))52 }53 }) {54 if (state.title.isNotBlank()) item(key = "title", contentType = "title") {55 Text(state.title, style = MaterialTheme.typography.titleSmall, color = MaterialTheme.colorScheme.onSurfaceVariant)56 }57 if (state.loading) item(key = "loading", contentType = "loading") {58 Box(Modifier.fillMaxWidth().padding(vertical = 24.dp), contentAlignment = Alignment.Center) {59 CircularProgressIndicator()60 }61 }62 if (state.error != null) item(key = "error", contentType = "error") {63 Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {64 Text(stringResource(Res.string.source_related_failed), style = MaterialTheme.typography.titleMedium)65 Text(state.error, color = MaterialTheme.colorScheme.error)66 TextButton(onClick = onRefresh, enabled = !state.loading) { Text(stringResource(Res.string.source_related_retry)) }67 }68 }69 if (!state.loading && state.error == null && state.groups.isEmpty()) item(key = "empty", contentType = "empty") {70 EmptyState(71 icon = Icons.Filled.AccountTree,72 title = stringResource(if (state.supported == false) Res.string.source_related_unavailable else Res.string.source_related_empty),73 subtitle = if (state.supported == false) stringResource(Res.string.source_related_unavailable_hint) else null,74 modifier = Modifier.fillMaxWidth().padding(vertical = 40.dp),75 )76 }77 state.groups.forEachIndexed { groupIndex, group ->78 item(key = "group:${group.name}", contentType = "heading") {79 Text(group.name, style = MaterialTheme.typography.titleMedium,80 modifier = Modifier.padding(top = 16.dp).semantics { heading() })81 }82 items(group.items, key = { "entry:$groupIndex:${it.url}" }, contentType = { "entry" }) { item ->83 Row(84 modifier = Modifier.fillMaxWidth().clip(MaterialTheme.shapes.medium)85 .hoverHighlight(onClick = { if (!state.opening) onOpen(item) })86 .padding(8.dp).heightIn(min = 96.dp),87 horizontalArrangement = Arrangement.spacedBy(16.dp), verticalAlignment = Alignment.CenterVertically,88 ) {89 CoverImage(model = item.thumbnailUrl, contentDescription = null, fallbackInitials = item.title.take(2).uppercase(),90 modifier = Modifier.size(56.dp, 80.dp).clip(MaterialTheme.shapes.small))91 Column(Modifier.weight(1f), verticalArrangement = Arrangement.spacedBy(4.dp)) {92 Text(item.title, style = MaterialTheme.typography.bodyLarge, fontWeight = FontWeight.Medium,93 maxLines = 3, overflow = TextOverflow.Ellipsis)94 item.description?.takeIf(String::isNotBlank)?.let {95 Text(it, style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant,96 maxLines = 2, overflow = TextOverflow.Ellipsis)97 }98 }99 }100 }101 }102 }103}104