Skip to content

Commit

Permalink
Merge pull request #3891 from Bnyro/master
Browse files Browse the repository at this point in the history
Fix (disable) notifications for already seen videos
  • Loading branch information
Bnyro authored Jun 2, 2023
2 parents 396b630 + aa21ef2 commit 972a1c6
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ object PreferenceHelper {
authEditor.putString(PreferenceKeys.USERNAME, newValue).apply()
}

fun setLatestVideoId(videoId: String) {
fun setLastSeenVideoId(videoId: String) {
editor.putString(PreferenceKeys.LAST_STREAM_VIDEO_ID, videoId).commit()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class SubscriptionsViewModel : ViewModel() {
this@SubscriptionsViewModel.videoFeed.postValue(videoFeed)
if (videoFeed.isNotEmpty()) {
// save the last recent video to the prefs for the notification worker
PreferenceHelper.setLatestVideoId(videoFeed[0].url!!.toID())
PreferenceHelper.setLastSeenVideoId(videoFeed[0].url!!.toID())
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,24 +84,24 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) :
}
} catch (e: Exception) {
return false
}.filter {
PreferenceHelper.getBoolean(PreferenceKeys.SHORTS_NOTIFICATIONS, false) || !it.isShort
}

val lastSeenStreamId = PreferenceHelper.getLastSeenVideoId()
val latestFeedStreamId = videoFeed.firstOrNull()?.url?.toID() ?: return true
val lastUserSeenVideoId = PreferenceHelper.getLastSeenVideoId()
val mostRecentStreamId = videoFeed.firstOrNull()?.url?.toID() ?: return true
// save the latest streams that got notified about
PreferenceHelper.setLastSeenVideoId(mostRecentStreamId)

// first time notifications are enabled or no new video available
if (lastSeenStreamId.isEmpty() || lastSeenStreamId == latestFeedStreamId) {
PreferenceHelper.setLatestVideoId(lastSeenStreamId)
return true
}
if (lastUserSeenVideoId.isEmpty() || lastUserSeenVideoId == mostRecentStreamId) return true

val channelsToIgnore = PreferenceHelper.getIgnorableNotificationChannels()
val enableShortsNotification = PreferenceHelper.getBoolean(PreferenceKeys.SHORTS_NOTIFICATIONS, false)

val channelGroups = videoFeed.asSequence()
// filter the new videos until the last seen video in the feed
.takeWhile { it.url!!.toID() != lastSeenStreamId }
.takeWhile { it.url!!.toID() != lastUserSeenVideoId }
// don't show notifications for shorts videos if not enabled
.filter { enableShortsNotification || !it.isShort }
// hide for notifications unsubscribed channels
.filter { it.uploaderUrl!!.toID() !in channelsToIgnore }
// group the new streams by the uploader
Expand All @@ -116,8 +116,6 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) :
channelGroups.forEach { (channelId, streams) ->
createNotificationsForChannel(channelId, streams)
}
// save the latest streams that got notified about
PreferenceHelper.setLatestVideoId(videoFeed.first().url!!.toID())
// return whether the work succeeded
return true
}
Expand Down

0 comments on commit 972a1c6

Please sign in to comment.