From cdc1c5efa3b92a0fe43651cc043175f33397611b Mon Sep 17 00:00:00 2001 From: arkon Date: Wed, 26 Jul 2023 23:26:58 -0400 Subject: [PATCH] Better handle saving animated drawables --- .../tachiyomi/data/library/LibraryUpdateNotifier.kt | 4 ++-- .../tachiyomi/ui/manga/MangaCoverScreenModel.kt | 4 ++-- .../kanade/tachiyomi/ui/reader/SaveImageNotifier.kt | 4 ++-- .../ui/reader/viewer/ReaderPageImageView.kt | 5 +---- .../tachiyomi/util/system/DrawableExtensions.kt | 13 +++++++++++++ 5 files changed, 20 insertions(+), 10 deletions(-) create mode 100644 app/src/main/java/eu/kanade/tachiyomi/util/system/DrawableExtensions.kt diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateNotifier.kt b/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateNotifier.kt index 6b530a171..0c4606a4d 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateNotifier.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateNotifier.kt @@ -6,7 +6,6 @@ import android.content.Context import android.content.Intent import android.graphics.Bitmap import android.graphics.BitmapFactory -import android.graphics.drawable.BitmapDrawable import android.net.Uri import androidx.core.app.NotificationCompat import androidx.core.app.NotificationManagerCompat @@ -23,6 +22,7 @@ import eu.kanade.tachiyomi.data.notification.Notifications import eu.kanade.tachiyomi.ui.main.MainActivity import eu.kanade.tachiyomi.util.lang.chop import eu.kanade.tachiyomi.util.system.cancelNotification +import eu.kanade.tachiyomi.util.system.getBitmapOrNull import eu.kanade.tachiyomi.util.system.notificationBuilder import eu.kanade.tachiyomi.util.system.notify import tachiyomi.core.Constants @@ -274,7 +274,7 @@ class LibraryUpdateNotifier(private val context: Context) { .size(NOTIF_ICON_SIZE) .build() val drawable = context.imageLoader.execute(request).drawable - return (drawable as? BitmapDrawable)?.bitmap + return drawable?.getBitmapOrNull() } private fun getNewChaptersDescription(chapters: Array): String { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaCoverScreenModel.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaCoverScreenModel.kt index d430a5ea2..ffb6553c4 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaCoverScreenModel.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaCoverScreenModel.kt @@ -1,7 +1,6 @@ package eu.kanade.tachiyomi.ui.manga import android.content.Context -import android.graphics.drawable.BitmapDrawable import android.net.Uri import androidx.compose.material3.SnackbarHostState import cafe.adriel.voyager.core.model.StateScreenModel @@ -16,6 +15,7 @@ import eu.kanade.tachiyomi.data.saver.Image import eu.kanade.tachiyomi.data.saver.ImageSaver import eu.kanade.tachiyomi.data.saver.Location import eu.kanade.tachiyomi.util.editCover +import eu.kanade.tachiyomi.util.system.getBitmapOrNull import eu.kanade.tachiyomi.util.system.toShareIntent import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch @@ -98,7 +98,7 @@ class MangaCoverScreenModel( val result = context.imageLoader.execute(req).drawable // TODO: Handle animated cover - val bitmap = (result as? BitmapDrawable)?.bitmap ?: return@withIOContext null + val bitmap = result?.getBitmapOrNull() ?: return@withIOContext null imageSaver.save( Image.Cover( bitmap = bitmap, diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/SaveImageNotifier.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/SaveImageNotifier.kt index 574cd1cab..6e1e8fafa 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/SaveImageNotifier.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/SaveImageNotifier.kt @@ -2,7 +2,6 @@ package eu.kanade.tachiyomi.ui.reader import android.content.Context import android.graphics.Bitmap -import android.graphics.drawable.BitmapDrawable import android.net.Uri import androidx.core.app.NotificationCompat import coil.imageLoader @@ -13,6 +12,7 @@ import eu.kanade.tachiyomi.data.notification.NotificationHandler import eu.kanade.tachiyomi.data.notification.NotificationReceiver import eu.kanade.tachiyomi.data.notification.Notifications import eu.kanade.tachiyomi.util.system.cancelNotification +import eu.kanade.tachiyomi.util.system.getBitmapOrNull import eu.kanade.tachiyomi.util.system.notificationBuilder import eu.kanade.tachiyomi.util.system.notify @@ -35,7 +35,7 @@ class SaveImageNotifier(private val context: Context) { .memoryCachePolicy(CachePolicy.DISABLED) .size(720, 1280) .target( - onSuccess = { showCompleteNotification(uri, (it as? BitmapDrawable)?.bitmap) }, + onSuccess = { showCompleteNotification(uri, it.getBitmapOrNull()) }, onError = { onError(null) }, ) .build() diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/ReaderPageImageView.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/ReaderPageImageView.kt index f010e9c36..02d4cce9e 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/ReaderPageImageView.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/ReaderPageImageView.kt @@ -277,10 +277,7 @@ open class ReaderPageImageView @JvmOverloads constructor( ) when (image) { - is Drawable -> { - val bitmap = (image as BitmapDrawable).bitmap - setImage(ImageSource.bitmap(bitmap)) - } + is BitmapDrawable -> setImage(ImageSource.bitmap(image.bitmap)) is InputStream -> setImage(ImageSource.inputStream(image)) else -> throw IllegalArgumentException("Not implemented for class ${image::class.simpleName}") } diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/system/DrawableExtensions.kt b/app/src/main/java/eu/kanade/tachiyomi/util/system/DrawableExtensions.kt new file mode 100644 index 000000000..2a09aeca9 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/util/system/DrawableExtensions.kt @@ -0,0 +1,13 @@ +package eu.kanade.tachiyomi.util.system + +import android.graphics.Bitmap +import android.graphics.drawable.BitmapDrawable +import android.graphics.drawable.Drawable +import androidx.core.graphics.drawable.toBitmap +import coil.drawable.ScaleDrawable + +fun Drawable.getBitmapOrNull(): Bitmap? = when (this) { + is BitmapDrawable -> bitmap + is ScaleDrawable -> child.toBitmap() + else -> null +}