-
Notifications
You must be signed in to change notification settings - Fork 586
/
Coil.kt
392 lines (376 loc) · 16 KB
/
Coil.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
* Copyright 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@file:JvmName("CoilImage")
@file:JvmMultifileClass
package dev.chrisbanes.accompanist.coil
import androidx.compose.foundation.Image
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.runtime.staticAmbientOf
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.graphics.ImageAsset
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.ContextAmbient
import androidx.compose.ui.unit.IntSize
import coil.Coil
import coil.ImageLoader
import coil.imageLoader
import coil.request.ImageRequest
import coil.request.ImageResult
import dev.chrisbanes.accompanist.imageloading.DataSource
import dev.chrisbanes.accompanist.imageloading.DefaultRefetchOnSizeChangeLambda
import dev.chrisbanes.accompanist.imageloading.EmptyRequestCompleteLambda
import dev.chrisbanes.accompanist.imageloading.ImageLoad
import dev.chrisbanes.accompanist.imageloading.ImageLoadState
import dev.chrisbanes.accompanist.imageloading.MaterialLoadingImage
import dev.chrisbanes.accompanist.imageloading.toPainter
/**
* Ambient containing the preferred [ImageLoader] to use in [CoilImage].
*/
val AmbientImageLoader = staticAmbientOf<ImageLoader?> { null }
object CoilImageConstants {
/**
* Returns the default [ImageLoader] value for the `imageLoader` parameter in [CoilImage].
*/
@Composable
fun defaultImageLoader(): ImageLoader {
return AmbientImageLoader.current ?: ContextAmbient.current.imageLoader
}
}
/**
* Creates a composable that will attempt to load the given [data] using [Coil], and provides
* complete content of how the current state is displayed:
*
* ```
* CoilImage(
* data = "https://www.image.url",
* ) { imageState ->
* when (imageState) {
* is ImageLoadState.Success -> // TODO
* is ImageLoadState.Error -> // TODO
* ImageLoadState.Loading -> // TODO
* ImageLoadState.Empty -> // TODO
* }
* }
* ```
*
* @param data The data to load. See [ImageRequest.Builder.data] for the types allowed.
* @param modifier [Modifier] used to adjust the layout algorithm or draw decoration content.
* @param requestBuilder Optional builder for the [ImageRequest].
* @param imageLoader The [ImageLoader] to use when requesting the image. Defaults to
* [CoilImageConstants.defaultImageLoader].
* @param shouldRefetchOnSizeChange Lambda which will be invoked when the size changes, allowing
* optional re-fetching of the image. Return true to re-fetch the image.
* @param onRequestCompleted Listener which will be called when the loading request has finished.
* @param content Content to be displayed for the given state.
*/
@Composable
fun CoilImage(
data: Any,
modifier: Modifier = Modifier,
requestBuilder: (ImageRequest.Builder.(size: IntSize) -> ImageRequest.Builder)? = null,
imageLoader: ImageLoader = CoilImageConstants.defaultImageLoader(),
shouldRefetchOnSizeChange: (currentResult: ImageLoadState, size: IntSize) -> Boolean = DefaultRefetchOnSizeChangeLambda,
onRequestCompleted: (ImageLoadState) -> Unit = EmptyRequestCompleteLambda,
content: @Composable (imageLoadState: ImageLoadState) -> Unit
) {
CoilImage(
request = data.toImageRequest(),
modifier = modifier,
requestBuilder = requestBuilder,
imageLoader = imageLoader,
shouldRefetchOnSizeChange = shouldRefetchOnSizeChange,
onRequestCompleted = onRequestCompleted,
content = content
)
}
/**
* Creates a composable that will attempt to load the given [request] using [Coil], and provides
* complete content of how the current state is displayed:
*
* ```
* CoilImage(
* request = ImageRequest.Builder(context).data(...).build(),
* ) { imageState ->
* when (imageState) {
* is ImageLoadState.Success -> // TODO
* is ImageLoadState.Error -> // TODO
* ImageLoadState.Loading -> // TODO
* ImageLoadState.Empty -> // TODO
* }
* }
* ```
*
* @param request The request to execute. If the request does not have a [ImageRequest.sizeResolver]
* set, one will be set on the request using the layout constraints.
* @param modifier [Modifier] used to adjust the layout algorithm or draw decoration content.
* @param requestBuilder Optional builder for the [ImageRequest].
* @param imageLoader The [ImageLoader] to use when requesting the image. Defaults to
* [CoilImageConstants.defaultImageLoader].
* @param shouldRefetchOnSizeChange Lambda which will be invoked when the size changes, allowing
* optional re-fetching of the image. Return true to re-fetch the image.
* @param onRequestCompleted Listener which will be called when the loading request has finished.
* @param content Content to be displayed for the given state.
*/
@Composable
fun CoilImage(
request: ImageRequest,
modifier: Modifier = Modifier,
requestBuilder: (ImageRequest.Builder.(size: IntSize) -> ImageRequest.Builder)? = null,
imageLoader: ImageLoader = CoilImageConstants.defaultImageLoader(),
shouldRefetchOnSizeChange: (currentResult: ImageLoadState, size: IntSize) -> Boolean = DefaultRefetchOnSizeChangeLambda,
onRequestCompleted: (ImageLoadState) -> Unit = EmptyRequestCompleteLambda,
content: @Composable (imageLoadState: ImageLoadState) -> Unit
) {
ImageLoad(
request = request,
executeRequest = { imageLoader.execute(it).toResult() },
transformRequestForSize = { r, size ->
val sizedRequest = when {
// If the request has a size resolver set we just execute the request as-is
r.defined.sizeResolver != null -> r
// If the size contains an unspecified sized dimension, we don't specify a size
// in the Coil request
size.width < 0 || size.height < 0 -> r
// If we have a non-zero size, we can modify the request to include the size
size != IntSize.Zero -> r.newBuilder().size(size.width, size.height).build()
// Otherwise we have a zero size, so no point executing a request
else -> null
}
if (sizedRequest != null && requestBuilder != null) {
// If we have a transformed request and builder, let it run
requestBuilder(sizedRequest.newBuilder(), size).build()
} else {
// Otherwise we just return the sizedRequest
sizedRequest
}
},
shouldRefetchOnSizeChange = shouldRefetchOnSizeChange,
onRequestCompleted = onRequestCompleted,
modifier = modifier,
content = content
)
}
/**
* Creates a composable that will attempt to load the given [data] using [Coil], and then
* display the result in an [Image].
*
* This version of the function is more opinionated, providing:
*
* - Support for displaying alternative content while the request is 'loading'.
* See the [loading] parameter.
* - Support for displaying alternative content if the request was unsuccessful.
* See the [error] parameter.
* - Support for automatically fading-in the image once loaded. See the [fadeIn] parameter.
*
* ```
* CoilImage(
* data = "https://www.image.url",
* fadeIn = true,
* loading = {
* Stack(Modifier.fillMaxSize()) {
* CircularProgressIndicator(Modifier.align(Alignment.Center))
* }
* }
* )
* ```
*
* @param data The data to load. See [ImageRequest.Builder.data] for the types allowed.
* @param modifier [Modifier] used to adjust the layout algorithm or draw decoration content.
* @param alignment Optional alignment parameter used to place the loaded [ImageAsset] 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 loaded [ImageAsset].
* @param colorFilter Optional colorFilter to apply for the [Painter] when it is rendered onscreen.
* @param error Content to be displayed when the request failed.
* @param loading Content to be displayed when the request is in progress.
* @param fadeIn Whether to run a fade-in animation when images are successfully loaded.
* Default: `false`.
* @param requestBuilder Optional builder for the [ImageRequest].
* @param imageLoader The [ImageLoader] to use when requesting the image. Defaults to
* [CoilImageConstants.defaultImageLoader].
* @param shouldRefetchOnSizeChange Lambda which will be invoked when the size changes, allowing
* optional re-fetching of the image. Return true to re-fetch the image.
* @param onRequestCompleted Listener which will be called when the loading request has finished.
*/
@Composable
fun CoilImage(
data: Any,
modifier: Modifier = Modifier,
alignment: Alignment = Alignment.Center,
contentScale: ContentScale = ContentScale.Fit,
colorFilter: ColorFilter? = null,
fadeIn: Boolean = false,
requestBuilder: (ImageRequest.Builder.(size: IntSize) -> ImageRequest.Builder)? = null,
imageLoader: ImageLoader = CoilImageConstants.defaultImageLoader(),
shouldRefetchOnSizeChange: (currentResult: ImageLoadState, size: IntSize) -> Boolean = DefaultRefetchOnSizeChangeLambda,
onRequestCompleted: (ImageLoadState) -> Unit = EmptyRequestCompleteLambda,
error: @Composable ((ImageLoadState.Error) -> Unit)? = null,
loading: @Composable (() -> Unit)? = null,
) {
CoilImage(
request = data.toImageRequest(),
modifier = modifier,
alignment = alignment,
contentScale = contentScale,
colorFilter = colorFilter,
fadeIn = fadeIn,
requestBuilder = requestBuilder,
imageLoader = imageLoader,
shouldRefetchOnSizeChange = shouldRefetchOnSizeChange,
onRequestCompleted = onRequestCompleted,
error = error,
loading = loading
)
}
/**
* Creates a composable that will attempt to load the given [request] using [Coil], and then
* display the result in an [Image].
*
* This version of the function is more opinionated, providing:
*
* - Support for displaying alternative content while the request is 'loading'.
* See the [loading] parameter.
* - Support for displaying alternative content if the request was unsuccessful.
* See the [error] parameter.
* - Support for automatically fading-in the image once loaded. See the [fadeIn] parameter.
*
* ```
* CoilImage(
* request = ImageRequest.Builder(context).data(...).build(),
* fadeIn = true,
* loading = {
* Stack(Modifier.fillMaxSize()) {
* CircularProgressIndicator(Modifier.align(Alignment.Center))
* }
* }
* )
* ```
*
* @param request The request to execute. If the request does not have a [ImageRequest.sizeResolver]
* set, one will be set on the request using the layout constraints.
* @param modifier [Modifier] used to adjust the layout algorithm or draw decoration content.
* @param alignment Optional alignment parameter used to place the loaded [ImageAsset] 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 loaded [ImageAsset].
* @param colorFilter Optional colorFilter to apply for the [Painter] when it is rendered onscreen.
* @param error Content to be displayed when the request failed.
* @param loading Content to be displayed when the request is in progress.
* @param fadeIn Whether to run a fade-in animation when images are successfully loaded. Default: `false`.
* @param requestBuilder Optional builder for the [ImageRequest].
* @param imageLoader The [ImageLoader] to use when requesting the image. Defaults to
* [CoilImageConstants.defaultImageLoader].
* @param shouldRefetchOnSizeChange Lambda which will be invoked when the size changes, allowing
* optional re-fetching of the image. Return true to re-fetch the image.
* @param onRequestCompleted Listener which will be called when the loading request has finished.
*/
@Composable
fun CoilImage(
request: ImageRequest,
modifier: Modifier = Modifier,
alignment: Alignment = Alignment.Center,
contentScale: ContentScale = ContentScale.Fit,
colorFilter: ColorFilter? = null,
fadeIn: Boolean = false,
requestBuilder: (ImageRequest.Builder.(size: IntSize) -> ImageRequest.Builder)? = null,
imageLoader: ImageLoader = CoilImageConstants.defaultImageLoader(),
shouldRefetchOnSizeChange: (currentResult: ImageLoadState, size: IntSize) -> Boolean = DefaultRefetchOnSizeChangeLambda,
onRequestCompleted: (ImageLoadState) -> Unit = EmptyRequestCompleteLambda,
error: @Composable ((ImageLoadState.Error) -> Unit)? = null,
loading: @Composable (() -> Unit)? = null,
) {
CoilImage(
request = request,
modifier = modifier,
requestBuilder = requestBuilder,
imageLoader = imageLoader,
shouldRefetchOnSizeChange = shouldRefetchOnSizeChange,
onRequestCompleted = onRequestCompleted,
) { imageState ->
when (imageState) {
is ImageLoadState.Success -> {
MaterialLoadingImage(
result = imageState,
fadeInEnabled = fadeIn,
alignment = alignment,
contentScale = contentScale,
colorFilter = colorFilter
)
}
is ImageLoadState.Error -> if (error != null) error(imageState)
ImageLoadState.Loading -> if (loading != null) loading()
ImageLoadState.Empty -> Unit
}
}
}
@Deprecated(
message = "Use ImageLoadState",
replaceWith = ReplaceWith("ImageLoadState", "dev.chrisbanes.accompanist.imageloading.ImageLoadState")
)
@Suppress("unused")
typealias RequestResult = ImageLoadState
@Deprecated(
message = "Use ImageLoadState.Success",
replaceWith = ReplaceWith(
"ImageLoadState.Success",
"dev.chrisbanes.accompanist.imageloading.ImageLoadState.Success"
)
)
@Suppress("unused")
typealias SuccessResult = ImageLoadState.Success
@Deprecated(
message = "Use ImageLoadState.Error",
replaceWith = ReplaceWith(
"ImageLoadState.Error",
"dev.chrisbanes.accompanist.imageloading.ImageLoadState.Error"
)
)
@Suppress("unused")
typealias ErrorResult = ImageLoadState.Error
private fun ImageResult.toResult(): ImageLoadState = when (this) {
is coil.request.SuccessResult -> {
ImageLoadState.Success(
painter = drawable.toPainter(),
source = metadata.dataSource.toDataSource()
)
}
is coil.request.ErrorResult -> {
ImageLoadState.Error(
painter = drawable?.toPainter(),
throwable = throwable
)
}
}
private fun coil.decode.DataSource.toDataSource(): DataSource = when (this) {
coil.decode.DataSource.NETWORK -> DataSource.NETWORK
coil.decode.DataSource.MEMORY -> DataSource.MEMORY
coil.decode.DataSource.MEMORY_CACHE -> DataSource.MEMORY
coil.decode.DataSource.DISK -> DataSource.DISK
}
@Composable
internal fun Any.toImageRequest(): ImageRequest {
// If the developer is accidentally using the wrong function (data vs request), just
// pass the request through
return if (this is ImageRequest) this else {
// Otherwise we construct a GetRequest using the data parameter
val context = ContextAmbient.current
remember(this) { ImageRequest.Builder(context).data(this).build() }
}
}