diff --git a/app/src/main/java/com/github/libretube/fragments/SubscriptionsFragment.kt b/app/src/main/java/com/github/libretube/fragments/SubscriptionsFragment.kt index c6261cdfa..3d1327132 100644 --- a/app/src/main/java/com/github/libretube/fragments/SubscriptionsFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/SubscriptionsFragment.kt @@ -144,6 +144,8 @@ class SubscriptionsFragment : Fragment() { binding.subRefresh.isRefreshing = false } 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()) } else { Toast.makeText(context, R.string.subscribeIsEmpty, Toast.LENGTH_SHORT).show() diff --git a/app/src/main/java/com/github/libretube/util/NotificationHelper.kt b/app/src/main/java/com/github/libretube/util/NotificationHelper.kt index ebdeb74e9..0d196af74 100644 --- a/app/src/main/java/com/github/libretube/util/NotificationHelper.kt +++ b/app/src/main/java/com/github/libretube/util/NotificationHelper.kt @@ -22,6 +22,7 @@ object NotificationHelper { fun enqueueWork( context: Context ) { + // get the notification preferences PreferenceHelper.setContext(context) val notificationsEnabled = PreferenceHelper.getBoolean( PreferenceKeys.NOTIFICATION_ENABLED, @@ -36,23 +37,27 @@ object NotificationHelper { val uniqueWorkName = "NotificationService" if (notificationsEnabled) { + // requirements for the work + // here: network needed to run the task val constraints = Constraints.Builder() .setRequiredNetworkType(NetworkType.CONNECTED) .build() - val myWorkBuilder = PeriodicWorkRequest.Builder( + // create the worker + val notificationWorker = PeriodicWorkRequest.Builder( NotificationWorker::class.java, checkingFrequency, TimeUnit.MINUTES ) .setConstraints(constraints) + .build() - val myWork = myWorkBuilder.build() + // enqueue the task WorkManager.getInstance(context) .enqueueUniquePeriodicWork( uniqueWorkName, ExistingPeriodicWorkPolicy.REPLACE, - myWork + notificationWorker ) } else { WorkManager.getInstance(context) @@ -76,8 +81,10 @@ object NotificationHelper { } catch (e: Exception) { return@runBlocking } + val lastSeenStreamId = PreferenceHelper.getLatestVideoId() val latestFeedStreamId = videoFeed[0].url?.replace("/watch?v=", "") + // first time notifications enabled if (lastSeenStreamId == "") PreferenceHelper.setLatestVideoId(lastSeenStreamId) 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!!) } }