Automatically backup data before updating

This commit is contained in:
FooIbar committed 2024-04-16 00:08:30 +08:00
1 parent b39e24467c
commit e0f16f31e6
6 files changed
+22 -3

No files matched your search

@@ -32,6 +32,7 @@ import com.hippo.ehviewer.dao.QuickSearch
import com.hippo.ehviewer.dao.Schema17to18
import com.hippo.ehviewer.download.DownloadManager
import com.hippo.ehviewer.util.sendTo
import com.hippo.unifile.UniFile
import com.hippo.unifile.asUniFile
import kotlinx.coroutines.flow.Flow
import splitties.arch.room.roomDb
@@ -260,10 +261,10 @@ object EhDB {
db.filterDao().update(filter)
}
fun exportDB(context: Context, uri: Uri) {
fun exportDB(context: Context, file: UniFile) {
db.query("PRAGMA wal_checkpoint(FULL)", null).use { it.moveToNext() }
val dbFile = context.getDatabasePath(DB_NAME)
dbFile.asUniFile() sendTo uri.asUniFile()
dbFile.asUniFile() sendTo file
}
suspend fun importDB(context: Context, uri: Uri) {
@@ -143,6 +143,7 @@ object Settings : DataStorePreferences(null) {
var showTagTranslations by boolPref(KEY_SHOW_TAG_TRANSLATIONS, false).observed { updateWhenTagTranslationChanges() }
var appLinkVerifyTip by boolPref("app_link_verify_tip", false)
var enabledSecurity by boolPref("enable_secure", false)
var backupBeforeUpdate by boolPref("backup_before_update", true)
var useCIUpdateChannel by boolPref("ci_update_channel", false)
var mediaScan by boolPref("media_scan", false).observed { updateWhenKeepMediaStatusChanges() }
var hasDefaultDownloadLabel by boolPref("has_default_download_label", false)
@@ -30,8 +30,10 @@ import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.fromHtml
import androidx.compose.ui.unit.dp
import com.hippo.ehviewer.BuildConfig
import com.hippo.ehviewer.EhDB
import com.hippo.ehviewer.R
import com.hippo.ehviewer.Settings
import com.hippo.ehviewer.download.downloadLocation
import com.hippo.ehviewer.ui.destinations.LicenseScreenDestination
import com.hippo.ehviewer.ui.tools.DialogState
import com.hippo.ehviewer.ui.tools.LocalDialogState
@@ -39,6 +41,7 @@ import com.hippo.ehviewer.ui.tools.observed
import com.hippo.ehviewer.updater.AppUpdater
import com.hippo.ehviewer.updater.Release
import com.hippo.ehviewer.util.AppConfig
import com.hippo.ehviewer.util.ReadableTime
import com.hippo.ehviewer.util.displayString
import com.hippo.ehviewer.util.installPackage
import com.ramcosta.composedestinations.annotation.Destination
@@ -48,6 +51,7 @@ import eu.kanade.tachiyomi.util.lang.withUIContext
import java.io.File
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.datetime.Clock
import moe.tarsin.coroutines.runSuspendCatching
private const val REPO_URL = "https://github.com/${BuildConfig.REPO_NAME}"
@@ -108,6 +112,10 @@ fun AboutScreen(navigator: DestinationsNavigator) {
title = stringResource(id = R.string.settings_about_version),
summary = versionCode(),
)
SwitchPreference(
title = stringResource(id = R.string.backup_before_update),
value = Settings::backupBeforeUpdate,
)
SwitchPreference(
title = stringResource(id = R.string.use_ci_update_channel),
value = Settings::useCIUpdateChannel,
@@ -145,6 +153,12 @@ suspend fun DialogState.showNewVersion(context: Context, release: Release) {
Text(text = release.changelog)
}
}
if (Settings.backupBeforeUpdate) {
val time = ReadableTime.getFilenamableTime(Clock.System.now().toEpochMilliseconds())
downloadLocation.createFile("$time.db")?.let {
EhDB.exportDB(context, it)
}
}
// TODO: Download in the background and show progress in notification
val file = File(AppConfig.tempDir, "update.apk").apply { delete() }
AppUpdater.downloadUpdate(release.downloadLink, file)
@@ -43,6 +43,7 @@ import com.hippo.ehviewer.ui.tools.observed
import com.hippo.ehviewer.util.AppConfig
import com.hippo.ehviewer.util.Crash
import com.hippo.ehviewer.util.ReadableTime
import com.hippo.unifile.asUniFile
import com.jamal.composeprefs3.ui.prefs.SwitchPref
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.annotation.RootGraph
@@ -181,7 +182,7 @@ fun AdvancedScreen(navigator: DestinationsNavigator) {
uri?.let {
context.runCatching {
grantUriPermission(BuildConfig.APPLICATION_ID, uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
EhDB.exportDB(context, uri)
EhDB.exportDB(context, uri.asUniFile())
launchSnackBar(getString(R.string.settings_advanced_export_data_to, uri.toString()))
}.onFailure {
logcat(it)
@@ -424,6 +424,7 @@
<string name="favorite_name">收藏画廊</string>
<string name="black_dark_theme">纯黑深色主题</string>
<string name="harmonize_category_color">类别颜色适配动态配色</string>
<string name="backup_before_update">更新前备份数据</string>
<string name="use_ci_update_channel">使用持续集成(CI)构建频道更新</string>
<string name="sort_by">排序依据</string>
<string name="added_time_desc">添加时间(降序)</string>
+1
View File
@@ -519,6 +519,7 @@
<string name="app_link_not_verified_message">For Android 12 and newer, you need to manually add link to verified links in order to open E-Hentai links in EhViewer.</string>
<string name="app_link_not_verified_title">App links not verified</string>
<string name="open_by_default">Open by default</string>
<string name="backup_before_update">Backup data before update</string>
<string name="use_ci_update_channel">Use CI update channel</string>
<string name="settings_privacy_require_unlock">Require Unlock</string>
<string name="settings_privacy_require_unlock_delay">Lock Delay</string>