mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-01-06 01:20:29 +05:30
Convert NotificationWorker to a CoroutineWorker.
This commit is contained in:
parent
75a3f3e53b
commit
5f1d41c790
@ -8,7 +8,7 @@ import android.os.Build
|
|||||||
import android.util.Log
|
import android.util.Log
|
||||||
import androidx.core.app.NotificationCompat
|
import androidx.core.app.NotificationCompat
|
||||||
import androidx.core.app.NotificationManagerCompat
|
import androidx.core.app.NotificationManagerCompat
|
||||||
import androidx.work.Worker
|
import androidx.work.CoroutineWorker
|
||||||
import androidx.work.WorkerParameters
|
import androidx.work.WorkerParameters
|
||||||
import com.github.libretube.R
|
import com.github.libretube.R
|
||||||
import com.github.libretube.api.SubscriptionHelper
|
import com.github.libretube.api.SubscriptionHelper
|
||||||
@ -22,14 +22,15 @@ import com.github.libretube.extensions.toID
|
|||||||
import com.github.libretube.helpers.PreferenceHelper
|
import com.github.libretube.helpers.PreferenceHelper
|
||||||
import com.github.libretube.ui.activities.MainActivity
|
import com.github.libretube.ui.activities.MainActivity
|
||||||
import com.github.libretube.ui.views.TimePickerPreference
|
import com.github.libretube.ui.views.TimePickerPreference
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
import java.time.LocalTime
|
import java.time.LocalTime
|
||||||
import kotlinx.coroutines.runBlocking
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The notification worker which checks for new streams in a certain frequency
|
* The notification worker which checks for new streams in a certain frequency
|
||||||
*/
|
*/
|
||||||
class NotificationWorker(appContext: Context, parameters: WorkerParameters) :
|
class NotificationWorker(appContext: Context, parameters: WorkerParameters) :
|
||||||
Worker(appContext, parameters) {
|
CoroutineWorker(appContext, parameters) {
|
||||||
|
|
||||||
private val notificationManager = NotificationManagerCompat.from(appContext)
|
private val notificationManager = NotificationManagerCompat.from(appContext)
|
||||||
|
|
||||||
@ -41,7 +42,7 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) :
|
|||||||
DOWNLOAD_PROGRESS_NOTIFICATION_ID
|
DOWNLOAD_PROGRESS_NOTIFICATION_ID
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun doWork(): Result {
|
override suspend fun doWork(): Result {
|
||||||
if (!checkTime()) Result.success()
|
if (!checkTime()) Result.success()
|
||||||
// check whether there are new streams and notify if there are some
|
// check whether there are new streams and notify if there are some
|
||||||
val result = checkForNewStreams()
|
val result = checkForNewStreams()
|
||||||
@ -82,71 +83,68 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) :
|
|||||||
/**
|
/**
|
||||||
* check whether new streams are available in subscriptions
|
* check whether new streams are available in subscriptions
|
||||||
*/
|
*/
|
||||||
private fun checkForNewStreams(): Boolean {
|
private suspend fun checkForNewStreams(): Boolean {
|
||||||
var success = true
|
|
||||||
|
|
||||||
Log.d(TAG(), "Work manager started")
|
Log.d(TAG(), "Work manager started")
|
||||||
|
|
||||||
runBlocking {
|
// fetch the users feed
|
||||||
// fetch the users feed
|
val videoFeed = try {
|
||||||
val videoFeed = try {
|
withContext(Dispatchers.IO) {
|
||||||
SubscriptionHelper.getFeed()
|
SubscriptionHelper.getFeed()
|
||||||
} catch (e: Exception) {
|
|
||||||
success = false
|
|
||||||
return@runBlocking
|
|
||||||
}
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
val lastSeenStreamId = PreferenceHelper.getLastSeenVideoId()
|
return false
|
||||||
val latestFeedStreamId = videoFeed.firstOrNull()?.url?.toID() ?: return@runBlocking
|
|
||||||
|
|
||||||
// first time notifications are enabled or no new video available
|
|
||||||
if (lastSeenStreamId == "" || lastSeenStreamId == latestFeedStreamId) {
|
|
||||||
PreferenceHelper.setLatestVideoId(lastSeenStreamId)
|
|
||||||
return@runBlocking
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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@runBlocking
|
|
||||||
|
|
||||||
// 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 }
|
|
||||||
|
|
||||||
Log.d(TAG(), "Create notifications for new videos")
|
|
||||||
|
|
||||||
// create a notification for each new stream
|
|
||||||
channelGroups.forEach { (_, streams) ->
|
|
||||||
createNotification(
|
|
||||||
group = streams.first().uploaderUrl!!.toID(),
|
|
||||||
title = streams.first().uploaderName.toString(),
|
|
||||||
urlPath = streams.first().uploaderUrl!!,
|
|
||||||
isGroupSummary = true
|
|
||||||
)
|
|
||||||
|
|
||||||
streams.forEach { streamItem ->
|
|
||||||
createNotification(
|
|
||||||
title = streamItem.title.toString(),
|
|
||||||
description = streamItem.uploaderName.toString(),
|
|
||||||
urlPath = streamItem.url!!,
|
|
||||||
group = streamItem.uploaderUrl!!.toID()
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// save the latest streams that got notified about
|
|
||||||
PreferenceHelper.setLatestVideoId(videoFeed.first().url!!.toID())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val lastSeenStreamId = PreferenceHelper.getLastSeenVideoId()
|
||||||
|
val latestFeedStreamId = videoFeed.firstOrNull()?.url?.toID() ?: return true
|
||||||
|
|
||||||
|
// first time notifications are enabled or no new video available
|
||||||
|
if (lastSeenStreamId == "" || lastSeenStreamId == latestFeedStreamId) {
|
||||||
|
PreferenceHelper.setLatestVideoId(lastSeenStreamId)
|
||||||
|
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 }
|
||||||
|
|
||||||
|
Log.d(TAG(), "Create notifications for new videos")
|
||||||
|
|
||||||
|
// create a notification for each new stream
|
||||||
|
channelGroups.forEach { (_, streams) ->
|
||||||
|
createNotification(
|
||||||
|
group = streams.first().uploaderUrl!!.toID(),
|
||||||
|
title = streams.first().uploaderName.toString(),
|
||||||
|
urlPath = streams.first().uploaderUrl!!,
|
||||||
|
isGroupSummary = true
|
||||||
|
)
|
||||||
|
|
||||||
|
streams.forEach { streamItem ->
|
||||||
|
createNotification(
|
||||||
|
title = streamItem.title.toString(),
|
||||||
|
description = streamItem.uploaderName.toString(),
|
||||||
|
urlPath = streamItem.url!!,
|
||||||
|
group = streamItem.uploaderUrl!!.toID()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// save the latest streams that got notified about
|
||||||
|
PreferenceHelper.setLatestVideoId(videoFeed.first().url!!.toID())
|
||||||
// return whether the work succeeded
|
// return whether the work succeeded
|
||||||
return success
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user