mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-14 14:20:30 +05:30
commit
5af9c8754b
@ -36,7 +36,8 @@ object NotificationHelper {
|
|||||||
|
|
||||||
val uniqueWorkName = "NotificationService"
|
val uniqueWorkName = "NotificationService"
|
||||||
|
|
||||||
if (notificationsEnabled) {
|
// schedule the work manager request if logged in and notifications enabled
|
||||||
|
if (notificationsEnabled && PreferenceHelper.getToken() != "") {
|
||||||
// requirements for the work
|
// requirements for the work
|
||||||
// here: network needed to run the task
|
// here: network needed to run the task
|
||||||
val constraints = Constraints.Builder()
|
val constraints = Constraints.Builder()
|
||||||
@ -60,6 +61,7 @@ object NotificationHelper {
|
|||||||
notificationWorker
|
notificationWorker
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
|
// cancel the work if notifications are disabled or the user is not logged in
|
||||||
WorkManager.getInstance(context)
|
WorkManager.getInstance(context)
|
||||||
.cancelUniqueWork(uniqueWorkName)
|
.cancelUniqueWork(uniqueWorkName)
|
||||||
}
|
}
|
||||||
@ -68,9 +70,10 @@ object NotificationHelper {
|
|||||||
/**
|
/**
|
||||||
* check whether new streams are available in subscriptions
|
* check whether new streams are available in subscriptions
|
||||||
*/
|
*/
|
||||||
fun checkForNewStreams(context: Context) {
|
fun checkForNewStreams(context: Context): Boolean {
|
||||||
|
var result = true
|
||||||
|
|
||||||
val token = PreferenceHelper.getToken()
|
val token = PreferenceHelper.getToken()
|
||||||
if (token == "") return
|
|
||||||
runBlocking {
|
runBlocking {
|
||||||
val task = async {
|
val task = async {
|
||||||
RetrofitInstance.authApi.getFeed(token)
|
RetrofitInstance.authApi.getFeed(token)
|
||||||
@ -79,6 +82,7 @@ object NotificationHelper {
|
|||||||
val videoFeed = try {
|
val videoFeed = try {
|
||||||
task.await()
|
task.await()
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
|
result = false
|
||||||
return@runBlocking
|
return@runBlocking
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -121,6 +125,8 @@ object NotificationHelper {
|
|||||||
createNotification(context, title!!, description!!)
|
createNotification(context, title!!, description!!)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// return whether the work succeeded
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -11,10 +11,9 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) : Wo
|
|||||||
private val TAG = "NotificationWorker"
|
private val TAG = "NotificationWorker"
|
||||||
|
|
||||||
override fun doWork(): Result {
|
override fun doWork(): Result {
|
||||||
// schedule the next task of the worker
|
|
||||||
NotificationHelper.enqueueWork(applicationContext)
|
|
||||||
// check whether there are new streams and notify if there are some
|
// check whether there are new streams and notify if there are some
|
||||||
NotificationHelper.checkForNewStreams(applicationContext)
|
val result = NotificationHelper.checkForNewStreams(applicationContext)
|
||||||
return Result.success()
|
// return success if the API request succeeded
|
||||||
|
return if (result) Result.success() else Result.retry()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user