Skip to content

Commit

Permalink
refactor: fix up the sync triggers, and update imports.
Browse files Browse the repository at this point in the history
  • Loading branch information
kaiserbh committed Jan 20, 2024
1 parent 7b3a586 commit bc326f8
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ import androidx.core.net.toUri
import cafe.adriel.voyager.navigator.LocalNavigator
import cafe.adriel.voyager.navigator.currentOrThrow
import com.hippo.unifile.UniFile
import eu.kanade.domain.sync.SyncPreferences
import eu.kanade.presentation.more.settings.Preference
import eu.kanade.presentation.more.settings.screen.data.CreateBackupScreen
import eu.kanade.presentation.more.settings.screen.data.RestoreBackupScreen
import eu.kanade.presentation.more.settings.screen.data.StorageInfo
import eu.kanade.presentation.more.settings.screen.data.SyncOptionsScreen
import eu.kanade.presentation.more.settings.screen.data.SyncSettingsSelector
import eu.kanade.presentation.more.settings.screen.data.SyncTriggerOptionsScreen
import eu.kanade.presentation.more.settings.widget.BasePreferenceWidget
import eu.kanade.presentation.more.settings.widget.PrefsHorizontalPadding
import eu.kanade.presentation.util.relativeTimeSpanString
Expand All @@ -65,7 +66,6 @@ import tachiyomi.core.util.system.logcat
import tachiyomi.domain.backup.service.BackupPreferences
import tachiyomi.domain.library.service.LibraryPreferences
import tachiyomi.domain.storage.service.StoragePreferences
import tachiyomi.domain.sync.SyncPreferences
import tachiyomi.i18n.MR
import tachiyomi.presentation.core.i18n.stringResource
import tachiyomi.presentation.core.util.collectAsState
Expand Down Expand Up @@ -443,21 +443,21 @@ object SettingsDataScreen : SearchableSettings {
Preference.PreferenceItem.EditTextPreference(
title = stringResource(MR.strings.pref_sync_host),
subtitle = stringResource(MR.strings.pref_sync_host_summ),
pref = syncPreferences.syncHost(),
pref = syncPreferences.clientHost(),
onValueChanged = { newValue ->
scope.launch {
// Trim spaces at the beginning and end, then remove trailing slash if present
val trimmedValue = newValue.trim()
val modifiedValue = trimmedValue.trimEnd { it == '/' }
syncPreferences.syncHost().set(modifiedValue)
syncPreferences.clientHost().set(modifiedValue)
}
true
},
),
Preference.PreferenceItem.EditTextPreference(
title = stringResource(MR.strings.pref_sync_api_key),
subtitle = stringResource(MR.strings.pref_sync_api_key_summ),
pref = syncPreferences.syncAPIKey(),
pref = syncPreferences.clientAPIKey(),
),
)
}
Expand Down Expand Up @@ -486,7 +486,7 @@ object SettingsDataScreen : SearchableSettings {
return Preference.PreferenceItem.TextPreference(
title = stringResource(MR.strings.pref_sync_options),
subtitle = stringResource(MR.strings.pref_sync_options_summ),
onClick = { navigator.push(SyncOptionsScreen()) },
onClick = { navigator.push(SyncTriggerOptionsScreen()) },
)
}

Expand Down
13 changes: 5 additions & 8 deletions app/src/main/java/eu/kanade/tachiyomi/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import coil.disk.DiskCache
import coil.util.DebugLogger
import eu.kanade.domain.DomainModule
import eu.kanade.domain.base.BasePreferences
import eu.kanade.domain.sync.SyncPreferences
import eu.kanade.domain.ui.UiPreferences
import eu.kanade.domain.ui.model.setAppCompatDelegateThemeMode
import eu.kanade.tachiyomi.crash.CrashActivity
Expand Down Expand Up @@ -52,7 +53,6 @@ import logcat.LogcatLogger
import org.conscrypt.Conscrypt
import tachiyomi.core.i18n.stringResource
import tachiyomi.core.util.system.logcat
import tachiyomi.domain.sync.SyncPreferences
import tachiyomi.i18n.MR
import tachiyomi.presentation.widget.WidgetManager
import uy.kohesive.injekt.Injekt
Expand Down Expand Up @@ -133,10 +133,8 @@ class App : Application(), DefaultLifecycleObserver, ImageLoaderFactory {
}

val syncPreferences: SyncPreferences by injectLazy()
val syncFlags = syncPreferences.syncFlags().get()
if (syncPreferences.isSyncEnabled() && syncFlags and
SyncPreferences.Flags.SYNC_ON_APP_START == SyncPreferences.Flags.SYNC_ON_APP_START
) {
val syncTriggerOpt = syncPreferences.getSyncTriggerOptions()
if (syncPreferences.isSyncEnabled() && syncTriggerOpt.syncOnAppStart) {
SyncDataJob.startNow(this@App)
}
}
Expand Down Expand Up @@ -174,9 +172,8 @@ class App : Application(), DefaultLifecycleObserver, ImageLoaderFactory {
SecureActivityDelegate.onApplicationStart()

val syncPreferences: SyncPreferences by injectLazy()
val syncFlags = syncPreferences.syncFlags().get()
if (syncPreferences.isSyncEnabled() && syncFlags
and SyncPreferences.Flags.SYNC_ON_APP_RESUME == SyncPreferences.Flags.SYNC_ON_APP_RESUME
val syncTriggerOpt = syncPreferences.getSyncTriggerOptions()
if (syncPreferences.isSyncEnabled() && syncTriggerOpt.syncOnAppResume
) {
SyncDataJob.startNow(this@App)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import androidx.work.workDataOf
import eu.kanade.domain.chapter.interactor.SyncChaptersWithSource
import eu.kanade.domain.manga.interactor.UpdateManga
import eu.kanade.domain.manga.model.toSManga
import eu.kanade.domain.sync.SyncPreferences
import eu.kanade.tachiyomi.data.cache.CoverCache
import eu.kanade.tachiyomi.data.download.DownloadManager
import eu.kanade.tachiyomi.data.notification.Notifications
Expand Down Expand Up @@ -64,7 +65,6 @@ 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
Expand Down Expand Up @@ -480,9 +480,8 @@ class LibraryUpdateJob(private val context: Context, workerParams: WorkerParamet
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
) {
val syncTriggerOpt = syncPreferences.getSyncTriggerOptions()
if (syncPreferences.isSyncEnabled() && syncTriggerOpt.syncOnLibraryUpdate) {
// Check if SyncDataJob is already running
if (wm.isRunning(SyncDataJob.TAG_MANUAL)) {
// SyncDataJob is already running
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package eu.kanade.tachiyomi.di
import android.app.Application
import eu.kanade.domain.base.BasePreferences
import eu.kanade.domain.source.service.SourcePreferences
import eu.kanade.domain.sync.SyncPreferences
import eu.kanade.domain.track.service.TrackPreferences
import eu.kanade.domain.ui.UiPreferences
import eu.kanade.tachiyomi.core.security.SecurityPreferences
Expand All @@ -16,7 +17,6 @@ import tachiyomi.domain.backup.service.BackupPreferences
import tachiyomi.domain.download.service.DownloadPreferences
import tachiyomi.domain.library.service.LibraryPreferences
import tachiyomi.domain.storage.service.StoragePreferences
import tachiyomi.domain.sync.SyncPreferences
import uy.kohesive.injekt.api.InjektModule
import uy.kohesive.injekt.api.InjektRegistrar
import uy.kohesive.injekt.api.addSingletonFactory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import eu.kanade.domain.chapter.model.toDbChapter
import eu.kanade.domain.manga.interactor.SetMangaViewerFlags
import eu.kanade.domain.manga.model.readerOrientation
import eu.kanade.domain.manga.model.readingMode
import eu.kanade.domain.sync.SyncPreferences
import eu.kanade.domain.track.interactor.TrackChapter
import eu.kanade.domain.track.service.TrackPreferences
import eu.kanade.tachiyomi.data.database.models.toDomainChapter
Expand Down Expand Up @@ -73,7 +74,6 @@ import tachiyomi.domain.history.model.HistoryUpdate
import tachiyomi.domain.manga.interactor.GetManga
import tachiyomi.domain.manga.model.Manga
import tachiyomi.domain.source.service.SourceManager
import tachiyomi.domain.sync.SyncPreferences
import tachiyomi.source.local.isLocal
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
Expand Down Expand Up @@ -519,7 +519,8 @@ class ReaderViewModel @JvmOverloads constructor(
*/
private suspend fun updateChapterProgress(readerChapter: ReaderChapter, page: Page) {
val pageIndex = page.index
val syncFlags = syncPreferences.syncFlags().get()
val syncTriggerOpt = syncPreferences.getSyncTriggerOptions()
val isSyncEnabled = syncPreferences.isSyncEnabled()

mutableState.update {
it.copy(currentPage = pageIndex + 1)
Expand All @@ -536,10 +537,7 @@ class ReaderViewModel @JvmOverloads constructor(
deleteChapterIfNeeded(readerChapter)

// Check if syncing is enabled for chapter read:
if (syncPreferences.isSyncEnabled() &&
syncFlags and
SyncPreferences.Flags.SYNC_ON_CHAPTER_READ == SyncPreferences.Flags.SYNC_ON_CHAPTER_READ
) {
if (isSyncEnabled && syncTriggerOpt.syncOnChapterRead) {
SyncDataJob.startNow(Injekt.get<Application>())
}
}
Expand All @@ -553,9 +551,7 @@ class ReaderViewModel @JvmOverloads constructor(
)

// Check if syncing is enabled for chapter open:
if (syncPreferences.isSyncEnabled() &&
syncFlags and SyncPreferences.Flags.SYNC_ON_CHAPTER_OPEN == SyncPreferences.Flags.SYNC_ON_CHAPTER_OPEN
) {
if (isSyncEnabled && syncTriggerOpt.syncOnChapterOpen && readerChapter.chapter.last_page_read == 0) {
SyncDataJob.startNow(Injekt.get<Application>())
}
}
Expand Down
3 changes: 2 additions & 1 deletion i18n/src/commonMain/resources/MR/base/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<string name="label_data_storage">Data and storage</string>
<string name="label_backup">Backup</string>
<string name="label_sync">Sync</string>
<string name="label_triggers">Triggers</string>
<string name="label_stats">Statistics</string>
<string name="label_migration">Migrate</string>
<string name="label_extensions">Extensions</string>
Expand Down Expand Up @@ -586,7 +587,7 @@
<string name="pref_sync_options">Create sync triggers</string>
<string name="pref_sync_options_summ">Can be used to set sync triggers</string>
<string name="sync_on_chapter_read">Sync on Chapter Read</string>
<string name="sync_on_chapter_open">Sync on Every Chapter Page</string>
<string name="sync_on_chapter_open">Sync on Chapter Open</string>
<string name="sync_on_app_start">Sync on App Start</string>
<string name="sync_on_app_resume">Sync on App Resume</string>
<string name="sync_on_library_update">Sync on Library Update</string>
Expand Down

0 comments on commit bc326f8

Please sign in to comment.