From adf02e53fd2da2c889a409a144cf1c81bde7f6f3 Mon Sep 17 00:00:00 2001 From: Andreas Date: Wed, 27 Apr 2022 15:24:35 +0200 Subject: [PATCH] Add error state to MangaCover composable (#7022) * Add error state to MangaCover - Add error drawable when thumbnailUrl isn't able to be loaded - Tweak usage of MangaCover * Change `contentDescription` to be nullable As the invoke function makes default nulls --- .../presentation/components/MangaCover.kt | 44 ++++++++++--------- .../presentation/history/HistoryScreen.kt | 4 +- .../eu/kanade/presentation/util/Resources.kt | 22 ++++++++++ 3 files changed, 46 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/eu/kanade/presentation/components/MangaCover.kt b/app/src/main/java/eu/kanade/presentation/components/MangaCover.kt index 33c8dfaf61..c949dcf8b2 100644 --- a/app/src/main/java/eu/kanade/presentation/components/MangaCover.kt +++ b/app/src/main/java/eu/kanade/presentation/components/MangaCover.kt @@ -11,29 +11,31 @@ import androidx.compose.ui.graphics.painter.ColorPainter import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.unit.dp import coil.compose.AsyncImage +import eu.kanade.presentation.util.bitmapPainterResource +import eu.kanade.tachiyomi.R -enum class MangaCoverAspect(val ratio: Float) { - SQUARE(1f / 1f), - COVER(2f / 3f) -} +enum class MangaCover(private val ratio: Float) { + Square(1f / 1f), + Book(2f / 3f); -@Composable -fun MangaCover( - modifier: Modifier = Modifier, - data: String?, - aspect: MangaCoverAspect, - contentDescription: String = "", - shape: Shape = RoundedCornerShape(4.dp) -) { - AsyncImage( - model = data, - placeholder = ColorPainter(CoverPlaceholderColor), - contentDescription = contentDescription, - modifier = modifier - .aspectRatio(aspect.ratio) - .clip(shape), - contentScale = ContentScale.Crop - ) + @Composable + operator fun invoke( + modifier: Modifier = Modifier, + data: String?, + contentDescription: String? = null, + shape: Shape? = null + ) { + AsyncImage( + model = data, + placeholder = ColorPainter(CoverPlaceholderColor), + error = bitmapPainterResource(id = R.drawable.cover_error), + contentDescription = contentDescription, + modifier = modifier + .aspectRatio(ratio) + .clip(shape ?: RoundedCornerShape(4.dp)), + contentScale = ContentScale.Crop, + ) + } } private val CoverPlaceholderColor = Color(0x1F888888) diff --git a/app/src/main/java/eu/kanade/presentation/history/HistoryScreen.kt b/app/src/main/java/eu/kanade/presentation/history/HistoryScreen.kt index ed9f6738a9..4523d1c775 100644 --- a/app/src/main/java/eu/kanade/presentation/history/HistoryScreen.kt +++ b/app/src/main/java/eu/kanade/presentation/history/HistoryScreen.kt @@ -25,7 +25,6 @@ import androidx.paging.compose.items import eu.kanade.domain.history.model.HistoryWithRelations import eu.kanade.presentation.components.EmptyScreen import eu.kanade.presentation.components.MangaCover -import eu.kanade.presentation.components.MangaCoverAspect import eu.kanade.presentation.util.horizontalPadding import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.preference.PreferencesHelper @@ -168,12 +167,11 @@ fun HistoryItem( .padding(horizontal = horizontalPadding, vertical = 8.dp), verticalAlignment = Alignment.CenterVertically, ) { - MangaCover( + MangaCover.Book( modifier = Modifier .fillMaxHeight() .clickable(onClick = onClickCover), data = history.thumbnailUrl, - aspect = MangaCoverAspect.COVER ) Column( modifier = Modifier diff --git a/app/src/main/java/eu/kanade/presentation/util/Resources.kt b/app/src/main/java/eu/kanade/presentation/util/Resources.kt index 51bdbe5a77..b22e6d02bb 100644 --- a/app/src/main/java/eu/kanade/presentation/util/Resources.kt +++ b/app/src/main/java/eu/kanade/presentation/util/Resources.kt @@ -1,8 +1,14 @@ package eu.kanade.presentation.util +import android.content.res.Resources +import androidx.annotation.DrawableRes import androidx.annotation.PluralsRes import androidx.compose.runtime.Composable +import androidx.compose.ui.graphics.asImageBitmap +import androidx.compose.ui.graphics.painter.BitmapPainter import androidx.compose.ui.platform.LocalContext +import androidx.core.content.ContextCompat +import androidx.core.graphics.drawable.toBitmap /** * Load a quantity string resource. @@ -30,3 +36,19 @@ fun quantityStringResource(@PluralsRes id: Int, quantity: Int, vararg formatArgs val context = LocalContext.current return context.resources.getQuantityString(id, quantity, *formatArgs) } + +/** + * Create a BitmapPainter from an drawable resource. + * + * > Only use this if [androidx.compose.ui.res.painterResource] doesn't work. + * + * @param id the resource identifier + * @return the bitmap associated with the resource + */ +@Composable +fun bitmapPainterResource(@DrawableRes id: Int): BitmapPainter { + val context = LocalContext.current + val drawable = ContextCompat.getDrawable(context, id) + ?: throw Resources.NotFoundException() + return BitmapPainter(drawable.toBitmap().asImageBitmap()) +}