DownloadManagerCard.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/downloads/DownloadManagerCard.kt
1package eu.friedcutlet.yokan.ui.screens.downloads23import androidx.compose.foundation.BorderStroke4import androidx.compose.foundation.layout.*5import androidx.compose.material.icons.Icons6import androidx.compose.material.icons.filled.DragHandle7import androidx.compose.material.icons.filled.KeyboardArrowDown8import androidx.compose.material.icons.filled.KeyboardArrowUp9import androidx.compose.material3.*10import androidx.compose.runtime.Composable11import androidx.compose.ui.Alignment12import androidx.compose.ui.Modifier13import androidx.compose.ui.semantics.contentDescription14import androidx.compose.ui.semantics.semantics15import androidx.compose.ui.text.font.FontWeight16import androidx.compose.ui.unit.dp17import org.jetbrains.compose.resources.stringResource18import yokan.composeapp.generated.resources.*1920@Composable21internal fun DownloadManagerCard(22 row: DownloadManagerRow,23 title: String,24 subtitle: String,25 missingDetails: Boolean,26 onCancel: () -> Unit,27 onPause: () -> Unit,28 onResume: () -> Unit,29 onRetry: () -> Unit,30 onOpen: () -> Unit,31 onDelete: () -> Unit,32 modifier: Modifier = Modifier,33 waitingForWifi: Boolean = false,34 dragHandle: Modifier? = null,35 isDragging: Boolean = false,36 onMoveUp: (() -> Unit)? = null,37 onMoveDown: (() -> Unit)? = null,38) {39 val attention = row.section == DownloadSection.NeedsAttention40 // Retry rebuilds the request from the chapter row, so it needs the chapter still in the library.41 val canRetry = row.canRetry && !missingDetails42 val status = stringResource(when {43 row.section == DownloadSection.Completed -> Res.string.dm_completed44 attention && row.failed > 0 -> Res.string.dm_failed45 attention -> Res.string.dm_interrupted46 waitingForWifi && row.live?.isPaused != true && row.live?.isCompleted != true -> Res.string.dm_waiting_wifi47 row.queued -> Res.string.dm_queued48 row.live?.isCompleted == true -> Res.string.dm_finishing49 row.live?.isPaused == true -> Res.string.dm_paused50 else -> Res.string.dm_downloading51 })52 Surface(53 modifier = modifier.fillMaxWidth().padding(horizontal = 16.dp, vertical = 5.dp),54 shape = MaterialTheme.shapes.large,55 color = MaterialTheme.colorScheme.surfaceContainer,56 border = BorderStroke(1.dp, if (attention) MaterialTheme.colorScheme.error.copy(alpha = .4f) else MaterialTheme.colorScheme.outlineVariant),57 shadowElevation = if (isDragging) 6.dp else 0.dp,58 ) {59 Column(Modifier.padding(16.dp), verticalArrangement = Arrangement.spacedBy(8.dp)) {60 Row(verticalAlignment = Alignment.CenterVertically) {61 if (dragHandle != null) {62 val description = stringResource(Res.string.dm_reorder)63 Box(Modifier.size(48.dp).then(dragHandle).semantics { contentDescription = description }, contentAlignment = Alignment.Center) {64 Icon(Icons.Default.DragHandle, null, tint = MaterialTheme.colorScheme.onSurfaceVariant)65 }66 }67 Column(Modifier.weight(1f), verticalArrangement = Arrangement.spacedBy(3.dp)) {68 Text(title, style = MaterialTheme.typography.titleMedium, fontWeight = FontWeight.SemiBold)69 Text(subtitle, style = MaterialTheme.typography.bodyMedium, color = MaterialTheme.colorScheme.onSurfaceVariant)70 }71 }72 FlowRow(horizontalArrangement = Arrangement.spacedBy(12.dp), verticalArrangement = Arrangement.spacedBy(4.dp)) {73 Text(status, style = MaterialTheme.typography.labelLarge,74 color = if (attention) MaterialTheme.colorScheme.error else MaterialTheme.colorScheme.primary)75 if (row.total > 0) {76 Text(stringResource(Res.string.dm_count, row.succeeded, row.total), style = MaterialTheme.typography.bodySmall)77 } else {78 Text(stringResource(Res.string.dm_unknown_total, row.succeeded), style = MaterialTheme.typography.bodySmall)79 }80 if (row.failed > 0) Text(stringResource(Res.string.dm_failed_count, row.failed), style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.error)81 }82 if (row.section == DownloadSection.InProgress) {83 val fraction = row.fraction84 if (fraction != null) {85 Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(12.dp)) {86 LinearProgressIndicator(progress = { fraction }, modifier = Modifier.weight(1f))87 Text(stringResource(Res.string.dm_percent, (fraction * 100).toInt()), style = MaterialTheme.typography.labelMedium)88 }89 } else if (row.live != null && !row.live.isPaused) {90 LinearProgressIndicator(modifier = Modifier.fillMaxWidth())91 }92 }93 if (missingDetails) Text(stringResource(Res.string.dm_missing), style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant)94 if (attention && !canRetry) Text(stringResource(Res.string.dm_restart_hint), style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant)95 if (row.section == DownloadSection.Completed && row.location == null) Text(stringResource(Res.string.dm_location_unknown), style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant)96 FlowRow(horizontalArrangement = Arrangement.spacedBy(8.dp), verticalArrangement = Arrangement.spacedBy(4.dp)) {97 if (row.live != null && !row.live.isCompleted) {98 OutlinedButton(onClick = if (row.live.isPaused) onResume else onPause) {99 Text(stringResource(if (row.live.isPaused) Res.string.resume else Res.string.tts_pause))100 }101 }102 if (canRetry) OutlinedButton(onClick = onRetry) { Text(stringResource(Res.string.retry)) }103 if (row.location != null) TextButton(onClick = onOpen) { Text(stringResource(Res.string.action_open)) }104 if (row.section == DownloadSection.InProgress) TextButton(onClick = onCancel) { Text(stringResource(Res.string.action_cancel)) }105 if (row.record != null && row.section != DownloadSection.InProgress) {106 TextButton(onClick = onDelete, colors = ButtonDefaults.textButtonColors(contentColor = MaterialTheme.colorScheme.error)) {107 Text(stringResource(if (row.location == null) Res.string.dm_delete_record else Res.string.dm_delete_files))108 }109 }110 if (dragHandle != null) {111 IconButton(onClick = { onMoveUp?.invoke() }, enabled = onMoveUp != null) { Icon(Icons.Default.KeyboardArrowUp, stringResource(Res.string.dm_move_up)) }112 IconButton(onClick = { onMoveDown?.invoke() }, enabled = onMoveDown != null) { Icon(Icons.Default.KeyboardArrowDown, stringResource(Res.string.dm_move_down)) }113 }114 }115 }116 }117}118