[Android] Audio: Improve loading state

Geoffrey Métais git at videolan.org
Mon Jul 15 17:55:40 CEST 2019


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Mon Jul 15 16:31:36 2019 +0200| [226ce3689dd5b6a6678d8f21347ff566ca56a0b5] | committer: Geoffrey Métais

Audio: Improve loading state

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

 .../src/org/videolan/vlc/gui/audio/AudioBrowserFragment.kt        | 8 ++++----
 .../videolan/vlc/providers/medialibrary/MedialibraryProvider.kt   | 6 +++---
 .../org/videolan/vlc/viewmodels/mobile/AudioBrowserViewModel.kt   | 6 ++++++
 3 files changed, 13 insertions(+), 7 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 3cccce8f1..33ff3a173 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.kt
@@ -268,6 +268,7 @@ class AudioBrowserFragment : BaseAudioBrowser<AudioBrowserViewModel>(), SwipeRef
     override fun onRefresh() {
         (requireActivity() as ContentActivity).closeSearchView()
         requireContext().reloadLibrary()
+        viewModel.setLoading()
     }
 
     override fun getTitle(): String = getString(R.string.audio)
@@ -291,11 +292,10 @@ class AudioBrowserFragment : BaseAudioBrowser<AudioBrowserViewModel>(), SwipeRef
         super.onTabSelected(tab)
         fastScroller.setRecyclerView(lists[tab.position]!!, viewModel.providers[currentTab])
         settings.edit().putInt(KEY_AUDIO_CURRENT_TAB, tab.position).apply()
-        val loading = viewModel.providers[currentTab].loading.value
-        if (loading == null || !loading)
-            handler.sendEmptyMessage(UNSET_REFRESHING)
-        else
+        if (viewModel.providers[currentTab].isRefreshing)
             handler.sendEmptyMessage(SET_REFRESHING)
+        else
+            handler.sendEmptyMessage(UNSET_REFRESHING)
         activity?.invalidateOptionsMenu()
     }
 
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 4cf7f1d8e..836d23291 100644
--- a/vlc-android/src/org/videolan/vlc/providers/medialibrary/MedialibraryProvider.kt
+++ b/vlc-android/src/org/videolan/vlc/providers/medialibrary/MedialibraryProvider.kt
@@ -35,6 +35,7 @@ import org.videolan.vlc.providers.HeaderProvider
 import org.videolan.vlc.providers.HeadersIndex
 import org.videolan.vlc.util.*
 import org.videolan.vlc.viewmodels.SortableModel
+import kotlin.properties.Delegates
 
 abstract class MedialibraryProvider<T : MediaLibraryItem>(val context: Context, val scope: SortableModel) : HeaderProvider(),
     ISortModel
@@ -42,7 +43,8 @@ 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 = false }
-    @Volatile private var isRefreshing = false
+    var isRefreshing by Delegates.observable(false) { _,_, value -> loading.postValue(value) }
+        private set
 
     protected open val sortKey : String = this.javaClass.simpleName
     var sort = AbstractMedialibrary.SORT_DEFAULT
@@ -82,7 +84,6 @@ abstract class MedialibraryProvider<T : MediaLibraryItem>(val context: Context,
         if (isRefreshing || !medialibrary.isStarted || !this::dataSource.isInitialized) return false
         headers.clear()
         if (!dataSource.isInvalid) {
-            loading.postValue(true)
             isRefreshing = true
             dataSource.invalidate()
         }
@@ -121,7 +122,6 @@ abstract class MedialibraryProvider<T : MediaLibraryItem>(val context: Context,
                     }
                 }
                 isRefreshing = false
-                loading.postValue(false)
             }
         }
 
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 06caf37ae..a4f76412e 100644
--- a/vlc-android/src/org/videolan/vlc/viewmodels/mobile/AudioBrowserViewModel.kt
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/mobile/AudioBrowserViewModel.kt
@@ -21,6 +21,7 @@
 package org.videolan.vlc.viewmodels.mobile
 
 import android.content.Context
+import androidx.annotation.MainThread
 import androidx.lifecycle.ViewModel
 import androidx.lifecycle.ViewModelProvider
 import androidx.lifecycle.ViewModelProviders
@@ -60,6 +61,11 @@ class AudioBrowserViewModel(context: Context) : MedialibraryViewModel(context) {
         super.refresh()
     }
 
+    @MainThread
+    internal fun setLoading() {
+        providers.forEach { it.loading.value = true }
+    }
+
     class Factory(val context: Context): ViewModelProvider.NewInstanceFactory() {
         override fun <T : ViewModel> create(modelClass: Class<T>): T {
             @Suppress("UNCHECKED_CAST")



More information about the Android mailing list