mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-14 14:20:30 +05:30
Convert queued videos in a playlist
This commit is contained in:
parent
dad4ecd27e
commit
fe42714c52
@ -17,13 +17,18 @@ import com.github.libretube.extensions.TAG
|
||||
import com.github.libretube.extensions.toStreamItem
|
||||
import com.github.libretube.extensions.toastFromMainThread
|
||||
import com.github.libretube.ui.models.PlaylistViewModel
|
||||
import com.github.libretube.util.PlayingQueue
|
||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
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(
|
||||
private val videoId: String
|
||||
private val videoId: String? = null
|
||||
) : DialogFragment() {
|
||||
private lateinit var binding: DialogAddtoplaylistBinding
|
||||
private val viewModel: PlaylistViewModel by activityViewModels()
|
||||
@ -53,30 +58,27 @@ class AddToPlaylistDialog(
|
||||
Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_SHORT).show()
|
||||
return@launchWhenCreated
|
||||
}
|
||||
if (response.isNotEmpty()) {
|
||||
val names = response.map { it.name }
|
||||
val arrayAdapter =
|
||||
ArrayAdapter(requireContext(), android.R.layout.simple_spinner_item, names)
|
||||
arrayAdapter.setDropDownViewResource(
|
||||
android.R.layout.simple_spinner_dropdown_item
|
||||
if (response.isEmpty()) return@launchWhenCreated
|
||||
val names = response.map { it.name }
|
||||
val arrayAdapter =
|
||||
ArrayAdapter(requireContext(), android.R.layout.simple_spinner_item, names)
|
||||
arrayAdapter.setDropDownViewResource(
|
||||
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) {
|
||||
var selectionIndex = 0
|
||||
response.forEachIndexed { index, playlist ->
|
||||
if (playlist.id == viewModel.lastSelectedPlaylistId) {
|
||||
selectionIndex = index
|
||||
}
|
||||
}
|
||||
binding.playlistsSpinner.setSelection(selectionIndex)
|
||||
}
|
||||
runOnUiThread {
|
||||
binding.addToPlaylist.setOnClickListener {
|
||||
val index = binding.playlistsSpinner.selectedItemPosition
|
||||
viewModel.lastSelectedPlaylistId = response[index].id!!
|
||||
addToPlaylist(response[index].id!!)
|
||||
dialog?.dismiss()
|
||||
}
|
||||
}
|
||||
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) {
|
||||
val appContext = context?.applicationContext ?: return
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
val success = try {
|
||||
PlaylistsHelper.addToPlaylist(
|
||||
playlistId,
|
||||
val streams = when {
|
||||
videoId != null -> listOf(
|
||||
RetrofitInstance.api.getStreams(videoId).toStreamItem(videoId)
|
||||
)
|
||||
else -> PlayingQueue.getStreams()
|
||||
}
|
||||
|
||||
val success = try {
|
||||
PlaylistsHelper.addToPlaylist(playlistId, *streams.toTypedArray())
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG(), e.toString())
|
||||
appContext.toastFromMainThread(R.string.unknown_error)
|
||||
|
@ -10,6 +10,7 @@ import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.github.libretube.databinding.QueueBottomSheetBinding
|
||||
import com.github.libretube.ui.adapters.PlayingQueueAdapter
|
||||
import com.github.libretube.ui.dialogs.AddToPlaylistDialog
|
||||
import com.github.libretube.util.PlayingQueue
|
||||
|
||||
class PlayingQueueSheet : ExpandedBottomSheet() {
|
||||
@ -50,15 +51,8 @@ class PlayingQueueSheet : ExpandedBottomSheet() {
|
||||
adapter.notifyDataSetChanged()
|
||||
}
|
||||
|
||||
binding.clear.setOnClickListener {
|
||||
val currentIndex = PlayingQueue.currentIndex()
|
||||
|
||||
val streams = PlayingQueue.getStreams().filterIndexed { position, _ ->
|
||||
position <= currentIndex
|
||||
}
|
||||
|
||||
PlayingQueue.setStreams(streams)
|
||||
adapter.notifyDataSetChanged()
|
||||
binding.addToPlaylist.setOnClickListener {
|
||||
AddToPlaylistDialog().show(childFragmentManager, null)
|
||||
}
|
||||
|
||||
binding.reverse.setOnClickListener {
|
||||
|
11
app/src/main/res/drawable/ic_playlist_add.xml
Normal file
11
app/src/main/res/drawable/ic_playlist_add.xml
Normal 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>
|
@ -73,13 +73,13 @@
|
||||
android:src="@drawable/ic_repeat" />
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/clear"
|
||||
android:id="@+id/add_to_playlist"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginHorizontal="5dp"
|
||||
android:background="?attr/selectableItemBackgroundBorderless"
|
||||
android:padding="5dp"
|
||||
android:src="@drawable/ic_close" />
|
||||
android:src="@drawable/ic_playlist_add" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user