Fix recycler and empty view hide/show logic

The `emptyViewHandler` already handles hiding and showing both the empty
view and the recycler view on data changes, so this commit removes this
part of the logic from the episodes fragment.

It also hides the empty view right after creating the recycle adapter
for the first time (when the fragment is created) to prevent the
progress bar and the empty view from being displayed at the same time.

`createRecycleAdapter()` signature was changed to make it explicit that
it depends on both the `recyclerView` and `emptyViewHandler`. Similarly,
`onFragmentLoaded()`, since it also depends on the new data that gets
loaded.
This commit is contained in:
Anderson Mesquita 2019-06-24 09:32:06 -04:00
parent bb8b1fc58f
commit 8e95ed75ab
1 changed files with 11 additions and 15 deletions

View File

@ -309,21 +309,19 @@ public class AllEpisodesFragment extends Fragment {
emptyView.attachToRecyclerView(recyclerView); emptyView.attachToRecyclerView(recyclerView);
emptyView.setTitle(R.string.no_all_episodes_head_label); emptyView.setTitle(R.string.no_all_episodes_head_label);
emptyView.setMessage(R.string.no_all_episodes_label); emptyView.setMessage(R.string.no_all_episodes_label);
emptyView.hide();
createRecycleAdapter(); createRecycleAdapter(recyclerView, emptyView);
emptyView.hide();
return root; return root;
} }
private void onFragmentLoaded() { private void onFragmentLoaded(List<FeedItem> episodes) {
if (episodes.size() > 0) { this.episodes = episodes;
recyclerView.setVisibility(View.VISIBLE);
listAdapter.notifyDataSetChanged(); listAdapter.notifyDataSetChanged();
} else {
createRecycleAdapter(); if (episodes.size() == 0) {
recyclerView.setVisibility(View.GONE); createRecycleAdapter(recyclerView, emptyView);
emptyView.updateAdapter(listAdapter);
} }
restoreScrollPosition(); restoreScrollPosition();
@ -334,12 +332,12 @@ public class AllEpisodesFragment extends Fragment {
* Currently, we need to recreate the list adapter in order to be able to undo last item via the * Currently, we need to recreate the list adapter in order to be able to undo last item via the
* snackbar. See #3084 for details. * snackbar. See #3084 for details.
*/ */
private void createRecycleAdapter() { private void createRecycleAdapter(RecyclerView recyclerView, EmptyViewHandler emptyViewHandler) {
MainActivity mainActivity = (MainActivity) getActivity(); MainActivity mainActivity = (MainActivity) getActivity();
listAdapter = new AllEpisodesRecycleAdapter(mainActivity, itemAccess, showOnlyNewEpisodes()); listAdapter = new AllEpisodesRecycleAdapter(mainActivity, itemAccess, showOnlyNewEpisodes());
listAdapter.setHasStableIds(true); listAdapter.setHasStableIds(true);
recyclerView.setAdapter(listAdapter); recyclerView.setAdapter(listAdapter);
emptyView.updateAdapter(listAdapter); emptyViewHandler.updateAdapter(listAdapter);
} }
private final AllEpisodesRecycleAdapter.ItemAccess itemAccess = new AllEpisodesRecycleAdapter.ItemAccess() { private final AllEpisodesRecycleAdapter.ItemAccess itemAccess = new AllEpisodesRecycleAdapter.ItemAccess() {
@ -455,10 +453,8 @@ public class AllEpisodesFragment extends Fragment {
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe(data -> { .subscribe(data -> {
recyclerView.setVisibility(View.VISIBLE);
progLoading.setVisibility(View.GONE); progLoading.setVisibility(View.GONE);
episodes = data; onFragmentLoaded(data);
onFragmentLoaded();
}, error -> Log.e(TAG, Log.getStackTraceString(error))); }, error -> Log.e(TAG, Log.getStackTraceString(error)));
} }