Skip to content

Commit

Permalink
Add early exit for transitions that are already completed in Resource…
Browse files Browse the repository at this point in the history
…AnimatedVisibility
  • Loading branch information
isaac-udy committed Jun 28, 2023
1 parent 708a361 commit e8a2959
Showing 1 changed file with 11 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ private fun Transition<Boolean>.animateAnimResource(
ResourceAnimationState.forAnimationStart(targetState)
)
}
val isCompleted = targetState == currentState

val anim = remember(resourceId, targetState) {
AnimationUtils.loadAnimation(context, resourceId).apply {
Expand All @@ -150,6 +151,10 @@ private fun Transition<Boolean>.animateAnimResource(
)

LaunchedEffect(anim) {
if (isCompleted) {
state.value = ResourceAnimationState.forAnimationEnd(targetState)
return@LaunchedEffect
}
withContext(Dispatchers.IO) {
val startTime = System.currentTimeMillis()
val transformation = Transformation()
Expand Down Expand Up @@ -198,6 +203,8 @@ private fun Transition<Boolean>.animateAnimatorResource(
ResourceAnimationState.forAnimationStart(targetState)
)
}
val isCompleted = targetState == currentState

val animator = remember(resourceId, targetState) {
AnimatorInflater.loadAnimator(context, resourceId)
}
Expand All @@ -211,6 +218,10 @@ private fun Transition<Boolean>.animateAnimatorResource(
) { if (it) 1.0f else 0.0f }

LaunchedEffect(animator) {
if (isCompleted) {
state.value = ResourceAnimationState.forAnimationEnd(targetState)
return@LaunchedEffect
}
val startTime = System.currentTimeMillis()
val animatorView = AnimatorView(context).apply {
layoutParams = ViewGroup.LayoutParams(size.width, size.height)
Expand Down

0 comments on commit e8a2959

Please sign in to comment.