Merge pull request #916 from Bnyro/master

fix notifcations
This commit is contained in:
Bnyro 2022-07-29 13:49:18 +02:00 committed by GitHub
commit 5af9c8754b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 7 deletions

View File

@ -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
} }
/** /**

View File

@ -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()
} }
} }