-
Notifications
You must be signed in to change notification settings - Fork 638
/
AsyncImage.kt
240 lines (226 loc) · 9.31 KB
/
AsyncImage.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
package coil.compose
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clipToBounds
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 androidx.compose.ui.layout.Layout
import androidx.compose.ui.layout.LayoutModifier
import androidx.compose.ui.layout.Measurable
import androidx.compose.ui.layout.MeasureResult
import androidx.compose.ui.layout.MeasureScope
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.role
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.unit.Constraints
import coil.ImageLoader
import coil.compose.AsyncImagePainter.Companion.DefaultTransform
import coil.compose.AsyncImagePainter.State
import coil.request.ImageRequest
import coil.size.Dimension
import coil.size.SizeResolver
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.mapNotNull
import coil.size.Size as CoilSize
/**
* 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 placeholder A [Painter] that is displayed while the image is loading.
* @param error A [Painter] that is displayed when the image request is unsuccessful.
* @param fallback A [Painter] that is displayed when the request's [ImageRequest.data] is null.
* @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 AsyncImage(
model: Any?,
contentDescription: String?,
imageLoader: ImageLoader,
modifier: Modifier = Modifier,
placeholder: Painter? = null,
error: Painter? = null,
fallback: Painter? = error,
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,
) = AsyncImage(
model = model,
contentDescription = contentDescription,
imageLoader = imageLoader,
modifier = modifier,
transform = transformOf(placeholder, error, fallback),
onState = onStateOf(onLoading, onSuccess, onError),
alignment = alignment,
contentScale = contentScale,
alpha = alpha,
colorFilter = colorFilter,
filterQuality = filterQuality
)
/**
* 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.
*/
@Composable
fun AsyncImage(
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,
) {
// Create and execute the image request.
val request = updateRequest(requestOf(model), contentScale)
val painter = rememberAsyncImagePainter(
request, imageLoader, transform, onState, contentScale, filterQuality
)
// Draw the content without a parent composable or subcomposition.
val sizeResolver = request.sizeResolver
Content(
modifier = if (sizeResolver is ConstraintsSizeResolver) {
modifier.then(sizeResolver)
} else {
modifier
},
painter = painter,
contentDescription = contentDescription,
alignment = alignment,
contentScale = contentScale,
alpha = alpha,
colorFilter = colorFilter
)
}
/** Draws the current image content. */
@Composable
internal fun Content(
modifier: Modifier,
painter: Painter,
contentDescription: String?,
alignment: Alignment,
contentScale: ContentScale,
alpha: Float,
colorFilter: ColorFilter?,
) = Layout(
modifier = modifier
.contentDescription(contentDescription)
.clipToBounds()
.then(
ContentPainterModifier(
painter = painter,
alignment = alignment,
contentScale = contentScale,
alpha = alpha,
colorFilter = colorFilter
)
),
measurePolicy = { _, constraints ->
layout(constraints.minWidth, constraints.minHeight) {}
}
)
@Composable
internal fun updateRequest(request: ImageRequest, contentScale: ContentScale): ImageRequest {
return if (request.defined.sizeResolver == null) {
val sizeResolver = if (contentScale == ContentScale.None) {
SizeResolver(CoilSize.ORIGINAL)
} else {
remember { ConstraintsSizeResolver() }
}
request.newBuilder().size(sizeResolver).build()
} else {
request
}
}
/** A [SizeResolver] that computes the size from the constrains passed during the layout phase. */
internal class ConstraintsSizeResolver : SizeResolver, LayoutModifier {
private val _constraints = MutableStateFlow(ZeroConstraints)
override suspend fun size() = _constraints.mapNotNull(Constraints::toSizeOrNull).first()
override fun MeasureScope.measure(
measurable: Measurable,
constraints: Constraints
): MeasureResult {
// Cache the current constraints.
_constraints.value = constraints
// Measure and layout the content.
val placeable = measurable.measure(constraints)
return layout(placeable.width, placeable.height) {
placeable.place(0, 0)
}
}
fun setConstraints(constraints: Constraints) {
_constraints.value = constraints
}
}
@Stable
private fun Modifier.contentDescription(contentDescription: String?): Modifier {
if (contentDescription != null) {
return semantics {
this.contentDescription = contentDescription
this.role = Role.Image
}
} else {
return this
}
}
@Stable
private fun Constraints.toSizeOrNull() = when {
isZero -> null
else -> CoilSize(
width = if (hasBoundedWidth) Dimension(maxWidth) else Dimension.Original,
height = if (hasBoundedHeight) Dimension(maxHeight) else Dimension.Original
)
}