From b7b536100c3cb4860b39963703089c5692cd27bc Mon Sep 17 00:00:00 2001 From: Gabriel Peal Date: Mon, 1 Apr 2024 16:56:24 -0700 Subject: [PATCH] Add support for tgs files in Compose (#2488) Fixes https://github.com/airbnb/lottie-android/issues/2477 --- .../compose/rememberLottieComposition.kt | 30 +++++++++++++++---- .../tests/ComposeScaleTypesTestCase.kt | 6 ---- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/lottie-compose/src/main/java/com/airbnb/lottie/compose/rememberLottieComposition.kt b/lottie-compose/src/main/java/com/airbnb/lottie/compose/rememberLottieComposition.kt index dfa486c062..f5ca1fb2f4 100644 --- a/lottie-compose/src/main/java/com/airbnb/lottie/compose/rememberLottieComposition.kt +++ b/lottie-compose/src/main/java/com/airbnb/lottie/compose/rememberLottieComposition.kt @@ -22,6 +22,7 @@ import kotlinx.coroutines.suspendCancellableCoroutine import kotlinx.coroutines.withContext import java.io.FileInputStream import java.io.IOException +import java.util.zip.GZIPInputStream import java.util.zip.ZipInputStream import kotlin.coroutines.resume import kotlin.coroutines.resumeWithException @@ -157,14 +158,19 @@ private fun lottieTask( null } else { val fis = FileInputStream(spec.fileName) + val actualCacheKey = if (cacheKey == DefaultCacheKey) spec.fileName else cacheKey when { spec.fileName.endsWith("zip") -> LottieCompositionFactory.fromZipStream( ZipInputStream(fis), - if (cacheKey == DefaultCacheKey) spec.fileName else cacheKey, + actualCacheKey, + ) + spec.fileName.endsWith("tgs") -> LottieCompositionFactory.fromJsonInputStream( + GZIPInputStream(fis), + actualCacheKey, ) else -> LottieCompositionFactory.fromJsonInputStream( fis, - if (cacheKey == DefaultCacheKey) spec.fileName else cacheKey, + actualCacheKey, ) } } @@ -181,8 +187,22 @@ private fun lottieTask( LottieCompositionFactory.fromJsonString(spec.jsonString, jsonStringCacheKey) } is LottieCompositionSpec.ContentProvider -> { - val inputStream = context.contentResolver.openInputStream(spec.uri) - LottieCompositionFactory.fromJsonInputStream(inputStream, if (cacheKey == DefaultCacheKey) spec.uri.toString() else cacheKey) + val fis = context.contentResolver.openInputStream(spec.uri) + val actualCacheKey = if (cacheKey == DefaultCacheKey) spec.uri.toString() else cacheKey + when { + spec.uri.toString().endsWith("zip") -> LottieCompositionFactory.fromZipStream( + ZipInputStream(fis), + actualCacheKey, + ) + spec.uri.toString().endsWith("tgs") -> LottieCompositionFactory.fromJsonInputStream( + GZIPInputStream(fis), + actualCacheKey, + ) + else -> LottieCompositionFactory.fromJsonInputStream( + fis, + actualCacheKey, + ) + } } } } @@ -310,4 +330,4 @@ private fun String.ensureLeadingPeriod(): String = when { isBlank() -> this startsWith(".") -> this else -> ".$this" -} \ No newline at end of file +} diff --git a/snapshot-tests/src/androidTest/java/com/airbnb/lottie/snapshots/tests/ComposeScaleTypesTestCase.kt b/snapshot-tests/src/androidTest/java/com/airbnb/lottie/snapshots/tests/ComposeScaleTypesTestCase.kt index e5149c6b62..0e6de827f2 100644 --- a/snapshot-tests/src/androidTest/java/com/airbnb/lottie/snapshots/tests/ComposeScaleTypesTestCase.kt +++ b/snapshot-tests/src/androidTest/java/com/airbnb/lottie/snapshots/tests/ComposeScaleTypesTestCase.kt @@ -12,18 +12,12 @@ import androidx.compose.foundation.layout.width import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.verticalScroll import androidx.compose.material.Text -import androidx.compose.runtime.getValue import androidx.compose.ui.Modifier import androidx.compose.ui.draw.scale import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.unit.dp import com.airbnb.lottie.compose.LottieAnimation -import com.airbnb.lottie.compose.LottieCompositionSpec -import com.airbnb.lottie.compose.LottieConstants -import com.airbnb.lottie.compose.animateLottieCompositionAsState -import com.airbnb.lottie.compose.rememberLottieComposition -import com.airbnb.lottie.snapshots.R import com.airbnb.lottie.snapshots.SnapshotTestCase import com.airbnb.lottie.snapshots.SnapshotTestCaseContext import com.airbnb.lottie.snapshots.loadCompositionFromAssetsSync