@@ -31,6 +31,7 @@ object ImageHelper {
31
31
private lateinit var imageLoader: ImageLoader
32
32
33
33
private val Context .coilFile get() = cacheDir.resolve(" coil" )
34
+ private const val HTTP_SCHEME = " http"
34
35
35
36
/* *
36
37
* Initialize the image loader
@@ -79,17 +80,33 @@ object ImageHelper {
79
80
.build()
80
81
}
81
82
83
+ /* *
84
+ * Checks if the corresponding image for the given key (e.g. a url) is cached.
85
+ */
86
+ private fun isCached (key : String ): Boolean {
87
+ val cacheSnapshot = imageLoader.diskCache?.openSnapshot(key)
88
+ val isCacheHit = cacheSnapshot?.data?.toFile()?.exists()
89
+ cacheSnapshot?.close()
90
+
91
+ return isCacheHit ? : false
92
+ }
93
+
82
94
/* *
83
95
* load an image from a url into an imageView
84
96
*/
85
97
fun loadImage (url : String? , target : ImageView , whiteBackground : Boolean = false) {
98
+ if (url.isNullOrEmpty()) return
99
+
86
100
// clear image to avoid loading issues at fast scrolling
87
101
target.setImageBitmap(null )
88
102
89
- // only load the image if the data saver mode is disabled
90
- if (DataSaverMode .isEnabled(target.context) || url.isNullOrEmpty()) return
91
103
val urlToLoad = ProxyHelper .rewriteUrlUsingProxyPreference(url)
92
104
105
+ // only load online images if the data saver mode is disabled
106
+ if (DataSaverMode .isEnabled(target.context)) {
107
+ if (urlToLoad.startsWith(HTTP_SCHEME ) && ! isCached(urlToLoad)) return
108
+ }
109
+
93
110
getImageWithCallback(target.context, urlToLoad) { result ->
94
111
// set the background to white for transparent images
95
112
if (whiteBackground) target.setBackgroundColor(Color .WHITE )
0 commit comments