Added Coil helper method

This commit is contained in:
Isira Seneviratne 2024-06-22 18:32:03 +05:30
parent 4d3b4a7b20
commit f74402bc94
4 changed files with 17 additions and 27 deletions

View File

@ -14,16 +14,12 @@ import androidx.core.app.NotificationManagerCompat
import androidx.core.app.PendingIntentCompat
import androidx.core.content.ContextCompat
import androidx.core.content.getSystemService
import androidx.core.graphics.drawable.toBitmapOrNull
import androidx.preference.PreferenceManager
import coil.executeBlocking
import coil.imageLoader
import coil.request.ImageRequest
import org.schabi.newpipe.R
import org.schabi.newpipe.extractor.stream.StreamInfoItem
import org.schabi.newpipe.local.feed.service.FeedUpdateInfo
import org.schabi.newpipe.util.NavigationHelper
import org.schabi.newpipe.util.image.ImageStrategy
import org.schabi.newpipe.util.image.CoilHelper
/**
* Helper for everything related to show notifications about new streams to the user.
@ -68,24 +64,15 @@ class NotificationHelper(val context: Context) {
summaryBuilder.setStyle(style)
// open the channel page when clicking on the summary notification
val intent = NavigationHelper
.getChannelIntent(context, data.serviceId, data.url)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
summaryBuilder.setContentIntent(
PendingIntentCompat.getActivity(
context,
data.pseudoId,
NavigationHelper
.getChannelIntent(context, data.serviceId, data.url)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK),
0,
false
)
PendingIntentCompat.getActivity(context, data.pseudoId, intent, 0, false)
)
val request = ImageRequest.Builder(context)
.data(data.avatarUrl?.takeIf { ImageStrategy.shouldLoadImages() })
.placeholder(R.drawable.ic_newpipe_triangle_white)
.error(R.drawable.ic_newpipe_triangle_white)
.build()
val avatarIcon = context.imageLoader.executeBlocking(request).drawable?.toBitmapOrNull()
val avatarIcon =
CoilHelper.loadBitmapBlocking(context, data.avatarUrl, R.drawable.ic_newpipe_triangle_white)
summaryBuilder.setLargeIcon(avatarIcon)

View File

@ -179,7 +179,7 @@ public class SeekbarPreviewThumbnailHolder {
// Gets the bitmap within the timeout of 15 seconds imposed by default by OkHttpClient
// Ensure that you are not running on the main thread, otherwise this will hang
final var bitmap = CoilHelper.INSTANCE.loadBitmap(App.getApp(), url);
final var bitmap = CoilHelper.INSTANCE.loadBitmapBlocking(App.getApp(), url);
if (sw != null) {
Log.d(TAG, "Download of bitmap for seekbarPreview from '" + url + "' took "

View File

@ -369,7 +369,7 @@ public final class ShareUtils {
@NonNull final Context context,
@NonNull final String thumbnailUrl) {
try {
final var bitmap = CoilHelper.INSTANCE.loadBitmap(context, thumbnailUrl);
final var bitmap = CoilHelper.INSTANCE.loadBitmapBlocking(context, thumbnailUrl);
if (bitmap == null) {
return null;
}

View File

@ -19,12 +19,15 @@ import org.schabi.newpipe.ktx.scale
import kotlin.math.min
object CoilHelper {
private const val TAG = "CoilHelper"
private val TAG = CoilHelper::class.java.simpleName
fun loadBitmap(context: Context, url: String): Bitmap? {
val request = ImageRequest.Builder(context)
.data(url)
.build()
@JvmOverloads
fun loadBitmapBlocking(
context: Context,
url: String?,
placeholderResId: Int = 0
): Bitmap? {
val request = getImageRequest(context, url, placeholderResId).build()
return context.imageLoader.executeBlocking(request).drawable?.toBitmapOrNull()
}