From 9b24c507c508d9a5a023cacae4e961af752b1cb0 Mon Sep 17 00:00:00 2001 From: Koitharu Date: Sat, 14 Sep 2024 12:12:55 +0300 Subject: [PATCH] Option to automatically download new chapters (close #425, close #602, close #955) --- .../kotatsu/core/prefs/AppSettings.kt | 4 ++ .../kotatsu/core/prefs/ColorScheme.kt | 2 + .../kotatsu/core/prefs/DownloadFormat.kt | 3 ++ .../koitharu/kotatsu/core/prefs/ListMode.kt | 5 ++- .../koitharu/kotatsu/core/prefs/NavItem.kt | 2 + .../kotatsu/core/prefs/NetworkPolicy.kt | 2 + .../core/prefs/ProgressIndicatorMode.kt | 3 ++ .../kotatsu/core/prefs/ReaderAnimation.kt | 3 ++ .../kotatsu/core/prefs/ReaderBackground.kt | 2 + .../koitharu/kotatsu/core/prefs/ReaderMode.kt | 3 ++ .../kotatsu/core/prefs/ScreenshotsPolicy.kt | 3 ++ .../core/prefs/SearchSuggestionType.kt | 2 + .../core/prefs/TrackerDownloadStrategy.kt | 9 ++++ .../ui/pager/ChaptersPagesViewModel.kt | 5 ++- .../ui/worker/DownloadNotificationFactory.kt | 44 +++++++++++++------ .../download/ui/worker/DownloadWorker.kt | 16 ++++--- .../tracker/TrackerSettingsFragment.kt | 8 ++++ .../kotatsu/tracker/work/TrackWorker.kt | 40 ++++++++++++++--- app/src/main/res/values/arrays.xml | 4 ++ app/src/main/res/values/strings.xml | 3 ++ app/src/main/res/xml/pref_tracker.xml | 7 +++ 21 files changed, 143 insertions(+), 27 deletions(-) create mode 100644 app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/TrackerDownloadStrategy.kt diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/AppSettings.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/AppSettings.kt index 1cff809e4..3d3b82081 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/AppSettings.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/AppSettings.kt @@ -160,6 +160,9 @@ class AppSettings @Inject constructor(@ApplicationContext context: Context) { val isTrackerNsfwDisabled: Boolean get() = prefs.getBoolean(KEY_TRACKER_NO_NSFW, false) + val trackerDownloadStrategy: TrackerDownloadStrategy + get() = prefs.getEnumValue(KEY_TRACKER_DOWNLOAD, TrackerDownloadStrategy.DISABLED) + var notificationSound: Uri get() = prefs.getString(KEY_NOTIFICATIONS_SOUND, null)?.toUriOrNull() ?: Settings.System.DEFAULT_NOTIFICATION_URI @@ -600,6 +603,7 @@ class AppSettings @Inject constructor(@ApplicationContext context: Context) { const val KEY_TRACK_WARNING = "track_warning" const val KEY_TRACKER_NOTIFICATIONS = "tracker_notifications" const val KEY_TRACKER_NO_NSFW = "tracker_no_nsfw" + const val KEY_TRACKER_DOWNLOAD = "tracker_download" const val KEY_NOTIFICATIONS_SETTINGS = "notifications_settings" const val KEY_NOTIFICATIONS_SOUND = "notifications_sound" const val KEY_NOTIFICATIONS_VIBRATE = "notifications_vibrate" diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/ColorScheme.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/ColorScheme.kt index c2ffee581..e927a35ac 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/ColorScheme.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/ColorScheme.kt @@ -1,11 +1,13 @@ package org.koitharu.kotatsu.core.prefs +import androidx.annotation.Keep import androidx.annotation.StringRes import androidx.annotation.StyleRes import com.google.android.material.color.DynamicColors import org.koitharu.kotatsu.R import org.koitharu.kotatsu.parsers.util.find +@Keep enum class ColorScheme( @StyleRes val styleResId: Int, @StringRes val titleResId: Int, diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/DownloadFormat.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/DownloadFormat.kt index 72acf3640..65b3b9c52 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/DownloadFormat.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/DownloadFormat.kt @@ -1,5 +1,8 @@ package org.koitharu.kotatsu.core.prefs +import androidx.annotation.Keep + +@Keep enum class DownloadFormat { AUTOMATIC, diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/ListMode.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/ListMode.kt index dc8269cad..5c2b77fc5 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/ListMode.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/ListMode.kt @@ -1,6 +1,9 @@ package org.koitharu.kotatsu.core.prefs +import androidx.annotation.Keep + +@Keep enum class ListMode { LIST, DETAILED_LIST, GRID; -} \ No newline at end of file +} diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/NavItem.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/NavItem.kt index 97a4f016d..03cb70a93 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/NavItem.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/NavItem.kt @@ -2,9 +2,11 @@ package org.koitharu.kotatsu.core.prefs import androidx.annotation.DrawableRes import androidx.annotation.IdRes +import androidx.annotation.Keep import androidx.annotation.StringRes import org.koitharu.kotatsu.R +@Keep enum class NavItem( @IdRes val id: Int, @StringRes val title: Int, diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/NetworkPolicy.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/NetworkPolicy.kt index fc5556801..cb6d546db 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/NetworkPolicy.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/NetworkPolicy.kt @@ -1,7 +1,9 @@ package org.koitharu.kotatsu.core.prefs import android.net.ConnectivityManager +import androidx.annotation.Keep +@Keep enum class NetworkPolicy( private val key: Int, ) { diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/ProgressIndicatorMode.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/ProgressIndicatorMode.kt index 6bf1da864..ca0d67cdd 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/ProgressIndicatorMode.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/ProgressIndicatorMode.kt @@ -1,5 +1,8 @@ package org.koitharu.kotatsu.core.prefs +import androidx.annotation.Keep + +@Keep enum class ProgressIndicatorMode { NONE, PERCENT_READ, PERCENT_LEFT, CHAPTERS_READ, CHAPTERS_LEFT; diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/ReaderAnimation.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/ReaderAnimation.kt index e95559322..30f9e46c4 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/ReaderAnimation.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/ReaderAnimation.kt @@ -1,5 +1,8 @@ package org.koitharu.kotatsu.core.prefs +import androidx.annotation.Keep + +@Keep enum class ReaderAnimation { // Do not rename this diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/ReaderBackground.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/ReaderBackground.kt index 5422b0322..0f4d7da7e 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/ReaderBackground.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/ReaderBackground.kt @@ -2,11 +2,13 @@ package org.koitharu.kotatsu.core.prefs import android.content.Context import android.view.ContextThemeWrapper +import androidx.annotation.Keep import androidx.core.content.ContextCompat import androidx.core.graphics.drawable.toDrawable import org.koitharu.kotatsu.core.util.ext.getThemeDrawable import com.google.android.material.R as materialR +@Keep enum class ReaderBackground { DEFAULT, LIGHT, DARK, WHITE, BLACK; diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/ReaderMode.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/ReaderMode.kt index 7d9f5fd2f..09904b383 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/ReaderMode.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/ReaderMode.kt @@ -1,5 +1,8 @@ package org.koitharu.kotatsu.core.prefs +import androidx.annotation.Keep + +@Keep enum class ReaderMode(val id: Int) { STANDARD(1), diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/ScreenshotsPolicy.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/ScreenshotsPolicy.kt index cb673e570..eb83f6c29 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/ScreenshotsPolicy.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/ScreenshotsPolicy.kt @@ -1,5 +1,8 @@ package org.koitharu.kotatsu.core.prefs +import androidx.annotation.Keep + +@Keep enum class ScreenshotsPolicy { // Do not rename this diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/SearchSuggestionType.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/SearchSuggestionType.kt index f25ef6f99..c9cb56a33 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/SearchSuggestionType.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/SearchSuggestionType.kt @@ -1,8 +1,10 @@ package org.koitharu.kotatsu.core.prefs +import androidx.annotation.Keep import androidx.annotation.StringRes import org.koitharu.kotatsu.R +@Keep enum class SearchSuggestionType( @StringRes val titleResId: Int, ) { diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/TrackerDownloadStrategy.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/TrackerDownloadStrategy.kt new file mode 100644 index 000000000..fbdddfc61 --- /dev/null +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/TrackerDownloadStrategy.kt @@ -0,0 +1,9 @@ +package org.koitharu.kotatsu.core.prefs + +import androidx.annotation.Keep + +@Keep +enum class TrackerDownloadStrategy { + + DISABLED, DOWNLOADED; +} diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/details/ui/pager/ChaptersPagesViewModel.kt b/app/src/main/kotlin/org/koitharu/kotatsu/details/ui/pager/ChaptersPagesViewModel.kt index 9ef984551..8dca45cf1 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/details/ui/pager/ChaptersPagesViewModel.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/details/ui/pager/ChaptersPagesViewModel.kt @@ -166,8 +166,9 @@ abstract class ChaptersPagesViewModel( fun download(chaptersIds: Set?) { launchJob(Dispatchers.Default) { downloadScheduler.schedule( - requireManga(), - chaptersIds, + manga = requireManga(), + chaptersIds = chaptersIds, + isSilent = false, ) onDownloadStarted.call(Unit) } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/download/ui/worker/DownloadNotificationFactory.kt b/app/src/main/kotlin/org/koitharu/kotatsu/download/ui/worker/DownloadNotificationFactory.kt index 6c0ebd85d..05cdbd931 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/download/ui/worker/DownloadNotificationFactory.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/download/ui/worker/DownloadNotificationFactory.kt @@ -37,7 +37,8 @@ import org.koitharu.kotatsu.search.ui.MangaListActivity import java.util.UUID import com.google.android.material.R as materialR -private const val CHANNEL_ID = "download" +private const val CHANNEL_ID_DEFAULT = "download" +private const val CHANNEL_ID_SILENT = "download_bg" private const val GROUP_ID = "downloads" class DownloadNotificationFactory @AssistedInject constructor( @@ -45,10 +46,11 @@ class DownloadNotificationFactory @AssistedInject constructor( private val workManager: WorkManager, private val coil: ImageLoader, @Assisted private val uuid: UUID, + @Assisted val isSilent: Boolean, ) { private val covers = HashMap() - private val builder = NotificationCompat.Builder(context, CHANNEL_ID) + private val builder = NotificationCompat.Builder(context, if (isSilent) CHANNEL_ID_SILENT else CHANNEL_ID_DEFAULT) private val mutex = Mutex() private val coverWidth = context.resources.getDimensionPixelSize( @@ -106,14 +108,18 @@ class DownloadNotificationFactory @AssistedInject constructor( } init { - createChannel() + createChannels() builder.setOnlyAlertOnce(true) builder.setDefaults(0) - builder.foregroundServiceBehavior = NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE + builder.foregroundServiceBehavior = if (isSilent) { + NotificationCompat.FOREGROUND_SERVICE_DEFERRED + } else { + NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE + } builder.setSilent(true) builder.setGroup(GROUP_ID) builder.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_CHILDREN) - builder.priority = NotificationCompat.PRIORITY_DEFAULT + builder.priority = if (isSilent) NotificationCompat.PRIORITY_MIN else NotificationCompat.PRIORITY_DEFAULT } suspend fun create(state: DownloadState?): Notification = mutex.withLock { @@ -283,20 +289,30 @@ class DownloadNotificationFactory @AssistedInject constructor( }.getOrNull() } - private fun createChannel() { + private fun createChannels() { val manager = NotificationManagerCompat.from(context) - val channel = NotificationChannelCompat.Builder(CHANNEL_ID, NotificationManagerCompat.IMPORTANCE_LOW) - .setName(context.getString(R.string.downloads)) - .setVibrationEnabled(false) - .setLightsEnabled(false) - .setSound(null, null) - .build() - manager.createNotificationChannel(channel) + manager.createNotificationChannel( + NotificationChannelCompat.Builder(CHANNEL_ID_DEFAULT, NotificationManagerCompat.IMPORTANCE_LOW) + .setName(context.getString(R.string.downloads)) + .setVibrationEnabled(false) + .setLightsEnabled(false) + .setSound(null, null) + .build(), + ) + manager.createNotificationChannel( + NotificationChannelCompat.Builder(CHANNEL_ID_SILENT, NotificationManagerCompat.IMPORTANCE_MIN) + .setName(context.getString(R.string.downloads_background)) + .setVibrationEnabled(false) + .setLightsEnabled(false) + .setSound(null, null) + .setShowBadge(false) + .build(), + ) } @AssistedFactory interface Factory { - fun create(uuid: UUID): DownloadNotificationFactory + fun create(uuid: UUID, isSilent: Boolean): DownloadNotificationFactory } } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/download/ui/worker/DownloadWorker.kt b/app/src/main/kotlin/org/koitharu/kotatsu/download/ui/worker/DownloadWorker.kt index f3fc3661a..c678d25d4 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/download/ui/worker/DownloadWorker.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/download/ui/worker/DownloadWorker.kt @@ -104,7 +104,10 @@ class DownloadWorker @AssistedInject constructor( notificationFactoryFactory: DownloadNotificationFactory.Factory, ) : CoroutineWorker(appContext, params) { - private val notificationFactory = notificationFactoryFactory.create(params.id) + private val notificationFactory = notificationFactoryFactory.create( + uuid = params.id, + isSilent = params.inputData.getBoolean(IS_SILENT, false), + ) private val notificationManager = appContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager private val slowdownDispatcher = DownloadSlowdownDispatcher(mangaRepositoryFactory, SLOWDOWN_DELAY) @@ -120,8 +123,7 @@ class DownloadWorker @AssistedInject constructor( setForeground(getForegroundInfo()) val mangaId = inputData.getLong(MANGA_ID, 0L) val manga = mangaDataRepository.findMangaById(mangaId) ?: return Result.failure() - lastPublishedState = DownloadState(manga, isIndeterminate = true) - publishState(DownloadState(manga, isIndeterminate = true)) + publishState(DownloadState(manga = manga, isIndeterminate = true).also { lastPublishedState = it }) val chaptersIds = inputData.getLongArray(CHAPTERS_IDS)?.takeUnless { it.isEmpty() } val downloadedIds = getDoneChapters(manga) return try { @@ -380,7 +382,9 @@ class DownloadWorker @AssistedInject constructor( } val notification = notificationFactory.create(state) if (state.isFinalState) { - notificationManager.notify(id.toString(), id.hashCode(), notification) + if (!notificationFactory.isSilent) { + notificationManager.notify(id.toString(), id.hashCode(), notification) + } } else if (notificationThrottler.throttle()) { notificationManager.notify(id.hashCode(), notification) } else { @@ -426,10 +430,11 @@ class DownloadWorker @AssistedInject constructor( private val settings: AppSettings, ) { - suspend fun schedule(manga: Manga, chaptersIds: Collection?) { + suspend fun schedule(manga: Manga, chaptersIds: Collection?, isSilent: Boolean) { dataRepository.storeManga(manga) val data = Data.Builder() .putLong(MANGA_ID, manga.id) + .putBoolean(IS_SILENT, isSilent) if (!chaptersIds.isNullOrEmpty()) { data.putLongArray(CHAPTERS_IDS, chaptersIds.toLongArray()) } @@ -549,6 +554,7 @@ class DownloadWorker @AssistedInject constructor( const val SLOWDOWN_DELAY = 200L const val MANGA_ID = "manga_id" const val CHAPTERS_IDS = "chapters" + const val IS_SILENT = "silent" const val TAG = "download" } } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/settings/tracker/TrackerSettingsFragment.kt b/app/src/main/kotlin/org/koitharu/kotatsu/settings/tracker/TrackerSettingsFragment.kt index 32e488026..2d6d771e4 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/settings/tracker/TrackerSettingsFragment.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/settings/tracker/TrackerSettingsFragment.kt @@ -11,13 +11,17 @@ import android.view.View import androidx.core.text.buildSpannedString import androidx.core.text.inSpans import androidx.fragment.app.viewModels +import androidx.preference.ListPreference import androidx.preference.MultiSelectListPreference import androidx.preference.Preference import dagger.hilt.android.AndroidEntryPoint import org.koitharu.kotatsu.R import org.koitharu.kotatsu.core.prefs.AppSettings +import org.koitharu.kotatsu.core.prefs.TrackerDownloadStrategy import org.koitharu.kotatsu.core.ui.BasePreferenceFragment import org.koitharu.kotatsu.core.util.ext.observe +import org.koitharu.kotatsu.core.util.ext.setDefaultValueCompat +import org.koitharu.kotatsu.parsers.util.names import org.koitharu.kotatsu.settings.tracker.categories.TrackerCategoriesConfigSheet import org.koitharu.kotatsu.settings.utils.DozeHelper import org.koitharu.kotatsu.settings.utils.MultiSummaryProvider @@ -50,6 +54,10 @@ class TrackerSettingsFragment : } } } + findPreference(AppSettings.KEY_TRACKER_DOWNLOAD)?.run { + entryValues = TrackerDownloadStrategy.entries.names() + setDefaultValueCompat(TrackerDownloadStrategy.DISABLED.name) + } dozeHelper.updatePreference() updateCategoriesEnabled() } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/tracker/work/TrackWorker.kt b/app/src/main/kotlin/org/koitharu/kotatsu/tracker/work/TrackWorker.kt index 54ff5e78d..ddb9a9b8d 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/tracker/work/TrackWorker.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/tracker/work/TrackWorker.kt @@ -25,6 +25,7 @@ import androidx.work.WorkManager import androidx.work.WorkQuery import androidx.work.WorkerParameters import androidx.work.await +import dagger.Lazy import dagger.Reusable import dagger.assisted.Assisted import dagger.assisted.AssistedInject @@ -47,10 +48,14 @@ import org.koitharu.kotatsu.core.exceptions.CloudFlareProtectedException import org.koitharu.kotatsu.core.logs.FileLogger import org.koitharu.kotatsu.core.logs.TrackerLogger import org.koitharu.kotatsu.core.prefs.AppSettings +import org.koitharu.kotatsu.core.prefs.TrackerDownloadStrategy import org.koitharu.kotatsu.core.util.ext.awaitUniqueWorkInfoByName import org.koitharu.kotatsu.core.util.ext.checkNotificationPermission import org.koitharu.kotatsu.core.util.ext.onEachIndexed import org.koitharu.kotatsu.core.util.ext.trySetForeground +import org.koitharu.kotatsu.download.ui.worker.DownloadWorker +import org.koitharu.kotatsu.local.data.LocalMangaRepository +import org.koitharu.kotatsu.parsers.util.mapToSet import org.koitharu.kotatsu.parsers.util.runCatchingCancellable import org.koitharu.kotatsu.parsers.util.toIntUp import org.koitharu.kotatsu.settings.SettingsActivity @@ -76,6 +81,8 @@ class TrackWorker @AssistedInject constructor( private val checkNewChaptersUseCase: CheckNewChaptersUseCase, private val workManager: WorkManager, @TrackerLogger private val logger: FileLogger, + private val localRepositoryLazy: Lazy, + private val downloadSchedulerLazy: Lazy, ) : CoroutineWorker(context, workerParams) { private val notificationManager by lazy { NotificationManagerCompat.from(applicationContext) } @@ -144,12 +151,16 @@ class TrackWorker @AssistedInject constructor( if (applicationContext.checkNotificationPermission(WORKER_CHANNEL_ID)) { notificationManager.notify(WORKER_NOTIFICATION_ID, createWorkerNotification(tracks.size, index + 1)) } - if (it is MangaUpdates.Failure) { - val e = it.error - logger.log("checkUpdatesAsync", e) - if (e is CloudFlareProtectedException) { - CaptchaNotifier(applicationContext).notify(e) + when (it) { + is MangaUpdates.Failure -> { + val e = it.error + logger.log("checkUpdatesAsync", e) + if (e is CloudFlareProtectedException) { + CaptchaNotifier(applicationContext).notify(e) + } } + + is MangaUpdates.Success -> processDownload(it) } }.mapNotNull { when (it) { @@ -237,6 +248,25 @@ class TrackWorker @AssistedInject constructor( } }.build() + private suspend fun processDownload(mangaUpdates: MangaUpdates.Success) { + if (!mangaUpdates.isValid || mangaUpdates.newChapters.isEmpty()) { + return + } + when (settings.trackerDownloadStrategy) { + TrackerDownloadStrategy.DISABLED -> Unit + TrackerDownloadStrategy.DOWNLOADED -> { + val localManga = localRepositoryLazy.get().findSavedManga(mangaUpdates.manga) + if (localManga != null) { + downloadSchedulerLazy.get().schedule( + manga = mangaUpdates.manga, + chaptersIds = mangaUpdates.newChapters.mapToSet { it.id }, + isSilent = true, + ) + } + } + } + } + @Reusable class Scheduler @Inject constructor( private val workManager: WorkManager, diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index 6610849ea..4a397a422 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -109,4 +109,8 @@ @string/chapters_read @string/chapters_left + + @string/never + @string/manga_with_downloaded_chapters + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1a812f9c9..0c7598e05 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -698,4 +698,7 @@ Sign in to set up integration with %s. This will allow you to track your manga reading progress and status Unstable feature This function is experimental. Please make sure you have a backup to avoid data loss + Background downloads + Download new chapters + Manga with downloaded chapters diff --git a/app/src/main/res/xml/pref_tracker.xml b/app/src/main/res/xml/pref_tracker.xml index 37069e2a8..71b113a1e 100644 --- a/app/src/main/res/xml/pref_tracker.xml +++ b/app/src/main/res/xml/pref_tracker.xml @@ -52,6 +52,13 @@ android:summary="@string/disable_nsfw_notifications_summary" android:title="@string/disable_nsfw_notifications" /> + +