[Android] Playlists: fix deleting wrong indexes + avoid swipe to dismiss state issue

Nicolas Pomepuy git at videolan.org
Thu Nov 5 11:45:33 CET 2020


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Thu Nov  5 10:12:07 2020 +0100| [22d2b379a389a62e8941294052eb717a99035e99] | committer: Nicolas Pomepuy

Playlists: fix deleting wrong indexes + avoid swipe to dismiss state issue

Fixes #1520

> https://code.videolan.org/videolan/vlc-android/commit/22d2b379a389a62e8941294052eb717a99035e99
---

 .../src/org/videolan/vlc/gui/PlaylistActivity.kt          | 15 +++++++++++----
 .../vlc/gui/helpers/SwipeDragItemTouchHelperCallback.kt   |  4 +++-
 2 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/application/vlc-android/src/org/videolan/vlc/gui/PlaylistActivity.kt b/application/vlc-android/src/org/videolan/vlc/gui/PlaylistActivity.kt
index 2f6ce4926..d1e7c9b6e 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/PlaylistActivity.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/PlaylistActivity.kt
@@ -82,6 +82,7 @@ import java.util.*
 @ExperimentalCoroutinesApi
 open class PlaylistActivity : AudioPlayerContainerActivity(), IEventsHandler<MediaLibraryItem>, IListEventsHandler, ActionMode.Callback, View.OnClickListener, CtxActionReceiver {
 
+    private lateinit var itemTouchHelperCallback: SwipeDragItemTouchHelperCallback
     private lateinit var audioBrowserAdapter: AudioBrowserAdapter
     private val mediaLibrary = Medialibrary.getInstance()
     private lateinit var binding: PlaylistActivityBinding
@@ -115,6 +116,7 @@ open class PlaylistActivity : AudioPlayerContainerActivity(), IEventsHandler<Med
             @Suppress("UNCHECKED_CAST")
             (tracks as? PagedList<MediaLibraryItem>)?.let { audioBrowserAdapter.submitList(it) }
             menu.let { UiTools.updateSortTitles(it, viewModel.tracksProvider) }
+            itemTouchHelperCallback.swipeEnabled = true
         })
 
         viewModel.tracksProvider.liveHeaders.observe(this, {
@@ -122,7 +124,8 @@ open class PlaylistActivity : AudioPlayerContainerActivity(), IEventsHandler<Med
         })
         audioBrowserAdapter = AudioBrowserAdapter(MediaLibraryItem.TYPE_MEDIA, this, this, isPlaylist)
         if (isPlaylist) {
-            itemTouchHelper = ItemTouchHelper(SwipeDragItemTouchHelperCallback(audioBrowserAdapter))
+            itemTouchHelperCallback = SwipeDragItemTouchHelperCallback(audioBrowserAdapter)
+            itemTouchHelper = ItemTouchHelper(itemTouchHelperCallback)
             itemTouchHelper!!.attachToRecyclerView(binding.songs)
         } else {
             binding.songs.addItemDecoration(RecyclerSectionItemDecoration(resources.getDimensionPixelSize(R.dimen.recycler_section_header_height), true, viewModel.tracksProvider))
@@ -341,7 +344,6 @@ open class PlaylistActivity : AudioPlayerContainerActivity(), IEventsHandler<Med
         }
         val indexes = audioBrowserAdapter.multiSelectHelper.selectionMap
 
-        stopActionMode()
         when (item.itemId) {
             R.id.action_mode_audio_play -> MediaUtils.openList(this, tracks, 0)
             R.id.action_mode_audio_append -> MediaUtils.appendMedia(this, tracks)
@@ -349,9 +351,10 @@ open class PlaylistActivity : AudioPlayerContainerActivity(), IEventsHandler<Med
             R.id.action_mode_audio_info -> showInfoDialog(list[0] as MediaWrapper)
             R.id.action_mode_audio_share -> lifecycleScope.launch { share(list.map { it as MediaWrapper }) }
             R.id.action_mode_audio_set_song -> setRingtone(list[0] as MediaWrapper)
-            R.id.action_mode_audio_delete -> if (isPlaylist) removeFromPlaylist(tracks, indexes) else removeItems(tracks)
+            R.id.action_mode_audio_delete -> if (isPlaylist) removeFromPlaylist(tracks, indexes.toMutableList()) else removeItems(tracks)
             else -> return false
         }
+        stopActionMode()
         return true
     }
 
@@ -432,6 +435,8 @@ open class PlaylistActivity : AudioPlayerContainerActivity(), IEventsHandler<Med
         val itemsRemoved = HashMap<Int, Long>()
         val playlist = viewModel.playlist as? Playlist
                 ?: return
+
+        itemTouchHelperCallback.swipeEnabled = false
         lifecycleScope.launchWhenStarted {
             val tracks = withContext(Dispatchers.IO) { playlist.tracks }
             for (mediaItem in list) {
@@ -442,7 +447,9 @@ open class PlaylistActivity : AudioPlayerContainerActivity(), IEventsHandler<Med
                 }
             }
             withContext(Dispatchers.IO) {
-                for (index in indexes) playlist.remove(index)
+                for ((index, playlistIndex) in indexes.sortedBy { it }.withIndex()) {
+                    playlist.remove(playlistIndex - index)
+                }
             }
             UiTools.snackerWithCancel(findViewById(android.R.id.content), getString(R.string.removed_from_playlist_anonymous), null, {
                 for ((key, value) in itemsRemoved) {
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/helpers/SwipeDragItemTouchHelperCallback.kt b/application/vlc-android/src/org/videolan/vlc/gui/helpers/SwipeDragItemTouchHelperCallback.kt
index 2e0e4e99f..1a57b7d3a 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/helpers/SwipeDragItemTouchHelperCallback.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/helpers/SwipeDragItemTouchHelperCallback.kt
@@ -30,6 +30,7 @@ import org.videolan.vlc.interfaces.SwipeDragHelperAdapter
 class SwipeDragItemTouchHelperCallback(private val mAdapter: SwipeDragHelperAdapter, private val longPressDragEnable: Boolean = false) : ItemTouchHelper.Callback() {
     private var dragFrom = -1
     private var dragTo = -1
+    var swipeEnabled = true
 
     override fun getMovementFlags(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder): Int {
         val dragFlags = ItemTouchHelper.UP or ItemTouchHelper.DOWN
@@ -65,8 +66,9 @@ class SwipeDragItemTouchHelperCallback(private val mAdapter: SwipeDragHelperAdap
         super.clearView(recyclerView, viewHolder)
     }
 
+    override fun isItemViewSwipeEnabled() = swipeEnabled
+
     override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
         mAdapter.onItemDismiss(viewHolder.layoutPosition)
     }
-
 }



More information about the Android mailing list