-
Notifications
You must be signed in to change notification settings - Fork 585
/
GlideImage.kt
277 lines (260 loc) · 10.9 KB
/
GlideImage.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
/*
* 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("GlideImage")
@file:JvmMultifileClass
package dev.chrisbanes.accompanist.glide
import android.graphics.drawable.Drawable
import androidx.compose.foundation.Image
import androidx.compose.runtime.Composable
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.ViewAmbient
import androidx.compose.ui.unit.IntSize
import com.bumptech.glide.Glide
import com.bumptech.glide.RequestBuilder
import com.bumptech.glide.RequestManager
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
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
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.suspendCancellableCoroutine
/**
* Ambient containing the preferred [RequestManager] to use in [GlideImage].
*/
val AmbientRequestManager = staticAmbientOf<RequestManager?> { null }
object GlideImageConstants {
/**
* Returns the default [RequestManager] value for the `requestManager` parameter
* in [GlideImage].
*/
@Composable
fun defaultRequestManager(): RequestManager {
return AmbientRequestManager.current ?: Glide.with(ViewAmbient.current)
}
}
/**
* Creates a composable that will attempt to load the given [data] using [Glide], and provides
* complete content of how the current state is displayed:
*
* ```
* GlideImage(
* 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.
* @param modifier [Modifier] used to adjust the layout algorithm or draw decoration content.
* @param requestBuilder Optional builder for the [RequestBuilder].
* @param requestManager The [RequestManager] to use when requesting the image. Defaults to the
* current value of [AmbientRequestManager].
* @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 GlideImage(
data: Any,
modifier: Modifier = Modifier,
requestBuilder: (RequestBuilder<Drawable>.(size: IntSize) -> RequestBuilder<Drawable>)? = null,
requestManager: RequestManager = GlideImageConstants.defaultRequestManager(),
shouldRefetchOnSizeChange: (currentResult: ImageLoadState, size: IntSize) -> Boolean = DefaultRefetchOnSizeChangeLambda,
onRequestCompleted: (ImageLoadState) -> Unit = EmptyRequestCompleteLambda,
content: @Composable (imageLoadState: ImageLoadState) -> Unit
) {
@OptIn(ExperimentalCoroutinesApi::class)
ImageLoad(
request = requestManager.load(data),
requestKey = data, // Glide RequestBuilder doesn't support equality so we use the data
executeRequest = { (r, size) -> requestManager.execute(r, size) },
transformRequestForSize = { r, size ->
Pair(requestBuilder?.invoke(r, size) ?: r, size)
},
shouldRefetchOnSizeChange = shouldRefetchOnSizeChange,
onRequestCompleted = onRequestCompleted,
modifier = modifier,
content = content
)
}
/**
* Creates a composable that will attempt to load the given [data] using [Glide], 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.
*
* ```
* GlideImage(
* data = "https://www.image.url",
* fadeIn = true,
* loading = {
* Stack(Modifier.fillMaxSize()) {
* CircularProgressIndicator(Modifier.align(Alignment.Center))
* }
* }
* )
* ```
*
* @param data The data to load.
* @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 [RequestBuilder].
* @param requestManager The [RequestManager] to use when requesting the image. Defaults to the
* current value of [AmbientRequestManager].
* @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 GlideImage(
data: Any,
modifier: Modifier = Modifier,
alignment: Alignment = Alignment.Center,
contentScale: ContentScale = ContentScale.Fit,
colorFilter: ColorFilter? = null,
fadeIn: Boolean = false,
requestBuilder: (RequestBuilder<Drawable>.(size: IntSize) -> RequestBuilder<Drawable>)? = null,
requestManager: RequestManager = GlideImageConstants.defaultRequestManager(),
shouldRefetchOnSizeChange: (currentResult: ImageLoadState, size: IntSize) -> Boolean = DefaultRefetchOnSizeChangeLambda,
onRequestCompleted: (ImageLoadState) -> Unit = EmptyRequestCompleteLambda,
error: @Composable ((ImageLoadState.Error) -> Unit)? = null,
loading: @Composable (() -> Unit)? = null,
) {
GlideImage(
data = data,
modifier = modifier,
requestBuilder = requestBuilder,
requestManager = requestManager,
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
}
}
}
/**
* A Coroutines wrapper around [Glide]
*/
@OptIn(ExperimentalCoroutinesApi::class)
private suspend fun RequestManager.execute(
request: RequestBuilder<Drawable>,
size: IntSize
): ImageLoadState = suspendCancellableCoroutine { cont ->
var failException: Throwable? = null
val target = object : EmptyCustomTarget(size.width, size.height) {
override fun onLoadFailed(errorDrawable: Drawable?) {
val result = ImageLoadState.Error(
painter = errorDrawable?.toPainter(),
throwable = failException
?: IllegalArgumentException("Error while loading $request")
)
cont.resume(result) {
// Clear any resources from the target if cancelled
clear(this)
}
}
}
val listener = object : RequestListener<Drawable> {
override fun onResourceReady(
drawable: Drawable,
model: Any,
target: Target<Drawable>,
dataSource: com.bumptech.glide.load.DataSource,
isFirstResource: Boolean
): Boolean {
val result = ImageLoadState.Success(
painter = drawable.toPainter(),
source = dataSource.toDataSource()
)
cont.resume(result) {
// Clear any resources from the target if cancelled
clear(target)
}
// Return true so that the target doesn't receive the drawable
return true
}
override fun onLoadFailed(
e: GlideException?,
model: Any,
target: Target<Drawable>,
isFirstResource: Boolean
): Boolean {
// Glide only passes the exception to the listener, so we store it
// for the target to use
failException = e
// Return false, allowing the target to receive it's onLoadFailed.
// This is needed so we can use any errorDrawable
return false
}
}
// Start the image request into the target
request.addListener(listener).into(target)
// If we're cancelled, clear the request from Glide
cont.invokeOnCancellation {
clear(target)
}
}
private fun com.bumptech.glide.load.DataSource.toDataSource(): DataSource = when (this) {
com.bumptech.glide.load.DataSource.LOCAL -> DataSource.DISK
com.bumptech.glide.load.DataSource.REMOTE -> DataSource.NETWORK
com.bumptech.glide.load.DataSource.DATA_DISK_CACHE -> DataSource.DISK
com.bumptech.glide.load.DataSource.RESOURCE_DISK_CACHE -> DataSource.DISK
com.bumptech.glide.load.DataSource.MEMORY_CACHE -> DataSource.MEMORY
}