From c04f90a6ac25a99fc72eccee2a97740c3f151092 Mon Sep 17 00:00:00 2001 From: birdbird <6892457-tzugen@users.noreply.gitlab.com> Date: Fri, 9 Jun 2023 07:57:34 +0000 Subject: [PATCH] Small fixes for shuffle --- .../ultrasonic/fragment/PlayerFragment.kt | 3 ++- .../ultrasonic/service/MediaPlayerManager.kt | 23 +++++++++++++++---- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/PlayerFragment.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/PlayerFragment.kt index 71a08cd0..7f50a870 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/PlayerFragment.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/PlayerFragment.kt @@ -927,7 +927,8 @@ class PlayerFragment : // Swipe to delete from playlist @SuppressLint("NotifyDataSetChanged") override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) { - val pos = viewHolder.bindingAdapterPosition + val viewPos = viewHolder.bindingAdapterPosition + val pos = mediaPlayerManager.getUnshuffledIndexOf(viewPos) val item = mediaPlayerManager.getMediaItemAt(pos) // Remove the item from the list quickly diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/MediaPlayerManager.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/MediaPlayerManager.kt index a580b3d3..dba110ec 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/MediaPlayerManager.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/MediaPlayerManager.kt @@ -720,16 +720,25 @@ class MediaPlayerManager( /** * Loops over the timeline windows to find the entry which matches the given closure. * + * @param returnWindow Determines which of the two indexes of a match to return: + * True for the position in the playlist, False for position in the play order. * @param searchClosure Determines the condition which the searched for window needs to match. * @return the index of the window that satisfies the search condition, * or [C.INDEX_UNSET] if not found. */ - private fun getWindowIndexWhere(searchClosure: (Int, Int) -> Boolean): Int { + @Suppress("KotlinConstantConditions") + private fun getWindowIndexWhere( + returnWindow: Boolean, + searchClosure: (Int, Int) -> Boolean + ): Int { val timeline = controller?.currentTimeline!! var windowIndex = timeline.getFirstWindowIndex(true) var count = 0 + while (windowIndex != C.INDEX_UNSET) { - if (searchClosure(count, windowIndex)) return count + val match = searchClosure(count, windowIndex) + if (match && returnWindow) return windowIndex + if (match && !returnWindow) return count count++ windowIndex = timeline.getNextWindowIndex( windowIndex, REPEAT_MODE_OFF, true @@ -748,7 +757,10 @@ class MediaPlayerManager( * @return The index of the item in the shuffled timeline, or [C.INDEX_UNSET] if not found. */ fun getShuffledIndexOf(searchPosition: Int): Int { - return getWindowIndexWhere { _, windowIndex -> windowIndex == searchPosition } + return getWindowIndexWhere(false) { + _, windowIndex -> + windowIndex == searchPosition + } } /** @@ -760,7 +772,10 @@ class MediaPlayerManager( * @return the index of the item in the unshuffled timeline, or [C.INDEX_UNSET] if not found. */ fun getUnshuffledIndexOf(shufflePosition: Int): Int { - return getWindowIndexWhere { count, _ -> count == shufflePosition } + return getWindowIndexWhere(true) { + count, _ -> + count == shufflePosition + } } val mediaItemCount: Int