DetailDownloads.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/detail/DetailDownloads.kt
1package eu.friedcutlet.yokan.ui.screens.detail23import androidx.compose.foundation.clickable4import androidx.compose.foundation.layout.Arrangement5import androidx.compose.foundation.layout.Column6import androidx.compose.foundation.layout.Row7import androidx.compose.foundation.layout.fillMaxWidth8import androidx.compose.foundation.layout.heightIn9import androidx.compose.foundation.layout.padding10import androidx.compose.foundation.layout.size11import androidx.compose.foundation.lazy.LazyColumn12import androidx.compose.foundation.lazy.items13import androidx.compose.foundation.lazy.rememberLazyListState14import androidx.compose.material.icons.Icons15import androidx.compose.material.icons.filled.CheckCircle16import androidx.compose.material.icons.filled.SaveAlt17import androidx.compose.material.icons.filled.Info18import androidx.compose.material3.AlertDialog19import androidx.compose.material3.Icon20import androidx.compose.material3.MaterialTheme21import androidx.compose.material3.Surface22import androidx.compose.material3.Text23import androidx.compose.material3.TextButton24import androidx.compose.runtime.Composable25import androidx.compose.ui.Alignment26import androidx.compose.ui.Modifier27import androidx.compose.ui.draw.clip28import androidx.compose.ui.text.font.FontWeight29import androidx.compose.ui.text.style.TextOverflow30import androidx.compose.ui.unit.dp31import eu.friedcutlet.yokan.database.DbLocalFolder32import eu.friedcutlet.yokan.domain.Chapter33import eu.friedcutlet.yokan.domain.ItemType34import eu.friedcutlet.yokan.ui.components.ScrollbarBox35import eu.friedcutlet.yokan.ui.components.compactDialogWidth36import org.jetbrains.compose.resources.stringResource37import yokan.composeapp.generated.resources.Res38import yokan.composeapp.generated.resources.action_cancel39import yokan.composeapp.generated.resources.delete4041@Composable42internal fun DownloadDestinationDialog(43 title: String,44 folders: List<DbLocalFolder>,45 defaultRoot: String,46 selectedName: String?,47 onSelect: (String?) -> Unit,48 onDismiss: () -> Unit,49) {50 val listState = rememberLazyListState()51 val resolvedSelectedName = selectedName?.takeIf { selected -> folders.any { it.name == selected } }5253 AlertDialog(54 onDismissRequest = onDismiss,55 modifier = Modifier.compactDialogWidth(),56 shape = MaterialTheme.shapes.extraLarge,57 title = {58 Text(title, style = MaterialTheme.typography.titleMedium, fontWeight = FontWeight.SemiBold)59 },60 text = {61 eu.friedcutlet.yokan.ui.components.ScrollbarBox(62 state = listState,63 modifier = Modifier.fillMaxWidth(),64 ) {65 LazyColumn(66 state = listState,67 modifier = Modifier.fillMaxWidth().heightIn(max = 360.dp),68 verticalArrangement = Arrangement.spacedBy(8.dp),69 ) {70 item(key = "default") {71 DownloadDestinationRow(72 title = "Default app folder",73 subtitle = defaultRoot,74 selected = resolvedSelectedName == null,75 onClick = { onSelect(null) },76 )77 }78 items(folders, key = { it.name }) { folder ->79 DownloadDestinationRow(80 title = folder.name,81 subtitle = folder.path,82 selected = resolvedSelectedName == folder.name,83 onClick = { onSelect(folder.name) },84 )85 }86 }87 }88 },89 confirmButton = {},90 dismissButton = {91 TextButton(onClick = onDismiss) { Text(stringResource(Res.string.action_cancel)) }92 },93 )94}9596@Composable97private fun DownloadDestinationRow(98 title: String,99 subtitle: String,100 selected: Boolean,101 onClick: () -> Unit,102) {103 Surface(104 modifier = Modifier105 .fillMaxWidth()106 .clip(MaterialTheme.shapes.large)107 .clickable(onClick = onClick),108 shape = MaterialTheme.shapes.large,109 color = if (selected) {110 MaterialTheme.colorScheme.primaryContainer.copy(alpha = 0.62f)111 } else {112 MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.34f)113 },114 ) {115 Row(116 modifier = Modifier.padding(horizontal = 12.dp, vertical = 10.dp),117 horizontalArrangement = Arrangement.spacedBy(12.dp),118 verticalAlignment = Alignment.CenterVertically,119 ) {120 Icon(121 imageVector = if (selected) Icons.Filled.CheckCircle else Icons.Filled.SaveAlt,122 contentDescription = null,123 tint = if (selected) {124 MaterialTheme.colorScheme.onPrimaryContainer125 } else {126 MaterialTheme.colorScheme.onSurfaceVariant127 },128 )129 Column(modifier = Modifier.weight(1f)) {130 Text(131 title,132 style = MaterialTheme.typography.bodyLarge,133 fontWeight = if (selected) FontWeight.SemiBold else FontWeight.Medium,134 color = if (selected) MaterialTheme.colorScheme.onPrimaryContainer else MaterialTheme.colorScheme.onSurface,135 )136 Text(137 subtitle,138 style = MaterialTheme.typography.bodySmall,139 color = if (selected) {140 MaterialTheme.colorScheme.onPrimaryContainer.copy(alpha = 0.76f)141 } else {142 MaterialTheme.colorScheme.onSurfaceVariant143 },144 maxLines = 2,145 overflow = TextOverflow.Ellipsis,146 )147 }148 }149 }150}151152/** Confirms deleting downloaded files, or local chapters when [isLocalEntry]; [deletesEntry] warns the whole title goes. */153@Composable154internal fun DeleteChaptersDialog(155 isLocalEntry: Boolean,156 deletesEntry: Boolean,157 selectedCount: Int,158 itemType: ItemType,159 onConfirm: () -> Unit,160 onDismiss: () -> Unit,161) {162 AlertDialog(163 onDismissRequest = onDismiss,164 title = {165 Text(166 if (isLocalEntry) {167 "Delete chapters"168 } else {169 "Delete downloads"170 },171 )172 },173 text = {174 Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {175 if (deletesEntry) {176 Row(177 verticalAlignment = Alignment.CenterVertically,178 horizontalArrangement = Arrangement.spacedBy(8.dp),179 ) {180 Icon(181 Icons.Filled.Info,182 contentDescription = "Warning",183 tint = MaterialTheme.colorScheme.error,184 )185 Text(186 "Are you sure you want to delete all chapters? This will delete the entire entry from the library.",187 color = MaterialTheme.colorScheme.error,188 style = MaterialTheme.typography.bodyMedium,189 )190 }191 } else {192 Text(193 if (isLocalEntry) {194 "Delete $selectedCount selected local " +195 "${chapterLabel(itemType).lowercase()}${if (selectedCount == 1) "" else "s"}?"196 } else {197 "Delete downloaded files for $selectedCount selected " +198 "${chapterLabel(itemType).lowercase()}${if (selectedCount == 1) "" else "s"}?"199 },200 style = MaterialTheme.typography.bodyMedium,201 )202 }203 }204 },205 confirmButton = {206 TextButton(onClick = onConfirm) {207 Text(stringResource(Res.string.delete), color = MaterialTheme.colorScheme.error)208 }209 },210 dismissButton = {211 TextButton(onClick = onDismiss) {212 Text(stringResource(Res.string.action_cancel))213 }214 },215 )216}217218internal sealed class PendingDownloadRequest {219 data class Single(val chapter: Chapter) : PendingDownloadRequest()220 data class Bulk(val chapters: List<Chapter>, val option: DetailDownloadOption) : PendingDownloadRequest()221}222223internal enum class DetailDownloadOption {224 Next,225 Next5,226 Next10,227 Next25,228 Unread,229 All,230 Selected,231}232233internal fun downloadOptionLabel(option: DetailDownloadOption, itemType: ItemType): String {234 val unit = if (itemType == ItemType.Anime) "episode" else "chapter"235 return when (option) {236 DetailDownloadOption.Next -> "Next $unit"237 DetailDownloadOption.Next5 -> "Next 5 ${unit}s"238 DetailDownloadOption.Next10 -> "Next 10 ${unit}s"239 DetailDownloadOption.Next25 -> "Next 25 ${unit}s"240 DetailDownloadOption.Unread -> if (itemType == ItemType.Anime) "Unwatched" else "Unread"241 DetailDownloadOption.All -> "All ${unit}s"242 DetailDownloadOption.Selected -> "Selected ${unit}s"243 }244}245246internal fun chaptersForDownloadOption(chapters: List<Chapter>, option: DetailDownloadOption): List<Chapter> =247 when (option) {248 DetailDownloadOption.Unread -> chapters.filter { !it.isRead }249 DetailDownloadOption.All -> chapters250 DetailDownloadOption.Selected -> chapters251 DetailDownloadOption.Next,252 DetailDownloadOption.Next5,253 DetailDownloadOption.Next10,254 DetailDownloadOption.Next25 -> {255 val count = when (option) {256 DetailDownloadOption.Next -> 1257 DetailDownloadOption.Next5 -> 5258 DetailDownloadOption.Next10 -> 10259 DetailDownloadOption.Next25 -> 25260 DetailDownloadOption.Unread,261 DetailDownloadOption.All,262 DetailDownloadOption.Selected -> 1263 }264 val lastReadIndex = chapters.indexOfLast { it.isRead }265 val startIndex = if (lastReadIndex < 0 || chapters.size == 1) 0 else lastReadIndex + 1266 chapters.drop(startIndex).take(count)267 }268 }269