UpdateErrorsScreen.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/activity/UpdateErrorsScreen.kt
1package eu.friedcutlet.yokan.ui.screens.activity23import androidx.compose.foundation.layout.Arrangement4import androidx.compose.foundation.layout.Box5import androidx.compose.foundation.layout.PaddingValues6import androidx.compose.foundation.layout.Row7import androidx.compose.foundation.layout.fillMaxSize8import androidx.compose.foundation.layout.padding9import androidx.compose.foundation.layout.size10import androidx.compose.foundation.lazy.LazyColumn11import androidx.compose.foundation.lazy.items12import androidx.compose.foundation.shape.RoundedCornerShape13import androidx.compose.material.icons.Icons14import androidx.compose.material.icons.automirrored.filled.ArrowBack15import androidx.compose.material.icons.filled.Close16import androidx.compose.material.icons.filled.Delete17import androidx.compose.material.icons.filled.SwapHoriz18import androidx.compose.material3.ExperimentalMaterial3Api19import androidx.compose.material3.Icon20import androidx.compose.material3.IconButton21import androidx.compose.material3.ListItem22import androidx.compose.material3.MaterialTheme23import androidx.compose.material3.Text24import androidx.compose.material3.TopAppBar25import androidx.compose.runtime.Composable26import androidx.compose.runtime.getValue27import androidx.compose.runtime.produceState28import androidx.compose.ui.Alignment29import androidx.compose.ui.Modifier30import androidx.compose.ui.draw.clip31import androidx.compose.ui.text.font.FontWeight32import androidx.compose.ui.text.style.TextOverflow33import androidx.compose.ui.unit.dp34import androidx.lifecycle.compose.collectAsStateWithLifecycle35import eu.friedcutlet.yokan.app.YokanRuntime36import eu.friedcutlet.yokan.database.DbUpdateError37import eu.friedcutlet.yokan.domain.Manga38import eu.friedcutlet.yokan.services.library.withoutUpdateError39import eu.friedcutlet.yokan.ui.components.CoverImage40import eu.friedcutlet.yokan.ui.navigation.Navigator41import eu.friedcutlet.yokan.ui.navigation.Route42import eu.friedcutlet.yokan.ui.theme.YokanScaffold as Scaffold43import kotlinx.coroutines.Dispatchers44import kotlinx.coroutines.withContext45import org.jetbrains.compose.resources.stringResource46import yokan.composeapp.generated.resources.Res47import yokan.composeapp.generated.resources.action_back4849@Composable50internal fun UpdateErrorsRoute(nav: Navigator) {51 val settings = YokanRuntime.settings52 val repository = YokanRuntime.repository53 val db by settings.db.collectAsStateWithLifecycle()54 val errors = db.updateErrors.orEmpty()55 val titles by produceState(emptyMap<Long, Manga>(), errors) {56 value = withContext(Dispatchers.Default) {57 errors.mapNotNull { error -> repository.manga(error.mangaId)?.let { error.mangaId to it } }.toMap()58 }59 }60 UpdateErrorsScreen(61 errors = errors,62 titles = titles,63 title = "Update errors",64 onBack = nav::back,65 onMigrate = { mangaId -> nav.navigate(Route.Migrate(mangaId)) },66 onDismissError = { mangaId -> settings.updateDb { it.withoutUpdateError(mangaId) } },67 onClearErrors = { settings.updateDb { it.copy(updateErrors = emptyList()) } },68 )69}7071@Composable72@OptIn(ExperimentalMaterial3Api::class)73internal fun UpdateErrorsScreen(74 errors: List<DbUpdateError>,75 titles: Map<Long, Manga>,76 title: String,77 onBack: () -> Unit,78 onMigrate: (mangaId: Long) -> Unit,79 onDismissError: (mangaId: Long) -> Unit,80 onClearErrors: () -> Unit,81) {82 Scaffold(83 topBar = {84 TopAppBar(85 title = { Text(title) },86 navigationIcon = {87 IconButton(onClick = onBack) {88 Icon(Icons.AutoMirrored.Filled.ArrowBack, contentDescription = stringResource(Res.string.action_back))89 }90 },91 actions = {92 if (errors.isNotEmpty()) {93 IconButton(94 onClick = onClearErrors,95 ) {96 Icon(Icons.Filled.Delete, contentDescription = "Clear all")97 }98 }99 },100 )101 },102 ) { padding ->103 if (errors.isEmpty()) {104 Box(105 modifier = Modifier.fillMaxSize().padding(padding),106 contentAlignment = Alignment.Center,107 ) {108 Text(109 text = "No update errors",110 style = MaterialTheme.typography.bodyLarge,111 color = MaterialTheme.colorScheme.onSurfaceVariant,112 )113 }114 } else {115 LazyColumn(116 modifier = Modifier.fillMaxSize().padding(padding),117 contentPadding = PaddingValues(vertical = 8.dp),118 ) {119 items(errors, key = { it.mangaId }) { err ->120 val manga = titles[err.mangaId]121 ListItem(122 leadingContent = {123 Box(124 modifier = Modifier125 .size(width = 40.dp, height = 56.dp)126 .clip(RoundedCornerShape(4.dp)),127 ) {128 CoverImage(129 model = manga?.let { it.customCoverImage ?: it.customCoverFromTracker ?: it.imageUrl },130 contentDescription = "${err.name} cover",131 modifier = Modifier.fillMaxSize(),132 shape = RoundedCornerShape(4.dp),133 fallbackInitials = err.name.split(" ").take(2)134 .joinToString("") { it.firstOrNull()?.uppercase() ?: "" },135 )136 }137 },138 headlineContent = {139 Text(140 text = err.name,141 maxLines = 1,142 overflow = TextOverflow.Ellipsis,143 fontWeight = FontWeight.SemiBold,144 )145 },146 supportingContent = {147 Text(148 text = err.error,149 maxLines = 2,150 overflow = TextOverflow.Ellipsis,151 color = MaterialTheme.colorScheme.error,152 style = MaterialTheme.typography.bodySmall,153 )154 },155 trailingContent = {156 Row(157 verticalAlignment = Alignment.CenterVertically,158 horizontalArrangement = Arrangement.spacedBy(4.dp),159 ) {160 if (manga != null) {161 IconButton(162 onClick = { onMigrate(manga.id) },163 ) {164 Icon(165 Icons.Filled.SwapHoriz,166 contentDescription = "Migrate",167 tint = MaterialTheme.colorScheme.primary,168 )169 }170 }171 IconButton(172 onClick = { onDismissError(err.mangaId) },173 ) {174 Icon(175 Icons.Filled.Close,176 contentDescription = "Dismiss",177 )178 }179 }180 },181 )182 }183 }184 }185 }186}187