BrowseScreen.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/browse/BrowseScreen.kt
1package eu.friedcutlet.yokan.ui.screens.browse23import androidx.compose.foundation.ExperimentalFoundationApi4import androidx.compose.foundation.background5import androidx.compose.foundation.layout.Arrangement6import androidx.compose.foundation.layout.Box7import androidx.compose.foundation.layout.Column8import androidx.compose.foundation.layout.Row9import androidx.compose.foundation.layout.fillMaxSize10import androidx.compose.foundation.layout.fillMaxWidth11import androidx.compose.foundation.layout.padding12import androidx.compose.foundation.layout.size13import androidx.compose.foundation.layout.width14import androidx.compose.foundation.lazy.LazyColumn15import androidx.compose.foundation.lazy.rememberLazyListState16import androidx.compose.foundation.pager.HorizontalPager17import androidx.compose.foundation.pager.rememberPagerState18import androidx.compose.foundation.shape.RoundedCornerShape19import androidx.compose.material.icons.Icons20import androidx.compose.material.icons.filled.Add21import androidx.compose.material.icons.filled.Extension22import androidx.compose.material.icons.filled.FilterList23import androidx.compose.material.icons.filled.Language24import androidx.compose.material.icons.filled.Search25import androidx.compose.material.icons.filled.TravelExplore26import androidx.compose.material.icons.outlined.DeleteOutline27import androidx.compose.material.icons.outlined.Download28import androidx.compose.material.icons.outlined.PushPin29import androidx.compose.material.icons.outlined.Settings30import androidx.compose.material.icons.outlined.SystemUpdateAlt31import androidx.compose.material3.AlertDialog32import androidx.compose.material3.Badge33import androidx.compose.material3.CircularProgressIndicator34import androidx.compose.material3.ElevatedButton35import androidx.compose.material3.ExperimentalMaterial3Api36import androidx.compose.material3.Icon37import androidx.compose.material3.IconButton38import androidx.compose.material3.ListItem39import androidx.compose.material3.ListItemDefaults40import androidx.compose.material3.MaterialTheme41import androidx.compose.material3.PrimaryScrollableTabRow42import androidx.compose.material3.Tab43import androidx.compose.material3.Text44import androidx.compose.material3.TextButton45import androidx.compose.material3.TopAppBar46import androidx.compose.material3.TopAppBarDefaults47import androidx.compose.material3.pulltorefresh.PullToRefreshBox48import androidx.compose.runtime.Composable49import androidx.compose.runtime.LaunchedEffect50import androidx.compose.runtime.getValue51import androidx.compose.runtime.mutableStateOf52import androidx.compose.runtime.remember53import androidx.compose.runtime.rememberCoroutineScope54import androidx.compose.runtime.setValue55import androidx.compose.runtime.snapshotFlow56import androidx.compose.ui.Alignment57import androidx.compose.ui.Modifier58import androidx.compose.ui.draw.clip59import androidx.compose.ui.graphics.Color60import androidx.compose.ui.layout.ContentScale61import androidx.compose.ui.text.font.FontWeight62import androidx.compose.ui.text.style.TextOverflow63import androidx.compose.ui.unit.dp64import androidx.compose.ui.unit.sp65import coil3.compose.AsyncImage66import eu.friedcutlet.yokan.domain.ItemType67import eu.friedcutlet.yokan.domain.Source68import eu.friedcutlet.yokan.ui.theme.YokanScaffold as Scaffold69import kotlinx.coroutines.launch70import org.jetbrains.compose.resources.stringResource71import yokan.composeapp.generated.resources.Res72import yokan.composeapp.generated.resources.action_cancel73import yokan.composeapp.generated.resources.action_open74import yokan.composeapp.generated.resources.action_refresh75import yokan.composeapp.generated.resources.nav_browse76import yokan.composeapp.generated.resources.ok77import yokan.composeapp.generated.resources.ui_search_dots78import eu.friedcutlet.yokan.ui.navigation.NavSection79import eu.friedcutlet.yokan.ui.navigation.Navigator80import eu.friedcutlet.yokan.ui.navigation.Route81import eu.friedcutlet.yokan.app.YokanRuntime82import androidx.lifecycle.compose.collectAsStateWithLifecycle83import androidx.lifecycle.viewmodel.compose.viewModel84import eu.friedcutlet.yokan.ui.components.ScrollbarBox85import eu.friedcutlet.yokan.ui.components.AppBarSearchField86import eu.friedcutlet.yokan.ui.components.hoverHighlight87import eu.friedcutlet.yokan.ui.components.EmptyState88import eu.friedcutlet.yokan.services.extensions.languageName8990@Composable91internal fun BrowseRoute(92 nav: Navigator,93 onAddSignedCatalog: (() -> Unit)?,94 signedCatalogSheet: @Composable () -> Unit,95) {96 val vm = viewModel { BrowseViewModel(YokanRuntime.repository, YokanRuntime.settings, YokanRuntime.sourceInstall) }97 val state by vm.state.collectAsStateWithLifecycle()98 val rootItemType by nav.itemType.collectAsStateWithLifecycle()99 val hiddenSections by nav.hiddenSections.collectAsStateWithLifecycle()100 BrowseScreen(101 state = state,102 rootItemType = rootItemType,103 hiddenSections = hiddenSections,104 onItemTypeSettled = nav::selectItemType,105 onNavigate = nav::navigate,106 onTogglePinned = vm::togglePinned,107 onRefresh = vm::refresh,108 onUpdateAll = vm::updateAll,109 onInstall = vm::install,110 onUninstall = vm::uninstall,111 onAddSignedCatalog = onAddSignedCatalog,112 addSignedCatalogLabel = onAddSignedCatalog?.let { "Add signed catalog" },113 signedCatalogSheet = signedCatalogSheet,114 )115}116117/** Top-level mode within the Browse tab: installed sources vs. extension catalogue. */118private enum class BrowseMode { Sources, Extensions }119120private data class BrowseTab(121 val itemType: ItemType,122 val mode: BrowseMode,123)124125@OptIn(ExperimentalMaterial3Api::class, ExperimentalFoundationApi::class)126@Composable127internal fun BrowseScreen(128 state: BrowseUiState,129 rootItemType: ItemType,130 hiddenSections: Set<NavSection>,131 onItemTypeSettled: (ItemType) -> Unit,132 onNavigate: (Route) -> Unit,133 onTogglePinned: (Source) -> Unit,134 onRefresh: (ItemType) -> Unit,135 onUpdateAll: (ItemType, List<Long>) -> Unit,136 onInstall: (sourceId: Long) -> Unit,137 onUninstall: (sourceId: Long) -> Unit,138 onAddSignedCatalog: (() -> Unit)? = null,139 addSignedCatalogLabel: String? = null,140 signedCatalogSheet: @Composable () -> Unit = {},141) {142 val itemTypes = remember(hiddenSections) {143 listOf(ItemType.Manga, ItemType.Anime, ItemType.Novel).filter { type ->144 when (type) {145 ItemType.Manga -> NavSection.MangaLibrary !in hiddenSections146 ItemType.Anime -> NavSection.AnimeLibrary !in hiddenSections147 ItemType.Novel -> NavSection.NovelLibrary !in hiddenSections148 }149 }150 }151 if (itemTypes.isEmpty()) return152153 // Mangayomi uses one continuous, scrollable tab hierarchy: all source tabs154 // first, followed by the matching extension catalogues.155 val browseTabs = remember(itemTypes) {156 itemTypes.map { BrowseTab(it, BrowseMode.Sources) } +157 itemTypes.map { BrowseTab(it, BrowseMode.Extensions) }158 }159 val initialPage = remember(browseTabs, rootItemType) {160 browseTabs.indexOfFirst {161 it.itemType == rootItemType && it.mode == BrowseMode.Sources162 }.takeIf { it >= 0 } ?: 0163 }164 val pagerState = rememberPagerState(initialPage = initialPage, pageCount = { browseTabs.size })165 val scope = rememberCoroutineScope()166 val selectedPage = pagerState.currentPage.coerceIn(browseTabs.indices)167 val currentTab = browseTabs[selectedPage]168 var searching by remember { mutableStateOf(false) }169 var query by remember { mutableStateOf("") }170171 LaunchedEffect(browseTabs.size) {172 if (pagerState.currentPage > browseTabs.lastIndex) {173 pagerState.scrollToPage(browseTabs.lastIndex)174 }175 }176 LaunchedEffect(pagerState, browseTabs) {177 snapshotFlow { pagerState.settledPage }.collect { page ->178 val tab = browseTabs.getOrNull(page) ?: return@collect179 onItemTypeSettled(tab.itemType)180 query = ""181 searching = false182 }183 }184185 Scaffold(186 topBar = {187 Column {188 TopAppBar(189 title = {190 Text(191 text = stringResource(Res.string.nav_browse),192 color = MaterialTheme.colorScheme.onSurfaceVariant,193 )194 },195 actions = {196 when (currentTab.mode) {197 BrowseMode.Sources -> {198 if (onAddSignedCatalog != null && !addSignedCatalogLabel.isNullOrBlank()) {199 IconButton(onClick = onAddSignedCatalog) {200 Icon(Icons.Filled.Add, contentDescription = addSignedCatalogLabel)201 }202 }203 IconButton(204 onClick = { onNavigate(Route.GlobalSearch(currentTab.itemType)) },205 ) {206 Icon(Icons.Filled.TravelExplore, contentDescription = "Global search")207 }208 IconButton(209 onClick = { onNavigate(Route.SourceFilters(currentTab.itemType)) },210 ) {211 Icon(Icons.Filled.FilterList, contentDescription = "Filter sources")212 }213 }214 BrowseMode.Extensions -> {215 if (searching) {216 AppBarSearchField(217 query = query,218 placeholder = stringResource(Res.string.ui_search_dots),219 onQueryChange = { query = it },220 onClose = {221 query = ""222 searching = false223 },224 modifier = Modifier.width(220.dp),225 )226 } else {227 IconButton(228 onClick = { onNavigate(Route.CreateExtension(currentTab.itemType)) },229 ) {230 Icon(Icons.Filled.Add, contentDescription = "Create extension")231 }232 IconButton(onClick = { searching = true }) {233 Icon(Icons.Filled.Search, contentDescription = "Search extensions")234 }235 }236 IconButton(237 onClick = { onNavigate(Route.ExtensionLanguages(currentTab.itemType)) },238 ) {239 Icon(Icons.Filled.Language, contentDescription = "Extension languages")240 }241 }242 }243 },244 colors = TopAppBarDefaults.topAppBarColors(containerColor = Color.Transparent),245 )246 PrimaryScrollableTabRow(247 selectedTabIndex = selectedPage,248 edgePadding = 0.dp,249 modifier = Modifier.fillMaxWidth(),250 containerColor = Color.Transparent,251 divider = {},252 ) {253 browseTabs.forEachIndexed { index, tab ->254 val updateCount = if (tab.mode == BrowseMode.Extensions) {255 state.sources[tab.itemType]?.updateCount ?: 0256 } else {257 0258 }259 Tab(260 selected = index == selectedPage,261 onClick = {262 if (index != selectedPage) {263 query = ""264 searching = false265 scope.launch { pagerState.animateScrollToPage(index) }266 }267 },268 text = {269 Row(270 verticalAlignment = Alignment.CenterVertically,271 horizontalArrangement = Arrangement.spacedBy(8.dp),272 ) {273 Text(274 "${tab.itemType.label} ${275 if (tab.mode == BrowseMode.Extensions) "Extensions" else "Sources"276 }",277 )278 if (updateCount > 0) {279 Badge(280 containerColor = MaterialTheme.colorScheme.surfaceVariant,281 contentColor = MaterialTheme.colorScheme.onSurfaceVariant,282 ) {283 Text(updateCount.toString())284 }285 }286 }287 },288 )289 }290 }291 }292 },293 ) { padding ->294 HorizontalPager(295 state = pagerState,296 beyondViewportPageCount = 1,297 modifier = Modifier.fillMaxSize().padding(padding),298 ) { page ->299 val tab = browseTabs[page]300 when (tab.mode) {301 BrowseMode.Sources -> SourcesPane(302 sources = state.sources[tab.itemType] ?: BrowseSources(),303 itemType = tab.itemType,304 onOpenSource = { source -> onNavigate(Route.SourceHome(source.id)) },305 onTogglePinned = onTogglePinned,306 onOpenLocal = { onNavigate(Route.LocalLibraryBrowse(tab.itemType)) },307 onShowExtensions = {308 val extensionPage = browseTabs.indexOfFirst {309 it.itemType == tab.itemType && it.mode == BrowseMode.Extensions310 }311 if (extensionPage >= 0) {312 scope.launch { pagerState.animateScrollToPage(extensionPage) }313 }314 },315 )316 BrowseMode.Extensions -> ExtensionsPane(317 catalog = state.sources[tab.itemType]?.catalog.orEmpty(),318 query = if (page == selectedPage) query else "",319 refreshing = tab.itemType in state.refreshing,320 refreshFailed = tab.itemType in state.refreshFailed,321 updatingAll = tab.itemType in state.updatingAll,322 installing = state.installing,323 installFailed = state.installFailed,324 onRefresh = { onRefresh(tab.itemType) },325 onUpdateAll = { ids -> onUpdateAll(tab.itemType, ids) },326 onInstall = onInstall,327 onUninstall = onUninstall,328 onOpenSettings = { source -> onNavigate(Route.ExtensionDetail(source.id)) },329 )330 }331 }332 }333 // One caller-owned sheet, never one instance per composed pager page.334 signedCatalogSheet()335}336337@OptIn(ExperimentalFoundationApi::class)338@Composable339private fun SourcesPane(340 sources: BrowseSources,341 itemType: ItemType,342 onShowExtensions: () -> Unit,343 onOpenSource: (Source) -> Unit,344 onTogglePinned: (Source) -> Unit,345 onOpenLocal: () -> Unit,346) {347 val lastUsed = sources.lastUsed348 val pinned = sources.pinned349 val byLanguage = sources.byLanguage350351 if (sources.visible.isEmpty()) {352 Column(353 modifier = Modifier.fillMaxSize().padding(top = 10.dp),354 verticalArrangement = Arrangement.Center,355 ) {356 EmptyState(357 icon = Icons.Filled.Extension,358 title = "No sources installed",359 modifier = Modifier.fillMaxWidth(),360 action = {361 ElevatedButton(onClick = onShowExtensions) {362 Icon(Icons.Filled.Extension, contentDescription = null)363 Text(364 text = "Show extensions",365 modifier = Modifier.padding(start = 8.dp),366 )367 }368 },369 )370 SectionHeader("Other")371 LocalLibraryTile(itemType = itemType, onClick = onOpenLocal)372 }373 return374 }375376 val listState = rememberLazyListState()377 ScrollbarBox(state = listState, modifier = Modifier.fillMaxSize()) {378 LazyColumn(379 state = listState,380 modifier = Modifier.fillMaxSize().padding(top = 10.dp),381 ) {382 if (lastUsed.isNotEmpty()) {383 item(key = "src-header-last-used") { SectionHeader("Last used") }384 items(lastUsed.size, key = { "last-${lastUsed[it].id}" }) { index ->385 SourceRow(lastUsed[index], onOpenSource, onTogglePinned)386 }387 }388 if (pinned.isNotEmpty()) {389 item(key = "src-header-pinned") { SectionHeader("Pinned") }390 items(pinned.size, key = { "pin-${pinned[it].id}" }) { index ->391 SourceRow(pinned[index], onOpenSource, onTogglePinned)392 }393 }394 byLanguage.forEach { (language, rows) ->395 item(key = "src-header-$language") { SectionHeader(language) }396 items(rows.size, key = { "src-$language-${rows[it].id}" }) { index ->397 SourceRow(rows[index], onOpenSource, onTogglePinned)398 }399 }400 item(key = "src-header-other") { SectionHeader("Other") }401 item(key = "local-library-tile") {402 LocalLibraryTile(403 itemType = itemType,404 onClick = onOpenLocal,405 )406 }407 }408 }409}410411@Composable412private fun LocalLibraryTile(itemType: ItemType, onClick: () -> Unit) {413 ListItem(414 modifier = Modifier415 .fillMaxWidth()416 .hoverHighlight(onClick = onClick),417 headlineContent = { Text("Local source ${itemType.label}") },418 supportingContent = {419 Text(420 text = "",421 style = MaterialTheme.typography.bodySmall,422 )423 },424 leadingContent = {425 Box(426 modifier = Modifier427 .size(37.dp)428 .clip(RoundedCornerShape(5.dp))429 .background(MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.5f)),430 contentAlignment = Alignment.Center,431 ) {432 Icon(Icons.Filled.Extension, contentDescription = null)433 }434 },435 trailingContent = {436 TextButton(onClick = onClick) { Text(stringResource(Res.string.action_open)) }437 },438 colors = ListItemDefaults.colors(containerColor = Color.Transparent),439 )440}441442@OptIn(ExperimentalFoundationApi::class, ExperimentalMaterial3Api::class)443@Composable444private fun ExtensionsPane(445 catalog: List<Source>,446 query: String,447 refreshing: Boolean,448 refreshFailed: Boolean,449 updatingAll: Boolean,450 installing: Set<Long>,451 installFailed: Set<Long>,452 onRefresh: () -> Unit,453 onUpdateAll: (List<Long>) -> Unit,454 onInstall: (sourceId: Long) -> Unit,455 onUninstall: (sourceId: Long) -> Unit,456 onOpenSettings: (Source) -> Unit,457) {458 val sections = remember(catalog, query) { extensionSections(catalog, query) }459 val updates = sections.updates460 val installed = sections.installed461 val availableByLanguage = sections.availableByLanguage462463 val listState = rememberLazyListState()464 PullToRefreshBox(465 modifier = Modifier.fillMaxSize(),466 isRefreshing = refreshing,467 onRefresh = onRefresh,468 ) {469 ScrollbarBox(state = listState, modifier = Modifier.fillMaxSize()) {470 LazyColumn(471 state = listState,472 modifier = Modifier.fillMaxSize().padding(top = 10.dp),473 ) {474 if (updates.isNotEmpty()) {475 item(key = "ext-header-updates") {476 SectionHeader(477 label = "Update pending",478 updating = updatingAll,479 onUpdateAll = { onUpdateAll(updates.map { it.id }) },480 )481 }482 items(updates.size, key = { "upd-${updates[it].id}" }) { index ->483 ExtensionRow(484 source = updates[index],485 mode = ExtensionRowMode.Update,486 working = updates[index].id in installing,487 failed = updates[index].id in installFailed,488 onInstall = onInstall,489 onUninstall = onUninstall,490 onOpenSettings = onOpenSettings,491 )492 }493 }494 if (installed.isNotEmpty()) {495 item(key = "ext-header-installed") { SectionHeader("Installed") }496 items(installed.size, key = { "ins-${installed[it].id}" }) { index ->497 ExtensionRow(498 source = installed[index],499 mode = ExtensionRowMode.Installed,500 working = installed[index].id in installing,501 failed = installed[index].id in installFailed,502 onInstall = onInstall,503 onUninstall = onUninstall,504 onOpenSettings = onOpenSettings,505 )506 }507 }508 availableByLanguage.forEach { (language, rows) ->509 item(key = "ext-available-$language") { SectionHeader(language) }510 items(rows.size, key = { "av-$language-${rows[it].id}" }) { index ->511 ExtensionRow(512 source = rows[index],513 mode = ExtensionRowMode.NotInstalled,514 working = rows[index].id in installing,515 failed = rows[index].id in installFailed,516 onInstall = onInstall,517 onUninstall = onUninstall,518 onOpenSettings = onOpenSettings,519 )520 }521 }522 if (sections.isEmpty) {523 item(key = "ext-empty") {524 Box(525 modifier = Modifier.fillMaxWidth().padding(24.dp),526 contentAlignment = Alignment.Center,527 ) {528 when {529 refreshing -> CircularProgressIndicator()530 refreshFailed && query.isBlank() -> Column(531 horizontalAlignment = Alignment.CenterHorizontally,532 verticalArrangement = Arrangement.spacedBy(8.dp),533 ) {534 Text(535 text = "Unable to refresh extensions",536 color = MaterialTheme.colorScheme.onSurfaceVariant,537 )538 ElevatedButton(onClick = onRefresh) {539 Text(stringResource(Res.string.action_refresh))540 }541 }542 else -> Text(543 text = if (query.isBlank()) {544 "No extensions available"545 } else {546 "No extensions match \"$query\""547 },548 color = MaterialTheme.colorScheme.onSurfaceVariant,549 )550 }551 }552 }553 }554 }555 }556 }557}558559@Composable560private fun SectionHeader(561 label: String,562 updating: Boolean = false,563 onUpdateAll: (() -> Unit)? = null,564) {565 if (onUpdateAll == null) {566 Text(567 text = label,568 fontWeight = FontWeight.Bold,569 fontSize = 13.sp,570 color = MaterialTheme.colorScheme.onSurface,571 modifier = Modifier.fillMaxWidth().padding(start = 12.dp, top = 8.dp, end = 12.dp),572 )573 return574 }575576 Row(577 modifier = Modifier.fillMaxWidth().padding(horizontal = 12.dp),578 verticalAlignment = Alignment.CenterVertically,579 horizontalArrangement = Arrangement.SpaceBetween,580 ) {581 Text(582 text = label,583 fontWeight = FontWeight.Bold,584 fontSize = 13.sp,585 color = MaterialTheme.colorScheme.onSurface,586 modifier = Modifier.weight(1f),587 )588 ElevatedButton(589 enabled = !updating,590 onClick = onUpdateAll,591 ) {592 if (updating) {593 CircularProgressIndicator(594 modifier = Modifier.size(16.dp),595 strokeWidth = 2.dp,596 )597 } else {598 Text("Update all")599 }600 }601 }602}603604@Composable605private fun SourceRow(source: Source, onOpen: (Source) -> Unit, onTogglePinned: (Source) -> Unit) {606 val openSource = { onOpen(source) }607 ListItem(608 modifier = Modifier609 .fillMaxWidth()610 .hoverHighlight(onClick = openSource),611 headlineContent = {612 Text(613 text = source.name,614 maxLines = 1,615 overflow = TextOverflow.Ellipsis,616 )617 },618 supportingContent = {619 Row(verticalAlignment = Alignment.CenterVertically) {620 Text(621 text = languageName(source.lang),622 fontSize = 12.sp,623 fontWeight = FontWeight.Light,624 )625 if (source.isNsfw) {626 NsfwBadge(modifier = Modifier.padding(start = 4.dp))627 }628 }629 },630 leadingContent = { SourceIcon(source) },631 trailingContent = {632 Row(633 verticalAlignment = Alignment.CenterVertically,634 horizontalArrangement = Arrangement.spacedBy(10.dp),635 ) {636 if (source.supportLatest) {637 TextButton(onClick = openSource) {638 Text("Latest")639 }640 }641 IconButton(onClick = { onTogglePinned(source) }) {642 Icon(643 imageVector = Icons.Outlined.PushPin,644 contentDescription = if (source.isPinned) "Unpin source" else "Pin source",645 tint = if (source.isPinned) {646 MaterialTheme.colorScheme.primary647 } else {648 MaterialTheme.colorScheme.onSurfaceVariant649 },650 )651 }652 }653 },654 colors = ListItemDefaults.colors(containerColor = Color.Transparent),655 )656}657658private enum class ExtensionRowMode { Update, Installed, NotInstalled }659660@Composable661private fun ExtensionRow(662 source: Source,663 mode: ExtensionRowMode,664 working: Boolean,665 failed: Boolean,666 onInstall: (sourceId: Long) -> Unit,667 onUninstall: (sourceId: Long) -> Unit,668 onOpenSettings: (Source) -> Unit,669) {670 var showUninstallDialog by remember(source.id) { mutableStateOf(false) }671672 val installOrUpdate: () -> Unit = { onInstall(source.id) }673 val openSettings: () -> Unit = { onOpenSettings(source) }674 val rowAction: (() -> Unit)? = when {675 working -> null676 source.isAdded -> openSettings677 else -> installOrUpdate678 }679680 ListItem(681 modifier = Modifier682 .fillMaxWidth()683 .hoverHighlight(onClick = rowAction),684 headlineContent = {685 Text(686 text = source.name,687 maxLines = 1,688 overflow = TextOverflow.Ellipsis,689 )690 },691 supportingContent = {692 Row(verticalAlignment = Alignment.CenterVertically) {693 Text(694 text = languageName(source.lang),695 fontSize = 12.sp,696 fontWeight = FontWeight.Light,697 )698 Text(699 text = source.version,700 fontSize = 12.sp,701 fontWeight = FontWeight.Light,702 modifier = Modifier.padding(start = 4.dp),703 )704 if (source.isNsfw) {705 NsfwBadge(modifier = Modifier.padding(start = 4.dp))706 }707 source.repo?.name?.takeIf { it.isNotBlank() }?.let { repoName ->708 Text(709 text = "- $repoName",710 fontSize = 12.sp,711 maxLines = 1,712 overflow = TextOverflow.Ellipsis,713 modifier = Modifier.padding(horizontal = 4.dp).weight(1f, fill = false),714 )715 }716 if (source.isObsolete) {717 Text(718 text = "OBSOLETE",719 color = MaterialTheme.colorScheme.primary,720 fontSize = 12.sp,721 fontWeight = FontWeight.Bold,722 modifier = Modifier.padding(horizontal = 4.dp),723 )724 }725 when {726 failed -> Text(727 text = "FAILED",728 color = MaterialTheme.colorScheme.error,729 fontSize = 12.sp,730 fontWeight = FontWeight.Bold,731 modifier = Modifier.padding(start = 4.dp),732 )733 }734 }735 },736 leadingContent = { SourceIcon(source) },737 trailingContent = {738 if (working) {739 CircularProgressIndicator(740 modifier = Modifier.size(20.dp),741 strokeWidth = 2.dp,742 )743 } else {744 Row(745 verticalAlignment = Alignment.CenterVertically,746 horizontalArrangement = Arrangement.spacedBy(2.dp),747 ) {748 when (mode) {749 ExtensionRowMode.Update -> {750 IconButton(751 onClick = installOrUpdate,752 ) {753 Icon(Icons.Outlined.SystemUpdateAlt, contentDescription = "Update extension")754 }755 }756 ExtensionRowMode.Installed -> {757 IconButton(onClick = openSettings) {758 Icon(Icons.Outlined.Settings, contentDescription = "Extension settings")759 }760 }761 ExtensionRowMode.NotInstalled -> {762 IconButton(763 onClick = installOrUpdate,764 ) {765 Icon(Icons.Outlined.Download, contentDescription = "Install extension")766 }767 }768 }769 if (source.isAdded) {770 IconButton(onClick = { showUninstallDialog = true }) {771 Icon(Icons.Outlined.DeleteOutline, contentDescription = "Uninstall extension")772 }773 }774 }775 }776 },777 colors = ListItemDefaults.colors(containerColor = Color.Transparent),778 )779780 if (showUninstallDialog) {781 AlertDialog(782 onDismissRequest = { showUninstallDialog = false },783 title = { Text(source.name) },784 text = { Text("Uninstall extension ${source.name}?") },785 dismissButton = {786 TextButton(onClick = { showUninstallDialog = false }) { Text(stringResource(Res.string.action_cancel)) }787 },788 confirmButton = {789 TextButton(790 onClick = {791 showUninstallDialog = false792 onUninstall(source.id)793 },794 ) {795 Text(stringResource(Res.string.ok))796 }797 },798 )799 }800}801802@Composable803private fun SourceIcon(source: Source) {804 val iconUrl = source.iconUrl?.takeIf { it.isNotBlank() }805 var loadFailed by remember(iconUrl) { mutableStateOf(false) }806 Box(807 modifier = Modifier808 .size(37.dp)809 .clip(RoundedCornerShape(5.dp))810 .background(MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.5f)),811 contentAlignment = Alignment.Center,812 ) {813 Icon(814 imageVector = Icons.Filled.Extension,815 contentDescription = null,816 modifier = Modifier.size(24.dp),817 )818 if (iconUrl != null && !loadFailed) {819 AsyncImage(820 model = iconUrl,821 contentDescription = "${source.name} icon",822 contentScale = ContentScale.Fit,823 modifier = Modifier.fillMaxSize(),824 onError = { loadFailed = true },825 )826 }827 }828}829830@Composable831private fun NsfwBadge(modifier: Modifier = Modifier) {832 Box(833 modifier = modifier834 .clip(RoundedCornerShape(4.dp))835 .background(Color.Red.copy(alpha = 0.8f))836 .padding(horizontal = 4.dp, vertical = 1.dp),837 ) {838 Text(839 text = "NSFW",840 color = Color.White,841 fontSize = 8.sp,842 fontWeight = FontWeight.Bold,843 )844 }845}846