Skip to content

Commit

Permalink
Add support for tgs files in Compose (#2488)
Browse files Browse the repository at this point in the history
Fixes #2477
  • Loading branch information
gpeal committed Apr 1, 2024
1 parent c4a0bc1 commit b7b5361
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
Expand Up @@ -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
Expand Down Expand Up @@ -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,
)
}
}
Expand All @@ -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,
)
}
}
}
}
Expand Down Expand Up @@ -310,4 +330,4 @@ private fun String.ensureLeadingPeriod(): String = when {
isBlank() -> this
startsWith(".") -> this
else -> ".$this"
}
}
Expand Up @@ -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
Expand Down

0 comments on commit b7b5361

Please sign in to comment.