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

View file

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

View file

@ -13,8 +13,8 @@ import androidx.work.WorkManager
import androidx.work.WorkerParameters
import androidx.work.workDataOf
import com.hippo.unifile.UniFile
import eu.kanade.domain.backup.service.BackupPreferences
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.notificationManager
import logcat.LogPriority
@ -26,10 +26,10 @@ class BackupCreatorJob(private val context: Context, workerParams: WorkerParamet
CoroutineWorker(context, workerParams) {
override suspend fun doWork(): Result {
val preferences = Injekt.get<PreferencesHelper>()
val backupPreferences = Injekt.get<BackupPreferences>()
val notifier = BackupNotifier(context)
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 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) {
val preferences = Injekt.get<PreferencesHelper>()
val interval = prefInterval ?: preferences.backupInterval().get()
val backupPreferences = Injekt.get<BackupPreferences>()
val interval = prefInterval ?: backupPreferences.backupInterval().get()
val workManager = WorkManager.getInstance(context)
if (interval > 0) {
val request = PeriodicWorkRequestBuilder<BackupCreatorJob>(

View file

@ -7,6 +7,7 @@ import com.hippo.unifile.UniFile
import data.Manga_sync
import data.Mangas
import eu.kanade.data.DatabaseHandler
import eu.kanade.domain.backup.service.BackupPreferences
import eu.kanade.domain.category.interactor.GetCategories
import eu.kanade.domain.category.model.Category
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.Manga
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.model.copyFrom
import eu.kanade.tachiyomi.util.system.hasPermission
@ -57,7 +57,7 @@ class BackupManager(
private val handler: DatabaseHandler = 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 getCategories: GetCategories = Injekt.get()
private val getFavorites: GetFavorites = Injekt.get()
@ -93,7 +93,7 @@ class BackupManager(
dir = dir.createDirectory("automatic")
// Delete older backups
val numberOfBackups = preferences.numberOfBackups().get()
val numberOfBackups = backupPreferences.numberOfBackups().get()
val backupRegex = Regex("""tachiyomi_\d+-\d+-\d+_\d+-\d+.proto.gz""")
dir.listFiles { _, filename -> backupRegex.matches(filename) }
.orEmpty()

View file

@ -2,15 +2,11 @@ package eu.kanade.tachiyomi.data.preference
import android.content.Context
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.getEnum
import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.util.system.DeviceUtil
import eu.kanade.tachiyomi.util.system.isDynamicColorAvailable
import java.io.File
import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.Locale
@ -23,12 +19,6 @@ class PreferencesHelper(
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 sideNavIconAlignment() = preferenceStore.getInt("pref_side_nav_icon_alignment", 0)
@ -47,8 +37,6 @@ class PreferencesHelper(
fun lastVersionCode() = preferenceStore.getInt("last_version_code", 0)
fun backupsDirectory() = preferenceStore.getString("backup_directory", defaultBackupDir.toString())
fun relativeTime() = preferenceStore.getInt("relative_time", 7)
fun dateFormat(format: String = preferenceStore.getString(Keys.dateFormat, "").get()): DateFormat = when (format) {
@ -56,10 +44,6 @@ class PreferencesHelper(
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 automaticExtUpdates() = preferenceStore.getBoolean("automatic_ext_updates", true)

View file

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

View file

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