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
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