diff --git a/app/src/main/java/com/github/libretube/preferences/InstanceSettings.kt b/app/src/main/java/com/github/libretube/preferences/InstanceSettings.kt index 437f8b152..12ea9de8c 100644 --- a/app/src/main/java/com/github/libretube/preferences/InstanceSettings.kt +++ b/app/src/main/java/com/github/libretube/preferences/InstanceSettings.kt @@ -25,13 +25,25 @@ import com.github.libretube.util.RetrofitInstance class InstanceSettings : PreferenceFragmentCompat() { val TAG = "InstanceSettings" + + /** + * result listeners for importing and exporting subscriptions + */ private lateinit var getContent: ActivityResultLauncher + private lateinit var createFile: ActivityResultLauncher override fun onCreate(savedInstanceState: Bundle?) { getContent = - registerForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri? -> - ImportHelper(requireActivity() as AppCompatActivity).importSubscriptions(uri) + registerForActivityResult( + ActivityResultContracts.GetContent() + ) { uri: Uri? -> + ImportHelper(requireActivity()).importSubscriptions(uri) } + createFile = registerForActivityResult( + ActivityResultContracts.CreateDocument() + ) { uri: Uri? -> + ImportHelper(requireActivity()).exportSubscriptions(uri) + } super.onCreate(savedInstanceState) } @@ -126,8 +138,8 @@ class InstanceSettings : PreferenceFragmentCompat() { true } - val importFromYt = findPreference(PreferenceKeys.IMPORT_SUBS) - importFromYt?.setOnPreferenceClickListener { + val importSubscriptions = findPreference(PreferenceKeys.IMPORT_SUBS) + importSubscriptions?.setOnPreferenceClickListener { // check StorageAccess val accessGranted = PermissionHelper.isStoragePermissionGranted(activity as AppCompatActivity) @@ -137,6 +149,12 @@ class InstanceSettings : PreferenceFragmentCompat() { else PermissionHelper.requestReadWrite(activity as AppCompatActivity) true } + + val exportSubscriptions = findPreference(PreferenceKeys.EXPORT_SUBS) + exportSubscriptions?.setOnPreferenceClickListener { + createFile.launch("application/json") + true + } } private fun initCustomInstances(instancePref: ListPreference) { diff --git a/app/src/main/java/com/github/libretube/preferences/PreferenceKeys.kt b/app/src/main/java/com/github/libretube/preferences/PreferenceKeys.kt index 18ad2c013..47e89cb2d 100644 --- a/app/src/main/java/com/github/libretube/preferences/PreferenceKeys.kt +++ b/app/src/main/java/com/github/libretube/preferences/PreferenceKeys.kt @@ -34,6 +34,7 @@ object PreferenceKeys { const val LOGIN_REGISTER = "login_register" const val DELETE_ACCOUNT = "delete_account" const val IMPORT_SUBS = "import_from_yt" + const val EXPORT_SUBS = "export_subs" /** * Player diff --git a/app/src/main/java/com/github/libretube/util/ImportHelper.kt b/app/src/main/java/com/github/libretube/util/ImportHelper.kt index 92b45aacd..2502c0315 100644 --- a/app/src/main/java/com/github/libretube/util/ImportHelper.kt +++ b/app/src/main/java/com/github/libretube/util/ImportHelper.kt @@ -1,23 +1,27 @@ package com.github.libretube.util +import android.app.Activity import android.net.Uri import android.util.Log import android.widget.Toast -import androidx.appcompat.app.AppCompatActivity import com.fasterxml.jackson.databind.ObjectMapper import com.github.libretube.R +import com.github.libretube.obj.NewPipeSubscription import com.github.libretube.obj.NewPipeSubscriptions +import com.github.libretube.preferences.PreferenceHelper import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch +import kotlinx.coroutines.runBlocking import java.io.BufferedReader +import java.io.FileOutputStream import java.io.InputStream import java.io.InputStreamReader import java.util.zip.ZipEntry import java.util.zip.ZipInputStream class ImportHelper( - private val activity: AppCompatActivity + private val activity: Activity ) { private val TAG = "ImportHelper" @@ -87,4 +91,43 @@ class ImportHelper( } return stringBuilder.toString() } + + /** + * write the text to the document + */ + fun exportSubscriptions(uri: Uri?) { + if (uri == null) return + try { + val mapper = ObjectMapper() + val token = PreferenceHelper.getToken() + runBlocking { + val subs = if (token != "") RetrofitInstance.authApi.subscriptions(token) + else RetrofitInstance.authApi.unauthenticatedSubscriptions( + SubscriptionHelper.getFormattedLocalSubscriptions() + ) + val newPipeChannels = mutableListOf() + subs.forEach { + newPipeChannels += NewPipeSubscription( + name = it.name, + service_id = 0, + url = "https://youtube.com/channel/" + it.url + ) + } + + val newPipeSubscriptions = NewPipeSubscriptions( + subscriptions = newPipeChannels + ) + + val data = mapper.writeValueAsBytes(newPipeSubscriptions) + + activity.contentResolver.openFileDescriptor(uri, "w")?.use { + FileOutputStream(it.fileDescriptor).use { fileOutputStream -> + fileOutputStream.write(data) + } + } + } + } catch (e: Exception) { + e.printStackTrace() + } + } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a260cd287..d6bfb73e3 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -291,4 +291,5 @@ Copied Downloaded Share with start time + Export Subscriptions diff --git a/app/src/main/res/xml/instance_settings.xml b/app/src/main/res/xml/instance_settings.xml index 7ce04b572..40fc7824e 100644 --- a/app/src/main/res/xml/instance_settings.xml +++ b/app/src/main/res/xml/instance_settings.xml @@ -59,11 +59,16 @@ + + \ No newline at end of file