diff --git a/app/src/main/java/eu/kanade/presentation/more/settings/screen/data/SyncOptionsScreen.kt b/app/src/main/java/eu/kanade/presentation/more/settings/screen/data/SyncOptionsScreen.kt index e810d1c75..95d00e270 100644 --- a/app/src/main/java/eu/kanade/presentation/more/settings/screen/data/SyncOptionsScreen.kt +++ b/app/src/main/java/eu/kanade/presentation/more/settings/screen/data/SyncOptionsScreen.kt @@ -120,4 +120,6 @@ private val SyncChoices = mapOf( SyncPreferences.Flags.SYNC_ON_CHAPTER_READ to MR.strings.sync_on_chapter_read, SyncPreferences.Flags.SYNC_ON_CHAPTER_OPEN to MR.strings.sync_on_chapter_open, SyncPreferences.Flags.SYNC_ON_APP_START to MR.strings.sync_on_app_start, + SyncPreferences.Flags.SYNC_ON_APP_RESUME to MR.strings.sync_on_app_resume, + SyncPreferences.Flags.SYNC_ON_LIBRARY_UPDATE to MR.strings.sync_on_library_update, ) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateJob.kt b/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateJob.kt index c7a714a01..07bcd7568 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateJob.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateJob.kt @@ -22,6 +22,7 @@ import eu.kanade.domain.manga.model.toSManga import eu.kanade.tachiyomi.data.cache.CoverCache import eu.kanade.tachiyomi.data.download.DownloadManager import eu.kanade.tachiyomi.data.notification.Notifications +import eu.kanade.tachiyomi.data.sync.SyncDataJob import eu.kanade.tachiyomi.source.UnmeteredSource import eu.kanade.tachiyomi.source.model.SManga import eu.kanade.tachiyomi.source.model.UpdateStrategy @@ -65,6 +66,7 @@ import tachiyomi.domain.manga.interactor.GetManga import tachiyomi.domain.manga.model.Manga import tachiyomi.domain.source.model.SourceNotInstalledException import tachiyomi.domain.source.service.SourceManager +import tachiyomi.domain.sync.SyncPreferences import tachiyomi.i18n.MR import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get @@ -478,24 +480,55 @@ class LibraryUpdateJob(private val context: Context, workerParams: WorkerParamet category: Category? = null, ): Boolean { val wm = context.workManager + // Check if the LibraryUpdateJob is already running if (wm.isRunning(TAG)) { // Already running either as a scheduled or manual job return false } - val inputData = workDataOf( - KEY_CATEGORY to category?.id, - ) - val request = OneTimeWorkRequestBuilder() - .addTag(TAG) - .addTag(WORK_NAME_MANUAL) - .setInputData(inputData) - .build() - wm.enqueueUniqueWork(WORK_NAME_MANUAL, ExistingWorkPolicy.KEEP, request) + val syncPreferences: SyncPreferences = Injekt.get() + + // Only proceed with SyncDataJob if sync is enabled and the specific sync on library update flag is set + if (syncPreferences.isSyncEnabled() && syncPreferences.syncFlags().get() and SyncPreferences.Flags.SYNC_ON_LIBRARY_UPDATE == SyncPreferences.Flags.SYNC_ON_LIBRARY_UPDATE) { + + // Check if SyncDataJob is already running + if (wm.isRunning(SyncDataJob.TAG_MANUAL)) { + // SyncDataJob is already running + return false + } + + // Define the SyncDataJob + val syncDataJob = OneTimeWorkRequestBuilder() + .addTag(SyncDataJob.TAG_MANUAL) + .build() + + // Chain SyncDataJob to run before LibraryUpdateJob + val inputData = workDataOf(KEY_CATEGORY to category?.id) + val libraryUpdateJob = OneTimeWorkRequestBuilder() + .addTag(TAG) + .addTag(WORK_NAME_MANUAL) + .setInputData(inputData) + .build() + + wm.beginUniqueWork(WORK_NAME_MANUAL, ExistingWorkPolicy.KEEP, syncDataJob) + .then(libraryUpdateJob) + .enqueue() + + } else { + val inputData = workDataOf(KEY_CATEGORY to category?.id) + val request = OneTimeWorkRequestBuilder() + .addTag(TAG) + .addTag(WORK_NAME_MANUAL) + .setInputData(inputData) + .build() + + wm.enqueueUniqueWork(WORK_NAME_MANUAL, ExistingWorkPolicy.KEEP, request) + } return true } + fun stop(context: Context) { val wm = context.workManager val workQuery = WorkQuery.Builder.fromTags(listOf(TAG)) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/sync/SyncDataJob.kt b/app/src/main/java/eu/kanade/tachiyomi/data/sync/SyncDataJob.kt index 3ea2551a0..f85d57034 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/sync/SyncDataJob.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/sync/SyncDataJob.kt @@ -60,7 +60,7 @@ class SyncDataJob(private val context: Context, workerParams: WorkerParameters) companion object { private const val TAG_JOB = "SyncDataJob" private const val TAG_AUTO = "$TAG_JOB:auto" - private const val TAG_MANUAL = "$TAG_JOB:manual" + const val TAG_MANUAL = "$TAG_JOB:manual" private val jobTagList = listOf(TAG_AUTO, TAG_MANUAL) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderViewModel.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderViewModel.kt index 557b34c4b..22c9c3b4e 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderViewModel.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderViewModel.kt @@ -533,7 +533,7 @@ class ReaderViewModel @JvmOverloads constructor( deleteChapterIfNeeded(readerChapter) // Check if syncing is enabled for chapter read: - if (syncPreferences.syncService().get() != 0 && + if (syncPreferences.isSyncEnabled() && syncFlags and SyncPreferences.Flags.SYNC_ON_CHAPTER_READ == SyncPreferences.Flags.SYNC_ON_CHAPTER_READ ) { SyncDataJob.startNow(Injekt.get()) @@ -549,7 +549,7 @@ class ReaderViewModel @JvmOverloads constructor( ) // Check if syncing is enabled for chapter open: - if (syncPreferences.syncService().get() != 0 && + if (syncPreferences.isSyncEnabled() && syncFlags and SyncPreferences.Flags.SYNC_ON_CHAPTER_OPEN == SyncPreferences.Flags.SYNC_ON_CHAPTER_OPEN ) { SyncDataJob.startNow(Injekt.get()) diff --git a/domain/src/main/java/tachiyomi/domain/sync/SyncPreferences.kt b/domain/src/main/java/tachiyomi/domain/sync/SyncPreferences.kt index 509124a9e..6e39496f7 100644 --- a/domain/src/main/java/tachiyomi/domain/sync/SyncPreferences.kt +++ b/domain/src/main/java/tachiyomi/domain/sync/SyncPreferences.kt @@ -11,10 +11,12 @@ class SyncPreferences( const val SYNC_ON_CHAPTER_READ = 0x1 const val SYNC_ON_CHAPTER_OPEN = 0x2 const val SYNC_ON_APP_START = 0x4 + const val SYNC_ON_APP_RESUME = 0x8 + const val SYNC_ON_LIBRARY_UPDATE = 0x10 const val Defaults = NONE - fun values() = listOf(NONE, SYNC_ON_CHAPTER_READ, SYNC_ON_CHAPTER_OPEN, SYNC_ON_APP_START) + fun values() = listOf(NONE, SYNC_ON_CHAPTER_READ, SYNC_ON_CHAPTER_OPEN, SYNC_ON_APP_START, SYNC_ON_APP_RESUME, SYNC_ON_LIBRARY_UPDATE) } fun syncHost() = preferenceStore.getString("sync_host", "https://sync.tachiyomi.org") @@ -35,4 +37,8 @@ class SyncPreferences( ) fun syncFlags() = preferenceStore.getInt("sync_flags", Flags.Defaults) + + fun isSyncEnabled(): Boolean { + return syncService().get() != 0 + } }