Split backup preferences from PreferencesHelper (#8051)

This commit is contained in:
Andreas 2022-09-22 14:57:42 +02:00 committed by GitHub
parent b37b3767f3
commit 5cdcc1679f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 76 additions and 33 deletions

View File

@ -0,0 +1,16 @@
package eu.kanade.domain.backup.service
import eu.kanade.tachiyomi.core.preference.PreferenceStore
import eu.kanade.tachiyomi.core.provider.FolderProvider
class BackupPreferences(
private val folderProvider: FolderProvider,
private val preferenceStore: PreferenceStore,
) {
fun backupsDirectory() = preferenceStore.getString("backup_directory", folderProvider.path())
fun numberOfBackups() = preferenceStore.getInt("backup_slots", 2)
fun backupInterval() = preferenceStore.getInt("backup_interval", 12)
}

View File

@ -13,12 +13,14 @@ import eu.kanade.data.AndroidDatabaseHandler
import eu.kanade.data.DatabaseHandler import eu.kanade.data.DatabaseHandler
import eu.kanade.data.dateAdapter import eu.kanade.data.dateAdapter
import eu.kanade.data.listOfStringsAdapter import eu.kanade.data.listOfStringsAdapter
import eu.kanade.domain.backup.service.BackupPreferences
import eu.kanade.domain.download.service.DownloadPreferences import eu.kanade.domain.download.service.DownloadPreferences
import eu.kanade.domain.library.service.LibraryPreferences import eu.kanade.domain.library.service.LibraryPreferences
import eu.kanade.domain.source.service.SourcePreferences import eu.kanade.domain.source.service.SourcePreferences
import eu.kanade.domain.track.service.TrackPreferences import eu.kanade.domain.track.service.TrackPreferences
import eu.kanade.tachiyomi.core.preference.AndroidPreferenceStore import eu.kanade.tachiyomi.core.preference.AndroidPreferenceStore
import eu.kanade.tachiyomi.core.preference.PreferenceStore import eu.kanade.tachiyomi.core.preference.PreferenceStore
import eu.kanade.tachiyomi.core.provider.AndroidBackupFolderProvider
import eu.kanade.tachiyomi.core.provider.AndroidDownloadFolderProvider import eu.kanade.tachiyomi.core.provider.AndroidDownloadFolderProvider
import eu.kanade.tachiyomi.core.security.SecurityPreferences import eu.kanade.tachiyomi.core.security.SecurityPreferences
import eu.kanade.tachiyomi.data.cache.ChapterCache import eu.kanade.tachiyomi.data.cache.ChapterCache
@ -170,6 +172,15 @@ class PreferenceModule(val application: Application) : InjektModule {
preferenceStore = get(), preferenceStore = get(),
) )
} }
addSingletonFactory {
AndroidBackupFolderProvider(application)
}
addSingletonFactory {
BackupPreferences(
folderProvider = get<AndroidBackupFolderProvider>(),
preferenceStore = get(),
)
}
addSingletonFactory { addSingletonFactory {
PreferencesHelper( PreferencesHelper(
context = application, context = application,

View File

@ -4,6 +4,7 @@ import android.content.Context
import android.os.Build import android.os.Build
import androidx.core.content.edit import androidx.core.content.edit
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import eu.kanade.domain.backup.service.BackupPreferences
import eu.kanade.domain.library.service.LibraryPreferences import eu.kanade.domain.library.service.LibraryPreferences
import eu.kanade.domain.source.service.SourcePreferences import eu.kanade.domain.source.service.SourcePreferences
import eu.kanade.tachiyomi.core.security.SecurityPreferences import eu.kanade.tachiyomi.core.security.SecurityPreferences
@ -44,6 +45,7 @@ object Migrations {
securityPreferences: SecurityPreferences, securityPreferences: SecurityPreferences,
libraryPreferences: LibraryPreferences, libraryPreferences: LibraryPreferences,
readerPreferences: ReaderPreferences, readerPreferences: ReaderPreferences,
backupPreferences: BackupPreferences,
): Boolean { ): Boolean {
val oldVersion = preferences.lastVersionCode().get() val oldVersion = preferences.lastVersionCode().get()
if (oldVersion < BuildConfig.VERSION_CODE) { if (oldVersion < BuildConfig.VERSION_CODE) {
@ -296,11 +298,11 @@ object Migrations {
} }
} }
if (oldVersion < 84) { if (oldVersion < 84) {
if (preferences.numberOfBackups().get() == 1) { if (backupPreferences.numberOfBackups().get() == 1) {
preferences.numberOfBackups().set(2) backupPreferences.numberOfBackups().set(2)
} }
if (preferences.backupInterval().get() == 0) { if (backupPreferences.backupInterval().get() == 0) {
preferences.backupInterval().set(12) backupPreferences.backupInterval().set(12)
BackupCreatorJob.setupTask(context) BackupCreatorJob.setupTask(context)
} }
} }

View File

@ -13,8 +13,8 @@ import androidx.work.WorkManager
import androidx.work.WorkerParameters import androidx.work.WorkerParameters
import androidx.work.workDataOf import androidx.work.workDataOf
import com.hippo.unifile.UniFile import com.hippo.unifile.UniFile
import eu.kanade.domain.backup.service.BackupPreferences
import eu.kanade.tachiyomi.data.notification.Notifications import eu.kanade.tachiyomi.data.notification.Notifications
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.util.system.logcat import eu.kanade.tachiyomi.util.system.logcat
import eu.kanade.tachiyomi.util.system.notificationManager import eu.kanade.tachiyomi.util.system.notificationManager
import logcat.LogPriority import logcat.LogPriority
@ -26,10 +26,10 @@ class BackupCreatorJob(private val context: Context, workerParams: WorkerParamet
CoroutineWorker(context, workerParams) { CoroutineWorker(context, workerParams) {
override suspend fun doWork(): Result { override suspend fun doWork(): Result {
val preferences = Injekt.get<PreferencesHelper>() val backupPreferences = Injekt.get<BackupPreferences>()
val notifier = BackupNotifier(context) val notifier = BackupNotifier(context)
val uri = inputData.getString(LOCATION_URI_KEY)?.toUri() val uri = inputData.getString(LOCATION_URI_KEY)?.toUri()
?: preferences.backupsDirectory().get().toUri() ?: backupPreferences.backupsDirectory().get().toUri()
val flags = inputData.getInt(BACKUP_FLAGS_KEY, BackupConst.BACKUP_ALL) val flags = inputData.getInt(BACKUP_FLAGS_KEY, BackupConst.BACKUP_ALL)
val isAutoBackup = inputData.getBoolean(IS_AUTO_BACKUP_KEY, true) val isAutoBackup = inputData.getBoolean(IS_AUTO_BACKUP_KEY, true)
@ -54,8 +54,8 @@ class BackupCreatorJob(private val context: Context, workerParams: WorkerParamet
} }
fun setupTask(context: Context, prefInterval: Int? = null) { fun setupTask(context: Context, prefInterval: Int? = null) {
val preferences = Injekt.get<PreferencesHelper>() val backupPreferences = Injekt.get<BackupPreferences>()
val interval = prefInterval ?: preferences.backupInterval().get() val interval = prefInterval ?: backupPreferences.backupInterval().get()
val workManager = WorkManager.getInstance(context) val workManager = WorkManager.getInstance(context)
if (interval > 0) { if (interval > 0) {
val request = PeriodicWorkRequestBuilder<BackupCreatorJob>( val request = PeriodicWorkRequestBuilder<BackupCreatorJob>(

View File

@ -7,6 +7,7 @@ import com.hippo.unifile.UniFile
import data.Manga_sync import data.Manga_sync
import data.Mangas import data.Mangas
import eu.kanade.data.DatabaseHandler import eu.kanade.data.DatabaseHandler
import eu.kanade.domain.backup.service.BackupPreferences
import eu.kanade.domain.category.interactor.GetCategories import eu.kanade.domain.category.interactor.GetCategories
import eu.kanade.domain.category.model.Category import eu.kanade.domain.category.model.Category
import eu.kanade.domain.history.model.HistoryUpdate import eu.kanade.domain.history.model.HistoryUpdate
@ -33,7 +34,6 @@ import eu.kanade.tachiyomi.data.backup.models.backupTrackMapper
import eu.kanade.tachiyomi.data.database.models.Chapter import eu.kanade.tachiyomi.data.database.models.Chapter
import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.data.database.models.Track import eu.kanade.tachiyomi.data.database.models.Track
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.source.SourceManager import eu.kanade.tachiyomi.source.SourceManager
import eu.kanade.tachiyomi.source.model.copyFrom import eu.kanade.tachiyomi.source.model.copyFrom
import eu.kanade.tachiyomi.util.system.hasPermission import eu.kanade.tachiyomi.util.system.hasPermission
@ -57,7 +57,7 @@ class BackupManager(
private val handler: DatabaseHandler = Injekt.get() private val handler: DatabaseHandler = Injekt.get()
private val sourceManager: SourceManager = Injekt.get() private val sourceManager: SourceManager = Injekt.get()
private val preferences: PreferencesHelper = Injekt.get() private val backupPreferences: BackupPreferences = Injekt.get()
private val libraryPreferences: LibraryPreferences = Injekt.get() private val libraryPreferences: LibraryPreferences = Injekt.get()
private val getCategories: GetCategories = Injekt.get() private val getCategories: GetCategories = Injekt.get()
private val getFavorites: GetFavorites = Injekt.get() private val getFavorites: GetFavorites = Injekt.get()
@ -93,7 +93,7 @@ class BackupManager(
dir = dir.createDirectory("automatic") dir = dir.createDirectory("automatic")
// Delete older backups // Delete older backups
val numberOfBackups = preferences.numberOfBackups().get() val numberOfBackups = backupPreferences.numberOfBackups().get()
val backupRegex = Regex("""tachiyomi_\d+-\d+-\d+_\d+-\d+.proto.gz""") val backupRegex = Regex("""tachiyomi_\d+-\d+-\d+_\d+-\d+.proto.gz""")
dir.listFiles { _, filename -> backupRegex.matches(filename) } dir.listFiles { _, filename -> backupRegex.matches(filename) }
.orEmpty() .orEmpty()

View File

@ -2,15 +2,11 @@ package eu.kanade.tachiyomi.data.preference
import android.content.Context import android.content.Context
import android.os.Build import android.os.Build
import android.os.Environment
import androidx.core.net.toUri
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.core.preference.PreferenceStore import eu.kanade.tachiyomi.core.preference.PreferenceStore
import eu.kanade.tachiyomi.core.preference.getEnum import eu.kanade.tachiyomi.core.preference.getEnum
import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.util.system.DeviceUtil import eu.kanade.tachiyomi.util.system.DeviceUtil
import eu.kanade.tachiyomi.util.system.isDynamicColorAvailable import eu.kanade.tachiyomi.util.system.isDynamicColorAvailable
import java.io.File
import java.text.DateFormat import java.text.DateFormat
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
import java.util.Locale import java.util.Locale
@ -23,12 +19,6 @@ class PreferencesHelper(
private val preferenceStore: PreferenceStore, private val preferenceStore: PreferenceStore,
) { ) {
private val defaultBackupDir = File(
Environment.getExternalStorageDirectory().absolutePath + File.separator +
context.getString(R.string.app_name),
"backup",
).toUri()
fun confirmExit() = preferenceStore.getBoolean("pref_confirm_exit", false) fun confirmExit() = preferenceStore.getBoolean("pref_confirm_exit", false)
fun sideNavIconAlignment() = preferenceStore.getInt("pref_side_nav_icon_alignment", 0) fun sideNavIconAlignment() = preferenceStore.getInt("pref_side_nav_icon_alignment", 0)
@ -47,8 +37,6 @@ class PreferencesHelper(
fun lastVersionCode() = preferenceStore.getInt("last_version_code", 0) fun lastVersionCode() = preferenceStore.getInt("last_version_code", 0)
fun backupsDirectory() = preferenceStore.getString("backup_directory", defaultBackupDir.toString())
fun relativeTime() = preferenceStore.getInt("relative_time", 7) fun relativeTime() = preferenceStore.getInt("relative_time", 7)
fun dateFormat(format: String = preferenceStore.getString(Keys.dateFormat, "").get()): DateFormat = when (format) { fun dateFormat(format: String = preferenceStore.getString(Keys.dateFormat, "").get()): DateFormat = when (format) {
@ -56,10 +44,6 @@ class PreferencesHelper(
else -> SimpleDateFormat(format, Locale.getDefault()) else -> SimpleDateFormat(format, Locale.getDefault())
} }
fun numberOfBackups() = preferenceStore.getInt("backup_slots", 2)
fun backupInterval() = preferenceStore.getInt("backup_interval", 12)
fun downloadedOnly() = preferenceStore.getBoolean("pref_downloaded_only", false) fun downloadedOnly() = preferenceStore.getBoolean("pref_downloaded_only", false)
fun automaticExtUpdates() = preferenceStore.getBoolean("automatic_ext_updates", true) fun automaticExtUpdates() = preferenceStore.getBoolean("automatic_ext_updates", true)

View File

@ -121,6 +121,7 @@ class MainActivity : BaseActivity() {
securityPreferences = Injekt.get(), securityPreferences = Injekt.get(),
libraryPreferences = libraryPreferences, libraryPreferences = libraryPreferences,
readerPreferences = Injekt.get(), readerPreferences = Injekt.get(),
backupPreferences = Injekt.get(),
) )
} else { } else {
false false

View File

@ -18,6 +18,7 @@ import androidx.core.os.bundleOf
import androidx.preference.PreferenceScreen import androidx.preference.PreferenceScreen
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.hippo.unifile.UniFile import com.hippo.unifile.UniFile
import eu.kanade.domain.backup.service.BackupPreferences
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.backup.BackupConst import eu.kanade.tachiyomi.data.backup.BackupConst
import eu.kanade.tachiyomi.data.backup.BackupCreatorJob import eu.kanade.tachiyomi.data.backup.BackupCreatorJob
@ -42,6 +43,7 @@ import eu.kanade.tachiyomi.util.system.openInBrowser
import eu.kanade.tachiyomi.util.system.toast import eu.kanade.tachiyomi.util.system.toast
import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.onEach
import uy.kohesive.injekt.injectLazy
class SettingsBackupController : SettingsController() { class SettingsBackupController : SettingsController() {
@ -50,6 +52,8 @@ class SettingsBackupController : SettingsController() {
*/ */
private var backupFlags = 0 private var backupFlags = 0
private val backupPreferences: BackupPreferences by injectLazy()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
requestPermissionsSafe(arrayOf(WRITE_EXTERNAL_STORAGE), 500) requestPermissionsSafe(arrayOf(WRITE_EXTERNAL_STORAGE), 500)
@ -105,7 +109,7 @@ class SettingsBackupController : SettingsController() {
titleRes = R.string.pref_backup_service_category titleRes = R.string.pref_backup_service_category
intListPreference { intListPreference {
bindTo(preferences.backupInterval()) bindTo(backupPreferences.backupInterval())
titleRes = R.string.pref_backup_interval titleRes = R.string.pref_backup_interval
entriesRes = arrayOf( entriesRes = arrayOf(
R.string.update_6hour, R.string.update_6hour,
@ -124,7 +128,7 @@ class SettingsBackupController : SettingsController() {
} }
} }
preference { preference {
bindTo(preferences.backupsDirectory()) bindTo(backupPreferences.backupsDirectory())
titleRes = R.string.pref_backup_directory titleRes = R.string.pref_backup_directory
onClick { onClick {
@ -136,7 +140,7 @@ class SettingsBackupController : SettingsController() {
} }
} }
preferences.backupsDirectory().changes() backupPreferences.backupsDirectory().changes()
.onEach { path -> .onEach { path ->
val dir = UniFile.fromUri(context, path.toUri()) val dir = UniFile.fromUri(context, path.toUri())
summary = dir.filePath + "/automatic" summary = dir.filePath + "/automatic"
@ -144,7 +148,7 @@ class SettingsBackupController : SettingsController() {
.launchIn(viewScope) .launchIn(viewScope)
} }
intListPreference { intListPreference {
bindTo(preferences.numberOfBackups()) bindTo(backupPreferences.numberOfBackups())
titleRes = R.string.pref_backup_slots titleRes = R.string.pref_backup_slots
entries = arrayOf("2", "3", "4", "5") entries = arrayOf("2", "3", "4", "5")
entryValues = entries entryValues = entries
@ -183,7 +187,7 @@ class SettingsBackupController : SettingsController() {
Intent.FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_WRITE_URI_PERMISSION
activity.contentResolver.takePersistableUriPermission(uri, flags) activity.contentResolver.takePersistableUriPermission(uri, flags)
preferences.backupsDirectory().set(uri.toString()) backupPreferences.backupsDirectory().set(uri.toString())
} }
CODE_BACKUP_CREATE -> { CODE_BACKUP_CREATE -> {
val flags = Intent.FLAG_GRANT_READ_URI_PERMISSION or val flags = Intent.FLAG_GRANT_READ_URI_PERMISSION or

View File

@ -0,0 +1,25 @@
package eu.kanade.tachiyomi.core.provider
import android.content.Context
import android.os.Environment
import androidx.core.net.toUri
import eu.kanade.tachiyomi.core.R
import java.io.File
class AndroidBackupFolderProvider(
private val context: Context
) : FolderProvider {
override fun directory(): File {
return File(
Environment.getExternalStorageDirectory().absolutePath + File.separator +
context.getString(R.string.app_name),
"backup",
)
}
override fun path(): String {
return directory().toUri().toString()
}
}