Skip to content

Commit

Permalink
Add ImageResult to ImagePainter.state. (#887)
Browse files Browse the repository at this point in the history
* Add ImageResult to ImagePainter.state.

* Avoid changing component2 signature.

* Remove unused.

* Revert "Avoid changing component2 signature."

This reverts commit a14dae6.

* Fix warning.
  • Loading branch information
colinrtwhite authored Sep 13, 2021
1 parent 31c8f35 commit c8a00bd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
18 changes: 10 additions & 8 deletions coil-compose-base/api/coil-compose-base.api
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,14 @@ public final class coil/compose/ImagePainter$State$Empty : coil/compose/ImagePai

public final class coil/compose/ImagePainter$State$Error : coil/compose/ImagePainter$State {
public static final field $stable I
public fun <init> (Landroidx/compose/ui/graphics/painter/Painter;Ljava/lang/Throwable;)V
public fun <init> (Landroidx/compose/ui/graphics/painter/Painter;Lcoil/request/ErrorResult;)V
public final fun component1 ()Landroidx/compose/ui/graphics/painter/Painter;
public final fun component2 ()Ljava/lang/Throwable;
public final fun copy (Landroidx/compose/ui/graphics/painter/Painter;Ljava/lang/Throwable;)Lcoil/compose/ImagePainter$State$Error;
public static synthetic fun copy$default (Lcoil/compose/ImagePainter$State$Error;Landroidx/compose/ui/graphics/painter/Painter;Ljava/lang/Throwable;ILjava/lang/Object;)Lcoil/compose/ImagePainter$State$Error;
public final fun component2 ()Lcoil/request/ErrorResult;
public final fun copy (Landroidx/compose/ui/graphics/painter/Painter;Lcoil/request/ErrorResult;)Lcoil/compose/ImagePainter$State$Error;
public static synthetic fun copy$default (Lcoil/compose/ImagePainter$State$Error;Landroidx/compose/ui/graphics/painter/Painter;Lcoil/request/ErrorResult;ILjava/lang/Object;)Lcoil/compose/ImagePainter$State$Error;
public fun equals (Ljava/lang/Object;)Z
public fun getPainter ()Landroidx/compose/ui/graphics/painter/Painter;
public final fun getResult ()Lcoil/request/ErrorResult;
public final fun getThrowable ()Ljava/lang/Throwable;
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
Expand All @@ -72,14 +73,15 @@ public final class coil/compose/ImagePainter$State$Loading : coil/compose/ImageP

public final class coil/compose/ImagePainter$State$Success : coil/compose/ImagePainter$State {
public static final field $stable I
public fun <init> (Landroidx/compose/ui/graphics/painter/Painter;Lcoil/request/ImageResult$Metadata;)V
public fun <init> (Landroidx/compose/ui/graphics/painter/Painter;Lcoil/request/SuccessResult;)V
public final fun component1 ()Landroidx/compose/ui/graphics/painter/Painter;
public final fun component2 ()Lcoil/request/ImageResult$Metadata;
public final fun copy (Landroidx/compose/ui/graphics/painter/Painter;Lcoil/request/ImageResult$Metadata;)Lcoil/compose/ImagePainter$State$Success;
public static synthetic fun copy$default (Lcoil/compose/ImagePainter$State$Success;Landroidx/compose/ui/graphics/painter/Painter;Lcoil/request/ImageResult$Metadata;ILjava/lang/Object;)Lcoil/compose/ImagePainter$State$Success;
public final fun component2 ()Lcoil/request/SuccessResult;
public final fun copy (Landroidx/compose/ui/graphics/painter/Painter;Lcoil/request/SuccessResult;)Lcoil/compose/ImagePainter$State$Success;
public static synthetic fun copy$default (Lcoil/compose/ImagePainter$State$Success;Landroidx/compose/ui/graphics/painter/Painter;Lcoil/request/SuccessResult;ILjava/lang/Object;)Lcoil/compose/ImagePainter$State$Success;
public fun equals (Ljava/lang/Object;)Z
public final fun getMetadata ()Lcoil/request/ImageResult$Metadata;
public fun getPainter ()Landroidx/compose/ui/graphics/painter/Painter;
public final fun getResult ()Lcoil/request/SuccessResult;
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}
Expand Down
33 changes: 24 additions & 9 deletions coil-compose-base/src/main/java/coil/compose/ImagePainter.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@file:SuppressLint("ComposableNaming")
@file:Suppress("unused")

package coil.compose

Expand Down Expand Up @@ -294,14 +295,28 @@ class ImagePainter internal constructor(
/** The request was successful. */
data class Success(
override val painter: Painter,
val metadata: ImageResult.Metadata,
) : State()
val result: SuccessResult,
) : State() {

@Deprecated(
message = "Migrate to `result.metadata`.",
replaceWith = ReplaceWith("result.metadata")
)
val metadata: ImageResult.Metadata get() = result.metadata
}

/** The request failed due to [throwable]. */
/** The request failed due to [ErrorResult.throwable]. */
data class Error(
override val painter: Painter?,
val throwable: Throwable,
) : State()
val result: ErrorResult,
) : State() {

@Deprecated(
message = "Migrate to `result.throwable`.",
replaceWith = ReplaceWith("result.throwable")
)
val throwable: Throwable get() = result.throwable
}
}
}

Expand Down Expand Up @@ -340,7 +355,7 @@ private fun updatePainter(
if (state is State.Loading) loading.value = state.painter

// Short circuit if the request isn't successful or if it's returned by the memory cache.
if (state !is State.Success || state.metadata.dataSource == DataSource.MEMORY_CACHE) {
if (state !is State.Success || state.result.metadata.dataSource == DataSource.MEMORY_CACHE) {
imagePainter.painter = painter
return
}
Expand All @@ -353,7 +368,7 @@ private fun updatePainter(
// Fall back to Scale.FIT to match the default image content scale.
scale = request.defined.scale ?: Scale.FIT,
durationMillis = transition.durationMillis,
fadeStart = !state.metadata.isPlaceholderMemoryCacheKeyPresent
fadeStart = !state.result.metadata.isPlaceholderMemoryCacheKeyPresent
)
}

Expand All @@ -373,11 +388,11 @@ private fun unsupportedData(name: String): Nothing {
private fun ImageResult.toState() = when (this) {
is SuccessResult -> State.Success(
painter = drawable.toPainter(),
metadata = metadata
result = this
)
is ErrorResult -> State.Error(
painter = drawable?.toPainter(),
throwable = throwable
result = this
)
}

Expand Down

0 comments on commit c8a00bd

Please # to comment.