list: swipe song rows right to queue, left to play next
This commit is contained in:
@@ -25,6 +25,7 @@ import android.view.View
|
|||||||
import androidx.core.view.updatePadding
|
import androidx.core.view.updatePadding
|
||||||
import androidx.fragment.app.activityViewModels
|
import androidx.fragment.app.activityViewModels
|
||||||
import androidx.navigation.findNavController
|
import androidx.navigation.findNavController
|
||||||
|
import androidx.recyclerview.widget.ItemTouchHelper
|
||||||
import com.google.android.material.appbar.AppBarLayout
|
import com.google.android.material.appbar.AppBarLayout
|
||||||
import com.google.android.material.transition.MaterialSharedAxis
|
import com.google.android.material.transition.MaterialSharedAxis
|
||||||
import kotlin.math.abs
|
import kotlin.math.abs
|
||||||
@@ -36,6 +37,7 @@ import org.oxycblt.auxio.databinding.FragmentDetailBinding
|
|||||||
import org.oxycblt.auxio.detail.list.DetailListAdapter
|
import org.oxycblt.auxio.detail.list.DetailListAdapter
|
||||||
import org.oxycblt.auxio.list.ListFragment
|
import org.oxycblt.auxio.list.ListFragment
|
||||||
import org.oxycblt.auxio.list.ListViewModel
|
import org.oxycblt.auxio.list.ListViewModel
|
||||||
|
import org.oxycblt.auxio.list.recycler.SwipeActionsCallback
|
||||||
import org.oxycblt.auxio.music.MusicViewModel
|
import org.oxycblt.auxio.music.MusicViewModel
|
||||||
import org.oxycblt.auxio.playback.PlaybackViewModel
|
import org.oxycblt.auxio.playback.PlaybackViewModel
|
||||||
import org.oxycblt.auxio.util.getDimenPixels
|
import org.oxycblt.auxio.util.getDimenPixels
|
||||||
@@ -122,6 +124,18 @@ abstract class DetailFragment<P : MusicParent, C : Music> :
|
|||||||
|
|
||||||
binding.detailRecycler.apply { adapter = getDetailListAdapter() }
|
binding.detailRecycler.apply { adapter = getDetailListAdapter() }
|
||||||
|
|
||||||
|
ItemTouchHelper(
|
||||||
|
SwipeActionsCallback(isEnabled = { listModel.selected.value.isEmpty() }) {
|
||||||
|
song,
|
||||||
|
action ->
|
||||||
|
when (action) {
|
||||||
|
SwipeActionsCallback.SwipeAction.QUEUE -> playbackModel.addToQueue(song)
|
||||||
|
SwipeActionsCallback.SwipeAction.PLAY_NEXT -> playbackModel.playNext(song)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
.attachToRecyclerView(binding.detailRecycler)
|
||||||
|
|
||||||
spacingSmall = requireContext().getDimenPixels(R.dimen.spacing_small)
|
spacingSmall = requireContext().getDimenPixels(R.dimen.spacing_small)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -33,8 +33,10 @@ import org.oxycblt.auxio.list.ListFragment
|
|||||||
import org.oxycblt.auxio.list.ListViewModel
|
import org.oxycblt.auxio.list.ListViewModel
|
||||||
import org.oxycblt.auxio.list.SelectableListListener
|
import org.oxycblt.auxio.list.SelectableListListener
|
||||||
import org.oxycblt.auxio.list.adapter.SelectionIndicatorAdapter
|
import org.oxycblt.auxio.list.adapter.SelectionIndicatorAdapter
|
||||||
|
import androidx.recyclerview.widget.ItemTouchHelper
|
||||||
import org.oxycblt.auxio.list.recycler.FastScrollRecyclerView
|
import org.oxycblt.auxio.list.recycler.FastScrollRecyclerView
|
||||||
import org.oxycblt.auxio.list.recycler.SongViewHolder
|
import org.oxycblt.auxio.list.recycler.SongViewHolder
|
||||||
|
import org.oxycblt.auxio.list.recycler.SwipeActionsCallback
|
||||||
import org.oxycblt.auxio.list.sort.Sort
|
import org.oxycblt.auxio.list.sort.Sort
|
||||||
import org.oxycblt.auxio.music.IndexingState
|
import org.oxycblt.auxio.music.IndexingState
|
||||||
import org.oxycblt.auxio.music.MusicViewModel
|
import org.oxycblt.auxio.music.MusicViewModel
|
||||||
@@ -74,6 +76,18 @@ class SongListFragment :
|
|||||||
listener = this@SongListFragment
|
listener = this@SongListFragment
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ItemTouchHelper(
|
||||||
|
SwipeActionsCallback(isEnabled = { listModel.selected.value.isEmpty() }) {
|
||||||
|
song,
|
||||||
|
action ->
|
||||||
|
when (action) {
|
||||||
|
SwipeActionsCallback.SwipeAction.QUEUE -> playbackModel.addToQueue(song)
|
||||||
|
SwipeActionsCallback.SwipeAction.PLAY_NEXT -> playbackModel.playNext(song)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
.attachToRecyclerView(binding.homeRecycler)
|
||||||
|
|
||||||
binding.homeNoMusicPlaceholder.apply {
|
binding.homeNoMusicPlaceholder.apply {
|
||||||
setImageResource(R.drawable.ic_song_48)
|
setImageResource(R.drawable.ic_song_48)
|
||||||
contentDescription = getString(R.string.lbl_songs)
|
contentDescription = getString(R.string.lbl_songs)
|
||||||
|
|||||||
@@ -0,0 +1,167 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2026 Auxio Project
|
||||||
|
* SwipeActionsCallback.kt is part of Auxio.
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.oxycblt.auxio.list.recycler
|
||||||
|
|
||||||
|
import android.graphics.Canvas
|
||||||
|
import android.graphics.Paint
|
||||||
|
import android.graphics.RectF
|
||||||
|
import android.graphics.drawable.Drawable
|
||||||
|
import androidx.appcompat.content.res.AppCompatResources
|
||||||
|
import androidx.recyclerview.widget.ItemTouchHelper
|
||||||
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
|
import com.google.android.material.R as MR
|
||||||
|
import org.oxycblt.auxio.R
|
||||||
|
import org.oxycblt.auxio.list.adapter.FlexibleListAdapter
|
||||||
|
import org.oxycblt.auxio.util.getAttrColorCompat
|
||||||
|
import org.oxycblt.auxio.util.getDimenPixels
|
||||||
|
import org.oxycblt.musikr.Song
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An [ItemTouchHelper.Callback] giving song rows swipe actions: swipe-right queues the song at
|
||||||
|
* the back of the deck, swipe-left plays it next at the deck's front. The row never leaves the
|
||||||
|
* list; releasing past the stock threshold commits the action, short of it springs back.
|
||||||
|
*
|
||||||
|
* @param isEnabled Whether swipes are currently allowed (false during multi-select).
|
||||||
|
* @param onAction Called with the swiped [Song] and the committed [SwipeAction].
|
||||||
|
* @author OxygenCobalt (Fork)
|
||||||
|
*/
|
||||||
|
class SwipeActionsCallback(
|
||||||
|
private val isEnabled: () -> Boolean,
|
||||||
|
private val onAction: (Song, SwipeAction) -> Unit,
|
||||||
|
) : ItemTouchHelper.Callback() {
|
||||||
|
|
||||||
|
/** The action committed by a swipe. */
|
||||||
|
enum class SwipeAction {
|
||||||
|
/** Swipe-right: queue the song at the back of the deck. */
|
||||||
|
QUEUE,
|
||||||
|
|
||||||
|
/** Swipe-left: play the song next at the deck's front. */
|
||||||
|
PLAY_NEXT,
|
||||||
|
}
|
||||||
|
|
||||||
|
private val paint = Paint().apply { isAntiAlias = true }
|
||||||
|
private var queueIcon: Drawable? = null
|
||||||
|
private var playNextIcon: Drawable? = null
|
||||||
|
|
||||||
|
override fun getMovementFlags(
|
||||||
|
recyclerView: RecyclerView,
|
||||||
|
viewHolder: RecyclerView.ViewHolder,
|
||||||
|
): Int =
|
||||||
|
if (viewHolder is SongViewHolder && isEnabled()) {
|
||||||
|
makeMovementFlags(0, ItemTouchHelper.START or ItemTouchHelper.END)
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onMove(
|
||||||
|
recyclerView: RecyclerView,
|
||||||
|
viewHolder: RecyclerView.ViewHolder,
|
||||||
|
target: RecyclerView.ViewHolder,
|
||||||
|
) = false
|
||||||
|
|
||||||
|
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
|
||||||
|
val position = viewHolder.bindingAdapterPosition
|
||||||
|
val recyclerView = viewHolder.itemView.parent as? RecyclerView ?: return
|
||||||
|
val song =
|
||||||
|
(recyclerView.adapter as? FlexibleListAdapter<*, *>)
|
||||||
|
?.getItem(position) as? Song ?: return
|
||||||
|
onAction(
|
||||||
|
song,
|
||||||
|
if (direction == ItemTouchHelper.END) SwipeAction.QUEUE else SwipeAction.PLAY_NEXT,
|
||||||
|
)
|
||||||
|
// The row stays: rebind it in place, cancelling the swipe-out animation.
|
||||||
|
recyclerView.adapter?.notifyItemChanged(position)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onChildDraw(
|
||||||
|
c: Canvas,
|
||||||
|
recyclerView: RecyclerView,
|
||||||
|
viewHolder: RecyclerView.ViewHolder,
|
||||||
|
dX: Float,
|
||||||
|
dY: Float,
|
||||||
|
actionState: Int,
|
||||||
|
isCurrentlyActive: Boolean,
|
||||||
|
) {
|
||||||
|
if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE && dX != 0f) {
|
||||||
|
drawUnderlay(c, viewHolder, dX)
|
||||||
|
}
|
||||||
|
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Draw the revealed action underlay behind the sliding row. */
|
||||||
|
private fun drawUnderlay(c: Canvas, viewHolder: RecyclerView.ViewHolder, dX: Float) {
|
||||||
|
val view = viewHolder.itemView
|
||||||
|
val context = view.context
|
||||||
|
val swipingEnd = dX > 0
|
||||||
|
|
||||||
|
val containerAttr: Int
|
||||||
|
val onContainerAttr: Int
|
||||||
|
val icon: Drawable
|
||||||
|
if (swipingEnd) {
|
||||||
|
containerAttr = MR.attr.colorTertiaryContainer
|
||||||
|
onContainerAttr = MR.attr.colorOnTertiaryContainer
|
||||||
|
icon =
|
||||||
|
queueIcon
|
||||||
|
?: AppCompatResources.getDrawable(context, R.drawable.ic_queue_add_24)!!.also {
|
||||||
|
queueIcon = it
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
containerAttr = MR.attr.colorPrimaryContainer
|
||||||
|
onContainerAttr = MR.attr.colorOnPrimaryContainer
|
||||||
|
icon =
|
||||||
|
playNextIcon
|
||||||
|
?: AppCompatResources.getDrawable(context, R.drawable.ic_play_next_24)!!.also {
|
||||||
|
playNextIcon = it
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
paint.color = context.getAttrColorCompat(containerAttr).defaultColor
|
||||||
|
val top = view.top.toFloat()
|
||||||
|
val bottom = view.bottom.toFloat()
|
||||||
|
val strip =
|
||||||
|
if (swipingEnd) {
|
||||||
|
RectF(view.left.toFloat(), top, view.left + dX, bottom)
|
||||||
|
} else {
|
||||||
|
RectF(view.right + dX, top, view.right.toFloat(), bottom)
|
||||||
|
}
|
||||||
|
c.drawRect(strip, paint)
|
||||||
|
|
||||||
|
val size = context.getDimenPixels(R.dimen.size_icon_medium).toFloat()
|
||||||
|
val margin = context.getDimenPixels(R.dimen.spacing_medium).toFloat()
|
||||||
|
if (strip.width() < size + margin * 2) {
|
||||||
|
// Not enough revealed yet for the icon to be legible.
|
||||||
|
return
|
||||||
|
}
|
||||||
|
icon.setTintList(context.getAttrColorCompat(onContainerAttr))
|
||||||
|
val centerY = (top + bottom) / 2
|
||||||
|
val left =
|
||||||
|
if (swipingEnd) {
|
||||||
|
view.left + margin
|
||||||
|
} else {
|
||||||
|
view.right - margin - size
|
||||||
|
}
|
||||||
|
icon.setBounds(
|
||||||
|
left.toInt(),
|
||||||
|
(centerY - size / 2).toInt(),
|
||||||
|
(left + size).toInt(),
|
||||||
|
(centerY + size / 2).toInt(),
|
||||||
|
)
|
||||||
|
icon.draw(c)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -31,10 +31,12 @@ import androidx.fragment.app.activityViewModels
|
|||||||
import androidx.fragment.app.viewModels
|
import androidx.fragment.app.viewModels
|
||||||
import androidx.navigation.fragment.findNavController
|
import androidx.navigation.fragment.findNavController
|
||||||
import androidx.recyclerview.widget.GridLayoutManager
|
import androidx.recyclerview.widget.GridLayoutManager
|
||||||
|
import androidx.recyclerview.widget.ItemTouchHelper
|
||||||
import com.google.android.material.transition.MaterialSharedAxis
|
import com.google.android.material.transition.MaterialSharedAxis
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
import org.oxycblt.auxio.R
|
import org.oxycblt.auxio.R
|
||||||
import org.oxycblt.auxio.databinding.FragmentSearchBinding
|
import org.oxycblt.auxio.databinding.FragmentSearchBinding
|
||||||
|
import org.oxycblt.auxio.list.recycler.SwipeActionsCallback
|
||||||
import org.oxycblt.auxio.detail.DetailViewModel
|
import org.oxycblt.auxio.detail.DetailViewModel
|
||||||
import org.oxycblt.auxio.detail.Show
|
import org.oxycblt.auxio.detail.Show
|
||||||
import org.oxycblt.auxio.list.Item
|
import org.oxycblt.auxio.list.Item
|
||||||
@@ -181,6 +183,18 @@ class SearchFragment : ListFragment<Music, FragmentSearchBinding>() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ItemTouchHelper(
|
||||||
|
SwipeActionsCallback(isEnabled = { listModel.selected.value.isEmpty() }) {
|
||||||
|
song,
|
||||||
|
action ->
|
||||||
|
when (action) {
|
||||||
|
SwipeActionsCallback.SwipeAction.QUEUE -> playbackModel.addToQueue(song)
|
||||||
|
SwipeActionsCallback.SwipeAction.PLAY_NEXT -> playbackModel.playNext(song)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
.attachToRecyclerView(binding.searchRecycler)
|
||||||
|
|
||||||
// --- VIEWMODEL SETUP ---
|
// --- VIEWMODEL SETUP ---
|
||||||
|
|
||||||
collectImmediately(searchModel.searchResults, ::updateSearchResults)
|
collectImmediately(searchModel.searchResults, ::updateSearchResults)
|
||||||
|
|||||||
Reference in New Issue
Block a user