[Android] Wait for the medialibrary to be ready before launching the paged queries

Nicolas Pomepuy git at videolan.org
Fri Oct 15 08:58:50 UTC 2021


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Wed Oct 13 14:42:31 2021 +0200| [7c9cd1a804a66d089c880f0d5fb133db7f1b1d01] | committer: Nicolas Pomepuy

Wait for the medialibrary to be ready before launching the paged queries

Fixes #2210
If we start the paging too soon, the medialibrary is not started.
In that case, all queries will come back empty, delaying the media display.
By waiting that the ML is started, we display he media as soon as we can.

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

 .../videolan/vlc/gui/audio/AudioBrowserFragment.kt | 19 ++++++++++++++++--
 .../videolan/vlc/gui/video/VideoGridFragment.kt    | 23 +++++++++++-----------
 .../providers/medialibrary/VideoGroupsProvider.kt  |  2 +-
 3 files changed, 30 insertions(+), 14 deletions(-)

diff --git a/application/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.kt b/application/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.kt
index d21eec11f..e974e655f 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.kt
@@ -39,12 +39,14 @@ import com.google.android.material.tabs.TabLayout
 import kotlinx.android.synthetic.main.audio_browser.*
 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
 import org.videolan.medialibrary.media.MediaLibraryItem
 import org.videolan.resources.CTX_PLAY_ALL
 import org.videolan.resources.KEY_AUDIO_CURRENT_TAB
 import org.videolan.resources.KEY_AUDIO_LAST_PLAYLIST
+import org.videolan.resources.util.waitForML
 import org.videolan.tools.KEY_ARTISTS_SHOW_ALL
 import org.videolan.tools.RESULT_RESTART
 import org.videolan.tools.Settings
@@ -193,7 +195,7 @@ class AudioBrowserFragment : BaseAudioBrowser<AudioBrowserViewModel>() {
     }
 
     private fun setupProvider(index: Int = viewModel.currentTab) {
-        val provider = viewModel.providers[index.coerceIn(0, viewModel.providers.size-1)]
+        val provider = viewModel.providers[index.coerceIn(0, viewModel.providers.size - 1)]
         if (provider.loading.hasObservers()) return
         provider.pagedList.observe(viewLifecycleOwner, { items ->
             @Suppress("UNCHECKED_CAST")
@@ -218,6 +220,19 @@ class AudioBrowserFragment : BaseAudioBrowser<AudioBrowserViewModel>() {
         provider.liveHeaders.observe(viewLifecycleOwner, {
             lists[currentTab].invalidateItemDecorations()
         })
+        lifecycleScope.launchWhenStarted {
+            waitForML()
+                provider.pagedList.observe(viewLifecycleOwner, { items ->
+                    @Suppress("UNCHECKED_CAST")
+                    if (items != null) adapters.getOrNull(index)?.submitList(items as PagedList<MediaLibraryItem>?)
+                    updateEmptyView()
+                    restorePositions.get(index)?.let {
+                        lists[index].scrollToPosition(it)
+                        restorePositions.delete(index)
+                    }
+                    setFabPlayShuffleAllVisibility(items.isNotEmpty())
+                })
+        }
     }
 
     override fun onStart() {
@@ -288,7 +303,7 @@ class AudioBrowserFragment : BaseAudioBrowser<AudioBrowserViewModel>() {
     private fun updateEmptyView() {
         swipeRefreshLayout.visibility = if (Medialibrary.getInstance().isInitiated) View.VISIBLE else View.GONE
         empty_loading.state =
-            if (!Permissions.canReadStorage(requireActivity())) EmptyLoadingState.MISSING_PERMISSION else if (viewModel.providers[currentTab].loading.value == true && empty) EmptyLoadingState.LOADING else if (empty) EmptyLoadingState.EMPTY else EmptyLoadingState.NONE
+                if (!Permissions.canReadStorage(requireActivity())) EmptyLoadingState.MISSING_PERMISSION else if (viewModel.providers[currentTab].loading.value == true && empty) EmptyLoadingState.LOADING else if (empty) EmptyLoadingState.EMPTY else EmptyLoadingState.NONE
     }
 
     override fun onPageSelected(position: Int) {
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 2710e597a..ea99c5110 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
@@ -45,10 +45,10 @@ import org.videolan.medialibrary.interfaces.media.VideoGroup
 import org.videolan.medialibrary.media.FolderImpl
 import org.videolan.medialibrary.media.MediaLibraryItem
 import org.videolan.resources.*
+import org.videolan.resources.util.waitForML
 import org.videolan.tools.*
 import org.videolan.vlc.R
 import org.videolan.vlc.databinding.VideoGridBinding
-import org.videolan.vlc.gui.ContentActivity
 import org.videolan.vlc.gui.SecondaryActivity
 import org.videolan.vlc.gui.browser.MediaBrowserFragment
 import org.videolan.vlc.gui.dialogs.CtxActionReceiver
@@ -60,7 +60,6 @@ import org.videolan.vlc.gui.helpers.UiTools
 import org.videolan.vlc.gui.helpers.UiTools.addToGroup
 import org.videolan.vlc.gui.helpers.UiTools.addToPlaylist
 import org.videolan.vlc.gui.view.EmptyLoadingState
-import org.videolan.vlc.gui.view.FastScroller
 import org.videolan.vlc.media.MediaUtils
 import org.videolan.vlc.media.PlaylistManager
 import org.videolan.vlc.media.getAll
@@ -125,15 +124,7 @@ class VideoGridFragment : MediaBrowserFragment<VideosViewModel>(), SwipeRefreshL
 
     private fun setDataObservers() {
         videoListAdapter.dataType = viewModel.groupingType
-        viewModel.provider.pagedList.observe(requireActivity(), {
-            (it as? PagedList<MediaLibraryItem>)?.let { videoListAdapter.submitList(it) }
-            updateEmptyView()
-            restoreMultiSelectHelper()
-            if (activity?.isFinishing == false && viewModel.group != null && it.size < 2) requireActivity().finish()
-            setFabPlayVisibility(true)
-        })
-
-        viewModel.provider.loading.observe(this, { loading ->
+        viewModel.provider.loading.observe(this at VideoGridFragment, { loading ->
             setRefreshing(loading) { refresh ->
                 if (!refresh) {
                     menu?.let { UiTools.updateSortTitles(it, viewModel.provider) }
@@ -142,6 +133,16 @@ class VideoGridFragment : MediaBrowserFragment<VideosViewModel>(), SwipeRefreshL
             }
         })
         videoListAdapter.showFilename.set(viewModel.groupingType == VideoGroupingType.NONE && viewModel.provider.sort == Medialibrary.SORT_FILENAME)
+        lifecycleScope.launch {
+            waitForML()
+            viewModel.provider.pagedList.observe(requireActivity(), {
+                (it as? PagedList<MediaLibraryItem>)?.let { videoListAdapter.submitList(it) }
+                updateEmptyView()
+                restoreMultiSelectHelper()
+                if (activity?.isFinishing == false && viewModel.group != null && it.size < 2) requireActivity().finish()
+                setFabPlayVisibility(true)
+            })
+        }
     }
 
     override fun onPrepareOptionsMenu(menu: Menu) {
diff --git a/application/vlc-android/src/org/videolan/vlc/providers/medialibrary/VideoGroupsProvider.kt b/application/vlc-android/src/org/videolan/vlc/providers/medialibrary/VideoGroupsProvider.kt
index 7960995df..cf0c5f86d 100644
--- a/application/vlc-android/src/org/videolan/vlc/providers/medialibrary/VideoGroupsProvider.kt
+++ b/application/vlc-android/src/org/videolan/vlc/providers/medialibrary/VideoGroupsProvider.kt
@@ -24,7 +24,7 @@ class VideoGroupsProvider(context: Context, model: SortableModel) : Medialibrary
         val medias = if (model.filterQuery.isNullOrEmpty()) {
             medialibrary.getVideoGroups(sort, desc, Settings.includeMissing, loadSize, startposition)
         } else {
-        medialibrary.searchVideoGroups(model.filterQuery, sort, desc, Settings.includeMissing, loadSize, startposition)
+            medialibrary.searchVideoGroups(model.filterQuery, sort, desc, Settings.includeMissing, loadSize, startposition)
         }.sanitizeGroups().also { if (Settings.showTvUi) completeHeaders(it, startposition) }
         model.viewModelScope.launch { completeHeaders(medias, startposition) }
         return medias



More information about the Android mailing list