-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathCoilImage.kt
135 lines (116 loc) · 4.57 KB
/
CoilImage.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
package com.himanshoe.core.common
import android.graphics.drawable.Animatable
import android.graphics.drawable.Drawable
import androidx.annotation.Px
import androidx.compose.foundation.Image
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.ImageAsset
import androidx.compose.ui.graphics.asImageAsset
import androidx.compose.ui.layout.WithConstraints
import androidx.compose.ui.platform.ContextAmbient
import androidx.compose.ui.unit.Dp.Companion.Infinity
import androidx.compose.ui.unit.IntSize.Companion.Zero
import androidx.core.graphics.drawable.toBitmap
import androidx.ui.tooling.preview.Preview
import coil.Coil
import coil.request.ImageRequest
import coil.size.Scale
import coil.target.Target
import kotlinx.coroutines.*
@Composable
fun CoilImage(
model: Any,
modifier : Modifier = Modifier,
customize: ImageRequest.Builder.() -> Unit = {}
) {
WithConstraints(modifier) {
var width =
if (constraints.maxWidth > Zero.width && constraints.maxWidth < Infinity.value.toInt()) {
constraints.maxWidth
} else {
-1
}
var height =
if (constraints.maxHeight > Zero.height && constraints.maxHeight < Infinity.value.toInt()) {
constraints.maxHeight
} else {
-1
}
//if height xor width not able to be determined, make image a square of the determined dimension
if (width == -1) width = height
if (height == -1) height = width
val image = remember { mutableStateOf(ImageAsset(width,height)) }
val context = ContextAmbient.current
var animationJob : Job? = remember { null }
onCommit(model) {
val target = object : Target {
override fun onStart(placeholder: Drawable?) {
placeholder?.apply {
animationJob?.cancel()
if(height != -1 && width != -1) {
animationJob = image.update(this, width, height)
} else if (height == -1) {
val scaledHeight = intrinsicHeight * (width / intrinsicWidth )
animationJob = image.update(this, width, scaledHeight)
} else if (width == -1) {
val scaledWidth = intrinsicWidth * (height / intrinsicHeight)
animationJob = image.update(this, scaledWidth, height)
}
}
}
override fun onSuccess(result: Drawable) {
animationJob?.cancel()
animationJob = image.update(result)
}
override fun onError(error: Drawable?) {
error?.run {
animationJob?.cancel()
animationJob = image.update(error)
}
}
}
val request = ImageRequest.Builder(context)
.data(model)
.size(width, height)
.scale(Scale.FILL)
.apply{customize(this)}
.target(target)
val requestDisposable = Coil.imageLoader(context).enqueue(request.build())
onDispose {
image.value = ImageAsset(width,height)
requestDisposable.dispose()
animationJob?.cancel()
}
}
Image(modifier = modifier, asset = image.value)
}
}
internal fun MutableState<ImageAsset>.update(drawable: Drawable, @Px width: Int? = null, @Px height: Int? = null) : Job? {
if (drawable is Animatable) {
(drawable as Animatable).start()
return GlobalScope.launch(Dispatchers.Default) {
while (true) {
val asset = drawable.toBitmap(
width = width ?: drawable.intrinsicWidth,
height = height ?: drawable.intrinsicHeight)
.asImageAsset()
withContext(Dispatchers.Main) {
value = asset
}
delay(16)
}
}
} else {
value = drawable.toBitmap(
width = width ?: drawable.intrinsicWidth,
height = height ?: drawable.intrinsicHeight)
.asImageAsset()
return null
}
}
@Preview
@Composable
fun ProfilePreview() {
CoilImage("https://images.unsplash.com/photo-1601758064224-c3c5ec910095?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1147&q=80")
}