mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-29 08:20:32 +05:30
Improve notification grouping for new streams.
This commit is contained in:
parent
0cce8e4c9b
commit
7bea6fdebd
@ -384,7 +384,7 @@ class MainActivity : BaseActivity() {
|
||||
intent?.getStringExtra(IntentData.channelId)?.let {
|
||||
navController.navigate(
|
||||
R.id.channelFragment,
|
||||
bundleOf(IntentData.channelName to it)
|
||||
bundleOf(IntentData.channelId to it)
|
||||
)
|
||||
}
|
||||
intent?.getStringExtra(IntentData.channelName)?.let {
|
||||
|
@ -13,6 +13,7 @@ import androidx.work.CoroutineWorker
|
||||
import androidx.work.WorkerParameters
|
||||
import com.github.libretube.R
|
||||
import com.github.libretube.api.SubscriptionHelper
|
||||
import com.github.libretube.api.obj.StreamItem
|
||||
import com.github.libretube.constants.DOWNLOAD_PROGRESS_NOTIFICATION_ID
|
||||
import com.github.libretube.constants.IntentData
|
||||
import com.github.libretube.constants.PUSH_CHANNEL_ID
|
||||
@ -118,22 +119,8 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) :
|
||||
Log.d(TAG(), "Create notifications for new videos")
|
||||
|
||||
// create a notification for each new stream
|
||||
channelGroups.forEach { (_, streams) ->
|
||||
createNotification(
|
||||
group = streams.first().uploaderUrl!!.toID(),
|
||||
title = streams.first().uploaderName.toString(),
|
||||
urlPath = streams.first().uploaderUrl!!,
|
||||
isGroupSummary = true
|
||||
)
|
||||
|
||||
streams.forEach { streamItem ->
|
||||
createNotification(
|
||||
title = streamItem.title.toString(),
|
||||
description = streamItem.uploaderName.toString(),
|
||||
urlPath = streamItem.url!!,
|
||||
group = streamItem.uploaderUrl!!.toID()
|
||||
)
|
||||
}
|
||||
channelGroups.forEach { (uploaderUrl, streams) ->
|
||||
createNotificationsForChannel(uploaderUrl!!, streams)
|
||||
}
|
||||
// save the latest streams that got notified about
|
||||
PreferenceHelper.setLatestVideoId(videoFeed.first().url!!.toID())
|
||||
@ -142,46 +129,66 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) :
|
||||
}
|
||||
|
||||
/**
|
||||
* Notification that is created when new streams are found
|
||||
* Group of notifications created when new streams are found in a given channel.
|
||||
*
|
||||
* For more information, see https://developer.android.com/develop/ui/views/notifications/group
|
||||
*/
|
||||
private fun createNotification(
|
||||
title: String,
|
||||
group: String,
|
||||
urlPath: String,
|
||||
description: String? = null,
|
||||
isGroupSummary: Boolean = false
|
||||
) {
|
||||
// increase the notification ID to guarantee uniqueness
|
||||
notificationId += 1
|
||||
private fun createNotificationsForChannel(group: String, streams: List<StreamItem>) {
|
||||
val intentFlags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK or
|
||||
Intent.FLAG_ACTIVITY_CLEAR_TASK
|
||||
|
||||
val intent = Intent(applicationContext, MainActivity::class.java).apply {
|
||||
flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
|
||||
if (isGroupSummary) {
|
||||
putExtra(IntentData.channelId, urlPath.toID())
|
||||
} else {
|
||||
putExtra(IntentData.videoId, urlPath.toID())
|
||||
}
|
||||
// Create stream notifications. These are automatically grouped on Android 7.0 and later.
|
||||
streams.forEach {
|
||||
val intent = Intent(applicationContext, MainActivity::class.java)
|
||||
.setFlags(intentFlags)
|
||||
.putExtra(IntentData.videoId, it.url!!.toID())
|
||||
val code = ++notificationId
|
||||
val pendingIntent = PendingIntentCompat
|
||||
.getActivity(applicationContext, code, intent, FLAG_UPDATE_CURRENT, false)
|
||||
|
||||
val notification = createNotificationBuilder(group)
|
||||
.setContentTitle(it.title)
|
||||
.setContentText(it.uploaderName)
|
||||
// The intent that will fire when the user taps the notification
|
||||
.setContentIntent(pendingIntent)
|
||||
.build()
|
||||
|
||||
notificationManager.notify(code, notification)
|
||||
}
|
||||
|
||||
val summaryId = ++notificationId
|
||||
|
||||
val intent = Intent(applicationContext, MainActivity::class.java)
|
||||
.setFlags(intentFlags)
|
||||
.putExtra(IntentData.channelId, group.toID())
|
||||
|
||||
val pendingIntent = PendingIntentCompat
|
||||
.getActivity(applicationContext, notificationId, intent, FLAG_UPDATE_CURRENT, false)
|
||||
.getActivity(applicationContext, summaryId, intent, FLAG_UPDATE_CURRENT, false)
|
||||
|
||||
val builder = NotificationCompat.Builder(applicationContext, PUSH_CHANNEL_ID)
|
||||
.setContentTitle(title)
|
||||
.setGroup(group)
|
||||
.setSmallIcon(R.drawable.ic_launcher_lockscreen)
|
||||
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
|
||||
// Create summary notification containing new streams for Android versions below 7.0.
|
||||
val newStreams = applicationContext.resources
|
||||
.getQuantityString(R.plurals.channel_new_streams, streams.size, streams.size)
|
||||
val summary = NotificationCompat.InboxStyle()
|
||||
streams.forEach {
|
||||
summary.addLine(it.title)
|
||||
}
|
||||
val summaryNotification = createNotificationBuilder(group)
|
||||
.setContentTitle(streams[0].uploaderName)
|
||||
.setContentText(newStreams)
|
||||
// The intent that will fire when the user taps the notification
|
||||
.setContentIntent(pendingIntent)
|
||||
.setGroupSummary(true)
|
||||
.setStyle(summary)
|
||||
.build()
|
||||
|
||||
notificationManager.notify(summaryId, summaryNotification)
|
||||
}
|
||||
|
||||
private fun createNotificationBuilder(group: String): NotificationCompat.Builder {
|
||||
return NotificationCompat.Builder(applicationContext, PUSH_CHANNEL_ID)
|
||||
.setSmallIcon(R.drawable.ic_launcher_lockscreen)
|
||||
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
|
||||
.setAutoCancel(true)
|
||||
|
||||
if (isGroupSummary) {
|
||||
builder.setGroupSummary(true)
|
||||
} else {
|
||||
builder.setContentText(description)
|
||||
}
|
||||
|
||||
// [notificationId] is a unique int for each notification that you must define
|
||||
notificationManager.notify(notificationId, builder.build())
|
||||
.setGroup(group)
|
||||
}
|
||||
}
|
||||
|
@ -485,4 +485,8 @@
|
||||
<item quantity="one">%d week ago</item>
|
||||
<item quantity="other">%d weeks ago</item>
|
||||
</plurals>
|
||||
<plurals name="channel_new_streams">
|
||||
<item quantity="one">%d new stream</item>
|
||||
<item quantity="other">%d new streams</item>
|
||||
</plurals>
|
||||
</resources>
|
Loading…
x
Reference in New Issue
Block a user