diff --git a/app/src/main/java/org/oxycblt/auxio/home/list/SongListFragment.kt b/app/src/main/java/org/oxycblt/auxio/home/list/SongListFragment.kt index 8703b7792..0555267b9 100644 --- a/app/src/main/java/org/oxycblt/auxio/home/list/SongListFragment.kt +++ b/app/src/main/java/org/oxycblt/auxio/home/list/SongListFragment.kt @@ -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) diff --git a/app/src/main/java/org/oxycblt/auxio/list/recycler/SwipeActionsCallback.kt b/app/src/main/java/org/oxycblt/auxio/list/recycler/SwipeActionsCallback.kt index a7c40b34d..049a91f5a 100644 --- a/app/src/main/java/org/oxycblt/auxio/list/recycler/SwipeActionsCallback.kt +++ b/app/src/main/java/org/oxycblt/auxio/list/recycler/SwipeActionsCallback.kt @@ -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) diff --git a/app/src/main/res/drawable/ic_play_next_24.xml b/app/src/main/res/drawable/ic_play_next_24.xml index b507eea91..cd0ffa6a8 100644 --- a/app/src/main/res/drawable/ic_play_next_24.xml +++ b/app/src/main/res/drawable/ic_play_next_24.xml @@ -1,11 +1,15 @@ + + android:viewportWidth="24" + android:viewportHeight="24"> + 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" /> diff --git a/app/src/main/res/drawable/ic_queue_add_24.xml b/app/src/main/res/drawable/ic_queue_add_24.xml index d0ea17445..35229c91c 100644 --- a/app/src/main/res/drawable/ic_queue_add_24.xml +++ b/app/src/main/res/drawable/ic_queue_add_24.xml @@ -1,11 +1,15 @@ + + android:viewportWidth="24" + android:viewportHeight="24"> + 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" />