mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-14 22:30:30 +05:30
Convert BackupHelper and ImportHelper to objects.
This commit is contained in:
parent
c05734d0fe
commit
423c257dd7
@ -24,28 +24,26 @@ import kotlinx.serialization.json.longOrNull
|
||||
/**
|
||||
* Backup and restore the preferences
|
||||
*/
|
||||
class BackupHelper(private val context: Context) {
|
||||
object BackupHelper {
|
||||
/**
|
||||
* Write a [BackupFile] containing the database content as well as the preferences
|
||||
*/
|
||||
@OptIn(ExperimentalSerializationApi::class)
|
||||
fun createAdvancedBackup(uri: Uri?, backupFile: BackupFile) {
|
||||
uri?.let {
|
||||
fun createAdvancedBackup(context: Context, uri: Uri, backupFile: BackupFile) {
|
||||
try {
|
||||
context.contentResolver.openOutputStream(it)?.use { outputStream ->
|
||||
context.contentResolver.openOutputStream(uri)?.use { outputStream ->
|
||||
JsonHelper.json.encodeToStream(backupFile, outputStream)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG(), "Error while writing backup: $e")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Restore data from a [BackupFile]
|
||||
*/
|
||||
@OptIn(ExperimentalSerializationApi::class)
|
||||
fun restoreAdvancedBackup(uri: Uri?) {
|
||||
fun restoreAdvancedBackup(context: Context, uri: Uri?) {
|
||||
val backupFile = uri?.let {
|
||||
context.contentResolver.openInputStream(it)?.use { inputStream ->
|
||||
JsonHelper.json.decodeFromStream<BackupFile>(inputStream)
|
||||
@ -79,14 +77,14 @@ class BackupHelper(private val context: Context) {
|
||||
}
|
||||
}
|
||||
|
||||
restorePreferences(backupFile.preferences)
|
||||
restorePreferences(context, backupFile.preferences)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Restore the shared preferences from a backup file
|
||||
*/
|
||||
private fun restorePreferences(preferences: List<PreferenceItem>?) {
|
||||
private fun restorePreferences(context: Context, preferences: List<PreferenceItem>?) {
|
||||
if (preferences == null) return
|
||||
PreferenceManager.getDefaultSharedPreferences(context).edit(commit = true) {
|
||||
// clear the previous settings
|
||||
|
@ -25,17 +25,15 @@ import kotlinx.serialization.json.decodeFromStream
|
||||
import kotlinx.serialization.json.encodeToStream
|
||||
import okio.use
|
||||
|
||||
class ImportHelper(
|
||||
private val activity: Activity
|
||||
) {
|
||||
object ImportHelper {
|
||||
/**
|
||||
* Import subscriptions by a file uri
|
||||
*/
|
||||
fun importSubscriptions(uri: Uri?) {
|
||||
fun importSubscriptions(activity: Activity, uri: Uri?) {
|
||||
if (uri == null) return
|
||||
try {
|
||||
val applicationContext = activity.applicationContext
|
||||
val channels = getChannelsFromUri(uri)
|
||||
val channels = getChannelsFromUri(activity, uri)
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
SubscriptionHelper.importSubscriptions(channels)
|
||||
}.invokeOnCompletion {
|
||||
@ -56,7 +54,7 @@ class ImportHelper(
|
||||
/**
|
||||
* Get a list of channel IDs from a file [Uri]
|
||||
*/
|
||||
private fun getChannelsFromUri(uri: Uri): List<String> {
|
||||
private fun getChannelsFromUri(activity: Activity, uri: Uri): List<String> {
|
||||
return when (val fileType = activity.contentResolver.getType(uri)) {
|
||||
"application/json", "application/*", "application/octet-stream" -> {
|
||||
// NewPipe subscriptions format
|
||||
@ -85,7 +83,7 @@ class ImportHelper(
|
||||
* Write the text to the document
|
||||
*/
|
||||
@OptIn(ExperimentalSerializationApi::class)
|
||||
fun exportSubscriptions(uri: Uri?) {
|
||||
fun exportSubscriptions(activity: Activity, uri: Uri?) {
|
||||
if (uri == null) return
|
||||
runBlocking(Dispatchers.IO) {
|
||||
val token = PreferenceHelper.getToken()
|
||||
@ -112,7 +110,7 @@ class ImportHelper(
|
||||
* Import Playlists
|
||||
*/
|
||||
@OptIn(ExperimentalSerializationApi::class)
|
||||
fun importPlaylists(uri: Uri?) {
|
||||
fun importPlaylists(activity: Activity, uri: Uri?) {
|
||||
if (uri == null) return
|
||||
|
||||
val importPlaylists = mutableListOf<ImportPlaylist>()
|
||||
@ -164,7 +162,7 @@ class ImportHelper(
|
||||
/**
|
||||
* Export Playlists
|
||||
*/
|
||||
fun exportPlaylists(uri: Uri?) {
|
||||
fun exportPlaylists(activity: Activity, uri: Uri?) {
|
||||
if (uri == null) return
|
||||
|
||||
runBlocking {
|
||||
|
@ -1,6 +1,5 @@
|
||||
package com.github.libretube.ui.preferences
|
||||
|
||||
import android.net.Uri
|
||||
import android.os.Bundle
|
||||
import androidx.activity.result.ActivityResultLauncher
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
@ -38,39 +37,23 @@ class BackupRestoreSettings : BasePreferenceFragment() {
|
||||
private lateinit var createPlaylistsFile: ActivityResultLauncher<String>
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
getSubscriptionsFile =
|
||||
registerForActivityResult(
|
||||
ActivityResultContracts.GetContent()
|
||||
) { uri ->
|
||||
ImportHelper(requireActivity()).importSubscriptions(uri)
|
||||
getSubscriptionsFile = registerForActivityResult(ActivityResultContracts.GetContent()) {
|
||||
ImportHelper.importSubscriptions(requireActivity(), it)
|
||||
}
|
||||
createSubscriptionsFile = registerForActivityResult(
|
||||
CreateDocument("application/json")
|
||||
) { uri ->
|
||||
ImportHelper(requireActivity()).exportSubscriptions(uri)
|
||||
createSubscriptionsFile = registerForActivityResult(CreateDocument(JSON)) {
|
||||
ImportHelper.exportSubscriptions(requireActivity(), it)
|
||||
}
|
||||
|
||||
getPlaylistsFile = registerForActivityResult(ActivityResultContracts.GetContent()) { uri ->
|
||||
ImportHelper(requireActivity()).importPlaylists(uri)
|
||||
getPlaylistsFile = registerForActivityResult(ActivityResultContracts.GetContent()) {
|
||||
ImportHelper.importPlaylists(requireActivity(), it)
|
||||
}
|
||||
|
||||
createPlaylistsFile = registerForActivityResult(
|
||||
CreateDocument("application/json")
|
||||
) { uri ->
|
||||
ImportHelper(requireActivity()).exportPlaylists(uri)
|
||||
createPlaylistsFile = registerForActivityResult(CreateDocument(JSON)) {
|
||||
ImportHelper.exportPlaylists(requireActivity(), it)
|
||||
}
|
||||
|
||||
getBackupFile =
|
||||
registerForActivityResult(
|
||||
ActivityResultContracts.GetContent()
|
||||
) { uri: Uri? ->
|
||||
BackupHelper(requireContext()).restoreAdvancedBackup(uri)
|
||||
getBackupFile = registerForActivityResult(ActivityResultContracts.GetContent()) {
|
||||
BackupHelper.restoreAdvancedBackup(requireContext(), it)
|
||||
}
|
||||
|
||||
createBackupFile = registerForActivityResult(
|
||||
CreateDocument("application/json")
|
||||
) { uri: Uri? ->
|
||||
BackupHelper(requireContext()).createAdvancedBackup(uri, backupFile)
|
||||
createBackupFile = registerForActivityResult(CreateDocument(JSON)) {
|
||||
BackupHelper.createAdvancedBackup(requireContext(), it, backupFile)
|
||||
}
|
||||
|
||||
super.onCreate(savedInstanceState)
|
||||
@ -116,8 +99,12 @@ class BackupRestoreSettings : BasePreferenceFragment() {
|
||||
|
||||
val restoreAdvancedBackup = findPreference<Preference>("restore")
|
||||
restoreAdvancedBackup?.setOnPreferenceClickListener {
|
||||
getBackupFile.launch("application/json")
|
||||
getBackupFile.launch(JSON)
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val JSON = "application/json"
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user