Skip to content

Commit

Permalink
Merge pull request #190 from Drhaal/swipe-gesture
Browse files Browse the repository at this point in the history
Swipe to remove from queue / loved
  • Loading branch information
enricocid authored Jun 7, 2020
2 parents 7685706 + df13030 commit 875686a
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -99,6 +100,10 @@ class LovedSongsAdapter(
return@setOnLongClickListener true
}
}

}


}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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)
}
}
}

Expand Down

0 comments on commit 875686a

Please sign in to comment.