/
Extensions.kt
112 lines (103 loc) · 2.97 KB
/
Extensions.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
@file:JvmName("-SingletonExtensions")
@file:Suppress("NOTHING_TO_INLINE")
package coil
import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.net.Uri
import android.widget.ImageView
import androidx.annotation.DrawableRes
import coil.request.Disposable
import coil.request.ImageRequest
import coil.request.ImageResult
import coil.util.CoilUtils
import okhttp3.HttpUrl
import java.io.File
import java.nio.ByteBuffer
/**
* Get the singleton [ImageLoader].
*/
inline val Context.imageLoader: ImageLoader
get() = Coil.imageLoader(this)
/**
* Load the image referenced by [data] and set it on this [ImageView].
*
* Example:
* ```
* imageView.load("https://www.example.com/image.jpg") {
* crossfade(true)
* transformations(CircleCropTransformation())
* }
* ```
*
* The default supported [data] types are:
*
* - [String] (treated as a [Uri])
* - [Uri] (`android.resource`, `content`, `file`, `http`, and `https` schemes)
* - [HttpUrl]
* - [File]
* - [DrawableRes] [Int]
* - [Drawable]
* - [Bitmap]
* - [ByteBuffer]
*
* @param data The data to load.
* @param imageLoader The [ImageLoader] that will be used to enqueue the [ImageRequest].
* By default, the singleton [ImageLoader] will be used.
* @param builder An optional lambda to configure the [ImageRequest].
*/
inline fun ImageView.load(
data: Any?,
imageLoader: ImageLoader = context.imageLoader,
builder: ImageRequest.Builder.() -> Unit = {}
): Disposable {
val request = ImageRequest.Builder(context)
.data(data)
.target(this)
.apply(builder)
.build()
return imageLoader.enqueue(request)
}
/**
* Dispose the request that's attached to this view (if there is one).
*/
inline fun ImageView.dispose() {
CoilUtils.dispose(this)
}
/**
* Get the [ImageResult] of the most recently executed image request that's attached to this view.
*/
inline val ImageView.result: ImageResult?
get() = CoilUtils.result(this)
@Deprecated(
message = "Migrate to 'load'.",
replaceWith = ReplaceWith(
expression = "load(data, imageLoader, builder)",
imports = ["coil.imageLoader", "coil.load"]
),
level = DeprecationLevel.ERROR // Temporary migration aid.
)
inline fun ImageView.loadAny(
data: Any?,
imageLoader: ImageLoader = context.imageLoader,
builder: ImageRequest.Builder.() -> Unit = {}
) = load(data, imageLoader, builder)
@Deprecated(
message = "Migrate to 'dispose'.",
replaceWith = ReplaceWith(
expression = "dispose()",
imports = ["coil.dispose"]
),
level = DeprecationLevel.ERROR // Temporary migration aid.
)
inline fun ImageView.clear() = dispose()
@Deprecated(
message = "Migrate to 'result'.",
replaceWith = ReplaceWith(
expression = "result",
imports = ["coil.result"]
),
level = DeprecationLevel.ERROR // Temporary migration aid.
)
inline val ImageView.metadata: ImageResult?
get() = CoilUtils.result(this)