[Android] Allow moving videos from a group to another

Nicolas Pomepuy git at videolan.org
Mon Oct 26 09:34:01 CET 2020


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Thu Oct 22 10:28:37 2020 +0200| [8c991851b9195ff1ae996a7017a3519f0af8d1ba] | committer: Nicolas Pomepuy

Allow moving videos from a group to another

> https://code.videolan.org/videolan/vlc-android/commit/8c991851b9195ff1ae996a7017a3519f0af8d1ba
---

 .../vlc-android/res/menu/action_mode_video.xml     |  6 ++++++
 .../videolan/vlc/gui/dialogs/AddToGroupDialog.kt   |  8 ++++++--
 .../videolan/vlc/gui/video/VideoGridFragment.kt    | 22 +++++++++++++++-------
 3 files changed, 27 insertions(+), 9 deletions(-)

diff --git a/application/vlc-android/res/menu/action_mode_video.xml b/application/vlc-android/res/menu/action_mode_video.xml
index 5c4594d0ad..535cec616c 100644
--- a/application/vlc-android/res/menu/action_mode_video.xml
+++ b/application/vlc-android/res/menu/action_mode_video.xml
@@ -48,4 +48,10 @@
             android:title="@string/remove_from_group"
             android:icon="@drawable/ic_remove_from_group"
             app:showAsAction="ifRoom" />
+
+    <item
+            android:id="@+id/action_add_to_group"
+            android:title="@string/add_to_group"
+            android:icon="@drawable/ic_add_to_group"
+            app:showAsAction="always" />
 </menu>
\ No newline at end of file
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/dialogs/AddToGroupDialog.kt b/application/vlc-android/src/org/videolan/vlc/gui/dialogs/AddToGroupDialog.kt
index 9d416374fa..b798e61451 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/dialogs/AddToGroupDialog.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/dialogs/AddToGroupDialog.kt
@@ -28,9 +28,10 @@ import android.os.Bundle
 import android.view.LayoutInflater
 import android.view.View
 import android.view.ViewGroup
-import androidx.lifecycle.ViewModelProvider
 import androidx.recyclerview.widget.LinearLayoutManager
 import com.google.android.material.bottomsheet.BottomSheetBehavior.STATE_EXPANDED
+import kotlinx.coroutines.ExperimentalCoroutinesApi
+import kotlinx.coroutines.ObsoleteCoroutinesApi
 import kotlinx.coroutines.launch
 import org.videolan.medialibrary.interfaces.Medialibrary
 import org.videolan.medialibrary.interfaces.media.MediaWrapper
@@ -48,6 +49,8 @@ import org.videolan.vlc.viewmodels.mobile.VideoGroupingType
 import org.videolan.vlc.viewmodels.mobile.VideosViewModel
 import java.util.*
 
+ at ExperimentalCoroutinesApi
+ at ObsoleteCoroutinesApi
 class AddToGroupDialog : VLCBottomSheetDialogFragment(), SimpleAdapter.ClickHandler {
     override fun getDefaultState(): Int = STATE_EXPANDED
 
@@ -98,7 +101,8 @@ class AddToGroupDialog : VLCBottomSheetDialogFragment(), SimpleAdapter.ClickHand
 
         binding.list.layoutManager = LinearLayoutManager(view.context)
         binding.list.adapter = adapter
-        val viewModel = ViewModelProvider(requireActivity(), VideosViewModel.Factory(requireContext(), VideoGroupingType.NAME, null, null)).get(VideosViewModel::class.java)
+        //we have to create the viewmodel that way to avoid the cache from ViewModelProvider which will send the model from the calling activity that may have a different groupingType
+        val viewModel = VideosViewModel.Factory(requireContext(), VideoGroupingType.NAME, null, null).create(VideosViewModel::class.java)
         viewModel.provider.pagedList.observe(viewLifecycleOwner, {
 
             adapter.submitList(it.filter { group -> group is VideoGroup && group.mediaCount() > 1 }.apply {
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.kt b/application/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.kt
index 32867e746c..59a2805e40 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.kt
@@ -322,6 +322,7 @@ class VideoGridFragment : MediaBrowserFragment<VideosViewModel>(), SwipeRefreshL
                 menu.findItem(R.id.action_video_append).isVisible = PlaylistManager.hasMedia()
                 menu.findItem(R.id.action_video_info).isVisible = count == 1
                 menu.findItem(R.id.action_remove_from_group).isVisible = viewModel.group != null
+                menu.findItem(R.id.action_add_to_group).isVisible = viewModel.group != null && count > 1
             }
             VideoGroupingType.NAME -> {
                 menu.findItem(R.id.action_ungroup).isVisible = !multiSelectHelper.getSelection().any { it !is VideoGroup }
@@ -358,6 +359,7 @@ class VideoGridFragment : MediaBrowserFragment<VideosViewModel>(), SwipeRefreshL
                         R.id.action_video_delete -> removeItems(list)
                         R.id.action_remove_from_group -> viewModel.removeFromGroup(list)
                         R.id.action_ungroup -> viewModel.ungroup(list)
+                        R.id.action_add_to_group -> addToGroup(list)
                         else -> {
                             stopActionMode()
                             return false
@@ -386,13 +388,7 @@ class VideoGridFragment : MediaBrowserFragment<VideosViewModel>(), SwipeRefreshL
                     R.id.action_group_similar -> lifecycleScope.launch { viewModel.groupSimilar(selection.getAll().first()) }
                     R.id.action_ungroup -> viewModel.ungroup(selection.first() as VideoGroup)
                     R.id.action_rename -> renameGroup(selection.first() as VideoGroup)
-                    R.id.action_add_to_group -> requireActivity().addToGroup(selection.getAll()) {
-                            lifecycleScope.launch {
-                                viewModel.createGroup(selection.getAll())?.let {
-                                    activity?.open(it)
-                                }
-                            }
-                        }
+                    R.id.action_add_to_group -> addToGroup(selection)
                     else -> return false
                 }
             }
@@ -401,6 +397,18 @@ class VideoGridFragment : MediaBrowserFragment<VideosViewModel>(), SwipeRefreshL
         return true
     }
 
+    private fun addToGroup(selection: List<MediaLibraryItem>) {
+        requireActivity().addToGroup(selection.getAll()) {
+            lifecycleScope.launch {
+                viewModel.createGroup(selection.getAll())?.let {
+                    // we already are in a group. Finishing to avoid stacking multiple group activities
+                    if (viewModel.groupingType == VideoGroupingType.NONE) requireActivity().finish()
+                    activity?.open(it)
+                }
+            }
+        }
+    }
+
     override fun onDestroyActionMode(mode: ActionMode) {
         actionMode = null
         setFabPlayVisibility(true)



More information about the Android mailing list