[Android] Manage audio lists loading with liveData
Geoffrey Métais
git at videolan.org
Thu Sep 27 14:05:50 CEST 2018
vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Sep 27 14:05:17 2018 +0200| [adcef71cf2cfabd3290e82a515fca9d4c97f0a42] | committer: Geoffrey Métais
Manage audio lists loading with liveData
> https://code.videolan.org/videolan/vlc-android/commit/adcef71cf2cfabd3290e82a515fca9d4c97f0a42
---
.../vlc/gui/audio/AudioBrowserFragment.java | 24 ++++++++++++----------
.../videolan/vlc/viewmodels/paged/MLPagedModel.kt | 11 +++++++---
2 files changed, 21 insertions(+), 14 deletions(-)
diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.java b/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.java
index fb8756db2..9ff38c71c 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.java
@@ -100,7 +100,7 @@ public class AudioBrowserFragment extends BaseAudioBrowser implements SwipeRefre
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (mSettings == null) mSettings = Settings.INSTANCE.getInstance(requireContext());
- setupAdapters(mSettings.getInt(Constants.KEY_AUDIO_CURRENT_TAB, 0));
+ setupModels();
}
@Override
@@ -145,7 +145,7 @@ public class AudioBrowserFragment extends BaseAudioBrowser implements SwipeRefre
mSwipeRefreshLayout.setOnRefreshListener(this);
}
- private void setupAdapters(final int currentTab) {
+ private void setupModels() {
if (mArtistsAdapter == null) {
artistModel = ViewModelProviders.of(requireActivity(), new PagedArtistsModel.Factory(requireContext(), mSettings.getBoolean(Constants.KEY_ARTISTS_SHOW_ALL, false))).get(PagedArtistsModel.class);
mArtistsAdapter = new AudioBrowserAdapter(MediaLibraryItem.TYPE_ARTIST, this, artistModel.getSort());
@@ -168,15 +168,7 @@ public class AudioBrowserFragment extends BaseAudioBrowser implements SwipeRefre
}
mAdapters = new AudioBrowserAdapter[] {mArtistsAdapter, mAlbumsAdapter, mSongsAdapter, mGenresAdapter, mPlaylistAdapter};
models = new MLPagedModel[] {artistModel, albumModel, tracksModel, genresModel, playlistsModel};
- //Register current tab first
- models[currentTab].getPagedList().observe(this, new Observer<PagedList<MediaLibraryItem>>() {
- @Override
- public void onChanged(@Nullable PagedList<MediaLibraryItem> items) {
- if (items != null) mAdapters[currentTab].submitList(items);
- }
- });
for (int i = 0; i < models.length; ++i ) {
- if (i == currentTab) continue;
final int index = i;
models[i].getPagedList().observe(this, new Observer<PagedList<MediaLibraryItem>>() {
@Override
@@ -184,6 +176,14 @@ public class AudioBrowserFragment extends BaseAudioBrowser implements SwipeRefre
if (items != null) mAdapters[index].submitList(items);
}
});
+ models[i].getLoading().observe(this, new Observer<Boolean>() {
+ @Override
+ public void onChanged(@Nullable Boolean loading) {
+ if (loading == null || mViewPager.getCurrentItem() != index) return;
+ if (loading) mHandler.sendEmptyMessageDelayed(SET_REFRESHING, 300);
+ else mHandler.sendEmptyMessage(UNSET_REFRESHING);
+ }
+ });
}
}
@@ -284,6 +284,9 @@ public class AudioBrowserFragment extends BaseAudioBrowser implements SwipeRefre
requireActivity().invalidateOptionsMenu();
mFastScroller.setRecyclerView(mLists[tab.getPosition()], getViewModel().getTotalCount());
mSettings.edit().putInt(Constants.KEY_AUDIO_CURRENT_TAB, tab.getPosition()).apply();
+ final Boolean loading = getViewModel().getLoading().getValue();
+ if (loading == null || loading == false) mHandler.sendEmptyMessage(UNSET_REFRESHING);
+ else mHandler.sendEmptyMessage(SET_REFRESHING);
}
@Override
@@ -338,7 +341,6 @@ public class AudioBrowserFragment extends BaseAudioBrowser implements SwipeRefre
public void onUpdateFinished(RecyclerView.Adapter adapter) {
super.onUpdateFinished(adapter);
if (adapter == getCurrentAdapter()) {
- if (!mMediaLibrary.isWorking()) mHandler.sendEmptyMessage(UNSET_REFRESHING);
mSwipeRefreshLayout.setEnabled(((LinearLayoutManager)getCurrentRV().getLayoutManager()).findFirstVisibleItemPosition() <= 0);
updateEmptyView(mViewPager.getCurrentItem());
mFastScroller.setRecyclerView(getCurrentRV(), getViewModel().getTotalCount());
diff --git a/vlc-android/src/org/videolan/vlc/viewmodels/paged/MLPagedModel.kt b/vlc-android/src/org/videolan/vlc/viewmodels/paged/MLPagedModel.kt
index ef9446f9f..de51b2f75 100644
--- a/vlc-android/src/org/videolan/vlc/viewmodels/paged/MLPagedModel.kt
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/paged/MLPagedModel.kt
@@ -1,5 +1,6 @@
package org.videolan.vlc.viewmodels.paged
+import android.arch.lifecycle.MutableLiveData
import android.arch.paging.DataSource
import android.arch.paging.LivePagedListBuilder
import android.arch.paging.PagedList
@@ -13,6 +14,8 @@ import org.videolan.vlc.viewmodels.SortableModel
abstract class MLPagedModel<T : MediaLibraryItem>(context: Context) : SortableModel(context), Medialibrary.OnMedialibraryReadyListener {
protected val medialibrary = Medialibrary.getInstance()
protected var filter : String? = null
+ val loading = MutableLiveData<Boolean>().apply { value = true }
+
private val pagingConfig = PagedList.Config.Builder()
.setPageSize(100)
.setPrefetchDistance(100)
@@ -68,18 +71,20 @@ abstract class MLPagedModel<T : MediaLibraryItem>(context: Context) : SortableMo
}
override fun refresh(): Boolean {
+ loading.value = true
pagedList.value?.dataSource?.invalidate()
return true
}
inner class MLDataSource : PositionalDataSource<T>() {
- override fun loadRange(params: LoadRangeParams, callback: LoadRangeCallback<T>) {
- callback.onResult(getPage(params.loadSize, params.startPosition).toList())
- }
override fun loadInitial(params: LoadInitialParams, callback: LoadInitialCallback<T>) {
val count = getTotalCount()
callback.onResult(getPage(params.requestedLoadSize, params.requestedStartPosition).toList(), params.requestedStartPosition, count)
+ loading.postValue(false)
+ }
+ override fun loadRange(params: LoadRangeParams, callback: LoadRangeCallback<T>) {
+ callback.onResult(getPage(params.loadSize, params.startPosition).toList())
}
}
More information about the Android
mailing list