From 1aa5222c99f50543c47d5f3bd4bb892e16e31efb Mon Sep 17 00:00:00 2001 From: arkon Date: Sun, 29 Oct 2023 12:03:46 -0400 Subject: [PATCH] Record time when last automatic backup was created Closes #3474 --- .../more/settings/screen/SettingsDataScreen.kt | 7 ++++++- .../eu/kanade/tachiyomi/data/backup/BackupCreateJob.kt | 7 ++++++- .../tachiyomi/domain/backup/service/BackupPreferences.kt | 3 +++ i18n/src/main/res/values/strings.xml | 1 + 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/eu/kanade/presentation/more/settings/screen/SettingsDataScreen.kt b/app/src/main/java/eu/kanade/presentation/more/settings/screen/SettingsDataScreen.kt index 75c1cf4675..1f21a3261b 100644 --- a/app/src/main/java/eu/kanade/presentation/more/settings/screen/SettingsDataScreen.kt +++ b/app/src/main/java/eu/kanade/presentation/more/settings/screen/SettingsDataScreen.kt @@ -33,6 +33,7 @@ import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource import eu.kanade.presentation.more.settings.Preference import eu.kanade.presentation.permissions.PermissionRequestHelper +import eu.kanade.presentation.util.relativeTimeSpanString import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.backup.BackupConst import eu.kanade.tachiyomi.data.backup.BackupCreateJob @@ -82,6 +83,7 @@ object SettingsDataScreen : SearchableSettings { val context = LocalContext.current val backupIntervalPref = backupPreferences.backupInterval() val backupInterval by backupIntervalPref.collectAsState() + val lastAutoBackup by backupPreferences.lastAutoBackupTimestamp().collectAsState() return Preference.PreferenceGroup( title = stringResource(R.string.label_backup), @@ -113,7 +115,10 @@ object SettingsDataScreen : SearchableSettings { title = stringResource(R.string.pref_backup_slots), entries = listOf(2, 3, 4, 5).associateWith { it.toString() }, ), - Preference.PreferenceItem.InfoPreference(stringResource(R.string.backup_info)), + Preference.PreferenceItem.InfoPreference( + stringResource(R.string.backup_info) + "\n\n" + + stringResource(R.string.last_auto_backup_info, relativeTimeSpanString(lastAutoBackup)), + ), ), ) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupCreateJob.kt b/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupCreateJob.kt index 875039e864..d46a58e00c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupCreateJob.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupCreateJob.kt @@ -23,6 +23,7 @@ import tachiyomi.core.util.system.logcat import tachiyomi.domain.backup.service.BackupPreferences import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get +import java.util.Date import java.util.concurrent.TimeUnit import kotlin.time.Duration.Companion.minutes import kotlin.time.toJavaDuration @@ -50,7 +51,11 @@ class BackupCreateJob(private val context: Context, workerParams: WorkerParamete return try { val location = BackupCreator(context).createBackup(uri, flags, isAutoBackup) - if (!isAutoBackup) notifier.showBackupComplete(UniFile.fromUri(context, location.toUri())) + if (isAutoBackup) { + backupPreferences.lastAutoBackupTimestamp().set(Date().time) + } else { + notifier.showBackupComplete(UniFile.fromUri(context, location.toUri())) + } Result.success() } catch (e: Exception) { logcat(LogPriority.ERROR, e) diff --git a/domain/src/main/java/tachiyomi/domain/backup/service/BackupPreferences.kt b/domain/src/main/java/tachiyomi/domain/backup/service/BackupPreferences.kt index 0f6616338f..e0f96dffab 100644 --- a/domain/src/main/java/tachiyomi/domain/backup/service/BackupPreferences.kt +++ b/domain/src/main/java/tachiyomi/domain/backup/service/BackupPreferences.kt @@ -13,4 +13,7 @@ class BackupPreferences( fun numberOfBackups() = preferenceStore.getInt("backup_slots", 2) fun backupInterval() = preferenceStore.getInt("backup_interval", 12) + + // TODO: move this and other "app state" preferences elsewhere and exclude from backups + fun lastAutoBackupTimestamp() = preferenceStore.getLong("__APP_STATE_last_auto_backup_timestamp", 0L) } diff --git a/i18n/src/main/res/values/strings.xml b/i18n/src/main/res/values/strings.xml index 7085c9d4c3..6e33f1f7f2 100644 --- a/i18n/src/main/res/values/strings.xml +++ b/i18n/src/main/res/values/strings.xml @@ -505,6 +505,7 @@ Restoring backup failed Canceled restore You should keep copies of backups in other places as well. Backups may contain sensitive data including any stored passwords; be careful if sharing. + Last automatically backed up: %s Syncing library