mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-14 06:10:31 +05:30
export subscriptions
This commit is contained in:
parent
6dad539aa9
commit
3d71d3f2f1
@ -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<String>
|
||||
private lateinit var createFile: ActivityResultLauncher<String>
|
||||
|
||||
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<Preference>(PreferenceKeys.IMPORT_SUBS)
|
||||
importFromYt?.setOnPreferenceClickListener {
|
||||
val importSubscriptions = findPreference<Preference>(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<Preference>(PreferenceKeys.EXPORT_SUBS)
|
||||
exportSubscriptions?.setOnPreferenceClickListener {
|
||||
createFile.launch("application/json")
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
private fun initCustomInstances(instancePref: ListPreference) {
|
||||
|
@ -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
|
||||
|
@ -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<NewPipeSubscription>()
|
||||
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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -291,4 +291,5 @@
|
||||
<string name="copied">Copied</string>
|
||||
<string name="downloadsucceeded">Downloaded</string>
|
||||
<string name="share_with_time">Share with start time</string>
|
||||
<string name="export_subscriptions">Export Subscriptions</string>
|
||||
</resources>
|
||||
|
@ -59,11 +59,16 @@
|
||||
<PreferenceCategory app:title="@string/restore">
|
||||
|
||||
<Preference
|
||||
android:icon="@drawable/ic_upload"
|
||||
android:icon="@drawable/ic_download_filled"
|
||||
android:summary="@string/import_from_yt_summary"
|
||||
app:key="import_from_yt"
|
||||
app:title="@string/import_from_yt" />
|
||||
|
||||
<Preference
|
||||
android:icon="@drawable/ic_upload"
|
||||
app:key="export_subs"
|
||||
app:title="@string/export_subscriptions" />
|
||||
|
||||
</PreferenceCategory>
|
||||
|
||||
</PreferenceScreen>
|
Loading…
Reference in New Issue
Block a user