Skip to content

Commit

Permalink
Merge pull request #4657 from Isira-Seneviratne/getImage_reuse
Browse files Browse the repository at this point in the history
refactor: Reuse ImageHelper.getImage()
  • Loading branch information
Isira-Seneviratne authored Aug 29, 2023
2 parents c6871a0 + 1e4e2fa commit 9337e44
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 31 deletions.
33 changes: 11 additions & 22 deletions app/src/main/java/com/github/libretube/helpers/ImageHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.net.Uri
import android.widget.ImageView
import androidx.core.graphics.drawable.toBitmap
import androidx.core.graphics.drawable.toBitmapOrNull
import coil.ImageLoader
import coil.disk.DiskCache
import coil.load
import coil.request.CachePolicy
import coil.request.ImageRequest
import coil.request.ImageResult
import com.github.libretube.api.CronetHelper
import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.extensions.toAndroidUri
import com.github.libretube.extensions.toAndroidUriOrNull
import com.github.libretube.util.DataSaverMode
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.nio.file.Path

object ImageHelper {
Expand Down Expand Up @@ -60,39 +61,27 @@ object ImageHelper {
target.load(urlToLoad, imageLoader)
}

fun downloadImage(context: Context, url: String, path: Path) {
getAsync(context, url) { bitmap ->
saveImage(context, bitmap, path.toAndroidUri())
suspend fun downloadImage(context: Context, url: String, path: Path) {
val bitmap = getImage(context, url) ?: return
withContext(Dispatchers.IO) {
context.contentResolver.openOutputStream(path.toAndroidUri())?.use {
bitmap.compress(Bitmap.CompressFormat.PNG, 25, it)
}
}
}

fun getAsync(context: Context, url: String?, onSuccess: (Bitmap) -> Unit) {
val request = ImageRequest.Builder(context)
.data(url)
.target { onSuccess(it.toBitmap()) }
.build()

imageLoader.enqueue(request)
}

suspend fun getImage(context: Context, url: String?): ImageResult {
suspend fun getImage(context: Context, url: String?): Bitmap? {
val request = ImageRequest.Builder(context)
.data(url)
.build()

return imageLoader.execute(request)
return imageLoader.execute(request).drawable?.toBitmapOrNull()
}

fun getDownloadedImage(context: Context, path: Path): Bitmap? {
return path.toAndroidUriOrNull()?.let { getImage(context, it) }
}

private fun saveImage(context: Context, bitmapImage: Bitmap, imagePath: Uri) {
context.contentResolver.openOutputStream(imagePath)?.use {
bitmapImage.compress(Bitmap.CompressFormat.PNG, 25, it)
}
}

private fun getImage(context: Context, imagePath: Uri): Bitmap? {
return context.contentResolver.openInputStream(imagePath)?.use {
BitmapFactory.decodeStream(it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.commit
import androidx.lifecycle.lifecycleScope
import com.github.libretube.R
import com.github.libretube.api.obj.StreamItem
import com.github.libretube.constants.IntentData
Expand Down Expand Up @@ -49,6 +50,7 @@ import com.github.libretube.ui.sheets.PlayingQueueSheet
import com.github.libretube.ui.sheets.VideoOptionsBottomSheet
import com.github.libretube.util.DataSaverMode
import com.github.libretube.util.PlayingQueue
import kotlinx.coroutines.launch
import kotlin.math.abs

class AudioPlayerFragment : Fragment(), AudioPlayerOptions {
Expand Down Expand Up @@ -297,10 +299,11 @@ class AudioPlayerFragment : Fragment(), AudioPlayerOptions {
// reset color filter if data saver mode got toggled or conditions for it changed
binding.thumbnail.setColorFilter(Color.TRANSPARENT)

ImageHelper.getAsync(requireContext(), thumbnailUrl) {
val binding = _binding ?: return@getAsync
binding.thumbnail.setImageBitmap(it)
binding.miniPlayerThumbnail.setImageBitmap(it)
lifecycleScope.launch {
val binding = _binding ?: return@launch
val bitmap = ImageHelper.getImage(requireContext(), thumbnailUrl)
binding.thumbnail.setImageBitmap(bitmap)
binding.miniPlayerThumbnail.setImageBitmap(bitmap)
binding.thumbnail.isVisible = true
binding.progress.isGone = true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.github.libretube.util

import android.content.Context
import android.graphics.Bitmap
import androidx.core.graphics.drawable.toBitmap
import com.github.libretube.api.obj.PreviewFrames
import com.github.libretube.helpers.ImageHelper
import com.github.libretube.obj.PreviewFrame
Expand All @@ -14,8 +13,8 @@ class OnlineTimeFrameReceiver(
) : TimeFrameReceiver() {
override suspend fun getFrameAtTime(position: Long): Bitmap? {
val previewFrame = getPreviewFrame(previewFrames, position) ?: return null
val drawable = ImageHelper.getImage(context, previewFrame.previewUrl).drawable ?: return null
return cutBitmapFromPreviewFrame(drawable.toBitmap(), previewFrame)
val bitmap = ImageHelper.getImage(context, previewFrame.previewUrl) ?: return null
return cutBitmapFromPreviewFrame(bitmap, previewFrame)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import android.util.Log
import androidx.core.app.NotificationCompat
import androidx.core.app.PendingIntentCompat
import androidx.core.content.getSystemService
import androidx.core.graphics.drawable.toBitmap
import androidx.work.CoroutineWorker
import androidx.work.WorkerParameters
import com.github.libretube.R
Expand Down Expand Up @@ -199,7 +198,7 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) :

private suspend fun downloadImage(url: String?): Bitmap? {
return if (PreferenceHelper.getBoolean(PreferenceKeys.SHOW_STREAM_THUMBNAILS, false)) {
ImageHelper.getImage(applicationContext, url).drawable?.toBitmap()
ImageHelper.getImage(applicationContext, url)
} else {
null
}
Expand Down

0 comments on commit 9337e44

Please sign in to comment.