Cleanup the notification worker

This commit is contained in:
Bnyro 2023-01-08 16:39:22 +01:00
parent afed0f57b5
commit 36db1d49e0
2 changed files with 30 additions and 17 deletions

View File

@ -0,0 +1,15 @@
package com.github.libretube.extensions
/**
* Returns a list of all items until the given condition is fulfilled
* @param predicate The condition which needs to be searched for
* @return a list of all items before the first true condition
*/
fun <T> List<T>.filterUntil(predicate: (T) -> Boolean): List<T>? {
val items = mutableListOf<T>()
this.forEach {
if (predicate(it)) return items
items.add(it)
}
return null
}

View File

@ -13,6 +13,7 @@ import com.github.libretube.R
import com.github.libretube.api.SubscriptionHelper import com.github.libretube.api.SubscriptionHelper
import com.github.libretube.constants.PUSH_CHANNEL_ID import com.github.libretube.constants.PUSH_CHANNEL_ID
import com.github.libretube.constants.PreferenceKeys import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.extensions.filterUntil
import com.github.libretube.extensions.toID import com.github.libretube.extensions.toID
import com.github.libretube.ui.activities.MainActivity import com.github.libretube.ui.activities.MainActivity
import com.github.libretube.ui.views.TimePickerPreference import com.github.libretube.ui.views.TimePickerPreference
@ -90,7 +91,7 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) :
} }
val lastSeenStreamId = PreferenceHelper.getLastSeenVideoId() val lastSeenStreamId = PreferenceHelper.getLastSeenVideoId()
val latestFeedStreamId = videoFeed[0].url!!.toID() val latestFeedStreamId = videoFeed.firstOrNull()?.url?.toID() ?: return@runBlocking
// first time notifications enabled or no new video available // first time notifications enabled or no new video available
if (lastSeenStreamId == "" || lastSeenStreamId == latestFeedStreamId) { if (lastSeenStreamId == "" || lastSeenStreamId == latestFeedStreamId) {
@ -98,20 +99,17 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) :
return@runBlocking return@runBlocking
} }
// filter the new videos out // filter the new videos until the last seen video in the feed
val lastSeenStreamItem = videoFeed.filter { it.url!!.toID() == lastSeenStreamId } val newStreams = videoFeed.filterUntil {
it.url!!.toID() == lastSeenStreamId
} ?: return@runBlocking
// previous video not found // return if the previous video didn't get found
if (lastSeenStreamItem.isEmpty()) return@runBlocking if (newStreams.isEmpty()) return@runBlocking
val lastStreamIndex = videoFeed.indexOf(lastSeenStreamItem[0])
val newVideos = videoFeed.filterIndexed { index, _ ->
index < lastStreamIndex
}
// hide for notifications unsubscribed channels // hide for notifications unsubscribed channels
val channelsToIgnore = PreferenceHelper.getIgnorableNotificationChannels() val channelsToIgnore = PreferenceHelper.getIgnorableNotificationChannels()
val filteredVideos = newVideos.filter { val filteredVideos = newStreams.filter {
channelsToIgnore.none { channelId -> channelsToIgnore.none { channelId ->
channelId == it.uploaderUrl?.toID() channelId == it.uploaderUrl?.toID()
} }
@ -122,9 +120,9 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) :
// create a notification for each new stream // create a notification for each new stream
channelGroups.forEach { (_, streams) -> channelGroups.forEach { (_, streams) ->
createNotification( createNotification(
group = streams[0].uploaderUrl!!.toID(), group = streams.first().uploaderUrl!!.toID(),
title = streams[0].uploaderName.toString(), title = streams.first().uploaderName.toString(),
isSummary = true isGroupSummary = true
) )
streams.forEach { streamItem -> streams.forEach { streamItem ->
@ -136,7 +134,7 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) :
} }
} }
// save the latest streams that got notified about // save the latest streams that got notified about
PreferenceHelper.setLatestVideoId(videoFeed[0].url!!.toID()) PreferenceHelper.setLatestVideoId(videoFeed.first().url!!.toID())
} }
// return whether the work succeeded // return whether the work succeeded
return success return success
@ -149,7 +147,7 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) :
title: String, title: String,
group: String, group: String,
description: String? = null, description: String? = null,
isSummary: Boolean = false isGroupSummary: Boolean = false
) { ) {
// increase the notification ID to guarantee uniqueness // increase the notification ID to guarantee uniqueness
notificationId += 1 notificationId += 1
@ -172,7 +170,7 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) :
.setContentIntent(pendingIntent) .setContentIntent(pendingIntent)
.setAutoCancel(true) .setAutoCancel(true)
if (isSummary) { if (isGroupSummary) {
builder.setGroupSummary(true) builder.setGroupSummary(true)
} else { } else {
builder.setContentText(description) builder.setContentText(description)