Refactor: Replace Certain SyncPreferences with appStateKey

- Revised several fields to utilize `appStateKey` for enhanced state management.
- Modified notification behavior:
  - Removed frequent 'sync completed' notifications to reduce redundancy.
  - Notifications now trigger only on sync errors, streamlining user alerts.

- Users should refer to the 'last sync timestamp' to verify successful synchronizations.
- The timestamp updates only when syncs complete successfully, providing a reliable success indicator.

Signed-off-by: KaiserBh <kaiserbh@proton.me>
This commit is contained in:
KaiserBh 2023-11-14 14:04:05 +11:00
parent dac701bb35
commit a08a815022
No known key found for this signature in database
GPG Key ID: 14D73B142042BBA9
4 changed files with 13 additions and 16 deletions

View File

@ -492,7 +492,7 @@ private fun getSyncNowPref(): Preference.PreferenceGroup {
private fun getAutomaticSyncGroup(syncPreferences: SyncPreferences): Preference.PreferenceGroup { private fun getAutomaticSyncGroup(syncPreferences: SyncPreferences): Preference.PreferenceGroup {
val context = LocalContext.current val context = LocalContext.current
val syncIntervalPref = syncPreferences.syncInterval() val syncIntervalPref = syncPreferences.syncInterval()
val lastSync by syncPreferences.syncLastSync().collectAsState() val lastSync by syncPreferences.lastSyncTimestamp().collectAsState()
return Preference.PreferenceGroup( return Preference.PreferenceGroup(
title = stringResource(R.string.pref_sync_service_category), title = stringResource(R.string.pref_sync_service_category),
@ -516,7 +516,7 @@ private fun getAutomaticSyncGroup(syncPreferences: SyncPreferences): Preference.
true true
}, },
), ),
Preference.PreferenceItem.InfoPreference(stringResource(R.string.last_synchronization, relativeTimeSpanString(lastSync.toEpochMilli()))), Preference.PreferenceItem.InfoPreference(stringResource(R.string.last_synchronization, relativeTimeSpanString(lastSync))),
), ),
) )
} }

View File

@ -36,6 +36,7 @@ import tachiyomi.domain.history.model.HistoryUpdate
import tachiyomi.domain.library.service.LibraryPreferences import tachiyomi.domain.library.service.LibraryPreferences
import tachiyomi.domain.manga.interactor.FetchInterval import tachiyomi.domain.manga.interactor.FetchInterval
import tachiyomi.domain.manga.model.Manga import tachiyomi.domain.manga.model.Manga
import tachiyomi.domain.sync.SyncPreferences
import tachiyomi.domain.track.model.Track import tachiyomi.domain.track.model.Track
import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get import uy.kohesive.injekt.api.get
@ -59,6 +60,7 @@ class BackupRestorer(
private val preferenceStore: PreferenceStore = Injekt.get() private val preferenceStore: PreferenceStore = Injekt.get()
private val libraryPreferences: LibraryPreferences = Injekt.get() private val libraryPreferences: LibraryPreferences = Injekt.get()
private val syncPreferences: SyncPreferences = Injekt.get()
private var now = ZonedDateTime.now() private var now = ZonedDateTime.now()
private var currentFetchWindow = fetchInterval.getWindow(now) private var currentFetchWindow = fetchInterval.getWindow(now)
@ -88,13 +90,7 @@ class BackupRestorer(
val logFile = writeErrorLog() val logFile = writeErrorLog()
if (sync) { if (sync) {
notifier.showRestoreComplete( syncPreferences.lastSyncTimestamp().set(Date().time)
time,
errors.size,
logFile.parent,
logFile.name,
contentTitle = context.getString(R.string.library_sync_complete),
)
} else { } else {
notifier.showRestoreComplete(time, errors.size, logFile.parent, logFile.name) notifier.showRestoreComplete(time, errors.size, logFile.parent, logFile.name)
} }

View File

@ -34,6 +34,7 @@ import java.io.File
import java.io.FileOutputStream import java.io.FileOutputStream
import java.io.IOException import java.io.IOException
import java.time.Instant import java.time.Instant
import java.util.Date
/** /**
* A manager to handle synchronization tasks in the app, such as updating * A manager to handle synchronization tasks in the app, such as updating
@ -139,7 +140,8 @@ class SyncManager(
// It's local sync no need to restore data. (just update remote data) // It's local sync no need to restore data. (just update remote data)
if (filteredFavorites.isEmpty()) { if (filteredFavorites.isEmpty()) {
backupNotify.showRestoreComplete(0, 0, "", "", contentTitle = context.getString(R.string.sync_complete)) // update the sync timestamp
syncPreferences.lastSyncTimestamp().set(Date().time)
return return
} }
@ -147,7 +149,6 @@ class SyncManager(
logcat(LogPriority.DEBUG) { "Got Backup Uri: $backupUri" } logcat(LogPriority.DEBUG) { "Got Backup Uri: $backupUri" }
if (backupUri != null) { if (backupUri != null) {
BackupRestoreJob.start(context, backupUri, sync = true) BackupRestoreJob.start(context, backupUri, sync = true)
syncPreferences.syncLastSync().set(Instant.now())
} else { } else {
logcat(LogPriority.ERROR) { "Failed to write sync data to file" } logcat(LogPriority.ERROR) { "Failed to write sync data to file" }
} }

View File

@ -1,25 +1,25 @@
package tachiyomi.domain.sync package tachiyomi.domain.sync
import tachiyomi.core.preference.PreferenceStore import tachiyomi.core.preference.PreferenceStore
import java.time.Instant import tachiyomi.core.preference.Preference
class SyncPreferences( class SyncPreferences(
private val preferenceStore: PreferenceStore, private val preferenceStore: PreferenceStore,
) { ) {
fun syncHost() = preferenceStore.getString("sync_host", "https://sync.tachiyomi.org") fun syncHost() = preferenceStore.getString("sync_host", "https://sync.tachiyomi.org")
fun syncAPIKey() = preferenceStore.getString("sync_api_key", "") fun syncAPIKey() = preferenceStore.getString("sync_api_key", "")
fun syncLastSync() = preferenceStore.getInstant("sync_last_sync", Instant.EPOCH) fun lastSyncTimestamp() = preferenceStore.getLong(Preference.appStateKey("last_sync_timestamp"), 0L)
fun syncInterval() = preferenceStore.getInt("sync_interval", 0) fun syncInterval() = preferenceStore.getInt("sync_interval", 0)
fun deviceName() = preferenceStore.getString( fun deviceName() = preferenceStore.getString(
"device_name", Preference.appStateKey("device_name"),
android.os.Build.MANUFACTURER + android.os.Build.PRODUCT, android.os.Build.MANUFACTURER + android.os.Build.PRODUCT,
) )
fun syncService() = preferenceStore.getInt("sync_service", 0) fun syncService() = preferenceStore.getInt("sync_service", 0)
private fun googleDriveAccessToken() = preferenceStore.getString("google_drive_access_token", "") private fun googleDriveAccessToken() = preferenceStore.getString(Preference.appStateKey("google_drive_access_token"), "")
fun setGoogleDriveAccessToken(accessToken: String) { fun setGoogleDriveAccessToken(accessToken: String) {
googleDriveAccessToken().set(accessToken) googleDriveAccessToken().set(accessToken)
@ -27,7 +27,7 @@ class SyncPreferences(
fun getGoogleDriveAccessToken() = googleDriveAccessToken().get() fun getGoogleDriveAccessToken() = googleDriveAccessToken().get()
private fun googleDriveRefreshToken() = preferenceStore.getString("google_drive_refresh_token", "") private fun googleDriveRefreshToken() = preferenceStore.getString(Preference.appStateKey("google_drive_refresh_token"), "")
fun setGoogleDriveRefreshToken(refreshToken: String) { fun setGoogleDriveRefreshToken(refreshToken: String) {
googleDriveRefreshToken().set(refreshToken) googleDriveRefreshToken().set(refreshToken)