Skip to content

Commit 7186d69

Browse files
authored
Merge pull request #7396 from Bnyro/master
feat: load cached and downloaded images even if data saver mode disabled
2 parents 2566743 + 2061435 commit 7186d69

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

app/src/main/java/com/github/libretube/helpers/ImageHelper.kt

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ object ImageHelper {
3131
private lateinit var imageLoader: ImageLoader
3232

3333
private val Context.coilFile get() = cacheDir.resolve("coil")
34+
private const val HTTP_SCHEME = "http"
3435

3536
/**
3637
* Initialize the image loader
@@ -79,17 +80,33 @@ object ImageHelper {
7980
.build()
8081
}
8182

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+
8294
/**
8395
* load an image from a url into an imageView
8496
*/
8597
fun loadImage(url: String?, target: ImageView, whiteBackground: Boolean = false) {
98+
if (url.isNullOrEmpty()) return
99+
86100
// clear image to avoid loading issues at fast scrolling
87101
target.setImageBitmap(null)
88102

89-
// only load the image if the data saver mode is disabled
90-
if (DataSaverMode.isEnabled(target.context) || url.isNullOrEmpty()) return
91103
val urlToLoad = ProxyHelper.rewriteUrlUsingProxyPreference(url)
92104

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+
93110
getImageWithCallback(target.context, urlToLoad) { result ->
94111
// set the background to white for transparent images
95112
if (whiteBackground) target.setBackgroundColor(Color.WHITE)

0 commit comments

Comments
 (0)