[Android] Display video groups of one item as regular video

Sébastien Toque git at videolan.org
Tue Nov 19 11:27:54 CET 2019


vlc-android | branch: master | Sébastien Toque <xilasz at gmail.com> | Sat Nov  2 22:14:38 2019 +0100| [cf174a13599ce0053153dc175e401437618ec892] | committer: Geoffrey Métais

Display video groups of one item as regular video

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

 .../videolan/vlc/gui/video/VideoGridFragment.kt    |  9 +++-----
 .../src/org/videolan/vlc/media/MediaUtils.kt       | 26 +++++++++++++---------
 .../providers/medialibrary/MedialibraryProvider.kt |  2 +-
 .../providers/medialibrary/VideoGroupsProvider.kt  | 13 +++++++----
 .../vlc/viewmodels/mobile/VideosViewModel.kt       | 10 ---------
 5 files changed, 29 insertions(+), 31 deletions(-)

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 d235fd884..ea5c7b80d 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.kt
@@ -385,13 +385,10 @@ class VideoGridFragment : MediaBrowserFragment<VideosViewModel>(), SwipeRefreshL
                 }
             }
             VideoGroupingType.NAME -> {
-                val selection = ArrayList<AbstractVideoGroup>()
-                for (mediaLibraryItem in multiSelectHelper.getSelection()) {
-                    selection.add(mediaLibraryItem as AbstractVideoGroup)
-                }
+                val selection = multiSelectHelper.getSelection()
                 when (item.itemId) {
-                    R.id.action_videogroup_play -> viewModel.playGroupsSelection(selection)
-                    R.id.action_videogroup_append -> viewModel.appendGroupsSelection(selection)
+                    R.id.action_videogroup_play -> MediaUtils.openList(activity, selection.getAll(), 0)
+                    R.id.action_videogroup_append -> MediaUtils.appendMedia(activity, selection.getAll())
                     R.id.action_videogroup_add_playlist -> launch { UiTools.addToPlaylist(requireActivity(), withContext(Dispatchers.Default) { selection.getAll() }) }
                     else -> return false
                 }
diff --git a/vlc-android/src/org/videolan/vlc/media/MediaUtils.kt b/vlc-android/src/org/videolan/vlc/media/MediaUtils.kt
index dc72a8491..2246469ae 100644
--- a/vlc-android/src/org/videolan/vlc/media/MediaUtils.kt
+++ b/vlc-android/src/org/videolan/vlc/media/MediaUtils.kt
@@ -218,18 +218,14 @@ object MediaUtils : CoroutineScope {
             when (count) {
                 0 -> return at SuspendDialogCallback
                 in 1..MEDIALIBRARY_PAGE_SIZE -> play(withContext(Dispatchers.IO) {
-                    provider.getAll().flatMap {
-                        it.media(AbstractMedialibrary.SORT_DEFAULT, false, it.mediaCount(), 0).toList()
-                    }
+                    provider.getAll().toList()
                 })
                 else -> {
                     var index = 0
                     while (index < count) {
                         val pageCount = min(MEDIALIBRARY_PAGE_SIZE, count - index)
                         val list = withContext(Dispatchers.IO) {
-                            provider.getPage(pageCount, index).flatMap {
-                                it.media(AbstractMedialibrary.SORT_DEFAULT, false, it.mediaCount(), 0).toList()
-                            }
+                            provider.getPage(pageCount, index).toList()
                         }
                         if (index == 0) play(list)
                         else service.append(list)
@@ -543,12 +539,22 @@ fun AbstractVideoGroup.getAll(sort: Int = AbstractMedialibrary.SORT_DEFAULT, des
     return all
 }
 
-fun List<AbstractVideoGroup>.getAll(sort: Int = AbstractMedialibrary.SORT_DEFAULT, desc: Boolean = false) : List<AbstractMediaWrapper> {
-return flatMap { it.getAll(sort, desc) }
+fun List<MediaLibraryItem>.getAll(sort: Int = AbstractMedialibrary.SORT_DEFAULT, desc: Boolean = false) =  flatMap {
+    when (it) {
+        is AbstractVideoGroup -> it.getAll(sort, desc)
+        is AbstractMediaWrapper -> listOf(it)
+        else -> listOf()
+    }
+}
+
+fun List<AbstractFolder>.getAll(type: Int = AbstractFolder.TYPE_FOLDER_VIDEO, sort: Int = AbstractMedialibrary.SORT_DEFAULT, desc: Boolean = false) =  flatMap {
+    it.getAll(type, sort, desc)
 }
 
-fun List<AbstractFolder>.getAll(type: Int = AbstractFolder.TYPE_FOLDER_VIDEO, sort: Int = AbstractMedialibrary.SORT_DEFAULT, desc: Boolean = false) : List<AbstractMediaWrapper> {
-    return flatMap { it.getAll(type, sort, desc) }
+private fun Array<MediaLibraryItem>.toList() = flatMap {
+    if (it is AbstractVideoGroup) {
+        it.media(AbstractMedialibrary.SORT_DEFAULT, false, it.mediaCount(), 0).toList()
+    } else listOf(this as AbstractMediaWrapper)
 }
 
 private sealed class Action
diff --git a/vlc-android/src/org/videolan/vlc/providers/medialibrary/MedialibraryProvider.kt b/vlc-android/src/org/videolan/vlc/providers/medialibrary/MedialibraryProvider.kt
index 11779ee2f..500f64c1b 100644
--- a/vlc-android/src/org/videolan/vlc/providers/medialibrary/MedialibraryProvider.kt
+++ b/vlc-android/src/org/videolan/vlc/providers/medialibrary/MedialibraryProvider.kt
@@ -96,7 +96,7 @@ abstract class MedialibraryProvider<T : MediaLibraryItem>(val context: Context,
 
     fun isEmpty() = pagedList.value.isNullOrEmpty()
 
-    fun completeHeaders(list: Array<T?>, startposition: Int) {
+    fun completeHeaders(list: Array<T>, startposition: Int) {
         for ((position, item) in list.withIndex()) {
             val previous = when {
                 position > 0 -> list[position - 1]
diff --git a/vlc-android/src/org/videolan/vlc/providers/medialibrary/VideoGroupsProvider.kt b/vlc-android/src/org/videolan/vlc/providers/medialibrary/VideoGroupsProvider.kt
index d52c83449..dd71fc446 100644
--- a/vlc-android/src/org/videolan/vlc/providers/medialibrary/VideoGroupsProvider.kt
+++ b/vlc-android/src/org/videolan/vlc/providers/medialibrary/VideoGroupsProvider.kt
@@ -1,15 +1,20 @@
 package org.videolan.vlc.providers.medialibrary
 
 import android.content.Context
+import org.videolan.medialibrary.interfaces.AbstractMedialibrary
 import org.videolan.medialibrary.interfaces.media.AbstractVideoGroup
+import org.videolan.medialibrary.media.MediaLibraryItem
 import org.videolan.vlc.viewmodels.SortableModel
 
 
-class VideoGroupsProvider(context: Context, model: SortableModel) : MedialibraryProvider<AbstractVideoGroup>(context, model) {
-    override fun getAll() : Array<AbstractVideoGroup> = medialibrary.getVideoGroups(sort, desc, getTotalCount(), 0)
+class VideoGroupsProvider(context: Context, model: SortableModel) : MedialibraryProvider<MediaLibraryItem>(context, model) {
+    override fun getAll() : Array<MediaLibraryItem> = medialibrary.getVideoGroups(sort, desc, getTotalCount(), 0).extractSingles()
 
     override fun getTotalCount() = medialibrary.videoGroupsCount
 
-    override fun getPage(loadSize: Int, startposition: Int) : Array<AbstractVideoGroup> = medialibrary.getVideoGroups(sort, desc, loadSize, startposition).also { completeHeaders(it, startposition) }
+    override fun getPage(loadSize: Int, startposition: Int) : Array<MediaLibraryItem> = medialibrary.getVideoGroups(sort, desc, loadSize, startposition).extractSingles().also { completeHeaders(it, startposition) }
+}
 
-}
\ No newline at end of file
+private fun Array<AbstractVideoGroup>.extractSingles() = map {
+    if (it.mediaCount() == 1) it.media(AbstractMedialibrary.SORT_DEFAULT, false, 1, 0)[0] else it
+}.toTypedArray()
\ No newline at end of file
diff --git a/vlc-android/src/org/videolan/vlc/viewmodels/mobile/VideosViewModel.kt b/vlc-android/src/org/videolan/vlc/viewmodels/mobile/VideosViewModel.kt
index a869c3419..611d5b001 100644
--- a/vlc-android/src/org/videolan/vlc/viewmodels/mobile/VideosViewModel.kt
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/mobile/VideosViewModel.kt
@@ -109,11 +109,6 @@ class VideosViewModel(context: Context, type: VideoGroupingType, val folder: Abs
         MediaUtils.openList(context, list, 0)
     }
 
-    internal fun playGroupsSelection(selection: List<AbstractVideoGroup>) = viewModelScope.launch {
-        val list = selection.flatMap { it.getAll() }
-        MediaUtils.openList(context, list, 0)
-    }
-
     internal fun addToPlaylist(activity: FragmentActivity, position: Int) = viewModelScope.launch {
         val item = provider.pagedList.value?.get(position) ?: return at launch
         withContext(Dispatchers.IO) {
@@ -130,11 +125,6 @@ class VideosViewModel(context: Context, type: VideoGroupingType, val folder: Abs
         MediaUtils.appendMedia(context, list)
     }
 
-    internal fun appendGroupsSelection(selection: List<AbstractVideoGroup>) = viewModelScope.launch {
-        val list = selection.flatMap { it.getAll() }
-        MediaUtils.appendMedia(context, list)
-    }
-
     internal fun playVideo(context: Activity?, mw: AbstractMediaWrapper, position: Int, fromStart: Boolean = false) {
         if (context === null) return
         mw.removeFlags(AbstractMediaWrapper.MEDIA_FORCE_AUDIO)



More information about the Android mailing list