[Android] Remove the right item when filtering

Nicolas Pomepuy git at videolan.org
Tue Mar 25 14:59:34 UTC 2025


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Wed Mar 19 13:14:28 2025 +0100| [84181514bca55948033ccdf65c256670c13568fc] | committer: Duncan McNamara

Remove the right item when filtering

> https://code.videolan.org/videolan/vlc-android/commit/84181514bca55948033ccdf65c256670c13568fc
---

 .../org/videolan/vlc/gui/audio/PlaylistAdapter.kt  | 22 ++++++++++++----------
 .../org/videolan/vlc/viewmodels/PlaylistModel.kt   | 14 ++++++++++++--
 2 files changed, 24 insertions(+), 12 deletions(-)

diff --git a/application/vlc-android/src/org/videolan/vlc/gui/audio/PlaylistAdapter.kt b/application/vlc-android/src/org/videolan/vlc/gui/audio/PlaylistAdapter.kt
index 820fd19421..20a9555d7b 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/audio/PlaylistAdapter.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/audio/PlaylistAdapter.kt
@@ -203,18 +203,20 @@ class PlaylistAdapter(private val player: IPlayer) : DiffUtilAdapter<MediaWrappe
     }
 
     override fun onItemDismiss(position: Int) {
-        val media = getItem(position)
-        val message = String.format(AppContextProvider.appResources.getString(R.string.remove_playlist_item), media.title)
-        if (player is Fragment) {
-            UiTools.snackerWithCancel(player.requireActivity(), message, overAudioPlayer = true, action = {}) {
-                 model?.run { insertMedia(position, media) }
-            }
-        } else if (player is Activity) {
-            UiTools.snackerWithCancel(player, message, action = {}) {
-                model?.run { insertMedia(position, media) }
+        model?.let {
+            val media = getItem(position)
+            val message = String.format(AppContextProvider.appResources.getString(R.string.remove_playlist_item), media.title)
+            if (player is Fragment) {
+                UiTools.snackerWithCancel(player.requireActivity(), message, overAudioPlayer = true, action = {}) {
+                    model?.run { insertMedia(it.getOriginalPosition(position), media) }
+                }
+            } else if (player is Activity) {
+                UiTools.snackerWithCancel(player, message, action = {}) {
+                    model?.run { insertMedia(it.getOriginalPosition(position), media) }
+                }
             }
+            remove(position)
         }
-        remove(position)
     }
 
     fun setModel(model: PlaylistModel) {
diff --git a/application/vlc-android/src/org/videolan/vlc/viewmodels/PlaylistModel.kt b/application/vlc-android/src/org/videolan/vlc/viewmodels/PlaylistModel.kt
index 4943e43508..218ffaeec8 100644
--- a/application/vlc-android/src/org/videolan/vlc/viewmodels/PlaylistModel.kt
+++ b/application/vlc-android/src/org/videolan/vlc/viewmodels/PlaylistModel.kt
@@ -51,7 +51,7 @@ class PlaylistModel : ViewModel(), PlaybackService.Callback by EmptyPBSCallback
     private var originalDataset : MutableList<MediaWrapper>? = null
     val selection : Int
         get() = if (filtering) -1 else service?.playlistManager?.currentIndex ?: -1
-    private var filtering = false
+    var filtering = false
         set(value) {
             field = value
             filteringState.value = value
@@ -95,7 +95,17 @@ class PlaylistModel : ViewModel(), PlaybackService.Callback by EmptyPBSCallback
 
     fun insertMedia(position: Int, media: MediaWrapper) = service?.insertItem(position, media)
 
-    fun remove(position: Int) = service?.remove(position)
+    /**
+     * Get original position even if current list is filtered
+     *
+     * @param position the current position in the filtered list or not
+     * @return the original position (in the unfiltered list)
+     */
+    fun getOriginalPosition(position: Int) = if (filtering && originalDataset != null) {
+        originalDataset!!.indexOf(dataset.get(position))
+    } else position
+
+    fun remove(position: Int) = service?.remove(getOriginalPosition(position))
 
     fun move(from: Int, to: Int) = service?.moveItem(from, to)
 



More information about the Android mailing list