Skip to content

Commit

Permalink
Skip crossfade transition if view is not visible. (#361)
Browse files Browse the repository at this point in the history
* Skip crossfade transition if view is not visible.

* Rename param.
  • Loading branch information
colinrtwhite authored Apr 12, 2020
1 parent 336e854 commit b486a0e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
11 changes: 11 additions & 0 deletions coil-base/src/main/java/coil/transition/CrossfadeTransition.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package coil.transition

import android.graphics.drawable.Drawable
import android.widget.ImageView
import androidx.core.view.isVisible
import androidx.vectordrawable.graphics.drawable.Animatable2Compat
import coil.annotation.ExperimentalCoilApi
import coil.decode.DataSource
Expand Down Expand Up @@ -36,6 +37,16 @@ class CrossfadeTransition @JvmOverloads constructor(
return
}

// Don't animate if the view is not visible as CrossfadeDrawable.onDraw
// won't be called until the view becomes visible.
if (!target.view.isVisible) {
when (result) {
is SuccessResult -> target.onSuccess(result.drawable)
is ErrorResult -> target.onError(result.drawable)
}
return
}

// Animate the drawable and suspend until the animation is completes.
suspendCancellableCoroutine<Unit> { continuation ->
val crossfadeDrawable = createCrossfade(continuation, target, result.drawable)
Expand Down
36 changes: 31 additions & 5 deletions coil-base/src/test/java/coil/transition/CrossfadeTransitionTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.widget.ImageView
import androidx.core.view.isVisible
import androidx.test.core.app.ApplicationProvider
import coil.annotation.ExperimentalCoilApi
import coil.decode.DataSource
Expand Down Expand Up @@ -48,7 +49,7 @@ class CrossfadeTransitionTest {
}

@Test
fun `success - isMemoryCache=true`() {
fun `success - memory cache`() {
val drawable = ColorDrawable()
var onSuccessCalled = false

Expand All @@ -70,7 +71,7 @@ class CrossfadeTransitionTest {
}

@Test
fun `success - isMemoryCache=false`() {
fun `success - disk`() {
val drawable = ColorDrawable()
var onSuccessCalled = false

Expand All @@ -95,8 +96,32 @@ class CrossfadeTransitionTest {
assertTrue(onSuccessCalled)
}

/** Regression test: https://github.com/coil-kt/coil/issues/304 */
@Test
fun failure() {
fun `success - view not visible`() {
val drawable = ColorDrawable()
val imageView = ImageView(context)
imageView.isVisible = false
var onSuccessCalled = false

runBlocking {
transition.transition(
target = createTransitionTarget(
imageView = imageView,
onSuccess = { result ->
assertFalse(onSuccessCalled)
onSuccessCalled = true

assertFalse(result is CrossfadeDrawable)
}
),
result = SuccessResult(drawable, DataSource.NETWORK)
)
}
}

@Test
fun `failure - disk`() {
val drawable = ColorDrawable()
var onSuccessCalled = false

Expand All @@ -122,14 +147,15 @@ class CrossfadeTransitionTest {
}

private inline fun createTransitionTarget(
imageView: ImageView = ImageView(context),
crossinline onStart: (placeholder: Drawable?) -> Unit = { fail() },
crossinline onError: (error: Drawable?) -> Unit = { fail() },
crossinline onSuccess: (result: Drawable) -> Unit = { fail() }
): TransitionTarget<*> {
return object : TransitionTarget<ImageView> {
override val view = ImageView(context)
override val view = imageView
override val drawable: Drawable?
get() = view.drawable
get() = imageView.drawable
override fun onStart(placeholder: Drawable?) = onStart(placeholder)
override fun onError(error: Drawable?) = onError(error)
override fun onSuccess(result: Drawable) = onSuccess(result)
Expand Down

0 comments on commit b486a0e

Please sign in to comment.