Skip to content

Commit 92a7f22

Browse files
Load notification icons using Coil
1 parent 03167a1 commit 92a7f22

2 files changed

Lines changed: 18 additions & 36 deletions

File tree

app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ dependencies {
269269
// Image loading
270270
//noinspection GradleDependency --> 2.8 is the last version, not 2.71828!
271271
implementation "com.squareup.picasso:picasso:2.8"
272+
implementation 'io.coil-kt:coil:2.6.0'
272273

273274
// Markdown library for Android
274275
implementation "io.noties.markwon:core:${markwonVersion}"

app/src/main/java/org/schabi/newpipe/local/feed/notifications/NotificationHelper.kt

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import android.app.PendingIntent
66
import android.content.Context
77
import android.content.Intent
88
import android.graphics.Bitmap
9-
import android.graphics.drawable.Drawable
109
import android.net.Uri
1110
import android.os.Build
1211
import android.provider.Settings
@@ -15,21 +14,22 @@ import androidx.core.app.NotificationManagerCompat
1514
import androidx.core.app.PendingIntentCompat
1615
import androidx.core.content.ContextCompat
1716
import androidx.core.content.getSystemService
17+
import androidx.core.graphics.drawable.toBitmapOrNull
1818
import androidx.preference.PreferenceManager
19-
import com.squareup.picasso.Picasso
20-
import com.squareup.picasso.Target
19+
import coil.executeBlocking
20+
import coil.imageLoader
21+
import coil.request.ImageRequest
2122
import org.schabi.newpipe.R
2223
import org.schabi.newpipe.extractor.stream.StreamInfoItem
2324
import org.schabi.newpipe.local.feed.service.FeedUpdateInfo
2425
import org.schabi.newpipe.util.NavigationHelper
25-
import org.schabi.newpipe.util.image.PicassoHelper
26+
import org.schabi.newpipe.util.image.ImageStrategy
2627

2728
/**
2829
* Helper for everything related to show notifications about new streams to the user.
2930
*/
3031
class NotificationHelper(val context: Context) {
3132
private val manager = NotificationManagerCompat.from(context)
32-
private val iconLoadingTargets = ArrayList<Target>()
3333

3434
/**
3535
* Show notifications for new streams from a single channel. The individual notifications are
@@ -80,39 +80,20 @@ class NotificationHelper(val context: Context) {
8080
)
8181
)
8282

83-
// a Target is like a listener for image loading events
84-
val target = object : Target {
85-
override fun onBitmapLoaded(bitmap: Bitmap, from: Picasso.LoadedFrom) {
86-
// set channel icon only if there is actually one (for Android versions < 7.0)
87-
summaryBuilder.setLargeIcon(bitmap)
88-
89-
// Show individual stream notifications, set channel icon only if there is actually
90-
// one
91-
showStreamNotifications(newStreams, data.serviceId, bitmap)
92-
// Show summary notification
93-
manager.notify(data.pseudoId, summaryBuilder.build())
94-
95-
iconLoadingTargets.remove(this) // allow it to be garbage-collected
96-
}
97-
98-
override fun onBitmapFailed(e: Exception, errorDrawable: Drawable) {
99-
// Show individual stream notifications
100-
showStreamNotifications(newStreams, data.serviceId, null)
101-
// Show summary notification
102-
manager.notify(data.pseudoId, summaryBuilder.build())
103-
iconLoadingTargets.remove(this) // allow it to be garbage-collected
104-
}
105-
106-
override fun onPrepareLoad(placeHolderDrawable: Drawable) {
107-
// Nothing to do
108-
}
109-
}
83+
val request = ImageRequest.Builder(context)
84+
.data(data.avatarUrl?.takeIf { ImageStrategy.shouldLoadImages() })
85+
.placeholder(R.drawable.ic_newpipe_triangle_white)
86+
.error(R.drawable.ic_newpipe_triangle_white)
87+
.build()
88+
val avatarIcon = context.imageLoader.executeBlocking(request).drawable?.toBitmapOrNull()
11089

111-
// add the target to the list to hold a strong reference and prevent it from being garbage
112-
// collected, since Picasso only holds weak references to targets
113-
iconLoadingTargets.add(target)
90+
summaryBuilder.setLargeIcon(avatarIcon)
11491

115-
PicassoHelper.loadNotificationIcon(data.avatarUrl).into(target)
92+
// Show individual stream notifications, set channel icon only if there is actually
93+
// one
94+
showStreamNotifications(newStreams, data.serviceId, avatarIcon)
95+
// Show summary notification
96+
manager.notify(data.pseudoId, summaryBuilder.build())
11697
}
11798

11899
private fun showStreamNotifications(

0 commit comments

Comments
 (0)