[Android] Fixes action mode selection order

Nicolas Pomepuy git at videolan.org
Tue Oct 20 14:52:36 CEST 2020


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Mon Oct 19 15:42:15 2020 +0200| [9c5bd999b80e163368378fdcc7d805f98676a4cf] | committer: Nicolas Pomepuy

Fixes action mode selection order

Fixes #1507

> https://code.videolan.org/videolan/vlc-android/commit/9c5bd999b80e163368378fdcc7d805f98676a4cf
---

 .../java/org/videolan/tools/MultiSelectHelper.kt   | 32 +++++++++++-----------
 .../src/org/videolan/vlc/gui/MoreFragment.kt       | 14 +++++-----
 .../src/org/videolan/vlc/gui/PlaylistActivity.kt   |  5 +---
 .../vlc/gui/browser/MediaBrowserFragment.kt        | 14 +++++-----
 4 files changed, 31 insertions(+), 34 deletions(-)

diff --git a/application/tools/src/main/java/org/videolan/tools/MultiSelectHelper.kt b/application/tools/src/main/java/org/videolan/tools/MultiSelectHelper.kt
index c54bd46d6b..db9a845c3b 100644
--- a/application/tools/src/main/java/org/videolan/tools/MultiSelectHelper.kt
+++ b/application/tools/src/main/java/org/videolan/tools/MultiSelectHelper.kt
@@ -8,17 +8,17 @@ import kotlin.math.min
 
 class MultiSelectHelper<T>(val adapter: MultiSelectAdapter<T>, private val payloadvalue: Any = 0) {
 
-    val selectionMap = SparseBooleanArray()
+    val selectionMap = ArrayList<Int>()
     var inActionMode = false
 
     fun getSelection(): List<T> {
-        val list = ArrayList<T>(selectionMap.size())
-        for (i in 0 until selectionMap.size()) adapter.getItem(selectionMap.keyAt(i))?.let { list.add(it) }
+        val list = ArrayList<T>(selectionMap.size)
+        for (i in 0 until selectionMap.size) adapter.getItem(selectionMap[i])?.let { list.add(it) }
         return list
     }
 
     @MainThread
-    fun getSelectionCount() = selectionMap.size()
+    fun getSelectionCount() = selectionMap.size
 
     fun toggleActionMode(inActionMode:Boolean, itemCount:Int) {
         this.inActionMode = inActionMode
@@ -26,16 +26,16 @@ class MultiSelectHelper<T>(val adapter: MultiSelectAdapter<T>, private val paylo
     }
 
     fun toggleSelection(position: Int, forceShift: Boolean = false) {
-        if ((KeyHelper.isShiftPressed || forceShift) && selectionMap.size() != 0) {
+        if ((KeyHelper.isShiftPressed || forceShift) && selectionMap.size != 0) {
             val positions = HashSet<Int>()
-            for (i in 0 until selectionMap.size()) {
-                positions.add(selectionMap.keyAt(i))
+            for (i in 0 until selectionMap.size) {
+                positions.add(selectionMap[i])
             }
-            val firstPosition = selectionMap.keyAt(0)
+            val firstPosition = selectionMap[0]
             selectionMap.clear()
 
             for (i in min(firstPosition, position)..max(firstPosition, position)) {
-                selectionMap.append(i, true)
+                selectionMap.add(i)
                 positions.add(i)
             }
 
@@ -44,20 +44,20 @@ class MultiSelectHelper<T>(val adapter: MultiSelectAdapter<T>, private val paylo
             }
             return
         }
-        if (isSelected(position)) selectionMap.delete(position)
-        else selectionMap.append(position, true)
+        if (isSelected(position)) selectionMap.remove(position)
+        else selectionMap.add(position)
         adapter.notifyItemChanged(position, payloadvalue)
     }
 
     fun clearSelection() {
-        if (selectionMap.size() == 0) return
-        val start = selectionMap.keyAt(0)
-        val count = selectionMap.keyAt(selectionMap.size() - 1) - start + 1
+        if (selectionMap.size == 0) return
+        val start = selectionMap.minOrNull()
+        val count = selectionMap.maxOrNull()
         selectionMap.clear()
-        adapter.notifyItemRangeChanged(start, count, payloadvalue)
+        adapter.notifyItemRangeChanged(start ?: 0, count ?: 0, payloadvalue)
     }
 
-    fun isSelected(position: Int) = selectionMap.get(position, false)
+    fun isSelected(position: Int) = selectionMap.contains(position)
 }
 
 interface MultiSelectAdapter<T> {
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/MoreFragment.kt b/application/vlc-android/src/org/videolan/vlc/gui/MoreFragment.kt
index 09a2130496..91084f4e07 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/MoreFragment.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/MoreFragment.kt
@@ -77,11 +77,11 @@ class MoreFragment : BaseFragment(), IRefreshable, IHistory,
     private val historyAdapter: HistoryAdapter = HistoryAdapter(true)
     override fun hasFAB() = false
     fun getMultiHelper(): MultiSelectHelper<HistoryModel>? = historyAdapter.multiSelectHelper as? MultiSelectHelper<HistoryModel>
-    private var savedSelection = SparseBooleanArray()
+    private var savedSelection = ArrayList<Int>()
 
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
-        (savedInstanceState?.getParcelable<SparseBooleanArrayParcelable>(KEY_SELECTION))?.let { savedSelection = it.data }
+        (savedInstanceState?.getIntegerArrayList(KEY_SELECTION))?.let { savedSelection = it }
     }
 
     override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
@@ -190,7 +190,7 @@ class MoreFragment : BaseFragment(), IRefreshable, IHistory,
 
     override fun onSaveInstanceState(outState: Bundle) {
         getMultiHelper()?.let {
-            outState.putParcelable(KEY_SELECTION, SparseBooleanArrayParcelable(it.selectionMap))
+            outState.putIntegerArrayList(KEY_SELECTION, it.selectionMap)
         }
         super.onSaveInstanceState(outState)
     }
@@ -248,12 +248,12 @@ class MoreFragment : BaseFragment(), IRefreshable, IHistory,
     private fun restoreMultiSelectHelper() {
         getMultiHelper()?.let {
 
-            if (savedSelection.size() > 0) {
+            if (savedSelection.size > 0) {
                 var hasOneSelected = false
-                for (i in 0 until savedSelection.size()) {
+                for (i in 0 until savedSelection.size) {
 
-                    it.selectionMap.append(savedSelection.keyAt(i), savedSelection.valueAt(i))
-                    if (savedSelection.valueAt(i)) hasOneSelected = true
+                    it.selectionMap.addAll(savedSelection)
+                    hasOneSelected = savedSelection.isNotEmpty()
                 }
                 if (hasOneSelected) startActionMode()
                 savedSelection.clear()
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 3c2e56a161..63cc4ddbef 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/PlaylistActivity.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/PlaylistActivity.kt
@@ -339,10 +339,7 @@ open class PlaylistActivity : AudioPlayerContainerActivity(), IEventsHandler<Med
             Toast.makeText(this, "DOWN !", Toast.LENGTH_SHORT).show()
             return true
         }
-        val indexes = ArrayList<Int>()
-        for (i in 0 until audioBrowserAdapter.multiSelectHelper.selectionMap.size()) {
-            indexes.add(audioBrowserAdapter.multiSelectHelper.selectionMap.keyAt(i))
-        }
+        val indexes = audioBrowserAdapter.multiSelectHelper.selectionMap
 
         stopActionMode()
         when (item.itemId) {
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/browser/MediaBrowserFragment.kt b/application/vlc-android/src/org/videolan/vlc/gui/browser/MediaBrowserFragment.kt
index 31c089a120..90a92df293 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/browser/MediaBrowserFragment.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/browser/MediaBrowserFragment.kt
@@ -66,7 +66,7 @@ abstract class MediaBrowserFragment<T : SortableModel> : BaseFragment(), Filtera
 
     private lateinit var searchButtonView: View
     lateinit var mediaLibrary: Medialibrary
-    private var savedSelection = SparseBooleanArray()
+    private var savedSelection = ArrayList<Int>()
     private val transition = ChangeBounds().apply {
         interpolator = AccelerateDecelerateInterpolator()
         duration = 300
@@ -80,7 +80,7 @@ abstract class MediaBrowserFragment<T : SortableModel> : BaseFragment(), Filtera
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
         mediaLibrary = Medialibrary.getInstance()
-        (savedInstanceState?.getParcelable<SparseBooleanArrayParcelable>(KEY_SELECTION))?.let { savedSelection = it.data }
+        (savedInstanceState?.getIntegerArrayList(KEY_SELECTION))?.let { savedSelection = it }
     }
 
     override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
@@ -113,7 +113,7 @@ abstract class MediaBrowserFragment<T : SortableModel> : BaseFragment(), Filtera
 
     override fun onSaveInstanceState(outState: Bundle) {
         getMultiHelper()?.let {
-            outState.putParcelable(KEY_SELECTION, SparseBooleanArrayParcelable(it.selectionMap))
+            outState.putIntegerArrayList(KEY_SELECTION, it.selectionMap)
         }
         super.onSaveInstanceState(outState)
     }
@@ -229,12 +229,12 @@ abstract class MediaBrowserFragment<T : SortableModel> : BaseFragment(), Filtera
     fun restoreMultiSelectHelper() {
         getMultiHelper()?.let {
 
-            if (savedSelection.size() > 0) {
+            if (savedSelection.size > 0) {
                 var hasOneSelected = false
-                for (i in 0 until savedSelection.size()) {
+                for (i in 0 until savedSelection.size) {
 
-                    it.selectionMap.append(savedSelection.keyAt(i), savedSelection.valueAt(i))
-                    if (savedSelection.valueAt(i)) hasOneSelected = true
+                    it.selectionMap.addAll(savedSelection)
+                    hasOneSelected = savedSelection.isNotEmpty()
                 }
                 if (hasOneSelected) startActionMode()
                 savedSelection.clear()



More information about the Android mailing list