feat: Add trigger to sync before library update.

Signed-off-by: KaiserBh <kaiserbh@proton.me>
This commit is contained in:
KaiserBh 2023-12-29 01:56:05 +11:00
parent 4396c012c6
commit 0d117b84c0
No known key found for this signature in database
GPG Key ID: 14D73B142042BBA9
5 changed files with 54 additions and 13 deletions

View File

@ -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,
)

View File

@ -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<LibraryUpdateJob>()
.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<SyncDataJob>()
.addTag(SyncDataJob.TAG_MANUAL)
.build()
// Chain SyncDataJob to run before LibraryUpdateJob
val inputData = workDataOf(KEY_CATEGORY to category?.id)
val libraryUpdateJob = OneTimeWorkRequestBuilder<LibraryUpdateJob>()
.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<LibraryUpdateJob>()
.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))

View File

@ -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)

View File

@ -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<Application>())
@ -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<Application>())

View File

@ -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
}
}