-
Notifications
You must be signed in to change notification settings - Fork 2
/
Picture.kt
543 lines (495 loc) · 18.1 KB
/
Picture.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
import android.app.Activity
import android.content.Context
import android.content.ContextWrapper
import android.content.res.Configuration
import android.os.Build.VERSION.SDK_INT
import androidx.annotation.FloatRange
import androidx.compose.animation.core.Animatable
import androidx.compose.animation.core.AnimationSpec
import androidx.compose.animation.core.exponentialDecay
import androidx.compose.animation.core.spring
import androidx.compose.foundation.gestures.*
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.*
import androidx.compose.runtime.saveable.Saver
import androidx.compose.runtime.saveable.listSaver
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.*
import androidx.compose.ui.graphics.drawscope.DrawScope
import androidx.compose.ui.input.pointer.PointerInputChange
import androidx.compose.ui.input.pointer.PointerInputScope
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.input.pointer.positionChange
import androidx.compose.ui.input.pointer.util.VelocityTracker
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.layout.layout
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
import coil.ImageLoader
import coil.compose.AsyncImagePainter
import coil.compose.SubcomposeAsyncImage
import coil.compose.SubcomposeAsyncImageScope
import coil.decode.GifDecoder
import coil.decode.ImageDecoderDecoder
import coil.decode.SvgDecoder
import coil.imageLoader
import coil.request.ImageRequest
import coil.transform.Transformation
import com.google.accompanist.placeholder.PlaceholderHighlight
import com.google.accompanist.placeholder.material.shimmer
import com.google.accompanist.placeholder.placeholder
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
@Composable
fun Picture(
modifier: Modifier = Modifier,
model: Any?,
transformations: List<Transformation> = emptyList(),
manualImageRequest: ImageRequest? = null,
manualImageLoader: ImageLoader? = null,
contentDescription: String? = null,
shape: Shape = CircleShape,
contentScale: ContentScale = ContentScale.Crop,
loading: @Composable (SubcomposeAsyncImageScope.(AsyncImagePainter.State.Loading) -> Unit)? = null,
success: @Composable (SubcomposeAsyncImageScope.(AsyncImagePainter.State.Success) -> Unit)? = null,
error: @Composable (SubcomposeAsyncImageScope.(AsyncImagePainter.State.Error) -> Unit)? = null,
onLoading: ((AsyncImagePainter.State.Loading) -> Unit)? = null,
onSuccess: ((AsyncImagePainter.State.Success) -> Unit)? = null,
onError: ((AsyncImagePainter.State.Error) -> Unit)? = null,
onState: ((AsyncImagePainter.State) -> Unit)? = null,
alignment: Alignment = Alignment.Center,
alpha: Float = DefaultAlpha,
colorFilter: ColorFilter? = null,
filterQuality: FilterQuality = DrawScope.DefaultFilterQuality,
zoomParams: ZoomParams = ZoomParams(),
shimmerEnabled: Boolean = true,
crossfadeEnabled: Boolean = true,
allowHardware: Boolean = true,
) {
val activity = LocalContext.current.findActivity()
val context = LocalContext.current
var errorOccurred by rememberSaveable { mutableStateOf(false) }
var shimmerVisible by rememberSaveable { mutableStateOf(true) }
val imageLoader =
manualImageLoader ?: context.imageLoader.newBuilder().components {
if (SDK_INT >= 28) add(ImageDecoderDecoder.Factory())
else add(GifDecoder.Factory())
add(SvgDecoder.Factory())
}.build()
val request = manualImageRequest ?: ImageRequest.Builder(context)
.data(model)
.crossfade(crossfadeEnabled)
.allowHardware(allowHardware)
.transformations(transformations)
.build()
val image: @Composable () -> Unit = {
SubcomposeAsyncImage(
model = request,
imageLoader = imageLoader,
contentDescription = contentDescription,
modifier = modifier
.clip(shape)
.then(if (shimmerEnabled) Modifier.shimmer(shimmerVisible) else Modifier),
contentScale = contentScale,
loading = {
if (loading != null) loading(it)
shimmerVisible = true
},
success = success,
error = error,
onSuccess = {
shimmerVisible = false
onSuccess?.invoke(it)
onState?.invoke(it)
},
onLoading = {
onLoading?.invoke(it)
onState?.invoke(it)
},
onError = {
if (error != null) shimmerVisible = false
onError?.invoke(it)
onState?.invoke(it)
errorOccurred = true
},
alignment = alignment,
alpha = alpha,
colorFilter = colorFilter,
filterQuality = filterQuality
)
}
if (zoomParams.zoomEnabled) {
Zoomable(
state = rememberZoomableState(
minScale = zoomParams.minZoomScale,
maxScale = zoomParams.maxZoomScale
),
onTap = {
if (zoomParams.hideBarsOnTap) {
activity?.apply { if (isSystemBarsHidden) showSystemBars() else hideSystemBars() }
}
zoomParams.onTap(it)
},
content = { image() }
)
} else image()
//Needed for triggering recomposition
LaunchedEffect(errorOccurred) {
if (errorOccurred && error == null) {
shimmerVisible = false
shimmerVisible = true
errorOccurred = false
}
}
}
object StatusBarUtils {
val Activity.isSystemBarsHidden: Boolean
get() {
return _isSystemBarsHidden
}
private var _isSystemBarsHidden = false
fun Activity.hideSystemBars() = WindowInsetsControllerCompat(
window,
window.decorView
).let { controller ->
controller.systemBarsBehavior =
WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
controller.hide(WindowInsetsCompat.Type.systemBars())
_isSystemBarsHidden = true
}
fun Activity.showSystemBars() = WindowInsetsControllerCompat(
window,
window.decorView
).show(WindowInsetsCompat.Type.systemBars()).also {
_isSystemBarsHidden = false
}
}
data class ZoomParams(
val zoomEnabled: Boolean = false,
val hideBarsOnTap: Boolean = false,
val minZoomScale: Float = 1f,
val maxZoomScale: Float = 4f,
val onTap: (Offset) -> Unit = {}
)
@Composable
fun Zoomable(
state: ZoomableState,
modifier: Modifier = Modifier,
enable: Boolean = true,
onTap: (Offset) -> Unit = { },
content: @Composable BoxScope.() -> Unit,
) {
val scope = rememberCoroutineScope()
val conf = LocalConfiguration.current
val density = LocalDensity.current
BoxWithConstraints(
modifier = modifier,
) {
var childWidth by remember { mutableStateOf(0) }
var childHeight by remember { mutableStateOf(0) }
LaunchedEffect(
childHeight,
childWidth,
state.scale,
) {
val maxX = (childWidth * state.scale - constraints.maxWidth)
.coerceAtLeast(0F) / 2F
val maxY = (childHeight * state.scale - constraints.maxHeight)
.coerceAtLeast(0F) / 2F
state.updateBounds(maxX, maxY)
}
val transformableState = rememberTransformableState { zoomChange, _, _ ->
if (enable) scope.launch { state.onZoomChange(zoomChange) }
}
val doubleTapModifier = if (enable) {
Modifier.pointerInput(Unit) {
detectTapGestures(
onDoubleTap = {
scope.launch {
if (state.scale == state.maxScale) state.animateScaleTo(state.minScale)
else {
state.animateScaleTo(state.scale + (state.maxScale - state.minScale) / 2)
state.setTapOffset(it, conf, density)
}
}
},
onTap = onTap
)
}
} else Modifier
Box(
modifier = Modifier
.pointerInput(Unit) {
detectDrag(
onDrag = { change, dragAmount ->
if (state.zooming && enable) {
if (change.positionChange() != Offset.Zero) change.consume()
scope.launch {
state.drag(dragAmount)
state.addPosition(
change.uptimeMillis,
change.position
)
}
}
},
onDragCancel = {
if (enable) state.resetTracking()
},
onDragEnd = {
if (state.zooming && enable) {
scope.launch { state.dragEnd() }
}
},
)
}
.then(doubleTapModifier)
.transformable(state = transformableState)
.layout { measurable, constraints ->
val placeable =
measurable.measure(constraints = constraints)
childHeight = placeable.height
childWidth = placeable.width
layout(
width = constraints.maxWidth,
height = constraints.maxHeight
) {
placeable.placeRelativeWithLayer(
(constraints.maxWidth - placeable.width) / 2,
(constraints.maxHeight - placeable.height) / 2
) {
scaleX = state.scale
scaleY = state.scale
translationX = state.translateX
translationY = state.translateY
}
}
}
) {
content.invoke(this)
}
}
}
private suspend fun PointerInputScope.detectDrag(
onDragStart: (Offset) -> Unit = { },
onDragEnd: () -> Unit = { },
onDragCancel: () -> Unit = { },
onDrag: (change: PointerInputChange, dragAmount: Offset) -> Unit
) {
forEachGesture {
awaitPointerEventScope {
val down = awaitFirstDown(requireUnconsumed = false)
var drag: PointerInputChange?
do {
drag = awaitTouchSlopOrCancellation(down.id, onDrag)
} while (drag != null && !drag.isConsumed)
if (drag != null) {
onDragStart.invoke(drag.position)
if (!drag(drag.id) { onDrag(it, it.positionChange()) }) onDragCancel()
else onDragEnd()
}
}
}
}
@Composable
fun rememberZoomableState(
@FloatRange(from = 0.0) minScale: Float = 1f,
@FloatRange(from = 0.0) maxScale: Float = Float.MAX_VALUE,
): ZoomableState = rememberSaveable(
saver = ZoomableState.Saver
) {
ZoomableState(
minScale = minScale,
maxScale = maxScale,
)
}
/**
* A state object that can be hoisted to observe scale and translate for [Zoomable].
*
* In most cases, this will be created via [rememberZoomableState].
*
* @param minScale the minimum scale value for [ZoomableState.minScale]
* @param maxScale the maximum scale value for [ZoomableState.maxScale]
* @param initialTranslateX the initial translateX value for [ZoomableState.translateX]
* @param initialTranslateY the initial translateY value for [ZoomableState.translateY]
* @param initialScale the initial scale value for [ZoomableState.scale]
*/
@Stable
class ZoomableState(
@FloatRange(from = 0.0) val minScale: Float = 1f,
@FloatRange(from = 0.0) val maxScale: Float = Float.MAX_VALUE,
@FloatRange(from = 0.0) initialTranslateX: Float = 0f,
@FloatRange(from = 0.0) initialTranslateY: Float = 0f,
@FloatRange(from = 0.0) initialScale: Float = minScale,
) {
private val velocityTracker = VelocityTracker()
private val _translateY = Animatable(initialTranslateY)
private val _translateX = Animatable(initialTranslateX)
private val _scale = Animatable(initialScale)
init {
require(minScale < maxScale) { "minScale must be < maxScale" }
}
/**
* The current scale value for [Zoomable]
*/
@get:FloatRange(from = 0.0)
val scale: Float
get() = _scale.value
/**
* The current translateY value for [Zoomable]
*/
@get:FloatRange(from = 0.0)
val translateY: Float
get() = _translateY.value
/**
* The current translateX value for [Zoomable]
*/
@get:FloatRange(from = 0.0)
val translateX: Float
get() = _translateX.value
internal val zooming: Boolean
get() = scale > minScale
/**
* Instantly sets scale of [Zoomable] to given [scale]
*/
suspend fun snapScaleTo(scale: Float) = coroutineScope {
_scale.snapTo(scale.coerceIn(minimumValue = minScale, maximumValue = maxScale))
}
/**
* Animates scale of [Zoomable] to given [scale]
*/
suspend fun animateScaleTo(
scale: Float,
animationSpec: AnimationSpec<Float> = spring(),
initialVelocity: Float = 0f,
) = coroutineScope {
_scale.animateTo(
targetValue = scale.coerceIn(minimumValue = minScale, maximumValue = maxScale),
animationSpec = animationSpec,
initialVelocity = initialVelocity,
)
}
private suspend fun fling(velocity: Offset) = coroutineScope {
launch {
_translateY.animateDecay(
velocity.y / 2f,
exponentialDecay()
)
}
launch {
_translateX.animateDecay(
velocity.x / 2f,
exponentialDecay()
)
}
}
internal suspend fun drag(dragDistance: Offset) = coroutineScope {
launch {
_translateY.snapTo(_translateY.value + dragDistance.y)
}
launch {
_translateX.snapTo(_translateX.value + dragDistance.x)
}
}
internal suspend fun dragEnd() {
val velocity = velocityTracker.calculateVelocity()
fling(Offset(velocity.x, velocity.y))
}
internal suspend fun updateBounds(maxX: Float, maxY: Float) = coroutineScope {
_translateY.updateBounds(-maxY, maxY)
_translateX.updateBounds(-maxX, maxX)
}
internal suspend fun onZoomChange(zoomChange: Float) = snapScaleTo(scale * zoomChange)
internal fun addPosition(timeMillis: Long, position: Offset) {
velocityTracker.addPosition(timeMillis = timeMillis, position = position)
}
internal fun resetTracking() = velocityTracker.resetTracking()
override fun toString(): String = "ZoomableState(" +
"minScale=$minScale, " +
"maxScale=$maxScale, " +
"translateY=$translateY" +
"translateX=$translateX" +
"scale=$scale" +
")"
suspend fun setTapOffset(tapOffset: Offset, configuration: Configuration, density: Density) =
coroutineScope {
val targetOffset: Offset = calcTargetOffset(tapOffset, configuration, density)
launch {
_translateX.animateTo(_translateX.value + targetOffset.x)
}
launch {
_translateY.animateTo(_translateY.value + targetOffset.y)
}
}
private fun calcTargetOffset(
tapOffset: Offset,
configuration: Configuration,
density: Density
): Offset {
val width = configuration.screenWidthDp.dp.toPx(density)
val height = configuration.screenHeightDp.dp.toPx(density)
val halfWidth = width / 2
val halfHeight = height / 2
val x = halfWidth - tapOffset.x
val y = halfHeight - tapOffset.y
return Offset(x, y)
}
companion object {
/**
* The default [Saver] implementation for [ZoomableState].
*/
val Saver: Saver<ZoomableState, *> = listSaver(
save = {
listOf(
it.translateX,
it.translateY,
it.scale,
it.minScale,
it.maxScale,
)
},
restore = {
ZoomableState(
initialTranslateX = it[0],
initialTranslateY = it[1],
initialScale = it[2],
minScale = it[3],
maxScale = it[4],
)
}
)
}
}
private fun Dp.toPx(density: Density): Float {
return with(density) { toPx() }
}
fun Context.findActivity(): Activity? = when (this) {
is Activity -> this
is ContextWrapper -> baseContext.findActivity()
else -> null
}
@Composable
fun Modifier.shimmer(
visible: Boolean,
color: Color = MaterialTheme.colorScheme.surfaceVariant
) = then(
Modifier.placeholder(
visible = visible,
color = color,
highlight = PlaceholderHighlight.shimmer()
)
)