-
Notifications
You must be signed in to change notification settings - Fork 638
/
SubcomposeAsyncImage.kt
251 lines (236 loc) · 10.6 KB
/
SubcomposeAsyncImage.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package coil.compose
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.LayoutScopeMarker
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.Stable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.graphics.DefaultAlpha
import androidx.compose.ui.graphics.FilterQuality
import androidx.compose.ui.graphics.drawscope.DrawScope.Companion.DefaultFilterQuality
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.layout.ContentScale
import coil.ImageLoader
import coil.compose.AsyncImagePainter.Companion.DefaultTransform
import coil.compose.AsyncImagePainter.State
import coil.request.ImageRequest
/**
* A composable that executes an [ImageRequest] asynchronously and renders the result.
*
* @param model Either an [ImageRequest] or the [ImageRequest.data] value.
* @param contentDescription Text used by accessibility services to describe what this image
* represents. This should always be provided unless this image is used for decorative purposes,
* and does not represent a meaningful action that a user can take.
* @param imageLoader The [ImageLoader] that will be used to execute the request.
* @param modifier Modifier used to adjust the layout algorithm or draw decoration content.
* @param loading An optional callback to overwrite what's drawn while the image request is loading.
* @param success An optional callback to overwrite what's drawn when the image request succeeds.
* @param error An optional callback to overwrite what's drawn when the image request fails.
* @param onLoading Called when the image request begins loading.
* @param onSuccess Called when the image request completes successfully.
* @param onError Called when the image request completes unsuccessfully.
* @param alignment Optional alignment parameter used to place the [AsyncImagePainter] in the given
* bounds defined by the width and height.
* @param contentScale Optional scale parameter used to determine the aspect ratio scaling to be
* used if the bounds are a different size from the intrinsic size of the [AsyncImagePainter].
* @param alpha Optional opacity to be applied to the [AsyncImagePainter] when it is rendered
* onscreen.
* @param colorFilter Optional [ColorFilter] to apply for the [AsyncImagePainter] when it is
* rendered onscreen.
* @param filterQuality Sampling algorithm applied to a bitmap when it is scaled and drawn into the
* destination.
*/
@Composable
fun SubcomposeAsyncImage(
model: Any?,
contentDescription: String?,
imageLoader: ImageLoader,
modifier: Modifier = Modifier,
loading: @Composable (SubcomposeAsyncImageScope.(State.Loading) -> Unit)? = null,
success: @Composable (SubcomposeAsyncImageScope.(State.Success) -> Unit)? = null,
error: @Composable (SubcomposeAsyncImageScope.(State.Error) -> Unit)? = null,
onLoading: ((State.Loading) -> Unit)? = null,
onSuccess: ((State.Success) -> Unit)? = null,
onError: ((State.Error) -> Unit)? = null,
alignment: Alignment = Alignment.Center,
contentScale: ContentScale = ContentScale.Fit,
alpha: Float = DefaultAlpha,
colorFilter: ColorFilter? = null,
filterQuality: FilterQuality = DefaultFilterQuality,
) = SubcomposeAsyncImage(
model = model,
contentDescription = contentDescription,
imageLoader = imageLoader,
modifier = modifier,
onState = onStateOf(onLoading, onSuccess, onError),
alignment = alignment,
contentScale = contentScale,
alpha = alpha,
colorFilter = colorFilter,
filterQuality = filterQuality,
content = contentOf(loading, success, error)
)
/**
* A composable that executes an [ImageRequest] asynchronously and renders the result.
*
* @param model Either an [ImageRequest] or the [ImageRequest.data] value.
* @param contentDescription Text used by accessibility services to describe what this image
* represents. This should always be provided unless this image is used for decorative purposes,
* and does not represent a meaningful action that a user can take.
* @param imageLoader The [ImageLoader] that will be used to execute the request.
* @param modifier Modifier used to adjust the layout algorithm or draw decoration content.
* @param transform A callback to transform a new [State] before it's applied to the
* [AsyncImagePainter]. Typically this is used to modify the state's [Painter].
* @param onState Called when the state of this painter changes.
* @param alignment Optional alignment parameter used to place the [AsyncImagePainter] in the given
* bounds defined by the width and height.
* @param contentScale Optional scale parameter used to determine the aspect ratio scaling to be
* used if the bounds are a different size from the intrinsic size of the [AsyncImagePainter].
* @param alpha Optional opacity to be applied to the [AsyncImagePainter] when it is rendered
* onscreen.
* @param colorFilter Optional [ColorFilter] to apply for the [AsyncImagePainter] when it is
* rendered onscreen.
* @param filterQuality Sampling algorithm applied to a bitmap when it is scaled and drawn into the
* destination.
* @param content A callback to draw the content inside an [SubcomposeAsyncImageScope].
*/
@Composable
fun SubcomposeAsyncImage(
model: Any?,
contentDescription: String?,
imageLoader: ImageLoader,
modifier: Modifier = Modifier,
transform: (State) -> State = DefaultTransform,
onState: ((State) -> Unit)? = null,
alignment: Alignment = Alignment.Center,
contentScale: ContentScale = ContentScale.Fit,
alpha: Float = DefaultAlpha,
colorFilter: ColorFilter? = null,
filterQuality: FilterQuality = DefaultFilterQuality,
content: @Composable SubcomposeAsyncImageScope.() -> Unit,
) {
// Create and execute the image request.
val request = updateRequest(requestOf(model), contentScale)
val painter = rememberAsyncImagePainter(
request, imageLoader, transform, onState, contentScale, filterQuality
)
val sizeResolver = request.sizeResolver
if (sizeResolver !is ConstraintsSizeResolver) {
// Fast path: draw the content without subcomposition as we don't need to resolve the
// constraints.
Box(
modifier = modifier,
contentAlignment = alignment,
propagateMinConstraints = true
) {
RealSubcomposeAsyncImageScope(
parentScope = this,
painter = painter,
contentDescription = contentDescription,
alignment = alignment,
contentScale = contentScale,
alpha = alpha,
colorFilter = colorFilter
).content()
}
} else {
// Slow path: draw the content with subcomposition as we need to resolve the constraints
// before calling `content`.
BoxWithConstraints(
modifier = modifier,
contentAlignment = alignment,
propagateMinConstraints = true
) {
// Ensure `painter.state` is up to date immediately. Resolving the constraints
// synchronously is necessary to ensure that images from the memory cache are resolved
// and `painter.state` is updated to `Success` before invoking `content`.
sizeResolver.setConstraints(constraints)
RealSubcomposeAsyncImageScope(
parentScope = this,
painter = painter,
contentDescription = contentDescription,
alignment = alignment,
contentScale = contentScale,
alpha = alpha,
colorFilter = colorFilter
).content()
}
}
}
/**
* A scope for the children of [SubcomposeAsyncImage].
*/
@LayoutScopeMarker
@Immutable
interface SubcomposeAsyncImageScope : BoxScope {
/** The painter that is drawn by [SubcomposeAsyncImageContent]. */
val painter: AsyncImagePainter
/** The content description for [SubcomposeAsyncImageContent]. */
val contentDescription: String?
/** The default alignment for any composables drawn in this scope. */
val alignment: Alignment
/** The content scale for [SubcomposeAsyncImageContent]. */
val contentScale: ContentScale
/** The alpha for [SubcomposeAsyncImageContent]. */
val alpha: Float
/** The color filter for [SubcomposeAsyncImageContent]. */
val colorFilter: ColorFilter?
}
/**
* A composable that draws [SubcomposeAsyncImage]'s content with [SubcomposeAsyncImageScope]'s
* properties.
*
* @see SubcomposeAsyncImageScope
*/
@Composable
fun SubcomposeAsyncImageScope.SubcomposeAsyncImageContent(
modifier: Modifier = Modifier,
painter: Painter = this.painter,
contentDescription: String? = this.contentDescription,
alignment: Alignment = this.alignment,
contentScale: ContentScale = this.contentScale,
alpha: Float = this.alpha,
colorFilter: ColorFilter? = this.colorFilter,
) = Content(
modifier = modifier,
painter = painter,
contentDescription = contentDescription,
alignment = alignment,
contentScale = contentScale,
alpha = alpha,
colorFilter = colorFilter
)
@Stable
private fun contentOf(
loading: @Composable (SubcomposeAsyncImageScope.(State.Loading) -> Unit)?,
success: @Composable (SubcomposeAsyncImageScope.(State.Success) -> Unit)?,
error: @Composable (SubcomposeAsyncImageScope.(State.Error) -> Unit)?,
): @Composable SubcomposeAsyncImageScope.() -> Unit {
return if (loading != null || success != null || error != null) {
{
var draw = true
when (val state = painter.state) {
is State.Loading -> if (loading != null) loading(state).also { draw = false }
is State.Success -> if (success != null) success(state).also { draw = false }
is State.Error -> if (error != null) error(state).also { draw = false }
is State.Empty -> {} // Skipped if rendering on the main thread.
}
if (draw) SubcomposeAsyncImageContent()
}
} else {
{ SubcomposeAsyncImageContent() }
}
}
private data class RealSubcomposeAsyncImageScope(
private val parentScope: BoxScope,
override val painter: AsyncImagePainter,
override val contentDescription: String?,
override val alignment: Alignment,
override val contentScale: ContentScale,
override val alpha: Float,
override val colorFilter: ColorFilter?,
) : SubcomposeAsyncImageScope, BoxScope by parentScope