Skip to content

Commit

Permalink
Merge pull request #3715 from Isira-Seneviratne/Notification_improvem…
Browse files Browse the repository at this point in the history
…ents

Improve stream notifications
  • Loading branch information
Isira-Seneviratne authored May 6, 2023
2 parents f4fdfc5 + 4b7ba2e commit c8739a7
Showing 1 changed file with 21 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import android.app.PendingIntent.FLAG_UPDATE_CURRENT
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.os.Build
import android.util.Log
import androidx.core.app.NotificationCompat
import androidx.core.app.PendingIntentCompat
Expand All @@ -16,7 +15,6 @@ import androidx.work.WorkerParameters
import com.github.libretube.R
import com.github.libretube.api.SubscriptionHelper
import com.github.libretube.api.obj.StreamItem
import com.github.libretube.constants.DOWNLOAD_PROGRESS_NOTIFICATION_ID
import com.github.libretube.constants.IntentData
import com.github.libretube.constants.PUSH_CHANNEL_ID
import com.github.libretube.constants.PreferenceKeys
Expand All @@ -35,16 +33,8 @@ import kotlinx.coroutines.withContext
*/
class NotificationWorker(appContext: Context, parameters: WorkerParameters) :
CoroutineWorker(appContext, parameters) {

private val notificationManager = appContext.getSystemService<NotificationManager>()!!

// the id where notification channels start
private var notificationId = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
notificationManager.activeNotifications.size + DOWNLOAD_PROGRESS_NOTIFICATION_ID
} else {
DOWNLOAD_PROGRESS_NOTIFICATION_ID
}

override suspend fun doWork(): Result {
if (!checkTime()) Result.success()
// check whether there are new streams and notify if there are some
Expand Down Expand Up @@ -102,28 +92,24 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) :
return true
}

// filter the new videos until the last seen video in the feed
val newStreams = videoFeed.takeWhile { it.url!!.toID() != lastSeenStreamId }

// return if the previous video didn't get found
if (newStreams.isEmpty()) return true

// hide for notifications unsubscribed channels
val channelsToIgnore = PreferenceHelper.getIgnorableNotificationChannels()
val filteredVideos = newStreams.filter {
channelsToIgnore.none { channelId ->
channelId == it.uploaderUrl?.toID()
}
}

// group the new streams by the uploader
val channelGroups = filteredVideos.groupBy { it.uploaderUrl }
val channelGroups = videoFeed.asSequence()
// filter the new videos until the last seen video in the feed
.takeWhile { it.url!!.toID() != lastSeenStreamId }
// hide for notifications unsubscribed channels
.filter { it.uploaderUrl!!.toID() !in channelsToIgnore }
// group the new streams by the uploader
.groupBy { it.uploaderUrl!!.toID() }

// return if the previous video didn't get found or all the channels have notifications disabled
if (channelGroups.isEmpty()) return true

Log.d(TAG(), "Create notifications for new videos")

// create a notification for each new stream
channelGroups.forEach { (uploaderUrl, streams) ->
createNotificationsForChannel(uploaderUrl!!, streams)
channelGroups.forEach { (channelId, streams) ->
createNotificationsForChannel(channelId, streams)
}
// save the latest streams that got notified about
PreferenceHelper.setLatestVideoId(videoFeed.first().url!!.toID())
Expand All @@ -145,13 +131,12 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) :
showStreamNotification(group, it, false)
}

val summaryId = ++notificationId
val notificationId = group.hashCode()
val intent = Intent(applicationContext, MainActivity::class.java)
.setFlags(INTENT_FLAGS)
.putExtra(IntentData.channelId, group.toID())

.putExtra(IntentData.channelId, group)
val pendingIntent = PendingIntentCompat
.getActivity(applicationContext, summaryId, intent, FLAG_UPDATE_CURRENT, false)
.getActivity(applicationContext, notificationId, intent, FLAG_UPDATE_CURRENT, false)

// Create summary notification containing new streams for Android versions below 7.0.
val newStreams = applicationContext.resources
Expand All @@ -170,7 +155,7 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) :
.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_SUMMARY)
.build()

notificationManager.notify(summaryId, summaryNotification)
notificationManager.notify(notificationId, summaryNotification)
}
}

Expand All @@ -179,12 +164,13 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) :
stream: StreamItem,
isSingleNotification: Boolean
) {
val videoId = stream.url!!.toID()
val intent = Intent(applicationContext, MainActivity::class.java)
.setFlags(INTENT_FLAGS)
.putExtra(IntentData.videoId, stream.url!!.toID())
val code = ++notificationId
.putExtra(IntentData.videoId, videoId)
val notificationId = videoId.hashCode()
val pendingIntent = PendingIntentCompat
.getActivity(applicationContext, code, intent, FLAG_UPDATE_CURRENT, false)
.getActivity(applicationContext, notificationId, intent, FLAG_UPDATE_CURRENT, false)

val notificationBuilder = createNotificationBuilder(group)
.setContentTitle(stream.title)
Expand All @@ -208,7 +194,7 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) :
)
}

notificationManager.notify(code, notificationBuilder.build())
notificationManager.notify(notificationId, notificationBuilder.build())
}

private fun createNotificationBuilder(group: String): NotificationCompat.Builder {
Expand Down

0 comments on commit c8739a7

Please sign in to comment.