From c8a00bd9eed9816daf886c56131fa531e90685eb Mon Sep 17 00:00:00 2001 From: Colin White Date: Mon, 13 Sep 2021 17:09:26 -0400 Subject: [PATCH] Add ImageResult to ImagePainter.state. (#887) * Add ImageResult to ImagePainter.state. * Avoid changing component2 signature. * Remove unused. * Revert "Avoid changing component2 signature." This reverts commit a14dae6bed04ed7e6b320af3fac84194293f3cff. * Fix warning. --- coil-compose-base/api/coil-compose-base.api | 18 +++++----- .../main/java/coil/compose/ImagePainter.kt | 33 ++++++++++++++----- 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/coil-compose-base/api/coil-compose-base.api b/coil-compose-base/api/coil-compose-base.api index eb5e731393..64e8746c38 100644 --- a/coil-compose-base/api/coil-compose-base.api +++ b/coil-compose-base/api/coil-compose-base.api @@ -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 (Landroidx/compose/ui/graphics/painter/Painter;Ljava/lang/Throwable;)V + public fun (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; @@ -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 (Landroidx/compose/ui/graphics/painter/Painter;Lcoil/request/ImageResult$Metadata;)V + public fun (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; } diff --git a/coil-compose-base/src/main/java/coil/compose/ImagePainter.kt b/coil-compose-base/src/main/java/coil/compose/ImagePainter.kt index 0aad74e0c1..8f29f468b3 100644 --- a/coil-compose-base/src/main/java/coil/compose/ImagePainter.kt +++ b/coil-compose-base/src/main/java/coil/compose/ImagePainter.kt @@ -1,4 +1,5 @@ @file:SuppressLint("ComposableNaming") +@file:Suppress("unused") package coil.compose @@ -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 + } } } @@ -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 } @@ -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 ) } @@ -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 ) }