From e253373536af283166972168d984700e90fbf7e0 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Fri, 29 Jul 2022 13:48:59 +0200 Subject: [PATCH] fix notifcations --- .../com/github/libretube/util/NotificationHelper.kt | 12 +++++++++--- .../com/github/libretube/util/NotificationWorker.kt | 7 +++---- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/github/libretube/util/NotificationHelper.kt b/app/src/main/java/com/github/libretube/util/NotificationHelper.kt index 727cbda56..fbeada8a3 100644 --- a/app/src/main/java/com/github/libretube/util/NotificationHelper.kt +++ b/app/src/main/java/com/github/libretube/util/NotificationHelper.kt @@ -36,7 +36,8 @@ object NotificationHelper { val uniqueWorkName = "NotificationService" - if (notificationsEnabled) { + // schedule the work manager request if logged in and notifications enabled + if (notificationsEnabled && PreferenceHelper.getToken() != "") { // requirements for the work // here: network needed to run the task val constraints = Constraints.Builder() @@ -60,6 +61,7 @@ object NotificationHelper { notificationWorker ) } else { + // cancel the work if notifications are disabled or the user is not logged in WorkManager.getInstance(context) .cancelUniqueWork(uniqueWorkName) } @@ -68,9 +70,10 @@ object NotificationHelper { /** * check whether new streams are available in subscriptions */ - fun checkForNewStreams(context: Context) { + fun checkForNewStreams(context: Context): Boolean { + var result = true + val token = PreferenceHelper.getToken() - if (token == "") return runBlocking { val task = async { RetrofitInstance.authApi.getFeed(token) @@ -79,6 +82,7 @@ object NotificationHelper { val videoFeed = try { task.await() } catch (e: Exception) { + result = false return@runBlocking } @@ -121,6 +125,8 @@ object NotificationHelper { createNotification(context, title!!, description!!) } } + // return whether the work succeeded + return result } /** diff --git a/app/src/main/java/com/github/libretube/util/NotificationWorker.kt b/app/src/main/java/com/github/libretube/util/NotificationWorker.kt index 711aecb4e..1ea349e28 100644 --- a/app/src/main/java/com/github/libretube/util/NotificationWorker.kt +++ b/app/src/main/java/com/github/libretube/util/NotificationWorker.kt @@ -11,10 +11,9 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) : Wo private val TAG = "NotificationWorker" 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 - NotificationHelper.checkForNewStreams(applicationContext) - return Result.success() + val result = NotificationHelper.checkForNewStreams(applicationContext) + // return success if the API request succeeded + return if (result) Result.success() else Result.retry() } }