Skip to content

Commit ef0e63d

Browse files
Merge pull request #4982 from sjudd:compose_previews
PiperOrigin-RevId: 511916995
2 parents e58802e + 01ed966 commit ef0e63d

File tree

1 file changed

+38
-0
lines changed
  • integration/compose/src/main/java/com/bumptech/glide/integration/compose

1 file changed

+38
-0
lines changed

integration/compose/src/main/java/com/bumptech/glide/integration/compose/GlideImage.kt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import androidx.compose.ui.graphics.DefaultAlpha
1515
import androidx.compose.ui.layout.ContentScale
1616
import androidx.compose.ui.layout.layout
1717
import androidx.compose.ui.platform.LocalContext
18+
import androidx.compose.ui.platform.LocalInspectionMode
1819
import androidx.compose.ui.semantics.SemanticsPropertyKey
1920
import androidx.compose.ui.semantics.SemanticsPropertyReceiver
2021
import androidx.compose.ui.semantics.semantics
@@ -28,6 +29,7 @@ import com.bumptech.glide.integration.ktx.InternalGlideApi
2829
import com.bumptech.glide.integration.ktx.ResolvableGlideSize
2930
import com.bumptech.glide.integration.ktx.Size
3031
import com.bumptech.glide.integration.ktx.Status
32+
import com.google.accompanist.drawablepainter.rememberDrawablePainter
3133

3234
/** Mutates and returns the given [RequestBuilder] to apply relevant options. */
3335
public typealias RequestBuilderTransform<T> = (RequestBuilder<T>) -> RequestBuilder<T>
@@ -111,6 +113,14 @@ public fun GlideImage(
111113
val overrideSize: Size? = requestBuilder.overrideSize()
112114
val (size, finalModifier) = rememberSizeAndModifier(overrideSize, modifier)
113115

116+
// TODO(judds): It seems like we should be able to use the production paths for
117+
// resource / drawables as well as Composables. It's not totally clear what part of the prod code
118+
// isn't supported.
119+
if (LocalInspectionMode.current && loading?.isResourceOrDrawable() == true) {
120+
PreviewResourceOrDrawable(loading, contentDescription, modifier)
121+
return
122+
}
123+
114124
SizedGlideImage(
115125
requestBuilder = requestBuilder,
116126
size = size,
@@ -125,6 +135,27 @@ public fun GlideImage(
125135
)
126136
}
127137

138+
@OptIn(ExperimentalGlideComposeApi::class)
139+
@Composable
140+
private fun PreviewResourceOrDrawable(
141+
loading: Placeholder,
142+
contentDescription: String?,
143+
modifier: Modifier,
144+
) {
145+
val drawable =
146+
when(loading) {
147+
is Placeholder.OfDrawable -> loading.drawable
148+
is Placeholder.OfResourceId -> LocalContext.current.getDrawable(loading.resourceId)
149+
is Placeholder.OfComposable ->
150+
throw IllegalArgumentException("Composables should go through the production codepath")
151+
}
152+
Image(
153+
painter = rememberDrawablePainter(drawable),
154+
modifier = modifier,
155+
contentDescription = contentDescription,
156+
)
157+
}
158+
128159
/**
129160
* Used to specify a [Drawable] to use in conjunction with [GlideImage]'s `loading` or `failure`
130161
* parameters.
@@ -177,6 +208,13 @@ public sealed class Placeholder {
177208
internal class OfResourceId(@DrawableRes internal val resourceId: Int) : Placeholder()
178209
internal class OfComposable(internal val composable: @Composable () -> Unit) : Placeholder()
179210

211+
internal fun isResourceOrDrawable() =
212+
when (this) {
213+
is OfDrawable -> true
214+
is OfResourceId -> true
215+
is OfComposable -> false
216+
}
217+
180218
internal fun maybeComposable(): (@Composable () -> Unit)? =
181219
when (this) {
182220
is OfComposable -> this.composable

0 commit comments

Comments
 (0)