Merge pull request #2951 from Isira-Seneviratne/Work_KTX

Use Work KTX functionality.
This commit is contained in:
Bnyro 2023-02-03 13:24:13 +01:00 committed by GitHub
commit 1e379d5a91
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 63 additions and 66 deletions

View File

@ -4,7 +4,7 @@ import android.content.Context
import androidx.work.Constraints
import androidx.work.ExistingPeriodicWorkPolicy
import androidx.work.NetworkType
import androidx.work.PeriodicWorkRequest
import androidx.work.PeriodicWorkRequestBuilder
import androidx.work.WorkManager
import com.github.libretube.constants.NOTIFICATION_WORK_NAME
import com.github.libretube.constants.PreferenceKeys
@ -56,8 +56,7 @@ object NotificationHelper {
.build()
// create the worker
val notificationWorker = PeriodicWorkRequest.Builder(
NotificationWorker::class.java,
val notificationWorker = PeriodicWorkRequestBuilder<NotificationWorker>(
checkingFrequency,
TimeUnit.MINUTES
)

View File

@ -8,7 +8,7 @@ import android.os.Build
import android.util.Log
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.work.Worker
import androidx.work.CoroutineWorker
import androidx.work.WorkerParameters
import com.github.libretube.R
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.ui.activities.MainActivity
import com.github.libretube.ui.views.TimePickerPreference
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.time.LocalTime
import kotlinx.coroutines.runBlocking
/**
* The notification worker which checks for new streams in a certain frequency
*/
class NotificationWorker(appContext: Context, parameters: WorkerParameters) :
Worker(appContext, parameters) {
CoroutineWorker(appContext, parameters) {
private val notificationManager = NotificationManagerCompat.from(appContext)
@ -41,7 +42,7 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) :
DOWNLOAD_PROGRESS_NOTIFICATION_ID
}
override fun doWork(): Result {
override suspend fun doWork(): Result {
if (!checkTime()) Result.success()
// check whether there are new streams and notify if there are some
val result = checkForNewStreams()
@ -82,34 +83,32 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) :
/**
* check whether new streams are available in subscriptions
*/
private fun checkForNewStreams(): Boolean {
var success = true
private suspend fun checkForNewStreams(): Boolean {
Log.d(TAG(), "Work manager started")
runBlocking {
// fetch the users feed
val videoFeed = try {
withContext(Dispatchers.IO) {
SubscriptionHelper.getFeed()
}
} catch (e: Exception) {
success = false
return@runBlocking
return false
}
val lastSeenStreamId = PreferenceHelper.getLastSeenVideoId()
val latestFeedStreamId = videoFeed.firstOrNull()?.url?.toID() ?: return@runBlocking
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@runBlocking
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@runBlocking
if (newStreams.isEmpty()) return true
// hide for notifications unsubscribed channels
val channelsToIgnore = PreferenceHelper.getIgnorableNotificationChannels()
@ -144,9 +143,8 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) :
}
// save the latest streams that got notified about
PreferenceHelper.setLatestVideoId(videoFeed.first().url!!.toID())
}
// return whether the work succeeded
return success
return true
}
/**