Merge pull request #2636 from Bnyro/master

Fix notifications when not logged in with account
This commit is contained in:
Bnyro 2023-01-08 17:05:45 +01:00 committed by GitHub
commit 7334e70de6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 117 additions and 59 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

@ -32,43 +32,44 @@ object NotificationHelper {
).toLong()
// schedule the work manager request if logged in and notifications enabled
if (notificationsEnabled && PreferenceHelper.getToken() != "") {
// required network type for the work
val networkType = when (
PreferenceHelper.getString(PreferenceKeys.REQUIRED_NETWORK, "all")
) {
"all" -> NetworkType.CONNECTED
"wifi" -> NetworkType.UNMETERED
"metered" -> NetworkType.METERED
else -> NetworkType.CONNECTED
}
// requirements for the work
// here: network needed to run the task
val constraints = Constraints.Builder()
.setRequiredNetworkType(networkType)
.build()
// create the worker
val notificationWorker = PeriodicWorkRequest.Builder(
NotificationWorker::class.java,
checkingFrequency,
TimeUnit.MINUTES
)
.setConstraints(constraints)
.build()
// enqueue the task
WorkManager.getInstance(context)
.enqueueUniquePeriodicWork(
NOTIFICATION_WORK_NAME,
existingPeriodicWorkPolicy,
notificationWorker
)
} else {
if (!notificationsEnabled) {
// cancel the work if notifications are disabled or the user is not logged in
WorkManager.getInstance(context)
.cancelUniqueWork(NOTIFICATION_WORK_NAME)
return
}
// required network type for the work
val networkType = when (
PreferenceHelper.getString(PreferenceKeys.REQUIRED_NETWORK, "all")
) {
"all" -> NetworkType.CONNECTED
"wifi" -> NetworkType.UNMETERED
"metered" -> NetworkType.METERED
else -> NetworkType.CONNECTED
}
// requirements for the work
// here: network needed to run the task
val constraints = Constraints.Builder()
.setRequiredNetworkType(networkType)
.build()
// create the worker
val notificationWorker = PeriodicWorkRequest.Builder(
NotificationWorker::class.java,
checkingFrequency,
TimeUnit.MINUTES
)
.setConstraints(constraints)
.build()
// enqueue the task
WorkManager.getInstance(context)
.enqueueUniquePeriodicWork(
NOTIFICATION_WORK_NAME,
existingPeriodicWorkPolicy,
notificationWorker
)
}
}

View File

@ -13,6 +13,7 @@ import com.github.libretube.R
import com.github.libretube.api.SubscriptionHelper
import com.github.libretube.constants.PUSH_CHANNEL_ID
import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.extensions.filterUntil
import com.github.libretube.extensions.toID
import com.github.libretube.ui.activities.MainActivity
import com.github.libretube.ui.views.TimePickerPreference
@ -26,12 +27,12 @@ import kotlinx.coroutines.runBlocking
class NotificationWorker(appContext: Context, parameters: WorkerParameters) :
Worker(appContext, parameters) {
private val notificationManager =
appContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
private val notificationManager = NotificationManagerCompat.from(appContext)
// the id where notification channels start
private var notificationId = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
notificationManager.activeNotifications.size + 5
val nManager = appContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
nManager.activeNotifications.size + 5
} else {
5
}
@ -90,28 +91,25 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) :
}
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 are enabled or no new video available
if (lastSeenStreamId == "" || lastSeenStreamId == latestFeedStreamId) {
PreferenceHelper.setLatestVideoId(lastSeenStreamId)
return@runBlocking
}
// filter the new videos out
val lastSeenStreamItem = videoFeed.filter { it.url!!.toID() == lastSeenStreamId }
// filter the new videos until the last seen video in the feed
val newStreams = videoFeed.filterUntil {
it.url!!.toID() == lastSeenStreamId
} ?: return@runBlocking
// previous video not found
if (lastSeenStreamItem.isEmpty()) return@runBlocking
val lastStreamIndex = videoFeed.indexOf(lastSeenStreamItem[0])
val newVideos = videoFeed.filterIndexed { index, _ ->
index < lastStreamIndex
}
// return if the previous video didn't get found
if (newStreams.isEmpty()) return@runBlocking
// hide for notifications unsubscribed channels
val channelsToIgnore = PreferenceHelper.getIgnorableNotificationChannels()
val filteredVideos = newVideos.filter {
val filteredVideos = newStreams.filter {
channelsToIgnore.none { channelId ->
channelId == it.uploaderUrl?.toID()
}
@ -119,12 +117,13 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) :
// group the new streams by the uploader
val channelGroups = filteredVideos.groupBy { it.uploaderUrl }
// create a notification for each new stream
channelGroups.forEach { (_, streams) ->
createNotification(
group = streams[0].uploaderUrl!!.toID(),
title = streams[0].uploaderName.toString(),
isSummary = true
group = streams.first().uploaderUrl!!.toID(),
title = streams.first().uploaderName.toString(),
isGroupSummary = true
)
streams.forEach { streamItem ->
@ -136,7 +135,7 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) :
}
}
// 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 success
@ -149,7 +148,7 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) :
title: String,
group: String,
description: String? = null,
isSummary: Boolean = false
isGroupSummary: Boolean = false
) {
// increase the notification ID to guarantee uniqueness
notificationId += 1
@ -172,15 +171,13 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) :
.setContentIntent(pendingIntent)
.setAutoCancel(true)
if (isSummary) {
if (isGroupSummary) {
builder.setGroupSummary(true)
} else {
builder.setContentText(description)
}
with(NotificationManagerCompat.from(applicationContext)) {
// notificationId is a unique int for each notification that you must define
notify(notificationId, builder.build())
}
// notificationId is a unique int for each notification that you must define
notificationManager.notify(notificationId, builder.build())
}
}

View File

@ -25,6 +25,51 @@
<item>https://piped-api.privacy.com.de/</item>
</string-array>
<string-array name="languages">
<item>@string/systemLanguage</item>
<item>العربية</item>
<item>Azərbaycan dili</item>
<item>Euskara</item>
<item>বাংলা</item>
<item>Català</item>
<item>简体中文</item>
<item>繁體中文</item>
<item>čeština</item>
<item>Dansk</item>
<item>English</item>
<item>Wikang Filipino</item>
<item>Suomi</item>
<item>Français</item>
<item>Deutsch</item>
<item>Ελληνικά</item>
<item>ગુજરાતી</item>
<item>עברית</item>
<item>हिन्दी</item>
<item>Magyar</item>
<item>Bahasa Indonesia</item>
<item>Italiano</item>
<item>日本語</item>
<item>조선말</item>
<item>latviešu</item>
<item>മലയാളം</item>
<item>मराठी</item>
<item>Norsk</item>
<item>فارسی</item>
<item>ଓଡ଼ିଆ</item>
<item>Polski</item>
<item>Português</item>
<item>Português (BR)</item>
<item>Română</item>
<item>Русский</item>
<item>Slovenčina</item>
<item>سۆرانی</item>
<item>Español</item>
<item>ภาษาไทย</item>
<item>Türkçe</item>
<item>Türkmençe</item>
<item>Українська</item>
</string-array>
<string-array name="languagesValue">
<item>sys</item>
<item>ar</item>