From 50d063095ad479a9a737e30dc1c31a8701a05fd4 Mon Sep 17 00:00:00 2001 From: Tyler Little Date: Mon, 12 Jun 2023 23:12:16 -0600 Subject: [PATCH] Implement animated gifs when clicking on an image --- app/build.gradle | 3 +++ .../ui/components/common/ImageViewerDialog.kt | 16 +++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/app/build.gradle b/app/build.gradle index 3f97c9ece..094520c7d 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -88,6 +88,9 @@ dependencies { implementation 'androidx.compose.runtime:runtime-livedata:1.4.3' implementation "androidx.lifecycle:lifecycle-runtime-compose:2.6.1" + // gif support + implementation "io.coil-kt:coil-gif:2.4.0" + // To use Kotlin annotation processing tool ksp "androidx.room:room-compiler:2.5.1" diff --git a/app/src/main/java/com/jerboa/ui/components/common/ImageViewerDialog.kt b/app/src/main/java/com/jerboa/ui/components/common/ImageViewerDialog.kt index 2cff9bb4c..2544131e1 100644 --- a/app/src/main/java/com/jerboa/ui/components/common/ImageViewerDialog.kt +++ b/app/src/main/java/com/jerboa/ui/components/common/ImageViewerDialog.kt @@ -2,6 +2,7 @@ package com.jerboa.ui.components.common import android.content.Context import android.net.Uri +import android.os.Build.VERSION.SDK_INT import android.webkit.MimeTypeMap import android.widget.Toast import androidx.compose.animation.animateColorAsState @@ -38,7 +39,10 @@ import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import androidx.compose.ui.window.Dialog import androidx.compose.ui.window.DialogProperties +import coil.ImageLoader import coil.compose.rememberAsyncImagePainter +import coil.decode.GifDecoder +import coil.decode.ImageDecoderDecoder import com.jerboa.saveBitmap import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -85,6 +89,16 @@ fun ImageViewerDialog(url: String, onBackRequest: () -> Unit) { val context = LocalContext.current val coroutineScope = rememberCoroutineScope() + val imageLoader = ImageLoader.Builder(LocalContext.current) + .components { + if (SDK_INT >= 28) { + add(ImageDecoderDecoder.Factory()) + } else { + add(GifDecoder.Factory()) + } + } + .build() + Dialog( onDismissRequest = onBackRequest, properties = DialogProperties( @@ -95,7 +109,7 @@ fun ImageViewerDialog(url: String, onBackRequest: () -> Unit) { ) { Box(Modifier.background(backgroundColor.value)) { Image( - painter = rememberAsyncImagePainter(url), + painter = rememberAsyncImagePainter(url, imageLoader = imageLoader), contentDescription = null, modifier = Modifier .fillMaxSize()