|
|
|
|
@ -1,9 +1,17 @@
|
|
|
|
|
package org.koitharu.kotatsu.suggestions.ui
|
|
|
|
|
|
|
|
|
|
import android.app.NotificationChannel
|
|
|
|
|
import android.app.NotificationManager
|
|
|
|
|
import android.content.Context
|
|
|
|
|
import android.os.Build
|
|
|
|
|
import androidx.core.app.NotificationCompat
|
|
|
|
|
import androidx.core.content.ContextCompat
|
|
|
|
|
import androidx.work.*
|
|
|
|
|
import java.util.concurrent.TimeUnit
|
|
|
|
|
import kotlin.math.pow
|
|
|
|
|
import org.koin.core.component.KoinComponent
|
|
|
|
|
import org.koin.core.component.inject
|
|
|
|
|
import org.koitharu.kotatsu.R
|
|
|
|
|
import org.koitharu.kotatsu.core.parser.MangaRepository
|
|
|
|
|
import org.koitharu.kotatsu.core.prefs.AppSettings
|
|
|
|
|
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.suggestions.domain.MangaSuggestion
|
|
|
|
|
import org.koitharu.kotatsu.suggestions.domain.SuggestionRepository
|
|
|
|
|
import java.util.concurrent.TimeUnit
|
|
|
|
|
import kotlin.math.pow
|
|
|
|
|
|
|
|
|
|
class SuggestionsWorker(appContext: Context, params: WorkerParameters) :
|
|
|
|
|
CoroutineWorker(appContext, params), KoinComponent {
|
|
|
|
|
@ -28,6 +34,37 @@ class SuggestionsWorker(appContext: Context, params: WorkerParameters) :
|
|
|
|
|
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 {
|
|
|
|
|
if (!appSettings.isSuggestionsEnabled) {
|
|
|
|
|
suggestionRepository.clear()
|
|
|
|
|
@ -74,6 +111,8 @@ class SuggestionsWorker(appContext: Context, params: WorkerParameters) :
|
|
|
|
|
private const val TAG_ONESHOT = "suggestions_oneshot"
|
|
|
|
|
private const val LIMIT = 140
|
|
|
|
|
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) {
|
|
|
|
|
val constraints = Constraints.Builder()
|
|
|
|
|
@ -96,6 +135,7 @@ class SuggestionsWorker(appContext: Context, params: WorkerParameters) :
|
|
|
|
|
val request = OneTimeWorkRequestBuilder<SuggestionsWorker>()
|
|
|
|
|
.setConstraints(constraints)
|
|
|
|
|
.addTag(TAG_ONESHOT)
|
|
|
|
|
.setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST)
|
|
|
|
|
.build()
|
|
|
|
|
WorkManager.getInstance(context)
|
|
|
|
|
.enqueue(request)
|
|
|
|
|
|