mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-14 14:20:30 +05:30
Convert backup helper methods to suspend functions.
This commit is contained in:
parent
d2a6dd5c31
commit
a5d6d8d9df
@ -11,8 +11,6 @@ import com.github.libretube.db.DatabaseHolder.Companion.Database
|
|||||||
import com.github.libretube.extensions.TAG
|
import com.github.libretube.extensions.TAG
|
||||||
import com.github.libretube.obj.BackupFile
|
import com.github.libretube.obj.BackupFile
|
||||||
import com.github.libretube.obj.PreferenceItem
|
import com.github.libretube.obj.PreferenceItem
|
||||||
import kotlinx.coroutines.Dispatchers
|
|
||||||
import kotlinx.coroutines.runBlocking
|
|
||||||
import kotlinx.serialization.ExperimentalSerializationApi
|
import kotlinx.serialization.ExperimentalSerializationApi
|
||||||
import kotlinx.serialization.json.booleanOrNull
|
import kotlinx.serialization.json.booleanOrNull
|
||||||
import kotlinx.serialization.json.decodeFromStream
|
import kotlinx.serialization.json.decodeFromStream
|
||||||
@ -43,14 +41,11 @@ object BackupHelper {
|
|||||||
* Restore data from a [BackupFile]
|
* Restore data from a [BackupFile]
|
||||||
*/
|
*/
|
||||||
@OptIn(ExperimentalSerializationApi::class)
|
@OptIn(ExperimentalSerializationApi::class)
|
||||||
fun restoreAdvancedBackup(context: Context, uri: Uri?) {
|
suspend fun restoreAdvancedBackup(context: Context, uri: Uri) {
|
||||||
val backupFile = uri?.let {
|
val backupFile = context.contentResolver.openInputStream(uri)?.use {
|
||||||
context.contentResolver.openInputStream(it)?.use { inputStream ->
|
JsonHelper.json.decodeFromStream<BackupFile>(it)
|
||||||
JsonHelper.json.decodeFromStream<BackupFile>(inputStream)
|
|
||||||
}
|
|
||||||
} ?: return
|
} ?: return
|
||||||
|
|
||||||
runBlocking(Dispatchers.IO) {
|
|
||||||
Database.watchHistoryDao().insertAll(
|
Database.watchHistoryDao().insertAll(
|
||||||
*backupFile.watchHistory.orEmpty().toTypedArray()
|
*backupFile.watchHistory.orEmpty().toTypedArray()
|
||||||
)
|
)
|
||||||
@ -79,7 +74,6 @@ object BackupHelper {
|
|||||||
|
|
||||||
restorePreferences(context, backupFile.preferences)
|
restorePreferences(context, backupFile.preferences)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Restore the shared preferences from a backup file
|
* Restore the shared preferences from a backup file
|
||||||
|
@ -3,6 +3,7 @@ package com.github.libretube.ui.preferences
|
|||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import androidx.activity.result.contract.ActivityResultContracts
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
import androidx.activity.result.contract.ActivityResultContracts.CreateDocument
|
import androidx.activity.result.contract.ActivityResultContracts.CreateDocument
|
||||||
|
import androidx.lifecycle.lifecycleScope
|
||||||
import androidx.preference.Preference
|
import androidx.preference.Preference
|
||||||
import com.github.libretube.R
|
import com.github.libretube.R
|
||||||
import com.github.libretube.helpers.BackupHelper
|
import com.github.libretube.helpers.BackupHelper
|
||||||
@ -10,22 +11,32 @@ import com.github.libretube.helpers.ImportHelper
|
|||||||
import com.github.libretube.obj.BackupFile
|
import com.github.libretube.obj.BackupFile
|
||||||
import com.github.libretube.ui.base.BasePreferenceFragment
|
import com.github.libretube.ui.base.BasePreferenceFragment
|
||||||
import com.github.libretube.ui.dialogs.BackupDialog
|
import com.github.libretube.ui.dialogs.BackupDialog
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
import java.time.LocalDateTime
|
import java.time.LocalDateTime
|
||||||
import java.time.format.DateTimeFormatter
|
import java.time.format.DateTimeFormatter
|
||||||
|
|
||||||
class BackupRestoreSettings : BasePreferenceFragment() {
|
class BackupRestoreSettings : BasePreferenceFragment() {
|
||||||
private val backupDateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH:mm:ss")
|
private val backupDateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH:mm:ss")
|
||||||
|
private var backupFile = BackupFile()
|
||||||
|
|
||||||
override val titleResourceId: Int = R.string.backup_restore
|
override val titleResourceId: Int = R.string.backup_restore
|
||||||
|
|
||||||
// backup and restore database
|
// backup and restore database
|
||||||
private val getBackupFile = registerForActivityResult(ActivityResultContracts.GetContent()) {
|
private val getBackupFile = registerForActivityResult(ActivityResultContracts.GetContent()) {
|
||||||
|
it?.let {
|
||||||
|
lifecycleScope.launch(Dispatchers.IO) {
|
||||||
BackupHelper.restoreAdvancedBackup(requireContext(), it)
|
BackupHelper.restoreAdvancedBackup(requireContext(), it)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
private val createBackupFile = registerForActivityResult(CreateDocument(JSON)) {
|
private val createBackupFile = registerForActivityResult(CreateDocument(JSON)) {
|
||||||
|
it?.let {
|
||||||
|
lifecycleScope.launch(Dispatchers.IO) {
|
||||||
BackupHelper.createAdvancedBackup(requireContext(), it, backupFile)
|
BackupHelper.createAdvancedBackup(requireContext(), it, backupFile)
|
||||||
}
|
}
|
||||||
private var backupFile = BackupFile()
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* result listeners for importing and exporting subscriptions
|
* result listeners for importing and exporting subscriptions
|
||||||
|
Loading…
Reference in New Issue
Block a user