[Android] Fix bad position when playing all videos while grouping by name

Nicolas Pomepuy git at videolan.org
Thu Oct 28 04:57:44 UTC 2021


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Mon Oct 11 09:58:34 2021 +0200| [6e2dc1ac7de97ebedd5801813e33051bd6f8a0ad] | committer: Nicolas Pomepuy

Fix bad position when playing all videos while grouping by name

Fixes #1421

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

 .../src/org/videolan/vlc/media/MediaUtils.kt       | 31 +++++++++++++---------
 .../vlc/viewmodels/mobile/VideosViewModel.kt       |  4 +--
 2 files changed, 20 insertions(+), 15 deletions(-)

diff --git a/application/vlc-android/src/org/videolan/vlc/media/MediaUtils.kt b/application/vlc-android/src/org/videolan/vlc/media/MediaUtils.kt
index 1db1bb856..a6d6c7946 100644
--- a/application/vlc-android/src/org/videolan/vlc/media/MediaUtils.kt
+++ b/application/vlc-android/src/org/videolan/vlc/media/MediaUtils.kt
@@ -233,32 +233,37 @@ object MediaUtils {
         }
     }
 
-    fun playAllTracks(context: Context?, provider: VideoGroupsProvider, position: Int, shuffle: Boolean) {
+    fun playAllTracks(context: Context?, provider: VideoGroupsProvider, mediaToPlay: MediaWrapper?, shuffle: Boolean) {
         if (context == null) return
         SuspendDialogCallback(context) { service ->
             val count = withContext(Dispatchers.IO) { provider.getTotalCount() }
-            fun play(list: List<MediaWrapper>) {
+            fun play(list: List<MediaWrapper>, position:Int) {
                 service.load(list, if (shuffle) SecureRandom().nextInt(min(count, MEDIALIBRARY_PAGE_SIZE)) else position)
                 if (shuffle && !service.isShuffling) service.shuffle()
             }
             when (count) {
                 0 -> return at SuspendDialogCallback
-                in 1..MEDIALIBRARY_PAGE_SIZE -> play(withContext(Dispatchers.IO) {
-                    provider.getAll().flatMap {
-                        it.media(Medialibrary.SORT_DEFAULT, false, Settings.includeMissing, it.mediaCount(), 0).toList()
+                in 1..MEDIALIBRARY_PAGE_SIZE -> {
+                    val flatList =  withContext(Dispatchers.IO) {
+                        val allGroups = provider.getAll()
+                        allGroups.flatMap {
+                            it.media(Medialibrary.SORT_DEFAULT, false, Settings.includeMissing, it.mediaCount(), 0).toList()
+                        }
                     }
-                })
+                    play(flatList, flatList.indexOf(mediaToPlay))
+                }
                 else -> {
                     var index = 0
-                    while (index < count) {
-                        val pageCount = min(MEDIALIBRARY_PAGE_SIZE, count - index)
-                        val list = withContext(Dispatchers.IO) {
-                            provider.getPage(pageCount, index).toList() as List<MediaWrapper>
+                    val completeList = ArrayList<MediaWrapper>()
+                    withContext(Dispatchers.IO) {
+                        while (index < count) {
+                            val pageCount = min(MEDIALIBRARY_PAGE_SIZE, count - index)
+                            val list = provider.getPage(pageCount, index).toList()
+                            completeList.addAll(list)
+                            index += pageCount
                         }
-                        if (index == 0) play(list)
-                        else service.append(list)
-                        index += pageCount
                     }
+                    play(completeList, completeList.indexOf(mediaToPlay))
                 }
             }
         }
diff --git a/application/vlc-android/src/org/videolan/vlc/viewmodels/mobile/VideosViewModel.kt b/application/vlc-android/src/org/videolan/vlc/viewmodels/mobile/VideosViewModel.kt
index d1aed411d..9f10267aa 100644
--- a/application/vlc-android/src/org/videolan/vlc/viewmodels/mobile/VideosViewModel.kt
+++ b/application/vlc-android/src/org/videolan/vlc/viewmodels/mobile/VideosViewModel.kt
@@ -139,7 +139,7 @@ class VideosViewModel(context: Context, type: VideoGroupingType, val folder: Fol
             when(val prov = provider) {
                 is VideosProvider -> MediaUtils.playAll(context, prov, position, false)
                 is FoldersProvider -> MediaUtils.playAllTracks(context, prov, position, false)
-                is VideoGroupsProvider -> MediaUtils.playAllTracks(context, prov, position, false)
+                is VideoGroupsProvider -> MediaUtils.playAllTracks(context, prov, mw, false)
             }
         } else {
             if (fromStart) mw.addFlags(MediaWrapper.MEDIA_FROM_START)
@@ -151,7 +151,7 @@ class VideosViewModel(context: Context, type: VideoGroupingType, val folder: Fol
         if (activity?.isStarted() == true) when (groupingType) {
             VideoGroupingType.NONE -> MediaUtils.playAll(activity, provider as VideosProvider, position, false)
             VideoGroupingType.FOLDER -> MediaUtils.playAllTracks(activity, (provider as FoldersProvider), position, false)
-            VideoGroupingType.NAME -> MediaUtils.playAllTracks(activity, (provider as VideoGroupsProvider), position, false)
+            VideoGroupingType.NAME -> MediaUtils.playAllTracks(activity, (provider as VideoGroupsProvider), null, false)
         }
     }
 



More information about the Android mailing list