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

111 lines
3.4 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
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.util.DataSaverMode
2022-10-15 19:59:12 +05:30
import java.io.File
import java.io.FileOutputStream
2022-12-19 21:28:34 +05:30
import okio.use
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
if (!DataSaverMode.isEnabled(target.context)) target.load(url, imageLoader)
2022-08-15 14:17:31 +05:30
}
2022-10-15 19:59:12 +05:30
fun downloadImage(context: Context, url: String, path: String) {
getAsync(context, url) { bitmap ->
saveImage(context, bitmap, Uri.fromFile(File(path)))
}
}
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)
}
fun getDownloadedImage(context: Context, path: String): Bitmap? {
val file = File(path)
2022-11-11 23:13:01 +05:30
if (!file.exists()) return null
2022-11-11 23:09:56 +05:30
return getImage(context, Uri.fromFile(file))
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) {
context.contentResolver.openFileDescriptor(imagePath, "w")?.use {
FileOutputStream(it.fileDescriptor).use { fos ->
bitmapImage.compress(Bitmap.CompressFormat.PNG, 25, fos)
}
}
}
private fun getImage(context: Context, imagePath: Uri): Bitmap? {
context.contentResolver.openInputStream(imagePath)?.use {
return BitmapFactory.decodeStream(it)
}
return null
}
/**
* Get a squared bitmap with the same width and height from a bitmap
* @param bitmap The bitmap to resize
*/
fun getSquareBitmap(bitmap: Bitmap?): Bitmap? {
bitmap ?: return null
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
}