[Android] Batch select implementation for Videos / History / Browser

Nicolas Pomepuy git at videolan.org
Thu Jun 27 11:02:33 CEST 2019


vlc-android | branch: master | Nicolas Pomepuy <nicolas.pomepuy at gmail.com> | Thu Jun 27 06:29:07 2019 +0200| [6a158ecd7e5b6c69d4faece38c08a503f2c08cd7] | committer: Nicolas Pomepuy

Batch select implementation for Videos / History / Browser

> https://code.videolan.org/videolan/vlc-android/commit/6a158ecd7e5b6c69d4faece38c08a503f2c08cd7
---

 .../src/org/videolan/vlc/gui/HistoryAdapter.kt     | 22 +++++--------
 .../src/org/videolan/vlc/gui/HistoryFragment.kt    | 36 ++++++++++------------
 .../vlc/gui/browser/BaseBrowserFragment.kt         |  5 ++-
 .../videolan/vlc/gui/video/VideoGridFragment.kt    |  3 +-
 4 files changed, 27 insertions(+), 39 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/HistoryAdapter.kt b/vlc-android/src/org/videolan/vlc/gui/HistoryAdapter.kt
index 23708b85c..563bd0f3a 100644
--- a/vlc-android/src/org/videolan/vlc/gui/HistoryAdapter.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/HistoryAdapter.kt
@@ -26,27 +26,20 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi
 import kotlinx.coroutines.ObsoleteCoroutinesApi
 import org.videolan.medialibrary.media.MediaLibraryItem
 import org.videolan.medialibrary.media.MediaWrapper
+import org.videolan.tools.MultiSelectAdapter
+import org.videolan.tools.MultiSelectHelper
 import org.videolan.vlc.databinding.HistoryItemBinding
 import org.videolan.vlc.gui.helpers.SelectorViewHolder
 import org.videolan.vlc.gui.helpers.getMediaIconDrawable
 import org.videolan.vlc.interfaces.IEventsHandler
+import org.videolan.vlc.util.UPDATE_SELECTION
 import org.videolan.vlc.util.Util
-import java.util.*
 
 @ObsoleteCoroutinesApi
 @ExperimentalCoroutinesApi
-class HistoryAdapter(private val mEventsHandler: IEventsHandler) : DiffUtilAdapter<MediaWrapper, HistoryAdapter.ViewHolder>() {
+class HistoryAdapter(private val mEventsHandler: IEventsHandler) : DiffUtilAdapter<MediaWrapper, HistoryAdapter.ViewHolder>(), MultiSelectAdapter<MediaWrapper> {
     private var mLayoutInflater: LayoutInflater? = null
-
-    val selection: List<MediaWrapper>
-        get() {
-            val selection = LinkedList<MediaWrapper>()
-            for (media in dataset) {
-                if (media.hasStateFlags(MediaLibraryItem.FLAG_SELECTED))
-                    selection.add(media)
-            }
-            return selection
-        }
+    var multiSelectHelper: MultiSelectHelper<MediaWrapper> = MultiSelectHelper(this, UPDATE_SELECTION)
 
     inner class ViewHolder(binding: HistoryItemBinding) : SelectorViewHolder<HistoryItemBinding>(binding) {
 
@@ -83,17 +76,16 @@ class HistoryAdapter(private val mEventsHandler: IEventsHandler) : DiffUtilAdapt
 
     override fun onBindViewHolder(holder: ViewHolder, position: Int) {
         val media = getItem(position)
-        val isSelected = media.hasStateFlags(MediaLibraryItem.FLAG_SELECTED)
         holder.binding.media = media
         holder.binding.cover = getMediaIconDrawable(holder.itemView.context, media.type)
-        holder.selectView(isSelected)
+        holder.selectView(multiSelectHelper.isSelected(position))
     }
 
     override fun onBindViewHolder(holder: ViewHolder, position: Int, payloads: List<Any>) {
         if (Util.isListEmpty(payloads))
             super.onBindViewHolder(holder, position, payloads)
         else
-            holder.selectView((payloads[0] as MediaLibraryItem).hasStateFlags(MediaLibraryItem.FLAG_SELECTED))
+            holder.selectView(multiSelectHelper.isSelected(position))
     }
 
     override fun getItemId(arg0: Int): Long {
diff --git a/vlc-android/src/org/videolan/vlc/gui/HistoryFragment.kt b/vlc-android/src/org/videolan/vlc/gui/HistoryFragment.kt
index 7d1f98de7..68fb7a4ce 100644
--- a/vlc-android/src/org/videolan/vlc/gui/HistoryFragment.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/HistoryFragment.kt
@@ -35,6 +35,7 @@ import kotlinx.coroutines.ObsoleteCoroutinesApi
 import org.videolan.medialibrary.media.MediaLibraryItem
 import org.videolan.medialibrary.media.MediaWrapper
 import org.videolan.tools.KeyHelper
+import org.videolan.tools.MultiSelectHelper
 import org.videolan.vlc.R
 import org.videolan.vlc.gui.browser.MediaBrowserFragment
 import org.videolan.vlc.gui.helpers.UiTools
@@ -51,6 +52,7 @@ private const val TAG = "VLC/HistoryFragment"
 @ExperimentalCoroutinesApi
 class HistoryFragment : MediaBrowserFragment<HistoryModel>(), IRefreshable, IHistory, SwipeRefreshLayout.OnRefreshListener, IEventsHandler {
 
+    private lateinit var multiSelectHelper: MultiSelectHelper<MediaWrapper>
     private val historyAdapter: HistoryAdapter = HistoryAdapter(this)
 
     @TargetApi(Build.VERSION_CODES.HONEYCOMB)
@@ -61,10 +63,12 @@ class HistoryFragment : MediaBrowserFragment<HistoryModel>(), IRefreshable, IHis
     override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
         super.onViewCreated(view, savedInstanceState)
         viewModel = ViewModelProviders.of(requireActivity(), HistoryModel.Factory(requireContext())).get(HistoryModel::class.java)
-        viewModel.dataset.observe(this, Observer<List<MediaWrapper>> { list ->  list?.let {
-            historyAdapter.update(it)
-            updateEmptyView()
-        } })
+        viewModel.dataset.observe(this, Observer<List<MediaWrapper>> { list ->
+            list?.let {
+                historyAdapter.update(it)
+                updateEmptyView()
+            }
+        })
     }
 
     override fun onStart() {
@@ -80,6 +84,8 @@ class HistoryFragment : MediaBrowserFragment<HistoryModel>(), IRefreshable, IHis
         list.nextFocusLeftId = android.R.id.list
         list.nextFocusRightId = android.R.id.list
         list.nextFocusForwardId = android.R.id.list
+
+        multiSelectHelper = historyAdapter.multiSelectHelper
         list.requestFocus()
         registerForContextMenu(list)
         swipeRefreshLayout!!.setOnRefreshListener(this)
@@ -146,7 +152,7 @@ class HistoryFragment : MediaBrowserFragment<HistoryModel>(), IRefreshable, IHis
     }
 
     override fun onPrepareActionMode(mode: ActionMode, menu: Menu): Boolean {
-        val selectionCount = historyAdapter.selection.size
+        val selectionCount = multiSelectHelper.getSelectionCount()
         if (selectionCount == 0) {
             stopActionMode()
             return false
@@ -157,7 +163,7 @@ class HistoryFragment : MediaBrowserFragment<HistoryModel>(), IRefreshable, IHis
     }
 
     override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean {
-        val selection = historyAdapter.selection
+        val selection = multiSelectHelper.getSelection()
         if (selection.isNotEmpty()) {
             when (item.itemId) {
                 R.id.action_history_play -> MediaUtils.openList(activity, selection, 0)
@@ -175,14 +181,7 @@ class HistoryFragment : MediaBrowserFragment<HistoryModel>(), IRefreshable, IHis
 
     override fun onDestroyActionMode(mode: ActionMode) {
         actionMode = null
-        var index = -1
-        for (media in viewModel.dataset.value) {
-            ++index
-            if (media.hasStateFlags(MediaLibraryItem.FLAG_SELECTED)) {
-                media.removeStateFlags(MediaLibraryItem.FLAG_SELECTED)
-                historyAdapter.notifyItemChanged(index, media)
-            }
-        }
+        multiSelectHelper.clearSelection()
     }
 
     override fun onClick(v: View, position: Int, item: MediaLibraryItem) {
@@ -191,7 +190,7 @@ class HistoryFragment : MediaBrowserFragment<HistoryModel>(), IRefreshable, IHis
             return
         }
         if (actionMode != null) {
-            item.toggleStateFlag(MediaLibraryItem.FLAG_SELECTED)
+            multiSelectHelper.toggleSelection(position)
             historyAdapter.notifyItemChanged(position, item)
             invalidateActionMode()
             return
@@ -201,14 +200,13 @@ class HistoryFragment : MediaBrowserFragment<HistoryModel>(), IRefreshable, IHis
     }
 
     override fun onLongClick(v: View, position: Int, item: MediaLibraryItem): Boolean {
-        if (actionMode != null) return false
-        item.toggleStateFlag(MediaLibraryItem.FLAG_SELECTED)
+        multiSelectHelper.toggleSelection(position, true)
         historyAdapter.notifyItemChanged(position, item)
-        startActionMode()
+        if (actionMode == null) startActionMode()
+        invalidateActionMode()
         return true
     }
 
-
     override fun onImageClick(v: View, position: Int, item: MediaLibraryItem) {
         if (actionMode != null) {
             onClick(v, position, item)
diff --git a/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.kt b/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.kt
index 69c2507a4..951ee4507 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.kt
@@ -405,14 +405,13 @@ abstract class BaseBrowserFragment : MediaBrowserFragment<BrowserModel>(), IRefr
     }
 
     override fun onLongClick(v: View, position: Int, item: MediaLibraryItem): Boolean {
-        if (actionMode != null || item.itemType != MediaLibraryItem.TYPE_MEDIA) return false
+        if (item.itemType != MediaLibraryItem.TYPE_MEDIA) return false
         val mediaWrapper = item as MediaWrapper
         if (mediaWrapper.type == MediaWrapper.TYPE_AUDIO ||
                 mediaWrapper.type == MediaWrapper.TYPE_VIDEO ||
                 mediaWrapper.type == MediaWrapper.TYPE_DIR) {
-            if (actionMode != null) return false
             adapter.multiSelectHelper.toggleSelection(position, true)
-            startActionMode()
+            if (actionMode == null) startActionMode()
         } else onCtxClick(v, position, item)
         return true
     }
diff --git a/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.kt b/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.kt
index a3586c1f9..18fe6001d 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.kt
@@ -335,9 +335,8 @@ class VideoGridFragment : MediaBrowserFragment<VideosViewModel>(), SwipeRefreshL
     }
 
     override fun onLongClick(v: View, position: Int, item: MediaLibraryItem): Boolean {
-        if (actionMode != null) return false
         multiSelectHelper.toggleSelection(position, true)
-        startActionMode()
+        if (actionMode == null) startActionMode()
         return true
     }
 



More information about the Android mailing list