list: square swipe reveal with resistance, Lucide action icons
This commit is contained in:
@@ -33,10 +33,8 @@ import org.oxycblt.auxio.list.ListFragment
|
||||
import org.oxycblt.auxio.list.ListViewModel
|
||||
import org.oxycblt.auxio.list.SelectableListListener
|
||||
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.SongViewHolder
|
||||
import org.oxycblt.auxio.list.recycler.SwipeActionsCallback
|
||||
import org.oxycblt.auxio.list.sort.Sort
|
||||
import org.oxycblt.auxio.music.IndexingState
|
||||
import org.oxycblt.auxio.music.MusicViewModel
|
||||
@@ -76,18 +74,6 @@ class 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 {
|
||||
setImageResource(R.drawable.ic_song_48)
|
||||
contentDescription = getString(R.string.lbl_songs)
|
||||
|
||||
@@ -26,6 +26,8 @@ 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 kotlin.math.abs
|
||||
import kotlin.math.min
|
||||
import org.oxycblt.auxio.R
|
||||
import org.oxycblt.auxio.list.adapter.FlexibleListAdapter
|
||||
import org.oxycblt.auxio.util.getAttrColorCompat
|
||||
@@ -34,8 +36,9 @@ 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.
|
||||
* the back of the deck, swipe-left plays it next at the deck's front. The row slides only as far
|
||||
* as a square underlay cell that reveals the action's icon, resists past that, and always
|
||||
* springs back; releasing past the cell commits the action.
|
||||
*
|
||||
* @param isEnabled Whether swipes are currently allowed (false during multi-select).
|
||||
* @param onAction Called with the swiped [Song] and the committed [SwipeAction].
|
||||
@@ -62,12 +65,22 @@ class SwipeActionsCallback(
|
||||
override fun getMovementFlags(
|
||||
recyclerView: RecyclerView,
|
||||
viewHolder: RecyclerView.ViewHolder,
|
||||
): Int =
|
||||
if (viewHolder is SongViewHolder && isEnabled()) {
|
||||
): Int {
|
||||
if (!isEnabled()) {
|
||||
return 0
|
||||
}
|
||||
// Gate on the item, not the ViewHolder: detail pages use their own row types.
|
||||
val position = viewHolder.bindingAdapterPosition
|
||||
if (position == RecyclerView.NO_POSITION) {
|
||||
return 0
|
||||
}
|
||||
val item = (recyclerView.adapter as? FlexibleListAdapter<*, *>)?.getItem(position)
|
||||
return if (item is Song) {
|
||||
makeMovementFlags(0, ItemTouchHelper.START or ItemTouchHelper.END)
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
|
||||
override fun onMove(
|
||||
recyclerView: RecyclerView,
|
||||
@@ -79,8 +92,8 @@ class SwipeActionsCallback(
|
||||
val position = viewHolder.bindingAdapterPosition
|
||||
val recyclerView = viewHolder.itemView.parent as? RecyclerView ?: return
|
||||
val song =
|
||||
(recyclerView.adapter as? FlexibleListAdapter<*, *>)
|
||||
?.getItem(position) as? Song ?: return
|
||||
(recyclerView.adapter as? FlexibleListAdapter<*, *>)?.getItem(position) as? Song
|
||||
?: return
|
||||
onAction(
|
||||
song,
|
||||
if (direction == ItemTouchHelper.END) SwipeAction.QUEUE else SwipeAction.PLAY_NEXT,
|
||||
@@ -89,6 +102,18 @@ class SwipeActionsCallback(
|
||||
recyclerView.adapter?.notifyItemChanged(position)
|
||||
}
|
||||
|
||||
/**
|
||||
* Commit when released past the revealed square, not halfway across the row.
|
||||
*/
|
||||
override fun getSwipeThreshold(viewHolder: RecyclerView.ViewHolder): Float {
|
||||
val width = viewHolder.itemView.width
|
||||
return if (width > 0) {
|
||||
min(1f, revealWidth(viewHolder).toFloat() / width)
|
||||
} else {
|
||||
0.2f
|
||||
}
|
||||
}
|
||||
|
||||
override fun onChildDraw(
|
||||
c: Canvas,
|
||||
recyclerView: RecyclerView,
|
||||
@@ -98,16 +123,29 @@ class SwipeActionsCallback(
|
||||
actionState: Int,
|
||||
isCurrentlyActive: Boolean,
|
||||
) {
|
||||
if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE && dX != 0f) {
|
||||
drawUnderlay(c, viewHolder, dX)
|
||||
// Clamp the slide to the square underlay: the row resists past the reveal.
|
||||
val clampedDX =
|
||||
if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
|
||||
val reveal = revealWidth(viewHolder).toFloat()
|
||||
dX.coerceIn(-reveal, reveal)
|
||||
} else {
|
||||
dX
|
||||
}
|
||||
if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE && clampedDX != 0f) {
|
||||
drawUnderlay(c, viewHolder, clampedDX)
|
||||
}
|
||||
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
|
||||
super.onChildDraw(c, recyclerView, viewHolder, clampedDX, dY, actionState, isCurrentlyActive)
|
||||
}
|
||||
|
||||
/** The square underlay cell's width: a standard large touchable size. */
|
||||
private fun revealWidth(viewHolder: RecyclerView.ViewHolder) =
|
||||
viewHolder.itemView.context.getDimenPixels(R.dimen.size_touchable_large)
|
||||
|
||||
/** 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 reveal = revealWidth(viewHolder).toFloat()
|
||||
val swipingEnd = dX > 0
|
||||
|
||||
val containerAttr: Int
|
||||
@@ -142,24 +180,21 @@ class SwipeActionsCallback(
|
||||
}
|
||||
c.drawRect(strip, paint)
|
||||
|
||||
// The icon sits centered in the square cell, fading in as the cell is revealed.
|
||||
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))
|
||||
icon.alpha = (255 * min(strip.width(), reveal) / reveal).toInt()
|
||||
val centerY = (top + bottom) / 2
|
||||
val left =
|
||||
val centerX =
|
||||
if (swipingEnd) {
|
||||
view.left + margin
|
||||
view.left + reveal / 2
|
||||
} else {
|
||||
view.right - margin - size
|
||||
view.right - reveal / 2
|
||||
}
|
||||
icon.setBounds(
|
||||
left.toInt(),
|
||||
(centerX - size / 2).toInt(),
|
||||
(centerY - size / 2).toInt(),
|
||||
(left + size).toInt(),
|
||||
(centerX + size / 2).toInt(),
|
||||
(centerY + size / 2).toInt(),
|
||||
)
|
||||
icon.draw(c)
|
||||
|
||||
@@ -1,11 +1,15 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- Lucide 'list-start' (lucide-static v1.72.0, ISC License) -->
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:tint="?attr/colorControlNormal"
|
||||
android:viewportWidth="960"
|
||||
android:viewportHeight="960">
|
||||
android:viewportWidth="24"
|
||||
android:viewportHeight="24">
|
||||
<path
|
||||
android:fillColor="@android:color/white"
|
||||
android:pathData="M780,900L720,840L840,720L720,600L780,540L960,720L780,900ZM320,840L320,760L160,760Q127,760 103.5,736.5Q80,713 80,680L80,200Q80,167 103.5,143.5Q127,120 160,120L800,120Q833,120 856.5,143.5Q880,167 880,200L880,480L800,480L800,200Q800,200 800,200Q800,200 800,200L160,200Q160,200 160,200Q160,200 160,200L160,680Q160,680 160,680Q160,680 160,680L680,680L680,760L600,760L600,840L320,840ZM440,600L520,600L520,480L640,480L640,400L520,400L520,280L440,280L440,400L320,400L320,480L440,480L440,600ZM160,680L160,680Q160,680 160,680Q160,680 160,680L160,200Q160,200 160,200Q160,200 160,200L160,200Q160,200 160,200Q160,200 160,200L160,680L160,680L160,680Z" />
|
||||
android:strokeColor="@android:color/white"
|
||||
android:strokeWidth="2"
|
||||
android:strokeLineCap="round"
|
||||
android:strokeLineJoin="round"
|
||||
android:pathData="M3 5h6 M3 12h13 M3 19h13 M16 8l-3-3 3-3 M21 19V7a2 2 0 0 0-2-2h-6" />
|
||||
</vector>
|
||||
|
||||
@@ -1,11 +1,15 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- Lucide 'list-end' (lucide-static v1.72.0, ISC License) -->
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:tint="?attr/colorControlNormal"
|
||||
android:viewportWidth="960"
|
||||
android:viewportHeight="960">
|
||||
android:viewportWidth="24"
|
||||
android:viewportHeight="24">
|
||||
<path
|
||||
android:fillColor="@android:color/white"
|
||||
android:pathData="M440,600L520,600L520,480L640,480L640,400L520,400L520,280L440,280L440,400L320,400L320,480L440,480L440,600ZM320,840L320,760L160,760Q127,760 103.5,736.5Q80,713 80,680L80,200Q80,167 103.5,143.5Q127,120 160,120L800,120Q833,120 856.5,143.5Q880,167 880,200L880,680Q880,713 856.5,736.5Q833,760 800,760L640,760L640,840L320,840ZM160,680L800,680Q800,680 800,680Q800,680 800,680L800,200Q800,200 800,200Q800,200 800,200L160,200Q160,200 160,200Q160,200 160,200L160,680Q160,680 160,680Q160,680 160,680ZM160,680Q160,680 160,680Q160,680 160,680L160,200Q160,200 160,200Q160,200 160,200L160,200Q160,200 160,200Q160,200 160,200L160,680Q160,680 160,680Q160,680 160,680L160,680Z" />
|
||||
android:strokeColor="@android:color/white"
|
||||
android:strokeWidth="2"
|
||||
android:strokeLineCap="round"
|
||||
android:strokeLineJoin="round"
|
||||
android:pathData="M16 5H3 M16 12H3 M9 19H3 M16 16l-3 3 3 3 M21 5v12a2 2 0 0 1-2 2h-6" />
|
||||
</vector>
|
||||
|
||||
Reference in New Issue
Block a user