This commit is contained in:
Bnyro 2022-07-28 15:58:22 +02:00
parent 24b4827e0b
commit f4418f30eb
2 changed files with 14 additions and 3 deletions

View File

@ -144,6 +144,8 @@ class SubscriptionsFragment : Fragment() {
binding.subRefresh.isRefreshing = false binding.subRefresh.isRefreshing = false
} }
if (response.isNotEmpty()) { if (response.isNotEmpty()) {
// save the last recent video to the prefs for the notification worker
PreferenceHelper.setLatestVideoId(response[0].url.toString().replace("/watch?v=", ""))
channelRecView.adapter = SubscriptionChannelAdapter(response.toMutableList()) channelRecView.adapter = SubscriptionChannelAdapter(response.toMutableList())
} else { } else {
Toast.makeText(context, R.string.subscribeIsEmpty, Toast.LENGTH_SHORT).show() Toast.makeText(context, R.string.subscribeIsEmpty, Toast.LENGTH_SHORT).show()

View File

@ -22,6 +22,7 @@ object NotificationHelper {
fun enqueueWork( fun enqueueWork(
context: Context context: Context
) { ) {
// get the notification preferences
PreferenceHelper.setContext(context) PreferenceHelper.setContext(context)
val notificationsEnabled = PreferenceHelper.getBoolean( val notificationsEnabled = PreferenceHelper.getBoolean(
PreferenceKeys.NOTIFICATION_ENABLED, PreferenceKeys.NOTIFICATION_ENABLED,
@ -36,23 +37,27 @@ object NotificationHelper {
val uniqueWorkName = "NotificationService" val uniqueWorkName = "NotificationService"
if (notificationsEnabled) { if (notificationsEnabled) {
// requirements for the work
// here: network needed to run the task
val constraints = Constraints.Builder() val constraints = Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED) .setRequiredNetworkType(NetworkType.CONNECTED)
.build() .build()
val myWorkBuilder = PeriodicWorkRequest.Builder( // create the worker
val notificationWorker = PeriodicWorkRequest.Builder(
NotificationWorker::class.java, NotificationWorker::class.java,
checkingFrequency, checkingFrequency,
TimeUnit.MINUTES TimeUnit.MINUTES
) )
.setConstraints(constraints) .setConstraints(constraints)
.build()
val myWork = myWorkBuilder.build() // enqueue the task
WorkManager.getInstance(context) WorkManager.getInstance(context)
.enqueueUniquePeriodicWork( .enqueueUniquePeriodicWork(
uniqueWorkName, uniqueWorkName,
ExistingPeriodicWorkPolicy.REPLACE, ExistingPeriodicWorkPolicy.REPLACE,
myWork notificationWorker
) )
} else { } else {
WorkManager.getInstance(context) WorkManager.getInstance(context)
@ -76,8 +81,10 @@ object NotificationHelper {
} catch (e: Exception) { } catch (e: Exception) {
return@runBlocking return@runBlocking
} }
val lastSeenStreamId = PreferenceHelper.getLatestVideoId() val lastSeenStreamId = PreferenceHelper.getLatestVideoId()
val latestFeedStreamId = videoFeed[0].url?.replace("/watch?v=", "") val latestFeedStreamId = videoFeed[0].url?.replace("/watch?v=", "")
// first time notifications enabled // first time notifications enabled
if (lastSeenStreamId == "") PreferenceHelper.setLatestVideoId(lastSeenStreamId) if (lastSeenStreamId == "") PreferenceHelper.setLatestVideoId(lastSeenStreamId)
else if (lastSeenStreamId != latestFeedStreamId) { else if (lastSeenStreamId != latestFeedStreamId) {
@ -109,6 +116,8 @@ object NotificationHelper {
) )
} }
} }
// save the id of the last recent video for the next time it's running
PreferenceHelper.setLatestVideoId(videoFeed[0].url?.replace("/watch?v=", "")!!)
createNotification(context, title!!, description!!) createNotification(context, title!!, description!!)
} }
} }