Migrate to expedited jobs

This commit is contained in:
Koitharu
2022-04-06 17:50:08 +03:00
parent 36a7a3ebbc
commit cca6d5fa04
3 changed files with 46 additions and 4 deletions

View File

@@ -1,9 +1,17 @@
package org.koitharu.kotatsu.suggestions.ui package org.koitharu.kotatsu.suggestions.ui
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context import android.content.Context
import android.os.Build
import androidx.core.app.NotificationCompat
import androidx.core.content.ContextCompat
import androidx.work.* import androidx.work.*
import java.util.concurrent.TimeUnit
import kotlin.math.pow
import org.koin.core.component.KoinComponent import org.koin.core.component.KoinComponent
import org.koin.core.component.inject import org.koin.core.component.inject
import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.core.parser.MangaRepository import org.koitharu.kotatsu.core.parser.MangaRepository
import org.koitharu.kotatsu.core.prefs.AppSettings import org.koitharu.kotatsu.core.prefs.AppSettings
import org.koitharu.kotatsu.history.domain.HistoryRepository import org.koitharu.kotatsu.history.domain.HistoryRepository
@@ -11,8 +19,6 @@ import org.koitharu.kotatsu.parsers.model.Manga
import org.koitharu.kotatsu.parsers.model.SortOrder import org.koitharu.kotatsu.parsers.model.SortOrder
import org.koitharu.kotatsu.suggestions.domain.MangaSuggestion import org.koitharu.kotatsu.suggestions.domain.MangaSuggestion
import org.koitharu.kotatsu.suggestions.domain.SuggestionRepository import org.koitharu.kotatsu.suggestions.domain.SuggestionRepository
import java.util.concurrent.TimeUnit
import kotlin.math.pow
class SuggestionsWorker(appContext: Context, params: WorkerParameters) : class SuggestionsWorker(appContext: Context, params: WorkerParameters) :
CoroutineWorker(appContext, params), KoinComponent { CoroutineWorker(appContext, params), KoinComponent {
@@ -28,6 +34,37 @@ class SuggestionsWorker(appContext: Context, params: WorkerParameters) :
Result.failure() Result.failure()
} }
override suspend fun getForegroundInfo(): ForegroundInfo {
val manager = applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val title = applicationContext.getString(R.string.suggestions_updating)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
WORKER_CHANNEL_ID,
title,
NotificationManager.IMPORTANCE_LOW
)
channel.setShowBadge(false)
channel.enableVibration(false)
channel.setSound(null, null)
channel.enableLights(false)
manager.createNotificationChannel(channel)
}
val notification = NotificationCompat.Builder(applicationContext, WORKER_CHANNEL_ID)
.setContentTitle(title)
.setPriority(NotificationCompat.PRIORITY_MIN)
.setDefaults(0)
.setColor(ContextCompat.getColor(applicationContext, R.color.blue_primary_dark))
.setSilent(true)
.setProgress(0, 0, true)
.setSmallIcon(android.R.drawable.stat_notify_sync)
.setForegroundServiceBehavior(NotificationCompat.FOREGROUND_SERVICE_DEFERRED)
.setOngoing(true)
.build()
return ForegroundInfo(WORKER_NOTIFICATION_ID, notification)
}
private suspend fun doWorkImpl(): Int { private suspend fun doWorkImpl(): Int {
if (!appSettings.isSuggestionsEnabled) { if (!appSettings.isSuggestionsEnabled) {
suggestionRepository.clear() suggestionRepository.clear()
@@ -74,6 +111,8 @@ class SuggestionsWorker(appContext: Context, params: WorkerParameters) :
private const val TAG_ONESHOT = "suggestions_oneshot" private const val TAG_ONESHOT = "suggestions_oneshot"
private const val LIMIT = 140 private const val LIMIT = 140
private const val DATA_COUNT = "count" private const val DATA_COUNT = "count"
private const val WORKER_CHANNEL_ID = "suggestion_worker"
private const val WORKER_NOTIFICATION_ID = 36
fun setup(context: Context) { fun setup(context: Context) {
val constraints = Constraints.Builder() val constraints = Constraints.Builder()
@@ -96,6 +135,7 @@ class SuggestionsWorker(appContext: Context, params: WorkerParameters) :
val request = OneTimeWorkRequestBuilder<SuggestionsWorker>() val request = OneTimeWorkRequestBuilder<SuggestionsWorker>()
.setConstraints(constraints) .setConstraints(constraints)
.addTag(TAG_ONESHOT) .addTag(TAG_ONESHOT)
.setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST)
.build() .build()
WorkManager.getInstance(context) WorkManager.getInstance(context)
.enqueue(request) .enqueue(request)

View File

@@ -53,7 +53,7 @@ class TrackWorker(context: Context, workerParams: WorkerParameters) :
if (tracks.isEmpty()) { if (tracks.isEmpty()) {
return Result.success() return Result.success()
} }
setForeground(createForegroundInfo()) setForeground(getForegroundInfo())
var success = 0 var success = 0
val workData = Data.Builder() val workData = Data.Builder()
.putInt(DATA_TOTAL, tracks.size) .putInt(DATA_TOTAL, tracks.size)
@@ -201,7 +201,7 @@ class TrackWorker(context: Context, workerParams: WorkerParameters) :
} }
} }
private fun createForegroundInfo(): ForegroundInfo { override suspend fun getForegroundInfo(): ForegroundInfo {
val title = applicationContext.getString(R.string.check_for_new_chapters) val title = applicationContext.getString(R.string.check_for_new_chapters)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel( val channel = NotificationChannel(
@@ -281,6 +281,7 @@ class TrackWorker(context: Context, workerParams: WorkerParameters) :
val request = OneTimeWorkRequestBuilder<TrackWorker>() val request = OneTimeWorkRequestBuilder<TrackWorker>()
.setConstraints(constraints) .setConstraints(constraints)
.addTag(TAG_ONESHOT) .addTag(TAG_ONESHOT)
.setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST)
.build() .build()
WorkManager.getInstance(context) WorkManager.getInstance(context)
.enqueue(request) .enqueue(request)

View File

@@ -268,4 +268,5 @@
<string name="content">Content</string> <string name="content">Content</string>
<string name="github" translatable="false">GitHub</string> <string name="github" translatable="false">GitHub</string>
<string name="discord" translatable="false">Discord</string> <string name="discord" translatable="false">Discord</string>
<string name="suggestions_updating">Suggestions updating</string>
</resources> </resources>