From d432ddb7a3ea341883af5bf730a918e738cd2348 Mon Sep 17 00:00:00 2001 From: Zhirkevich Alexander Y Date: Tue, 16 Jul 2024 16:14:40 +0300 Subject: [PATCH] syncronize dotlottie assets loading --- .../compottie/DotLottieAssetsManager.kt | 45 ++++++++++--------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/compottie-dot/src/commonMain/kotlin/io/github/alexzhirkevich/compottie/DotLottieAssetsManager.kt b/compottie-dot/src/commonMain/kotlin/io/github/alexzhirkevich/compottie/DotLottieAssetsManager.kt index 3eec0fb4..a959c3a6 100644 --- a/compottie-dot/src/commonMain/kotlin/io/github/alexzhirkevich/compottie/DotLottieAssetsManager.kt +++ b/compottie-dot/src/commonMain/kotlin/io/github/alexzhirkevich/compottie/DotLottieAssetsManager.kt @@ -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 @@ -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 + } } }