Refactor backup/restore

Close #575
This commit is contained in:
MM20
2023-10-28 17:15:04 +02:00
parent c9d9b3a4e9
commit 47cf4e9483
24 changed files with 93 additions and 329 deletions
@@ -0,0 +1,8 @@
package de.mm20.launcher2.backup
import java.io.File
interface Backupable {
suspend fun backup(toDir: File)
suspend fun restore(fromDir: File)
}
@@ -701,6 +701,7 @@
<item quantity="other">Full in %1$s minutes</item>
</plurals>
<string name="backup_select_components">Select what to backup:</string>
<string name="backup_info">The following data will be backed up:</string>
<string name="backup_not_included">Connected accounts and 3rd party app widgets will not be backed up.</string>
<string name="backup_component_favorites">Favorites &amp; hidden apps</string>
<string name="backup_component_settings">Settings</string>
@@ -3,6 +3,7 @@ package de.mm20.launcher2.preferences
import android.content.Context
import androidx.datastore.core.DataStoreFactory
import androidx.datastore.core.handlers.ReplaceFileCorruptionHandler
import de.mm20.launcher2.backup.Backupable
import de.mm20.launcher2.crashreporter.CrashReporter
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
@@ -11,6 +12,19 @@ import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.firstOrNull
import java.io.File
internal class LauncherStoreBackupComponent(
private val context: Context,
private val dataStore: LauncherDataStore
): Backupable {
override suspend fun backup(toDir: File) {
dataStore.export(toDir)
}
override suspend fun restore(fromDir: File) {
dataStore.import(context, fromDir)
}
}
suspend fun LauncherDataStore.export(toDir: File) {
val scope = CoroutineScope(Dispatchers.IO + SupervisorJob())
val backupDataStore = DataStoreFactory.create(
@@ -1,8 +1,11 @@
package de.mm20.launcher2.preferences
import de.mm20.launcher2.backup.Backupable
import org.koin.android.ext.koin.androidContext
import org.koin.core.qualifier.named
import org.koin.dsl.module
val preferencesModule = module {
single { androidContext().dataStore }
factory<Backupable>(named<LauncherDataStore>()) { LauncherStoreBackupComponent(androidContext(), get()) }
}