[Android] Audio: queue providers refresh

Geoffrey Métais git at videolan.org
Mon Dec 2 13:38:16 CET 2019


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Fri Nov 29 11:52:28 2019 +0100| [1c5f50cd0a538f3bffdfd36471777b435c1e6911] | committer: Nicolas Pomepuy

Audio: queue providers refresh

Fix #1089

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

 .../videolan/vlc/gui/audio/AudioBrowserFragment.kt   |  4 ++--
 .../providers/medialibrary/MedialibraryProvider.kt   | 18 ++++++++++++------
 .../vlc/viewmodels/mobile/AudioBrowserViewModel.kt   | 20 +++++++++++---------
 3 files changed, 25 insertions(+), 17 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.kt b/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.kt
index 446f835c2..4646c59db 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.kt
@@ -116,8 +116,7 @@ class AudioBrowserFragment : BaseAudioBrowser<AudioBrowserViewModel>() {
             val audioPagerAdapter = AudioPagerAdapter(views.toTypedArray(), titles)
             @Suppress("UNCHECKED_CAST")
             viewPager.adapter = audioPagerAdapter
-            val tabPosition = settings.getInt(KEY_AUDIO_CURRENT_TAB, 0)
-            currentTab = tabPosition
+            currentTab = viewModel.currentTab
             savedInstanceState?.getIntegerArrayList(KEY_LISTS_POSITIONS)?.withIndex()?.forEach {
                 restorePositions.put(it.index, it.value)
             }
@@ -297,6 +296,7 @@ class AudioBrowserFragment : BaseAudioBrowser<AudioBrowserViewModel>() {
 
     override fun onTabSelected(tab: TabLayout.Tab) {
         adapter = adapters[tab.position]
+        viewModel.currentTab = tab.position
         super.onTabSelected(tab)
         songs_fast_scroller?.setRecyclerView(lists[tab.position], viewModel.providers[tab.position])
         settings.edit().putInt(KEY_AUDIO_CURRENT_TAB, tab.position).apply()
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 711de3934..90c2c1f57 100644
--- a/vlc-android/src/org/videolan/vlc/providers/medialibrary/MedialibraryProvider.kt
+++ b/vlc-android/src/org/videolan/vlc/providers/medialibrary/MedialibraryProvider.kt
@@ -22,18 +22,13 @@ package org.videolan.vlc.providers.medialibrary
 
 import android.content.Context
 import androidx.lifecycle.MutableLiveData
-import androidx.lifecycle.viewModelScope
 import androidx.paging.Config
 import androidx.paging.DataSource
 import androidx.paging.PositionalDataSource
 import androidx.paging.toLiveData
-import kotlinx.coroutines.CoroutineStart
-import kotlinx.coroutines.Dispatchers
-import kotlinx.coroutines.ExperimentalCoroutinesApi
-import kotlinx.coroutines.launch
+import kotlinx.coroutines.CompletableDeferred
 import org.videolan.medialibrary.interfaces.AbstractMedialibrary
 import org.videolan.medialibrary.media.MediaLibraryItem
-import org.videolan.tools.retry
 import org.videolan.vlc.providers.HeaderProvider
 import org.videolan.vlc.util.MEDIALIBRARY_PAGE_SIZE
 import org.videolan.vlc.util.ModelsHelper
@@ -48,8 +43,14 @@ abstract class MedialibraryProvider<T : MediaLibraryItem>(val context: Context,
     protected val medialibrary = AbstractMedialibrary.getInstance()
     private lateinit var dataSource : DataSource<Int, T>
     val loading = MutableLiveData<Boolean>().apply { value = true }
+    private var refreshDeferred : CompletableDeferred<Unit>? = null
     var isRefreshing = medialibrary.isWorking
         private set(value) {
+            refreshDeferred = if (value) CompletableDeferred()
+            else {
+                refreshDeferred?.complete(Unit)
+                null
+            }
             loading.postValue(value || medialibrary.isWorking)
             field = value
         }
@@ -89,6 +90,11 @@ abstract class MedialibraryProvider<T : MediaLibraryItem>(val context: Context,
         }
     }
 
+    suspend fun awaitRefresh() {
+        refresh()
+        refreshDeferred?.await()
+    }
+
     fun refresh(): Boolean {
         if (isRefreshing || !medialibrary.isStarted || !this::dataSource.isInitialized) return false
         privateHeaders.clear()
diff --git a/vlc-android/src/org/videolan/vlc/viewmodels/mobile/AudioBrowserViewModel.kt b/vlc-android/src/org/videolan/vlc/viewmodels/mobile/AudioBrowserViewModel.kt
index f200e69d5..ca10a62b2 100644
--- a/vlc-android/src/org/videolan/vlc/viewmodels/mobile/AudioBrowserViewModel.kt
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/mobile/AudioBrowserViewModel.kt
@@ -25,14 +25,17 @@ import androidx.annotation.MainThread
 import androidx.lifecycle.ViewModel
 import androidx.lifecycle.ViewModelProvider
 import androidx.lifecycle.ViewModelProviders
+import androidx.lifecycle.viewModelScope
 import kotlinx.coroutines.ExperimentalCoroutinesApi
 import kotlinx.coroutines.ObsoleteCoroutinesApi
+import kotlinx.coroutines.launch
 import org.videolan.vlc.gui.audio.AudioBrowserFragment
 import org.videolan.vlc.providers.medialibrary.AlbumsProvider
 import org.videolan.vlc.providers.medialibrary.ArtistsProvider
 import org.videolan.vlc.providers.medialibrary.GenresProvider
 import org.videolan.vlc.providers.medialibrary.TracksProvider
 import org.videolan.vlc.util.KEY_ARTISTS_SHOW_ALL
+import org.videolan.vlc.util.KEY_AUDIO_CURRENT_TAB
 import org.videolan.vlc.util.Settings
 import org.videolan.vlc.viewmodels.MedialibraryViewModel
 
@@ -40,11 +43,12 @@ import org.videolan.vlc.viewmodels.MedialibraryViewModel
 @ExperimentalCoroutinesApi
 class AudioBrowserViewModel(context: Context) : MedialibraryViewModel(context) {
 
+    var currentTab = Settings.getInstance(context).getInt(KEY_AUDIO_CURRENT_TAB, 0)
     val artistsProvider = ArtistsProvider(context, this,
             Settings.getInstance(context).getBoolean(KEY_ARTISTS_SHOW_ALL, false))
-    val albumsProvider = AlbumsProvider(null, context, this)
+    private val albumsProvider = AlbumsProvider(null, context, this)
     val tracksProvider = TracksProvider(null, context, this)
-    val genresProvider = GenresProvider(context, this)
+    private val genresProvider = GenresProvider(context, this)
     override val providers = arrayOf(artistsProvider, albumsProvider, tracksProvider, genresProvider)
     private val settings = Settings.getInstance(context)
     val providersInCard = arrayOf(true, true, false, false)
@@ -59,7 +63,7 @@ class AudioBrowserViewModel(context: Context) : MedialibraryViewModel(context) {
         watchGenres()
         watchMedia()
         //Initial state coming from preferences and falling back to [providersInCard] hardcoded values
-        for (i in 0 until displayModeKeys.size) {
+        for (i in displayModeKeys.indices) {
             providersInCard[i] = settings.getBoolean(displayModeKeys[i], providersInCard[i])
         }
 
@@ -67,12 +71,10 @@ class AudioBrowserViewModel(context: Context) : MedialibraryViewModel(context) {
 
     override fun refresh() {
         artistsProvider.showAll = settings.getBoolean(KEY_ARTISTS_SHOW_ALL, false)
-        super.refresh()
-    }
-
-    @MainThread
-    internal fun setLoading() {
-        providers.forEach { it.loading.value = true }
+        viewModelScope.launch {
+            providers[currentTab].awaitRefresh()
+            for (index in providers.indices) if (index != currentTab) providers[index].awaitRefresh()
+        }
     }
 
     class Factory(val context: Context): ViewModelProvider.NewInstanceFactory() {



More information about the Android mailing list