Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dotlottie/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ dotlottieRust {
}

group = "com.github.LottieFiles"
version = "0.14.0"
version = "0.14.1"

android {
namespace = "com.lottiefiles.dotlottie.core"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import com.dotlottie.dlplayer.Config as DLConfig
Expand Down Expand Up @@ -70,7 +71,7 @@ class DotLottieAnimation @JvmOverloads constructor(
private var height: Int = 0
private var mConfig: Config? = null
private var mLottieDrawable: DotLottieDrawable? = null
private val coroutineScope = CoroutineScope(SupervisorJob())
private var coroutineScope = CoroutineScope(SupervisorJob())
private var setupConfigJob: Job? = null
private var setupDrawableJob: Job? = null
private var layoutListener: ViewTreeObserver.OnGlobalLayoutListener? = null
Expand Down Expand Up @@ -337,8 +338,8 @@ class DotLottieAnimation @JvmOverloads constructor(
layoutListener = object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
if (width > 0 && height > 0) {
// Start animation setup only when dimensions are available
setupDotLottieDrawable()
// Start animation setup only when dimensions are available.
setupDrawableIfNeeded()
// Remove the listener to avoid redundant calls
removeLayoutListener()
}
Expand All @@ -365,8 +366,24 @@ class DotLottieAnimation @JvmOverloads constructor(
}


private fun ensureScopeActive() {
if (!coroutineScope.isActive) {
coroutineScope = CoroutineScope(SupervisorJob())
}
}

private fun setupDrawableIfNeeded() {
if (mLottieDrawable != null) return
if (setupConfigJob?.isActive == true || setupDrawableJob?.isActive == true) return
when {
mConfig != null -> setupConfig()
::attributes.isInitialized && attributes.src.isNotBlank() -> setupDotLottieDrawable()
}
}

private fun setupConfig() {
val config = mConfig ?: return
ensureScopeActive()
setupConfigJob?.cancel()
setupConfigJob = coroutineScope.launch {
runCatching {
Expand Down Expand Up @@ -416,6 +433,7 @@ class DotLottieAnimation @JvmOverloads constructor(
}

private fun setupDotLottieDrawable() {
ensureScopeActive()
setupDrawableJob?.cancel()
setupDrawableJob = coroutineScope.launch {
runCatching {
Comment thread
afsalz marked this conversation as resolved.
Expand All @@ -426,6 +444,8 @@ class DotLottieAnimation @JvmOverloads constructor(
DotLottieUtils.getContent(context, DotLottieSource.Asset(attributes.src))
}

if (!isActive || !this@DotLottieAnimation.isAttachedToWindow) return@launch

mLottieDrawable = DotLottieDrawable(
animationData = content,
width = width,
Expand Down Expand Up @@ -482,6 +502,17 @@ class DotLottieAnimation @JvmOverloads constructor(
}
}

override fun onAttachedToWindow() {
super.onAttachedToWindow()
if (mLottieDrawable == null) {
if (width > 0 && height > 0) {
setupDrawableIfNeeded()
} else if (layoutListener == null) {
waitForLayout()
}
}
}

override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
removeLayoutListener()
Expand Down
Loading