From 423c257dd746e0a2bcc4686335bd15c93c90ec3b Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Fri, 3 Feb 2023 18:55:12 +0530 Subject: [PATCH] Convert BackupHelper and ImportHelper to objects. --- .../github/libretube/helpers/BackupHelper.kt | 22 ++++---- .../github/libretube/helpers/ImportHelper.kt | 16 +++--- .../ui/preferences/BackupRestoreSettings.kt | 51 +++++++------------ 3 files changed, 36 insertions(+), 53 deletions(-) diff --git a/app/src/main/java/com/github/libretube/helpers/BackupHelper.kt b/app/src/main/java/com/github/libretube/helpers/BackupHelper.kt index aaa852b55..10e21c839 100644 --- a/app/src/main/java/com/github/libretube/helpers/BackupHelper.kt +++ b/app/src/main/java/com/github/libretube/helpers/BackupHelper.kt @@ -24,20 +24,18 @@ 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 { - try { - context.contentResolver.openOutputStream(it)?.use { outputStream -> - JsonHelper.json.encodeToStream(backupFile, outputStream) - } - } catch (e: Exception) { - Log.e(TAG(), "Error while writing backup: $e") + fun createAdvancedBackup(context: Context, uri: Uri, backupFile: BackupFile) { + try { + context.contentResolver.openOutputStream(uri)?.use { outputStream -> + JsonHelper.json.encodeToStream(backupFile, outputStream) } + } catch (e: Exception) { + Log.e(TAG(), "Error while writing backup: $e") } } @@ -45,7 +43,7 @@ class BackupHelper(private val context: Context) { * 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(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?) { + private fun restorePreferences(context: Context, preferences: List?) { if (preferences == null) return PreferenceManager.getDefaultSharedPreferences(context).edit(commit = true) { // clear the previous settings diff --git a/app/src/main/java/com/github/libretube/helpers/ImportHelper.kt b/app/src/main/java/com/github/libretube/helpers/ImportHelper.kt index c61b70dab..cb8a4ceb1 100644 --- a/app/src/main/java/com/github/libretube/helpers/ImportHelper.kt +++ b/app/src/main/java/com/github/libretube/helpers/ImportHelper.kt @@ -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 { + private fun getChannelsFromUri(activity: Activity, uri: Uri): List { 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() @@ -164,7 +162,7 @@ class ImportHelper( /** * Export Playlists */ - fun exportPlaylists(uri: Uri?) { + fun exportPlaylists(activity: Activity, uri: Uri?) { if (uri == null) return runBlocking { diff --git a/app/src/main/java/com/github/libretube/ui/preferences/BackupRestoreSettings.kt b/app/src/main/java/com/github/libretube/ui/preferences/BackupRestoreSettings.kt index 63020dc01..f75aa407c 100644 --- a/app/src/main/java/com/github/libretube/ui/preferences/BackupRestoreSettings.kt +++ b/app/src/main/java/com/github/libretube/ui/preferences/BackupRestoreSettings.kt @@ -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 override fun onCreate(savedInstanceState: Bundle?) { - getSubscriptionsFile = - registerForActivityResult( - ActivityResultContracts.GetContent() - ) { uri -> - ImportHelper(requireActivity()).importSubscriptions(uri) - } - createSubscriptionsFile = registerForActivityResult( - CreateDocument("application/json") - ) { uri -> - ImportHelper(requireActivity()).exportSubscriptions(uri) + getSubscriptionsFile = registerForActivityResult(ActivityResultContracts.GetContent()) { + ImportHelper.importSubscriptions(requireActivity(), it) } - - getPlaylistsFile = registerForActivityResult(ActivityResultContracts.GetContent()) { uri -> - ImportHelper(requireActivity()).importPlaylists(uri) + createSubscriptionsFile = registerForActivityResult(CreateDocument(JSON)) { + ImportHelper.exportSubscriptions(requireActivity(), it) } - - createPlaylistsFile = registerForActivityResult( - CreateDocument("application/json") - ) { uri -> - ImportHelper(requireActivity()).exportPlaylists(uri) + getPlaylistsFile = registerForActivityResult(ActivityResultContracts.GetContent()) { + ImportHelper.importPlaylists(requireActivity(), it) } - - getBackupFile = - registerForActivityResult( - ActivityResultContracts.GetContent() - ) { uri: Uri? -> - BackupHelper(requireContext()).restoreAdvancedBackup(uri) - } - - createBackupFile = registerForActivityResult( - CreateDocument("application/json") - ) { uri: Uri? -> - BackupHelper(requireContext()).createAdvancedBackup(uri, backupFile) + createPlaylistsFile = registerForActivityResult(CreateDocument(JSON)) { + ImportHelper.exportPlaylists(requireActivity(), it) + } + getBackupFile = registerForActivityResult(ActivityResultContracts.GetContent()) { + BackupHelper.restoreAdvancedBackup(requireContext(), it) + } + createBackupFile = registerForActivityResult(CreateDocument(JSON)) { + BackupHelper.createAdvancedBackup(requireContext(), it, backupFile) } super.onCreate(savedInstanceState) @@ -116,8 +99,12 @@ class BackupRestoreSettings : BasePreferenceFragment() { val restoreAdvancedBackup = findPreference("restore") restoreAdvancedBackup?.setOnPreferenceClickListener { - getBackupFile.launch("application/json") + getBackupFile.launch(JSON) true } } + + companion object { + private const val JSON = "application/json" + } }