Skip to content

Commit

Permalink
syncronize dotlottie assets loading
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhirkevich Alexander Y authored and Zhirkevich Alexander Y committed Jul 16, 2024
1 parent 2de3925 commit d432ddb
Showing 1 changed file with 25 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package io.github.alexzhirkevich.compottie
import io.github.alexzhirkevich.compottie.assets.ImageRepresentable
import io.github.alexzhirkevich.compottie.assets.LottieImageSpec
import io.github.alexzhirkevich.compottie.assets.LottieAssetsManager
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import okio.Path
import okio.Path.Companion.toPath

Expand All @@ -11,27 +13,30 @@ internal class DotLottieAssetsManager(
private val root : Path? = null
) : LottieAssetsManager {

override suspend fun image(image: LottieImageSpec): ImageRepresentable? {

val trimPath = image.path
.removePrefix("/")
.removeSuffix("/")
.takeIf(String::isNotEmpty)

val trimName = image.name
.removePrefix("/")
.removeSuffix("/")
.takeIf(String::isNotEmpty)
private val mutex = Mutex()

load(null, trimPath, trimName)?.let {
return ImageRepresentable.Bytes(it)
}

return load("/images", trimPath, trimName)?.let {
ImageRepresentable.Bytes(it)
} ?: run {
Compottie.logger?.warn("Failed to decode dotLottie asset $trimName")
null
override suspend fun image(image: LottieImageSpec): ImageRepresentable? {
return mutex.withLock {
val trimPath = image.path
.removePrefix("/")
.removeSuffix("/")
.takeIf(String::isNotEmpty)

val trimName = image.name
.removePrefix("/")
.removeSuffix("/")
.takeIf(String::isNotEmpty)

load(null, trimPath, trimName)?.let {
return ImageRepresentable.Bytes(it)
}

load("/images", trimPath, trimName)?.let {
ImageRepresentable.Bytes(it)
} ?: run {
Compottie.logger?.warn("Failed to decode dotLottie asset $trimName")
null
}
}
}

Expand Down

0 comments on commit d432ddb

Please sign in to comment.