Commit 226ce368 authored by Geoffrey Métais's avatar Geoffrey Métais

Audio: Improve loading state

parent cef38d5b
......@@ -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()
}
......
......@@ -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)
}
}
......
......@@ -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")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment