Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swipe to remove from queue / loved #190

Merged
merged 1 commit into from Jun 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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