Skip to content

Commit

Permalink
Added Coil helper method
Browse files Browse the repository at this point in the history
  • Loading branch information
Isira-Seneviratne committed Jun 26, 2024
1 parent fce8a78 commit 996c2d7
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ private Bitmap getBitMapFrom(final String url) {

// 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 "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ private static ClipData generateClipDataForImagePreview(
@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;
}
Expand Down
13 changes: 8 additions & 5 deletions app/src/main/java/org/schabi/newpipe/util/image/CoilHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down

0 comments on commit 996c2d7

Please sign in to comment.