Skip to content

Commit

Permalink
Do not sync automatically when not connected to a network.
Browse files Browse the repository at this point in the history
  • Loading branch information
NGB-Was-Taken authored Nov 2, 2024
1 parent ffc1e2d commit a4faad6
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ object SettingsDataScreen : SearchableSettings {
subtitle = stringResource(SYMR.strings.pref_sync_now_subtitle),
onClick = {
if (!SyncDataJob.isRunning(context)) {
SyncDataJob.startNow(context)
SyncDataJob.startNow(context, manual = true)
} else {
context.toast(SYMR.strings.sync_in_progress)
}
Expand Down
11 changes: 8 additions & 3 deletions app/src/main/java/eu/kanade/tachiyomi/data/sync/SyncDataJob.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import androidx.work.WorkerParameters
import eu.kanade.domain.sync.SyncPreferences
import eu.kanade.tachiyomi.data.notification.Notifications
import eu.kanade.tachiyomi.util.system.cancelNotification
import eu.kanade.tachiyomi.util.system.isOnline
import eu.kanade.tachiyomi.util.system.isRunning
import eu.kanade.tachiyomi.util.system.setForegroundSafely
import eu.kanade.tachiyomi.util.system.workManager
Expand All @@ -31,6 +32,9 @@ class SyncDataJob(private val context: Context, workerParams: WorkerParameters)

override suspend fun doWork(): Result {
if (tags.contains(TAG_AUTO)) {
if (!context.isOnline()) {
return Result.retry()
}
// Find a running manual worker. If exists, try again later
if (context.workManager.isRunning(TAG_MANUAL)) {
return Result.retry()
Expand Down Expand Up @@ -93,17 +97,18 @@ class SyncDataJob(private val context: Context, workerParams: WorkerParameters)
}
}

fun startNow(context: Context) {
fun startNow(context: Context, manual: Boolean = false) {
val wm = context.workManager
if (wm.isRunning(TAG_JOB)) {
// Already running either as a scheduled or manual job
return
}
val tag = if (manual) TAG_MANUAL else TAG_AUTO
val request = OneTimeWorkRequestBuilder<SyncDataJob>()
.addTag(TAG_JOB)
.addTag(TAG_MANUAL)
.addTag(tag)
.build()
context.workManager.enqueueUniqueWork(TAG_MANUAL, ExistingWorkPolicy.KEEP, request)
context.workManager.enqueueUniqueWork(tag, ExistingWorkPolicy.KEEP, request)
}

fun stop(context: Context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ object LibraryTab : Tab {
},
onClickSyncNow = {
if (!SyncDataJob.isRunning(context)) {
SyncDataJob.startNow(context)
SyncDataJob.startNow(context, manual = true)
} else {
context.toast(SYMR.strings.sync_in_progress)
}
Expand Down

0 comments on commit a4faad6

Please sign in to comment.