Merge pull request #2705 from Bnyro/master

Convert queued videos in a playlist
This commit is contained in:
Bnyro 2023-01-15 13:17:35 +01:00 committed by GitHub
commit 620e985dd2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 38 deletions

View File

@ -17,13 +17,18 @@ import com.github.libretube.extensions.TAG
import com.github.libretube.extensions.toStreamItem import com.github.libretube.extensions.toStreamItem
import com.github.libretube.extensions.toastFromMainThread import com.github.libretube.extensions.toastFromMainThread
import com.github.libretube.ui.models.PlaylistViewModel import com.github.libretube.ui.models.PlaylistViewModel
import com.github.libretube.util.PlayingQueue
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
/**
* Dialog to insert new videos to a playlist
* @param videoId The id of the video to add. If non is provided, insert the whole playing queue
*/
class AddToPlaylistDialog( class AddToPlaylistDialog(
private val videoId: String private val videoId: String? = null
) : DialogFragment() { ) : DialogFragment() {
private lateinit var binding: DialogAddtoplaylistBinding private lateinit var binding: DialogAddtoplaylistBinding
private val viewModel: PlaylistViewModel by activityViewModels() private val viewModel: PlaylistViewModel by activityViewModels()
@ -53,30 +58,27 @@ class AddToPlaylistDialog(
Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_SHORT).show() Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_SHORT).show()
return@launchWhenCreated return@launchWhenCreated
} }
if (response.isNotEmpty()) { if (response.isEmpty()) return@launchWhenCreated
val names = response.map { it.name } val names = response.map { it.name }
val arrayAdapter = val arrayAdapter =
ArrayAdapter(requireContext(), android.R.layout.simple_spinner_item, names) ArrayAdapter(requireContext(), android.R.layout.simple_spinner_item, names)
arrayAdapter.setDropDownViewResource( arrayAdapter.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item android.R.layout.simple_spinner_dropdown_item
)
binding.playlistsSpinner.adapter = arrayAdapter
// select the last used playlist
viewModel.lastSelectedPlaylistId?.let { id ->
binding.playlistsSpinner.setSelection(
response.indexOfFirst { it.id == id }.takeIf { it >= 0 } ?: 0
) )
binding.playlistsSpinner.adapter = arrayAdapter }
if (viewModel.lastSelectedPlaylistId != null) { runOnUiThread {
var selectionIndex = 0 binding.addToPlaylist.setOnClickListener {
response.forEachIndexed { index, playlist -> val index = binding.playlistsSpinner.selectedItemPosition
if (playlist.id == viewModel.lastSelectedPlaylistId) { viewModel.lastSelectedPlaylistId = response[index].id!!
selectionIndex = index addToPlaylist(response[index].id!!)
} dialog?.dismiss()
}
binding.playlistsSpinner.setSelection(selectionIndex)
}
runOnUiThread {
binding.addToPlaylist.setOnClickListener {
val index = binding.playlistsSpinner.selectedItemPosition
viewModel.lastSelectedPlaylistId = response[index].id!!
addToPlaylist(response[index].id!!)
dialog?.dismiss()
}
} }
} }
} }
@ -85,11 +87,15 @@ class AddToPlaylistDialog(
private fun addToPlaylist(playlistId: String) { private fun addToPlaylist(playlistId: String) {
val appContext = context?.applicationContext ?: return val appContext = context?.applicationContext ?: return
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {
val success = try { val streams = when {
PlaylistsHelper.addToPlaylist( videoId != null -> listOf(
playlistId,
RetrofitInstance.api.getStreams(videoId).toStreamItem(videoId) RetrofitInstance.api.getStreams(videoId).toStreamItem(videoId)
) )
else -> PlayingQueue.getStreams()
}
val success = try {
PlaylistsHelper.addToPlaylist(playlistId, *streams.toTypedArray())
} catch (e: Exception) { } catch (e: Exception) {
Log.e(TAG(), e.toString()) Log.e(TAG(), e.toString())
appContext.toastFromMainThread(R.string.unknown_error) appContext.toastFromMainThread(R.string.unknown_error)

View File

@ -10,6 +10,7 @@ import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.databinding.QueueBottomSheetBinding import com.github.libretube.databinding.QueueBottomSheetBinding
import com.github.libretube.ui.adapters.PlayingQueueAdapter import com.github.libretube.ui.adapters.PlayingQueueAdapter
import com.github.libretube.ui.dialogs.AddToPlaylistDialog
import com.github.libretube.util.PlayingQueue import com.github.libretube.util.PlayingQueue
class PlayingQueueSheet : ExpandedBottomSheet() { class PlayingQueueSheet : ExpandedBottomSheet() {
@ -50,15 +51,8 @@ class PlayingQueueSheet : ExpandedBottomSheet() {
adapter.notifyDataSetChanged() adapter.notifyDataSetChanged()
} }
binding.clear.setOnClickListener { binding.addToPlaylist.setOnClickListener {
val currentIndex = PlayingQueue.currentIndex() AddToPlaylistDialog().show(childFragmentManager, null)
val streams = PlayingQueue.getStreams().filterIndexed { position, _ ->
position <= currentIndex
}
PlayingQueue.setStreams(streams)
adapter.notifyDataSetChanged()
} }
binding.reverse.setOnClickListener { binding.reverse.setOnClickListener {

View File

@ -0,0 +1,11 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:autoMirrored="true"
android:tint="?attr/colorControlNormal"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@android:color/white"
android:pathData="M14,10H3v2h11V10zM14,6H3v2h11V6zM18,14v-4h-2v4h-4v2h4v4h2v-4h4v-2H18zM3,16h7v-2H3V16z" />
</vector>

View File

@ -73,13 +73,13 @@
android:src="@drawable/ic_repeat" /> android:src="@drawable/ic_repeat" />
<ImageView <ImageView
android:id="@+id/clear" android:id="@+id/add_to_playlist"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginHorizontal="5dp" android:layout_marginHorizontal="5dp"
android:background="?attr/selectableItemBackgroundBorderless" android:background="?attr/selectableItemBackgroundBorderless"
android:padding="5dp" android:padding="5dp"
android:src="@drawable/ic_close" /> android:src="@drawable/ic_playlist_add" />
</LinearLayout> </LinearLayout>