Commit adcef71c authored by Geoffrey Métais's avatar Geoffrey Métais

Manage audio lists loading with liveData

parent 5406d9c8
......@@ -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());
......
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())
}
}
......
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