Yokan / rebuild
Course 1 8624561

DownloadPolicySettings.kt

composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/settings/downloads/DownloadPolicySettings.kt

implementation117 lines
1package eu.friedcutlet.yokan.ui.screens.settings.downloads23import androidx.compose.foundation.layout.*4import androidx.compose.foundation.lazy.LazyColumn5import androidx.compose.foundation.lazy.LazyListScope6import androidx.compose.foundation.lazy.items7import androidx.compose.foundation.selection.toggleable8import androidx.compose.material3.*9import androidx.compose.runtime.*10import androidx.compose.ui.Alignment11import androidx.compose.ui.Modifier12import androidx.compose.ui.semantics.Role13import androidx.compose.ui.unit.dp14import eu.friedcutlet.yokan.database.*15import eu.friedcutlet.yokan.ui.screens.settings.*16import org.jetbrains.compose.resources.stringResource17import yokan.composeapp.generated.resources.*1819internal fun LazyListScope.downloadPolicyItems(20    settings: DbSettings,21    state: DownloadPolicyUiState,22    onUpdate: ((DbSettings) -> DbSettings) -> Unit,23    onPreview: () -> Unit,24) {25    val policy = settings.features.downloads26    imageDataSaverItems(settings.features.imageDataSaver, state.sources, onUpdate)27    fun update(transform: (DbDownloadPolicy) -> DbDownloadPolicy) = onUpdate { current ->28        current.copy(features = current.features.copy(downloads = transform(current.features.downloads)))29    }30    item("policy-images") { SettingsSection(stringResource(Res.string.download_image_processing)) }31    item("policy-split") { SwitchTile(stringResource(Res.string.download_split_images), stringResource(Res.string.download_split_images_hint), policy.splitTallImages, { value -> update { it.copy(splitTallImages = value) } }) }32    if (policy.splitTallImages) item("policy-split-height") {33        val heights = listOf(1024, 2048, 3072, 4096, 6144, 8192)34        val labels = heights.associateWith { stringResource(Res.string.download_image_pixels, it) }35        ChoiceTile(stringResource(Res.string.download_split_height), heights, policy.splitImageHeight.coerceIn(1024, 8192), { labels[it].orEmpty() }) { value -> update { it.copy(splitImageHeight = value) } }36    }37    item("policy-automatic") { SettingsSection(stringResource(Res.string.download_policy_automation)) }38    item("policy-new") { SwitchTile(stringResource(Res.string.download_policy_new), stringResource(Res.string.download_policy_new_hint), policy.autoDownloadNew, { value -> update { it.copy(autoDownloadNew = value) } }) }39    item("policy-unread") { SwitchTile(stringResource(Res.string.download_policy_unread), null, policy.unreadOnly, { value -> update { it.copy(unreadOnly = value) } }) }40    item("policy-filler") { SwitchTile(stringResource(Res.string.download_policy_filler), null, policy.includeFiller, { value -> update { it.copy(includeFiller = value) } }) }41    item("policy-include") { PolicyCategoryTile(stringResource(Res.string.download_policy_include), state.categories, policy.includeCategoryIds, true) { ids -> update { it.copy(includeCategoryIds = ids) } } }42    item("policy-exclude") { PolicyCategoryTile(stringResource(Res.string.download_policy_exclude), state.categories, policy.excludeCategoryIds, false) { ids -> update { it.copy(excludeCategoryIds = ids) } } }43    item("policy-ahead") { SettingsSection(stringResource(Res.string.download_policy_ahead)) }44    item("policy-ahead-hint") { InfoRow(stringResource(Res.string.download_policy_ahead_hint)) }45    item("policy-ahead-manga") { PolicyCountTile(stringResource(Res.string.download_policy_ahead_manga), policy.ahead(DbItemType.Manga)) { value -> update { it.copy(aheadManga = value) } } }46    item("policy-ahead-anime") { PolicyCountTile(stringResource(Res.string.download_policy_ahead_anime), policy.ahead(DbItemType.Anime)) { value -> update { it.copy(aheadAnime = value) } } }47    item("policy-ahead-novel") { PolicyCountTile(stringResource(Res.string.download_policy_ahead_novel), policy.ahead(DbItemType.Novel)) { value -> update { it.copy(aheadNovel = value) } } }48    item("policy-cleanup") { SettingsSection(stringResource(Res.string.download_policy_cleanup)) }49    item("policy-consumed") { SwitchTile(stringResource(Res.string.download_policy_consumed), stringResource(Res.string.download_policy_consumed_hint), policy.cleanupAfterConsumption || settings.deleteDownloadAfterReading, { value ->50        onUpdate { current -> current.copy(deleteDownloadAfterReading = value, features = current.features.copy(downloads = current.features.downloads.copy(cleanupAfterConsumption = value))) }51    }) }52    item("policy-manual") { SwitchTile(stringResource(Res.string.download_policy_manual), null, policy.cleanupAfterManualMark, { value -> update { it.copy(cleanupAfterManualMark = value) } }) }53    item("policy-keep") { PolicyCountTile(stringResource(Res.string.download_policy_keep), policy.keepLastConsumed.coerceIn(0, 1000), 1000) { value -> update { it.copy(keepLastConsumed = value) } } }54    item("policy-bookmarks") { SwitchTile(stringResource(Res.string.download_policy_bookmarks), null, policy.protectBookmarks, { value -> update { it.copy(protectBookmarks = value) } }) }55    item("policy-cleanup-exclude") { PolicyCategoryTile(stringResource(Res.string.download_policy_cleanup_exclude), state.categories, policy.cleanupExcludedCategoryIds, false) { ids -> update { it.copy(cleanupExcludedCategoryIds = ids) } } }56    item("policy-preview") { YokanMenuTile(title = stringResource(Res.string.download_policy_preview), subtitle = stringResource(if (state.busy) Res.string.download_policy_busy else Res.string.download_policy_preview_hint), onClick = { if (!state.busy) onPreview() }) }57    if (state.failed) item("policy-error") { InfoRow(stringResource(Res.string.download_policy_failed)) }58    state.deletedCount?.let { count -> item("policy-result") { InfoRow(stringResource(Res.string.download_policy_deleted, count)) } }59}6061@Composable62private fun PolicyCountTile(title: String, count: Int, maximum: Int = 100, onChange: (Int) -> Unit) {63    val off = stringResource(Res.string.download_policy_off)64    ChoiceTile(title, (listOf(0, 1, 2, 3, 5, 10, 20, 50, 100, maximum, count)).distinct().sorted(), count, { if (it == 0) off else it.toString() }, onSelect = onChange)65}6667@Composable68internal fun PolicyCategoryTile(69    title: String,70    categories: List<DbCategory>,71    selected: Set<Long>,72    allWhenEmpty: Boolean,73    label: (DbCategory) -> String = { "${it.forItemType.name}: ${it.name}" },74    onChange: (Set<Long>) -> Unit,75) {76    var open by remember { mutableStateOf(false) }77    val empty = stringResource(if (allWhenEmpty) Res.string.download_policy_all_categories else Res.string.download_policy_no_categories)78    YokanMenuTile(title = title, subtitle = if (selected.isEmpty()) empty else categories.filter { it.id in selected }.joinToString(transform = label).ifEmpty { empty }, onClick = { open = true })79    if (open) AlertDialog(80        onDismissRequest = { open = false },81        title = { Text(title) },82        text = {83            if (categories.isEmpty()) Text(stringResource(Res.string.download_policy_no_categories_hint))84            else LazyColumn(Modifier.fillMaxWidth().heightIn(max = 360.dp)) {85                items(categories, key = { it.id }) { category ->86                    Row(Modifier.fillMaxWidth().heightIn(min = 48.dp).toggleable(category.id in selected, role = Role.Checkbox) {87                        onChange(if (it) selected + category.id else selected - category.id)88                    }.padding(horizontal = 8.dp, vertical = 8.dp), verticalAlignment = Alignment.CenterVertically) {89                        Checkbox(category.id in selected, onCheckedChange = null)90                        Spacer(Modifier.width(12.dp))91                        Text(label(category), style = MaterialTheme.typography.bodyLarge)92                    }93                }94            }95        },96        confirmButton = { TextButton(onClick = { open = false }) { Text(stringResource(Res.string.download_policy_done)) } },97    )98}99100@Composable101internal fun DownloadCleanupDialog(state: DownloadPolicyUiState, onDismiss: () -> Unit, onConfirm: () -> Unit) {102    val preview = state.preview ?: return103    AlertDialog(104        onDismissRequest = onDismiss,105        title = { Text(stringResource(Res.string.download_policy_preview_title)) },106        text = {107            Column(verticalArrangement = Arrangement.spacedBy(12.dp)) {108                Text(stringResource(if (preview.isEmpty()) Res.string.download_policy_preview_empty else Res.string.download_policy_preview_count, preview.size))109                if (preview.isNotEmpty()) LazyColumn(Modifier.fillMaxWidth().heightIn(max = 300.dp)) {110                    items(preview, key = { it.chapterId }) { item -> ListItem(headlineContent = { Text(item.title) }, supportingContent = { Text(item.chapter) }) }111                }112            }113        },114        confirmButton = { TextButton(onClick = if (preview.isEmpty()) onDismiss else onConfirm) { Text(stringResource(if (preview.isEmpty()) Res.string.download_policy_done else Res.string.download_policy_delete)) } },115        dismissButton = { if (preview.isNotEmpty()) TextButton(onClick = onDismiss) { Text(stringResource(Res.string.action_cancel)) } },116    )117}118

Search chapters, reference and source filenames.