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

116 lines
3.6 KiB
Kotlin
Raw Normal View History

package com.github.libretube.helpers
2022-08-15 14:17:31 +05:30
import android.content.Context
2022-10-15 19:59:12 +05:30
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.net.Uri
2022-08-15 14:17:31 +05:30
import android.widget.ImageView
2023-03-01 05:02:51 +05:30
import androidx.core.graphics.drawable.toBitmap
2022-08-15 14:17:31 +05:30
import coil.ImageLoader
import coil.disk.DiskCache
import coil.load
2022-11-17 20:29:12 +05:30
import coil.request.CachePolicy
2022-10-15 19:59:12 +05:30
import coil.request.ImageRequest
import coil.request.ImageResult
2022-08-15 14:17:31 +05:30
import com.github.libretube.api.CronetHelper
2022-09-08 21:59:00 +05:30
import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.extensions.toAndroidUri
2023-03-17 05:47:45 +05:30
import com.github.libretube.extensions.toAndroidUriOrNull
import com.github.libretube.util.DataSaverMode
import java.nio.file.Path
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-11-17 20:29:12 +05:30
""
)
2022-08-15 14:17:31 +05:30
imageLoader = ImageLoader.Builder(context)
.callFactory(CronetHelper.callFactory)
2022-11-17 20:29:12 +05:30
.apply {
when (maxImageCacheSize) {
"" -> {
diskCachePolicy(CachePolicy.DISABLED)
2022-11-17 20:29:12 +05:30
}
else -> diskCache(
DiskCache.Builder()
2023-01-09 20:19:09 +05:30
.directory(context.cacheDir.resolve("coil"))
2022-11-17 20:29:12 +05:30
.maxSizeBytes(maxImageCacheSize.toInt() * 1024 * 1024L)
.build()
)
}
}
2022-08-15 14:17:31 +05:30
.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
2023-03-11 20:41:43 +05:30
if (DataSaverMode.isEnabled(target.context) || url == null) return
val urlToLoad = ProxyHelper.unwrapIfEnabled(url)
target.load(urlToLoad, imageLoader)
2022-08-15 14:17:31 +05:30
}
2022-10-15 19:59:12 +05:30
fun downloadImage(context: Context, url: String, path: Path) {
getAsync(context, url) { bitmap ->
2023-03-17 05:47:45 +05:30
saveImage(context, bitmap, path.toAndroidUri())
}
}
fun getAsync(context: Context, url: String?, onSuccess: (Bitmap) -> Unit) {
2022-10-15 19:59:12 +05:30
val request = ImageRequest.Builder(context)
.data(url)
2023-03-01 05:02:51 +05:30
.target { onSuccess(it.toBitmap()) }
2022-10-15 19:59:12 +05:30
.build()
imageLoader.enqueue(request)
}
suspend fun getImage(context: Context, url: String?): ImageResult {
val request = ImageRequest.Builder(context)
.data(url)
.build()
return imageLoader.execute(request)
}
fun getDownloadedImage(context: Context, path: Path): Bitmap? {
2023-03-17 05:47:45 +05:30
return path.toAndroidUriOrNull()?.let { getImage(context, it) }
2022-10-15 19:59:12 +05:30
}
2022-10-19 23:23:18 +05:30
2022-10-15 19:59:12 +05:30
private fun saveImage(context: Context, bitmapImage: Bitmap, imagePath: Uri) {
2023-03-05 14:17:53 +05:30
context.contentResolver.openOutputStream(imagePath)?.use {
bitmapImage.compress(Bitmap.CompressFormat.PNG, 25, it)
2022-10-15 19:59:12 +05:30
}
}
private fun getImage(context: Context, imagePath: Uri): Bitmap? {
2023-03-05 14:17:53 +05:30
return context.contentResolver.openInputStream(imagePath)?.use {
BitmapFactory.decodeStream(it)
2022-10-15 19:59:12 +05:30
}
}
/**
* Get a squared bitmap with the same width and height from a bitmap
* @param bitmap The bitmap to resize
*/
2023-04-10 19:24:09 +05:30
fun getSquareBitmap(bitmap: Bitmap): Bitmap {
val newSize = minOf(bitmap.width, bitmap.height)
return Bitmap.createBitmap(
bitmap,
(bitmap.width - newSize) / 2,
(bitmap.height - newSize) / 2,
newSize,
newSize
)
}
2022-08-15 14:17:31 +05:30
}