LibreTube/app/src/main/java/com/github/libretube/util/ImageHelper.kt

46 lines
1.3 KiB
Kotlin
Raw Normal View History

2022-08-15 14:17:31 +05:30
package com.github.libretube.util
import android.content.Context
import android.widget.ImageView
import coil.ImageLoader
import coil.disk.DiskCache
import coil.load
import com.github.libretube.api.CronetHelper
2022-09-08 21:59:00 +05:30
import com.github.libretube.constants.PreferenceKeys
2022-08-15 14:17:31 +05:30
object ImageHelper {
lateinit var imageLoader: ImageLoader
/**
* Initialize the image loader
*/
fun initializeImageLoader(context: Context) {
2022-08-25 14:59:12 +05:30
val maxImageCacheSize = PreferenceHelper.getString(
2022-08-15 14:17:31 +05:30
PreferenceKeys.MAX_IMAGE_CACHE,
2022-08-25 14:59:12 +05:30
"128"
).toInt()
2022-08-15 14:17:31 +05:30
val diskCache = DiskCache.Builder()
.directory(context.filesDir.resolve("coil"))
.maxSizeBytes(maxImageCacheSize * 1024 * 1024L)
.build()
imageLoader = ImageLoader.Builder(context)
.callFactory(CronetHelper.callFactory)
.diskCache(diskCache)
.build()
}
/**
* load an image from a url into an imageView
*/
fun loadImage(url: String?, target: ImageView) {
// only load the image if the data saver mode is disabled
val dataSaverModeEnabled = PreferenceHelper.getBoolean(
PreferenceKeys.DATA_SAVER_MODE,
false
)
if (!dataSaverModeEnabled) target.load(url, imageLoader)
}
}