1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2025-07-16 17:02:57 +00:00

Load notification icons using Coil

This commit is contained in:
Isira Seneviratne 2024-06-19 10:19:02 +05:30
parent 03167a1e9c
commit 92a7f22d3c
2 changed files with 18 additions and 36 deletions

View File

@ -269,6 +269,7 @@ dependencies {
// Image loading // Image loading
//noinspection GradleDependency --> 2.8 is the last version, not 2.71828! //noinspection GradleDependency --> 2.8 is the last version, not 2.71828!
implementation "com.squareup.picasso:picasso:2.8" implementation "com.squareup.picasso:picasso:2.8"
implementation 'io.coil-kt:coil:2.6.0'
// Markdown library for Android // Markdown library for Android
implementation "io.noties.markwon:core:${markwonVersion}" implementation "io.noties.markwon:core:${markwonVersion}"

View File

@ -6,7 +6,6 @@ import android.app.PendingIntent
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.graphics.Bitmap import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.net.Uri import android.net.Uri
import android.os.Build import android.os.Build
import android.provider.Settings import android.provider.Settings
@ -15,21 +14,22 @@ import androidx.core.app.NotificationManagerCompat
import androidx.core.app.PendingIntentCompat import androidx.core.app.PendingIntentCompat
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.core.content.getSystemService import androidx.core.content.getSystemService
import androidx.core.graphics.drawable.toBitmapOrNull
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import com.squareup.picasso.Picasso import coil.executeBlocking
import com.squareup.picasso.Target import coil.imageLoader
import coil.request.ImageRequest
import org.schabi.newpipe.R import org.schabi.newpipe.R
import org.schabi.newpipe.extractor.stream.StreamInfoItem import org.schabi.newpipe.extractor.stream.StreamInfoItem
import org.schabi.newpipe.local.feed.service.FeedUpdateInfo import org.schabi.newpipe.local.feed.service.FeedUpdateInfo
import org.schabi.newpipe.util.NavigationHelper import org.schabi.newpipe.util.NavigationHelper
import org.schabi.newpipe.util.image.PicassoHelper import org.schabi.newpipe.util.image.ImageStrategy
/** /**
* Helper for everything related to show notifications about new streams to the user. * Helper for everything related to show notifications about new streams to the user.
*/ */
class NotificationHelper(val context: Context) { class NotificationHelper(val context: Context) {
private val manager = NotificationManagerCompat.from(context) private val manager = NotificationManagerCompat.from(context)
private val iconLoadingTargets = ArrayList<Target>()
/** /**
* Show notifications for new streams from a single channel. The individual notifications are * Show notifications for new streams from a single channel. The individual notifications are
@ -80,39 +80,20 @@ class NotificationHelper(val context: Context) {
) )
) )
// a Target is like a listener for image loading events val request = ImageRequest.Builder(context)
val target = object : Target { .data(data.avatarUrl?.takeIf { ImageStrategy.shouldLoadImages() })
override fun onBitmapLoaded(bitmap: Bitmap, from: Picasso.LoadedFrom) { .placeholder(R.drawable.ic_newpipe_triangle_white)
// set channel icon only if there is actually one (for Android versions < 7.0) .error(R.drawable.ic_newpipe_triangle_white)
summaryBuilder.setLargeIcon(bitmap) .build()
val avatarIcon = context.imageLoader.executeBlocking(request).drawable?.toBitmapOrNull()
// Show individual stream notifications, set channel icon only if there is actually summaryBuilder.setLargeIcon(avatarIcon)
// one
showStreamNotifications(newStreams, data.serviceId, bitmap)
// Show summary notification
manager.notify(data.pseudoId, summaryBuilder.build())
iconLoadingTargets.remove(this) // allow it to be garbage-collected // Show individual stream notifications, set channel icon only if there is actually
} // one
showStreamNotifications(newStreams, data.serviceId, avatarIcon)
override fun onBitmapFailed(e: Exception, errorDrawable: Drawable) { // Show summary notification
// Show individual stream notifications manager.notify(data.pseudoId, summaryBuilder.build())
showStreamNotifications(newStreams, data.serviceId, null)
// Show summary notification
manager.notify(data.pseudoId, summaryBuilder.build())
iconLoadingTargets.remove(this) // allow it to be garbage-collected
}
override fun onPrepareLoad(placeHolderDrawable: Drawable) {
// Nothing to do
}
}
// add the target to the list to hold a strong reference and prevent it from being garbage
// collected, since Picasso only holds weak references to targets
iconLoadingTargets.add(target)
PicassoHelper.loadNotificationIcon(data.avatarUrl).into(target)
} }
private fun showStreamNotifications( private fun showStreamNotifications(