Backup & restore
This commit is contained in:
@@ -2,6 +2,7 @@ package de.mm20.launcher2.preferences
|
||||
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
import androidx.datastore.core.DataMigration
|
||||
import androidx.datastore.core.DataStore
|
||||
import androidx.datastore.core.handlers.ReplaceFileCorruptionHandler
|
||||
import androidx.datastore.dataStore
|
||||
@@ -14,14 +15,7 @@ internal val Context.dataStore: LauncherDataStore by dataStore(
|
||||
fileName = "settings.pb",
|
||||
serializer = SettingsSerializer,
|
||||
produceMigrations = {
|
||||
listOf(
|
||||
FactorySettingsMigration(it),
|
||||
Migration_1_2(),
|
||||
Migration_2_3(),
|
||||
Migration_3_4(),
|
||||
Migration_4_5(),
|
||||
Migration_5_6(),
|
||||
)
|
||||
getMigrations(it)
|
||||
},
|
||||
corruptionHandler = ReplaceFileCorruptionHandler {
|
||||
CrashReporter.logException(it)
|
||||
@@ -30,4 +24,15 @@ internal val Context.dataStore: LauncherDataStore by dataStore(
|
||||
}
|
||||
)
|
||||
|
||||
internal const val SchemaVersion = 6
|
||||
internal const val SchemaVersion = 6
|
||||
|
||||
internal fun getMigrations(context: Context): List<DataMigration<Settings>> {
|
||||
return listOf(
|
||||
FactorySettingsMigration(context),
|
||||
Migration_1_2(),
|
||||
Migration_2_3(),
|
||||
Migration_3_4(),
|
||||
Migration_4_5(),
|
||||
Migration_5_6(),
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package de.mm20.launcher2.preferences
|
||||
|
||||
import android.content.Context
|
||||
import androidx.datastore.core.DataStoreFactory
|
||||
import androidx.datastore.core.handlers.ReplaceFileCorruptionHandler
|
||||
import de.mm20.launcher2.crashreporter.CrashReporter
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import kotlinx.coroutines.cancel
|
||||
import kotlinx.coroutines.flow.firstOrNull
|
||||
import java.io.File
|
||||
|
||||
suspend fun LauncherDataStore.export(toDir: File) {
|
||||
val scope = CoroutineScope(Dispatchers.IO + SupervisorJob())
|
||||
val backupDataStore = DataStoreFactory.create(
|
||||
serializer = SettingsSerializer,
|
||||
produceFile = {
|
||||
File(toDir, "settings")
|
||||
},
|
||||
scope = scope
|
||||
)
|
||||
val settings = this.data.firstOrNull() ?: return
|
||||
backupDataStore.updateData {
|
||||
settings
|
||||
}
|
||||
scope.cancel()
|
||||
}
|
||||
|
||||
|
||||
suspend fun LauncherDataStore.import(context: Context, fromDir: File) {
|
||||
val scope = CoroutineScope(Dispatchers.IO + SupervisorJob())
|
||||
val backupDataStore = DataStoreFactory.create(
|
||||
serializer = SettingsSerializer,
|
||||
migrations = getMigrations(context),
|
||||
corruptionHandler = ReplaceFileCorruptionHandler {
|
||||
CrashReporter.logException(it)
|
||||
Settings.getDefaultInstance()
|
||||
},
|
||||
produceFile = {
|
||||
File(fromDir, "settings")
|
||||
},
|
||||
scope = scope
|
||||
)
|
||||
val settings = backupDataStore.data.firstOrNull() ?: return
|
||||
|
||||
this.updateData {
|
||||
settings
|
||||
}
|
||||
scope.cancel()
|
||||
}
|
||||
Reference in New Issue
Block a user