Dragging and dropping items

This commit is contained in:
Bnyro 2022-10-23 15:09:58 +02:00
parent e7ef030473
commit 1962268460
4 changed files with 31 additions and 2 deletions

View File

@ -0,0 +1,7 @@
package com.github.libretube.extensions
fun <T> MutableList<T>.move(oldPosition: Int, newPosition: Int) {
val item = this.get(oldPosition)
this.removeAt(oldPosition)
this.add(newPosition, item)
}

View File

@ -8,8 +8,9 @@ import com.github.libretube.databinding.QueueRowBinding
import com.github.libretube.ui.viewholders.PlayingQueueViewHolder import com.github.libretube.ui.viewholders.PlayingQueueViewHolder
import com.github.libretube.util.ImageHelper import com.github.libretube.util.ImageHelper
import com.github.libretube.util.PlayingQueue import com.github.libretube.util.PlayingQueue
import com.github.libretube.util.ThemeHelper
class PlayingQueueAdapter() : RecyclerView.Adapter<PlayingQueueViewHolder>() { class PlayingQueueAdapter : RecyclerView.Adapter<PlayingQueueViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PlayingQueueViewHolder { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PlayingQueueViewHolder {
val binding = QueueRowBinding.inflate( val binding = QueueRowBinding.inflate(
LayoutInflater.from(parent.context), LayoutInflater.from(parent.context),
@ -32,6 +33,11 @@ class PlayingQueueAdapter() : RecyclerView.Adapter<PlayingQueueViewHolder>() {
duration.text = streamItem.duration?.let { duration.text = streamItem.duration?.let {
DateUtils.formatElapsedTime(it) DateUtils.formatElapsedTime(it)
} }
if (PlayingQueue.currentIndex() == position) {
root.setBackgroundColor(
ThemeHelper.getThemeColor(root.context, android.R.attr.colorControlHighlight)
)
}
} }
} }
} }

View File

@ -35,12 +35,25 @@ class PlayingQueueSheet : BottomSheetDialogFragment() {
0, 0,
ItemTouchHelper.LEFT ItemTouchHelper.LEFT
) { ) {
override fun getMovementFlags(
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder
): Int {
val dragFlags = ItemTouchHelper.UP or ItemTouchHelper.DOWN
return makeMovementFlags(dragFlags, 0)
}
override fun onMove( override fun onMove(
recyclerView: RecyclerView, recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder, viewHolder: RecyclerView.ViewHolder,
target: RecyclerView.ViewHolder target: RecyclerView.ViewHolder
): Boolean { ): Boolean {
TODO("Not yet implemented") val from = viewHolder.absoluteAdapterPosition
val to = target.absoluteAdapterPosition
adapter.notifyItemMoved(from, to)
PlayingQueue.move(from, to)
return true
} }
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) { override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {

View File

@ -2,6 +2,7 @@ package com.github.libretube.util
import com.github.libretube.api.RetrofitInstance import com.github.libretube.api.RetrofitInstance
import com.github.libretube.api.obj.StreamItem import com.github.libretube.api.obj.StreamItem
import com.github.libretube.extensions.move
import com.github.libretube.extensions.toID import com.github.libretube.extensions.toID
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@ -67,6 +68,8 @@ object PlayingQueue {
fun remove(index: Int) = queue.removeAt(index) fun remove(index: Int) = queue.removeAt(index)
fun move(from: Int, to: Int) = queue.move(from, to)
private fun fetchMoreFromPlaylist(playlistId: String, nextPage: String?) { private fun fetchMoreFromPlaylist(playlistId: String, nextPage: String?) {
var playlistNextPage: String? = nextPage var playlistNextPage: String? = nextPage
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {