From df130305bd4801c349cb2d50934a9c8f9df89dd1 Mon Sep 17 00:00:00 2001 From: drhaal Date: Wed, 29 Apr 2020 01:08:44 +0200 Subject: [PATCH] add swipe gesture to remove from loved/queud songs --- .../adapters/LovedSongsAdapter.kt | 5 + .../musicplayergo/helpers/DialogHelper.kt | 92 +++++++++++++++++-- 2 files changed, 87 insertions(+), 10 deletions(-) diff --git a/project/app/src/main/java/com/iven/musicplayergo/adapters/LovedSongsAdapter.kt b/project/app/src/main/java/com/iven/musicplayergo/adapters/LovedSongsAdapter.kt index 2b0e4d6d3..fd49fbd50 100644 --- a/project/app/src/main/java/com/iven/musicplayergo/adapters/LovedSongsAdapter.kt +++ b/project/app/src/main/java/com/iven/musicplayergo/adapters/LovedSongsAdapter.kt @@ -5,6 +5,7 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.TextView +import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.RecyclerView import com.afollestad.materialdialogs.MaterialDialog import com.iven.musicplayergo.MusicRepository @@ -99,6 +100,10 @@ class LovedSongsAdapter( return@setOnLongClickListener true } } + } + + } } + diff --git a/project/app/src/main/java/com/iven/musicplayergo/helpers/DialogHelper.kt b/project/app/src/main/java/com/iven/musicplayergo/helpers/DialogHelper.kt index 307bad824..ec34746f9 100644 --- a/project/app/src/main/java/com/iven/musicplayergo/helpers/DialogHelper.kt +++ b/project/app/src/main/java/com/iven/musicplayergo/helpers/DialogHelper.kt @@ -5,6 +5,8 @@ import android.view.Gravity import android.view.View import androidx.appcompat.widget.PopupMenu import androidx.recyclerview.widget.GridLayoutManager +import androidx.recyclerview.widget.ItemTouchHelper +import androidx.recyclerview.widget.RecyclerView import com.afollestad.materialdialogs.LayoutMode import com.afollestad.materialdialogs.MaterialDialog import com.afollestad.materialdialogs.bottomsheets.BottomSheet @@ -32,10 +34,10 @@ object DialogHelper { ) = MaterialDialog(context, BottomSheet(LayoutMode.WRAP_CONTENT)).show { title(R.string.queue) + val queueDialog = this + val queueAdapter = QueueAdapter(context, this, mediaPlayerHolder) - customListAdapter( - QueueAdapter(context, this, mediaPlayerHolder) - ) + customListAdapter(queueAdapter) val recyclerView = getRecyclerView() @@ -57,6 +59,22 @@ object DialogHelper { } } } + + addBidirectionalSwipeHandler(recyclerView) + { viewHolder: RecyclerView.ViewHolder, + _: Int -> + mediaPlayerHolder.apply { + queueSongs.removeAt(viewHolder.adapterPosition) + queueAdapter.swapQueueSongs(queueSongs) + + if (queueSongs.isEmpty()) { + isQueue = false + mediaPlayerInterface.onQueueStartedOrEnded(false) + queueDialog.dismiss() + } + queueAdapter.notifyItemRemoved(viewHolder.adapterPosition) + } + } } @JvmStatic @@ -136,14 +154,16 @@ object DialogHelper { title(R.string.loved_songs) + val lovedSongsAdapter = LovedSongsAdapter( + context, + this, + uiControlInterface, + mediaPlayerHolder, + musicRepository + ) + customListAdapter( - LovedSongsAdapter( - context, - this, - uiControlInterface, - mediaPlayerHolder, - musicRepository - ) + lovedSongsAdapter ) val recyclerView = getRecyclerView() @@ -169,6 +189,58 @@ object DialogHelper { } } } + + addBidirectionalSwipeHandler(recyclerView) + { viewHolder: RecyclerView.ViewHolder, + _: Int -> + val lovedSongs = goPreferences.lovedSongs?.toMutableList() + lovedSongs?.removeAt(viewHolder.adapterPosition) + goPreferences.lovedSongs = lovedSongs + lovedSongsAdapter.swapSongs(lovedSongs) + lovedSongsAdapter.notifyItemRemoved(viewHolder.adapterPosition) + } + } + } + + private fun addBidirectionalSwipeHandler( + swipeHolder: RecyclerView, + onSwiped: ( + viewHolder: RecyclerView.ViewHolder, + direction: Int + ) -> Unit + ) { + val swipeLeftCallback = instantiateSwipeHandler(ItemTouchHelper.RIGHT, onSwiped) + val swipeLeftHelper = ItemTouchHelper(swipeLeftCallback) + swipeLeftHelper.attachToRecyclerView(swipeHolder) + val swipeRightCallback = instantiateSwipeHandler(ItemTouchHelper.LEFT, onSwiped) + val swipeRightHelper = ItemTouchHelper(swipeRightCallback) + swipeRightHelper.attachToRecyclerView(swipeHolder) + } + + private fun instantiateSwipeHandler( + direction: Int, + onSwiped: ( + viewHolder: RecyclerView.ViewHolder, + direction: Int + ) -> Unit + ): ItemTouchHelper.SimpleCallback { + return object : ItemTouchHelper.SimpleCallback( + 0, + direction + ) { + + override fun onMove( + recyclerView: RecyclerView, + viewHolder: RecyclerView.ViewHolder, + target: RecyclerView.ViewHolder + ): Boolean = false + + override fun onSwiped( + viewHolder: RecyclerView.ViewHolder, + direction: Int + ) { + onSwiped(viewHolder, direction) + } } }