Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for tgs files in Compose #2488

Merged
merged 1 commit into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
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"
}
}
Original file line number Diff line number Diff line change
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