From d2076fc1d4375ab8cf5f3c12f1d17afbbeb50075 Mon Sep 17 00:00:00 2001 From: h4h13 Date: Sun, 8 Dec 2019 22:08:51 +0530 Subject: [PATCH] Added swipe songs --- .../adapter/base/MediaEntryViewHolder.java | 11 +- .../adapter/song/PlayingQueueAdapter.kt | 71 +++++++- .../mainactivity/PlayingQueueFragment.kt | 49 +++--- .../retromusic/helper/MusicPlayerRemote.kt | 6 +- app/src/main/res/layout/item_queue.xml | 153 ++++++++++-------- 5 files changed, 196 insertions(+), 94 deletions(-) diff --git a/app/src/main/java/code/name/monkey/retromusic/adapter/base/MediaEntryViewHolder.java b/app/src/main/java/code/name/monkey/retromusic/adapter/base/MediaEntryViewHolder.java index 9a627edf..f1652ea5 100644 --- a/app/src/main/java/code/name/monkey/retromusic/adapter/base/MediaEntryViewHolder.java +++ b/app/src/main/java/code/name/monkey/retromusic/adapter/base/MediaEntryViewHolder.java @@ -25,11 +25,12 @@ import androidx.annotation.Nullable; import androidx.recyclerview.widget.RecyclerView; import com.google.android.material.card.MaterialCardView; +import com.h6ah4i.android.widget.advrecyclerview.utils.AbstractDraggableSwipeableItemViewHolder; import code.name.monkey.appthemehelper.util.ATHUtil; import code.name.monkey.retromusic.R; -public class MediaEntryViewHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener, View.OnClickListener { +public class MediaEntryViewHolder extends AbstractDraggableSwipeableItemViewHolder implements View.OnLongClickListener, View.OnClickListener { @Nullable public TextView title; @@ -71,6 +72,8 @@ public class MediaEntryViewHolder extends RecyclerView.ViewHolder implements Vie @Nullable public ImageView image; + @Nullable + public View dummyContainer; public MediaEntryViewHolder(@NonNull View itemView) { super(itemView); @@ -91,6 +94,7 @@ public class MediaEntryViewHolder extends RecyclerView.ViewHolder implements Vie recyclerView = itemView.findViewById(R.id.recycler_view); mask = itemView.findViewById(R.id.mask); playSongs = itemView.findViewById(R.id.playSongs); + dummyContainer = itemView.findViewById(R.id.dummy_view); if (imageContainerCard != null) { imageContainerCard.setCardBackgroundColor(ATHUtil.INSTANCE.resolveColor(itemView.getContext(), R.attr.colorSurface)); @@ -99,6 +103,11 @@ public class MediaEntryViewHolder extends RecyclerView.ViewHolder implements Vie itemView.setOnLongClickListener(this); } + @Override + public View getSwipeableContainerView() { + return null; + } + @Override public boolean onLongClick(View v) { return false; diff --git a/app/src/main/java/code/name/monkey/retromusic/adapter/song/PlayingQueueAdapter.kt b/app/src/main/java/code/name/monkey/retromusic/adapter/song/PlayingQueueAdapter.kt index 9f2e17cd..1fa489f3 100644 --- a/app/src/main/java/code/name/monkey/retromusic/adapter/song/PlayingQueueAdapter.kt +++ b/app/src/main/java/code/name/monkey/retromusic/adapter/song/PlayingQueueAdapter.kt @@ -8,15 +8,24 @@ import android.widget.ImageView import androidx.appcompat.app.AppCompatActivity import code.name.monkey.retromusic.R import code.name.monkey.retromusic.helper.MusicPlayerRemote +import code.name.monkey.retromusic.helper.MusicPlayerRemote.isPlaying +import code.name.monkey.retromusic.helper.MusicPlayerRemote.playNextSong +import code.name.monkey.retromusic.helper.MusicPlayerRemote.removeFromQueue import code.name.monkey.retromusic.model.Song import code.name.monkey.retromusic.util.MusicUtil import code.name.monkey.retromusic.util.ViewUtil import com.h6ah4i.android.widget.advrecyclerview.draggable.DraggableItemAdapter -import com.h6ah4i.android.widget.advrecyclerview.draggable.DraggableItemViewHolder import com.h6ah4i.android.widget.advrecyclerview.draggable.ItemDraggableRange import com.h6ah4i.android.widget.advrecyclerview.draggable.annotation.DraggableItemStateFlags +import com.h6ah4i.android.widget.advrecyclerview.swipeable.SwipeableItemAdapter +import com.h6ah4i.android.widget.advrecyclerview.swipeable.SwipeableItemConstants +import com.h6ah4i.android.widget.advrecyclerview.swipeable.action.SwipeResultAction +import com.h6ah4i.android.widget.advrecyclerview.swipeable.action.SwipeResultActionDefault +import com.h6ah4i.android.widget.advrecyclerview.swipeable.action.SwipeResultActionRemoveItem +import com.h6ah4i.android.widget.advrecyclerview.swipeable.annotation.SwipeableItemResults import java.util.* + class PlayingQueueAdapter( activity: AppCompatActivity, dataSet: ArrayList, @@ -24,9 +33,10 @@ class PlayingQueueAdapter( itemLayoutRes: Int ) : SongAdapter( activity, dataSet, itemLayoutRes, false, null -), DraggableItemAdapter { +), DraggableItemAdapter, SwipeableItemAdapter { private var color = -1 + private var songToRemove: Song? = null override fun createViewHolder(view: View): SongAdapter.ViewHolder { return ViewHolder(view) @@ -125,7 +135,11 @@ class PlayingQueueAdapter( notifyDataSetChanged() } - inner class ViewHolder(itemView: View) : SongAdapter.ViewHolder(itemView), DraggableItemViewHolder { + fun setSongToRemove(song: Song) { + songToRemove = song + } + + inner class ViewHolder(itemView: View) : SongAdapter.ViewHolder(itemView) { @DraggableItemStateFlags private var mDragStateFlags: Int = 0 @@ -159,6 +173,10 @@ class PlayingQueueAdapter( override fun setDragStateFlags(@DraggableItemStateFlags flags: Int) { mDragStateFlags = flags } + + override fun getSwipeableContainerView(): View? { + return dummyContainer + } } companion object { @@ -167,4 +185,51 @@ class PlayingQueueAdapter( private const val CURRENT = 1 private const val UP_NEXT = 2 } + + override fun onSwipeItem(holder: ViewHolder?, position: Int, @SwipeableItemResults result: Int): SwipeResultAction { + return if (result === SwipeableItemConstants.RESULT_CANCELED) { + SwipeResultActionDefault() + } else { + SwipedResultActionRemoveItem(this, position, activity) + } + } + + override fun onGetSwipeReactionType(holder: ViewHolder?, position: Int, x: Int, y: Int): Int { + return if (onCheckCanStartDrag(holder!!, position, x, y)) { + SwipeableItemConstants.REACTION_CAN_NOT_SWIPE_BOTH_H; + } else { + SwipeableItemConstants.REACTION_CAN_SWIPE_BOTH_H; + } + } + + override fun onSwipeItemStarted(p0: ViewHolder?, p1: Int) { + } + + override fun onSetSwipeBackground(holder: ViewHolder?, position: Int, result: Int) { + + } + + internal class SwipedResultActionRemoveItem(private val adapter: PlayingQueueAdapter, + private val position: Int, + private val activity: AppCompatActivity) : SwipeResultActionRemoveItem() { + private var songToRemove: Song? = null + private val isPlaying: Boolean = MusicPlayerRemote.isPlaying + private val songProgressMillis = 0 + override fun onPerformAction() { + //currentlyShownSnackbar = null + } + + override fun onSlideAnimationEnd() { + //initializeSnackBar(adapter, position, activity, isPlaying) + songToRemove = adapter.dataSet[position] + //If song removed was the playing song, then play the next song + if (isPlaying(songToRemove!!)) { + playNextSong() + } + //Swipe animation is much smoother when we do the heavy lifting after it's completed + adapter.setSongToRemove(songToRemove!!) + removeFromQueue(songToRemove!!) + } + + } } diff --git a/app/src/main/java/code/name/monkey/retromusic/fragments/mainactivity/PlayingQueueFragment.kt b/app/src/main/java/code/name/monkey/retromusic/fragments/mainactivity/PlayingQueueFragment.kt index e498b996..ca85b8cc 100644 --- a/app/src/main/java/code/name/monkey/retromusic/fragments/mainactivity/PlayingQueueFragment.kt +++ b/app/src/main/java/code/name/monkey/retromusic/fragments/mainactivity/PlayingQueueFragment.kt @@ -23,8 +23,10 @@ import code.name.monkey.retromusic.adapter.song.PlayingQueueAdapter import code.name.monkey.retromusic.fragments.base.AbsLibraryPagerRecyclerViewFragment import code.name.monkey.retromusic.helper.MusicPlayerRemote import code.name.monkey.retromusic.util.ViewUtil -import com.h6ah4i.android.widget.advrecyclerview.animator.RefactoredDefaultItemAnimator +import com.h6ah4i.android.widget.advrecyclerview.animator.DraggableItemAnimator import com.h6ah4i.android.widget.advrecyclerview.draggable.RecyclerViewDragDropManager +import com.h6ah4i.android.widget.advrecyclerview.swipeable.RecyclerViewSwipeManager +import com.h6ah4i.android.widget.advrecyclerview.touchguard.RecyclerViewTouchActionGuardManager import com.h6ah4i.android.widget.advrecyclerview.utils.WrapperAdapterUtils import kotlinx.android.synthetic.main.activity_playing_queue.* @@ -33,28 +35,32 @@ import kotlinx.android.synthetic.main.activity_playing_queue.* */ class PlayingQueueFragment : AbsLibraryPagerRecyclerViewFragment() { - private var wrappedAdapter: RecyclerView.Adapter<*>? = null + private lateinit var wrappedAdapter: RecyclerView.Adapter<*> private var recyclerViewDragDropManager: RecyclerViewDragDropManager? = null + private var recyclerViewSwipeManager: RecyclerViewSwipeManager? = null + private var recyclerViewTouchActionGuardManager: RecyclerViewTouchActionGuardManager? = null override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) setupRecyclerView() } - private fun setupRecyclerView() { + recyclerViewTouchActionGuardManager = RecyclerViewTouchActionGuardManager() recyclerViewDragDropManager = RecyclerViewDragDropManager() - val animator = RefactoredDefaultItemAnimator() + recyclerViewSwipeManager = RecyclerViewSwipeManager() - wrappedAdapter = recyclerViewDragDropManager?.createWrappedAdapter(createAdapter()) + val animator = DraggableItemAnimator() + animator.supportsChangeAnimations = false; + wrappedAdapter = recyclerViewDragDropManager?.createWrappedAdapter(adapter!!) as RecyclerView.Adapter<*> + wrappedAdapter = recyclerViewSwipeManager?.createWrappedAdapter(wrappedAdapter) as RecyclerView.Adapter<*> + recyclerView().layoutManager = layoutManager + recyclerView().adapter = wrappedAdapter + recyclerView().itemAnimator = animator + recyclerViewTouchActionGuardManager?.attachRecyclerView(recyclerView) + recyclerViewDragDropManager?.attachRecyclerView(recyclerView) + recyclerViewSwipeManager?.attachRecyclerView(recyclerView) - - recyclerView.apply { - layoutManager = createLayoutManager() - adapter = wrappedAdapter - itemAnimator = animator - recyclerViewDragDropManager?.attachRecyclerView(this) - } - createLayoutManager().scrollToPositionWithOffset(MusicPlayerRemote.position + 1, 0) + layoutManager?.scrollToPositionWithOffset(MusicPlayerRemote.position + 1, 0) ViewUtil.setUpFastScrollRecyclerViewColor(requireContext(), recyclerView) } @@ -66,6 +72,11 @@ class PlayingQueueFragment : AbsLibraryPagerRecyclerViewFragment - - - + android:layout_height="wrap_content"> - - - - - - + android:layout_height="match_parent"> - + android:clickable="true" + android:focusable="true" + android:foreground="?rectSelector" + android:gravity="center_vertical" + android:minHeight="64dp" + android:orientation="horizontal" + tools:ignore="MissingPrefix"> - - + - + - \ No newline at end of file + + + + + + + + + + + + + + + + \ No newline at end of file