Merge pull request #5870 from Bnyro/master

feat: support for importing temporary playlists from YouTube
This commit is contained in:
Bnyro 2024-04-06 13:37:43 +02:00 committed by GitHub
commit dc5a712c3b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 76 additions and 0 deletions

View File

@ -306,6 +306,8 @@
<data android:pathPrefix="/playlist" />
<!-- search prefix -->
<data android:pathPrefix="/results" />
<!-- temporary playlist prefix -->
<data android:pathPrefix="/watch_videos" />
</intent-filter>
<!-- Support being detected as media player -->

View File

@ -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"

View File

@ -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

View File

@ -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)

View File

@ -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()
}
}

View File

@ -546,4 +546,6 @@
<string name="tooltip_create_playlist">Create playlist</string>
<string name="tooltip_scroll_to_top">Scroll to top</string>
<string name="also_clear_watch_positions">Also clear watch positions</string>
<string name="import_temp_playlist">Import temporary playlist?</string>
<string name="import_temp_playlist_summary">Do you want to create a new playlist named \'%1$s\'? The playlist will contain %2$d videos.</string>
</resources>