Add UI functionality for importing and exporting playlists

This commit is contained in:
Bnyro 2022-12-01 14:09:57 +01:00
parent 8ce809d30f
commit c1793691ed
6 changed files with 127 additions and 53 deletions

View File

@ -14,6 +14,7 @@ import com.github.libretube.extensions.awaitQuery
import com.github.libretube.extensions.toLocalPlaylistItem
import com.github.libretube.extensions.toStreamItem
import com.github.libretube.extensions.toastFromMainThread
import com.github.libretube.obj.ImportPlaylist
import com.github.libretube.util.PreferenceHelper
import com.github.libretube.util.ProxyHelper
import retrofit2.HttpException
@ -175,6 +176,14 @@ object PlaylistsHelper {
)
}
suspend fun importPlaylists(playlist: List<ImportPlaylist>) {
}
suspend fun exportPlaylists(): List<ImportPlaylist> {
}
fun getPrivateType(): PlaylistType {
return if (loggedIn()) PlaylistType.PRIVATE else PlaylistType.LOCAL
}

View File

@ -49,8 +49,6 @@ object PreferenceKeys {
const val CLEAR_CUSTOM_INSTANCES = "clearCustomInstances"
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

View File

@ -7,7 +7,6 @@ import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.result.contract.ActivityResultContracts.CreateDocument
import androidx.preference.Preference
import com.github.libretube.R
import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.obj.BackupFile
import com.github.libretube.ui.base.BasePreferenceFragment
import com.github.libretube.ui.dialogs.BackupDialog
@ -29,19 +28,35 @@ class BackupRestoreSettings : BasePreferenceFragment() {
private lateinit var getSubscriptionsFile: ActivityResultLauncher<String>
private lateinit var createSubscriptionsFile: ActivityResultLauncher<String>
/**
* result listeners for importing and exporting playlists
*/
private lateinit var getPlaylistsFile: ActivityResultLauncher<String>
private lateinit var createPlaylistsFile: ActivityResultLauncher<String>
override fun onCreate(savedInstanceState: Bundle?) {
getSubscriptionsFile =
registerForActivityResult(
ActivityResultContracts.GetContent()
) { uri: Uri? ->
) { uri ->
ImportHelper(requireActivity()).importSubscriptions(uri)
}
createSubscriptionsFile = registerForActivityResult(
CreateDocument("application/json")
) { uri: Uri? ->
) { uri ->
ImportHelper(requireActivity()).exportSubscriptions(uri)
}
getPlaylistsFile = registerForActivityResult(ActivityResultContracts.GetContent()) { uri ->
ImportHelper(requireActivity()).importPlaylists(uri)
}
createPlaylistsFile = registerForActivityResult(
CreateDocument("application/json")
) { uri ->
ImportHelper(requireActivity()).exportPlaylists(uri)
}
getBackupFile =
registerForActivityResult(
ActivityResultContracts.GetContent()
@ -61,19 +76,30 @@ class BackupRestoreSettings : BasePreferenceFragment() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.import_export_settings, rootKey)
val importSubscriptions = findPreference<Preference>(PreferenceKeys.IMPORT_SUBS)
val importSubscriptions = findPreference<Preference>("import_subscriptions")
importSubscriptions?.setOnPreferenceClickListener {
// check StorageAccess
getSubscriptionsFile.launch("*/*")
true
}
val exportSubscriptions = findPreference<Preference>(PreferenceKeys.EXPORT_SUBS)
val exportSubscriptions = findPreference<Preference>("export_subscriptions")
exportSubscriptions?.setOnPreferenceClickListener {
createSubscriptionsFile.launch("subscriptions.json")
true
}
val importPlaylists = findPreference<Preference>("import_playlists")
importPlaylists?.setOnPreferenceClickListener {
getPlaylistsFile.launch("*/*")
true
}
val exportPlaylists = findPreference<Preference>("export_playlists")
exportPlaylists?.setOnPreferenceClickListener {
createPlaylistsFile.launch("subscriptions.json")
true
}
val advancesBackup = findPreference<Preference>("backup")
advancesBackup?.setOnPreferenceClickListener {
BackupDialog {

View File

@ -6,10 +6,12 @@ import android.util.Log
import android.widget.Toast
import com.fasterxml.jackson.databind.ObjectMapper
import com.github.libretube.R
import com.github.libretube.api.PlaylistsHelper
import com.github.libretube.api.RetrofitInstance
import com.github.libretube.api.SubscriptionHelper
import com.github.libretube.extensions.TAG
import com.github.libretube.extensions.toastFromMainThread
import com.github.libretube.obj.ImportPlaylistFile
import com.github.libretube.obj.NewPipeSubscription
import com.github.libretube.obj.NewPipeSubscriptions
import kotlinx.coroutines.CoroutineScope
@ -36,12 +38,10 @@ class ImportHelper(
}
} catch (e: IllegalArgumentException) {
Log.e(TAG(), e.toString())
Toast.makeText(
activity,
activity.toastFromMainThread(
activity.getString(R.string.unsupported_file_format) +
" (${activity.contentResolver.getType(uri)}",
Toast.LENGTH_SHORT
).show()
" (${activity.contentResolver.getType(uri)}"
)
} catch (e: Exception) {
Log.e(TAG(), e.toString())
Toast.makeText(activity, e.localizedMessage, Toast.LENGTH_SHORT).show()
@ -55,12 +55,7 @@ class ImportHelper(
return when (val fileType = activity.contentResolver.getType(uri)) {
"application/json", "application/octet-stream" -> {
// NewPipe subscriptions format
val mapper = ObjectMapper()
val json = activity.contentResolver.openInputStream(uri)?.use {
it.bufferedReader().use { reader -> reader.readText() }
}.orEmpty()
val subscriptions = mapper.readValue(json, NewPipeSubscriptions::class.java)
val subscriptions = ObjectMapper().readValue(uri.readText(), NewPipeSubscriptions::class.java)
subscriptions.subscriptions.orEmpty().map {
it.url!!.replace("https://www.youtube.com/channel/", "")
}
@ -84,40 +79,85 @@ class ImportHelper(
*/
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://www.youtube.com" + it.url
)
}
val newPipeSubscriptions = NewPipeSubscriptions(
subscriptions = newPipeChannels
runBlocking {
val subs = if (PreferenceHelper.getToken() != "") {
RetrofitInstance.authApi.subscriptions(PreferenceHelper.getToken())
} else {
RetrofitInstance.authApi.unauthenticatedSubscriptions(
SubscriptionHelper.getFormattedLocalSubscriptions()
)
}
val newPipeChannels = mutableListOf<NewPipeSubscription>()
subs.forEach {
newPipeChannels += NewPipeSubscription(
name = it.name,
service_id = 0,
url = "https://www.youtube.com" + it.url
)
}
val data = mapper.writeValueAsBytes(newPipeSubscriptions)
val newPipeSubscriptions = NewPipeSubscriptions(
subscriptions = newPipeChannels
)
activity.contentResolver.openFileDescriptor(uri, "w")?.use {
FileOutputStream(it.fileDescriptor).use { fileOutputStream ->
fileOutputStream.write(data)
}
uri.write(newPipeSubscriptions)
activity.toastFromMainThread(R.string.exportsuccess)
}
}
/**
* Import Playlists
*/
fun importPlaylists(uri: Uri?) {
if (uri == null) return
val playlistFile = ObjectMapper().readValue(uri.readText(), ImportPlaylistFile::class.java)
playlistFile.playlists.orEmpty().forEach {
CoroutineScope(Dispatchers.IO).launch {
playlistFile.playlists?.let {
PlaylistsHelper.importPlaylists(it)
}
}
} catch (e: Exception) {
e.printStackTrace()
}
activity.toastFromMainThread(R.string.importsuccess)
}
/**
* Export Playlists
*/
fun exportPlaylists(uri: Uri?) {
if (uri == null) return
runBlocking {
val playlists = PlaylistsHelper.exportPlaylists()
val playlistFile = ImportPlaylistFile(
format = "Piped",
version = 1,
playlists = playlists
)
uri.write(playlistFile)
activity.toastFromMainThread(R.string.exportsuccess)
}
}
private fun Uri.readText(): String {
return activity.contentResolver.openInputStream(this)?.use {
it.bufferedReader().use { reader -> reader.readText() }
}.orEmpty()
}
private fun Uri.write(text: Any) {
activity.contentResolver.openFileDescriptor(this, "w")?.use {
FileOutputStream(it.fileDescriptor).use { fileOutputStream ->
fileOutputStream.write(
ObjectMapper().writeValueAsBytes(text)
)
}
}
}
}

View File

@ -406,7 +406,8 @@
<string name="import_playlists">Import playlists</string>
<string name="export_playlists">Export playlists</string>
<string name="app_backup">App Backup</string>
<string name="backup_restore_summary">Import &amp; export subscriptions, playlists, ...</string>
<string name="backup_restore_summary">Import &amp; export subscriptions, playlists, …</string>
<string name="exportsuccess">Successfully exported!</string>
<!-- Notification channel strings -->
<string name="download_channel_name">Download Service</string>

View File

@ -7,12 +7,12 @@
<Preference
android:icon="@drawable/ic_download_filled"
android:summary="@string/import_from_yt_summary"
app:key="import_from_yt"
app:key="import_subscriptions"
app:title="@string/import_from_yt" />
<Preference
android:icon="@drawable/ic_upload"
app:key="export_subs"
app:key="export_subscriptions"
app:title="@string/export_subscriptions" />
</PreferenceCategory>
@ -22,12 +22,12 @@
<Preference
android:icon="@drawable/ic_download_filled"
android:summary="@string/import_playlists"
app:key="import_from_yt"
app:key="import_playlists"
app:title="@string/import_from_yt" />
<Preference
android:icon="@drawable/ic_upload"
app:key="export_subs"
app:key="export_playlists"
app:title="@string/export_playlists" />
</PreferenceCategory>