ntfy-android/app/src/main/java/io/heckel/ntfy/work/PollWorker.kt

61 lines
2.5 KiB
Kotlin
Raw Normal View History

package io.heckel.ntfy.work
import android.content.Context
import androidx.work.CoroutineWorker
import androidx.work.WorkerParameters
import io.heckel.ntfy.BuildConfig
2022-01-19 08:28:48 +13:00
import io.heckel.ntfy.db.Database
import io.heckel.ntfy.db.Repository
2022-01-17 18:19:05 +13:00
import io.heckel.ntfy.log.Log
import io.heckel.ntfy.msg.ApiService
import io.heckel.ntfy.msg.NotificationDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kotlin.random.Random
class PollWorker(ctx: Context, params: WorkerParameters) : CoroutineWorker(ctx, params) {
2021-11-23 09:45:43 +13:00
// IMPORTANT WARNING:
// Every time the worker is changed, the periodic work has to be REPLACEd.
// This is facilitated in the MainActivity using the VERSION below.
2022-01-17 18:19:05 +13:00
init {
Log.init(ctx) // Init in all entrypoints
}
override suspend fun doWork(): Result {
2022-01-17 18:19:05 +13:00
return withContext(Dispatchers.IO) {
Log.d(TAG, "Polling for new notifications")
val database = Database.getInstance(applicationContext)
2021-11-23 09:45:43 +13:00
val sharedPrefs = applicationContext.getSharedPreferences(Repository.SHARED_PREFS_ID, Context.MODE_PRIVATE)
2022-01-28 13:57:43 +13:00
val repository = Repository.getInstance(sharedPrefs, database)
val dispatcher = NotificationDispatcher(applicationContext, repository)
val api = ApiService()
repository.getSubscriptions().forEach{ subscription ->
try {
val notifications = api.poll(subscription.id, subscription.baseUrl, subscription.topic, since = subscription.lastActive)
val newNotifications = repository
.onlyNewNotifications(subscription.id, notifications)
.map { it.copy(notificationId = Random.nextInt()) }
newNotifications.forEach { notification ->
if (repository.addNotification(notification)) {
dispatcher.dispatch(subscription, notification)
}
}
} catch (e: Exception) {
Log.e(TAG, "Failed checking messages: ${e.message}", e)
}
}
Log.d(TAG, "Finished polling for new notifications")
return@withContext Result.success()
}
}
companion object {
const val VERSION = BuildConfig.VERSION_CODE
const val TAG = "NtfyPollWorker"
const val WORK_NAME_PERIODIC = "NtfyPollWorkerPeriodic" // Do not change
}
}