From f889b36e6cf9015782d7c33240e11b140cd5c70c Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sat, 6 Apr 2024 13:37:25 +0200 Subject: [PATCH] feat: support for importing temporary playlists from YouTube --- app/src/main/AndroidManifest.xml | 2 + .../github/libretube/constants/IntentData.kt | 1 + .../libretube/ui/activities/MainActivity.kt | 11 ++++ .../libretube/ui/activities/RouterActivity.kt | 5 ++ .../ui/dialogs/ImportTempPlaylistDialog.kt | 55 +++++++++++++++++++ app/src/main/res/values/strings.xml | 2 + 6 files changed, 76 insertions(+) create mode 100644 app/src/main/java/com/github/libretube/ui/dialogs/ImportTempPlaylistDialog.kt diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 4bf7a04f34..e4df93379b 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -306,6 +306,8 @@ + + diff --git a/app/src/main/java/com/github/libretube/constants/IntentData.kt b/app/src/main/java/com/github/libretube/constants/IntentData.kt index 69233de773..91df36f6df 100644 --- a/app/src/main/java/com/github/libretube/constants/IntentData.kt +++ b/app/src/main/java/com/github/libretube/constants/IntentData.kt @@ -5,6 +5,7 @@ object IntentData { const val playerData = "playerData" const val id = "id" const val videoId = "videoId" + const val videoIds = "videoIds" const val channelId = "channelId" const val channelName = "channelName" const val playlistId = "playlistId" diff --git a/app/src/main/java/com/github/libretube/ui/activities/MainActivity.kt b/app/src/main/java/com/github/libretube/ui/activities/MainActivity.kt index 5f007deb5e..71fbda6bd9 100644 --- a/app/src/main/java/com/github/libretube/ui/activities/MainActivity.kt +++ b/app/src/main/java/com/github/libretube/ui/activities/MainActivity.kt @@ -41,6 +41,7 @@ import com.github.libretube.helpers.ThemeHelper import com.github.libretube.helpers.WindowHelper import com.github.libretube.ui.base.BaseActivity import com.github.libretube.ui.dialogs.ErrorDialog +import com.github.libretube.ui.dialogs.ImportTempPlaylistDialog import com.github.libretube.ui.fragments.AudioPlayerFragment import com.github.libretube.ui.fragments.DownloadsFragment import com.github.libretube.ui.fragments.PlayerFragment @@ -426,6 +427,16 @@ class MainActivity : BaseActivity() { intent?.getStringExtra(IntentData.playlistId)?.let { navController.navigate(NavDirections.openPlaylist(playlistId = it)) } + intent?.getStringArrayExtra(IntentData.videoIds)?.let { + ImportTempPlaylistDialog() + .apply { + arguments = bundleOf( + IntentData.playlistName to intent?.getStringExtra(IntentData.playlistName), + IntentData.videoIds to it + ) + } + .show(supportFragmentManager, null) + } intent?.getStringExtra(IntentData.videoId)?.let { // the bottom navigation bar has to be created before opening the video // otherwise the player layout measures aren't calculated properly diff --git a/app/src/main/java/com/github/libretube/ui/activities/RouterActivity.kt b/app/src/main/java/com/github/libretube/ui/activities/RouterActivity.kt index 6fefe6f162..e7d619db6e 100644 --- a/app/src/main/java/com/github/libretube/ui/activities/RouterActivity.kt +++ b/app/src/main/java/com/github/libretube/ui/activities/RouterActivity.kt @@ -44,6 +44,11 @@ class RouterActivity : BaseActivity() { lastSegment == "playlist" -> { putExtra(IntentData.playlistId, uri.getQueryParameter("list")) } + lastSegment == "watch_videos" -> { + putExtra(IntentData.playlistName, uri.getQueryParameter("title")) + val videoIds = uri.getQueryParameter("video_ids")?.split(",") + putExtra(IntentData.videoIds, videoIds?.toTypedArray()) + } else -> { val id = if (lastSegment == "watch") uri.getQueryParameter("v") else lastSegment putExtra(IntentData.videoId, id) diff --git a/app/src/main/java/com/github/libretube/ui/dialogs/ImportTempPlaylistDialog.kt b/app/src/main/java/com/github/libretube/ui/dialogs/ImportTempPlaylistDialog.kt new file mode 100644 index 0000000000..07eea91384 --- /dev/null +++ b/app/src/main/java/com/github/libretube/ui/dialogs/ImportTempPlaylistDialog.kt @@ -0,0 +1,55 @@ +package com.github.libretube.ui.dialogs + +import android.app.Dialog +import android.os.Bundle +import android.util.Log +import androidx.fragment.app.DialogFragment +import com.github.libretube.R +import com.github.libretube.api.PlaylistsHelper +import com.github.libretube.constants.IntentData +import com.github.libretube.extensions.TAG +import com.github.libretube.extensions.toastFromMainDispatcher +import com.github.libretube.obj.PipedImportPlaylist +import com.google.android.material.dialog.MaterialAlertDialogBuilder +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import java.util.Date + +class ImportTempPlaylistDialog : DialogFragment() { + override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { + val title = arguments?.getString(IntentData.playlistName) + ?.takeIf { it.isNotEmpty() } + ?: Date().toString() + val videoIds = arguments?.getStringArray(IntentData.videoIds).orEmpty() + + return MaterialAlertDialogBuilder(requireContext()) + .setTitle(R.string.import_temp_playlist) + .setMessage( + requireContext() + .getString(R.string.import_temp_playlist_summary, title, videoIds.size) + ) + .setNegativeButton(R.string.cancel, null) + .setPositiveButton(R.string.okay) { _, _ -> + val context = requireContext().applicationContext + + CoroutineScope(Dispatchers.IO).launch { + try { + val playlist = PipedImportPlaylist( + name = title, + videos = videoIds.toList() + ) + + PlaylistsHelper.importPlaylists(listOf(playlist)) + context.toastFromMainDispatcher(R.string.playlistCreated) + } catch (e: Exception) { + Log.e(TAG(), e.toString()) + e.localizedMessage?.let { + context.toastFromMainDispatcher(it) + } + } + } + } + .create() + } +} \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 9225fd21f3..bed434d395 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -546,4 +546,6 @@ Create playlist Scroll to top Also clear watch positions + Import temporary playlist? + Do you want to create a new playlist named \'%1$s\'? The playlist will contain %2$d videos. \ No newline at end of file