Reorganize playback history lifecycle

This commit is contained in:
Anderson Mesquita 2019-05-26 12:31:22 -04:00
parent 90d5208402
commit 5e5dfa5584
1 changed files with 21 additions and 67 deletions

View File

@ -1,6 +1,5 @@
package de.danoeh.antennapod.fragment; package de.danoeh.antennapod.fragment;
import android.content.Context;
import android.content.res.TypedArray; import android.content.res.TypedArray;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
@ -13,6 +12,10 @@ import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.widget.ListView; import android.widget.ListView;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import java.util.List; import java.util.List;
import de.danoeh.antennapod.R; import de.danoeh.antennapod.R;
@ -34,9 +37,6 @@ import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable; import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers; import io.reactivex.schedulers.Schedulers;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
public class PlaybackHistoryFragment extends ListFragment { public class PlaybackHistoryFragment extends ListFragment {
@ -47,22 +47,9 @@ public class PlaybackHistoryFragment extends ListFragment {
private List<FeedItem> playbackHistory; private List<FeedItem> playbackHistory;
private FeedItemlistAdapter adapter; private FeedItemlistAdapter adapter;
private boolean itemsLoaded = false;
private boolean viewsCreated = false;
private List<Downloader> downloaderList; private List<Downloader> downloaderList;
private Disposable disposable; private Disposable disposable;
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (viewsCreated && itemsLoaded) {
onFragmentLoaded();
}
}
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -80,16 +67,16 @@ public class PlaybackHistoryFragment extends ListFragment {
final int vertPadding = getResources().getDimensionPixelSize(R.dimen.list_vertical_padding); final int vertPadding = getResources().getDimensionPixelSize(R.dimen.list_vertical_padding);
lv.setPadding(0, vertPadding, 0, vertPadding); lv.setPadding(0, vertPadding, 0, vertPadding);
viewsCreated = true;
if (itemsLoaded) {
onFragmentLoaded();
}
EmptyViewHandler emptyView = new EmptyViewHandler(getActivity()); EmptyViewHandler emptyView = new EmptyViewHandler(getActivity());
emptyView.setTitle(R.string.no_history_head_label); emptyView.setTitle(R.string.no_history_head_label);
emptyView.setMessage(R.string.no_history_label); emptyView.setMessage(R.string.no_history_label);
emptyView.attachToListView(getListView()); emptyView.attachToListView(getListView());
// played items shoudln't be transparent for this fragment since, *all* items
// in this fragment will, by definition, be played. So it serves no purpose and can make
// it harder to read.
adapter = new FeedItemlistAdapter(getActivity(), itemAccess, true, false);
setListAdapter(adapter);
} }
@Override @Override
@ -105,35 +92,18 @@ public class PlaybackHistoryFragment extends ListFragment {
super.onStop(); super.onStop();
EventBus.getDefault().unregister(this); EventBus.getDefault().unregister(this);
EventDistributor.getInstance().unregister(contentUpdate); EventDistributor.getInstance().unregister(contentUpdate);
if(disposable != null) { if (disposable != null) {
disposable.dispose(); disposable.dispose();
} }
} }
@Override
public void onDetach() {
super.onDetach();
if(disposable != null) {
disposable.dispose();
}
}
@Override
public void onDestroyView() {
super.onDestroyView();
adapter = null;
viewsCreated = false;
}
@Subscribe(sticky = true) @Subscribe(sticky = true)
public void onEvent(DownloadEvent event) { public void onEvent(DownloadEvent event) {
Log.d(TAG, "onEvent() called with: " + "event = [" + event + "]"); Log.d(TAG, "onEvent() called with: " + "event = [" + event + "]");
DownloaderUpdate update = event.update; DownloaderUpdate update = event.update;
downloaderList = update.downloaders; downloaderList = update.downloaders;
if (adapter != null) {
adapter.notifyDataSetChanged(); adapter.notifyDataSetChanged();
} }
}
@Override @Override
public void onListItemClick(ListView l, View v, int position, long id) { public void onListItemClick(ListView l, View v, int position, long id) {
@ -145,29 +115,25 @@ public class PlaybackHistoryFragment extends ListFragment {
@Override @Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
if(!isAdded()) { if (!isAdded()) {
return; return;
} }
super.onCreateOptionsMenu(menu, inflater); super.onCreateOptionsMenu(menu, inflater);
if (itemsLoaded) {
MenuItem clearHistory = menu.add(Menu.NONE, R.id.clear_history_item, Menu.CATEGORY_CONTAINER, R.string.clear_history_label); MenuItem clearHistory = menu.add(Menu.NONE, R.id.clear_history_item, Menu.CATEGORY_CONTAINER, R.string.clear_history_label);
MenuItemCompat.setShowAsAction(clearHistory, MenuItemCompat.SHOW_AS_ACTION_IF_ROOM); MenuItemCompat.setShowAsAction(clearHistory, MenuItemCompat.SHOW_AS_ACTION_IF_ROOM);
TypedArray drawables = getActivity().obtainStyledAttributes(new int[]{R.attr.content_discard}); TypedArray drawables = getActivity().obtainStyledAttributes(new int[]{R.attr.content_discard});
clearHistory.setIcon(drawables.getDrawable(0)); clearHistory.setIcon(drawables.getDrawable(0));
drawables.recycle(); drawables.recycle();
} }
}
@Override @Override
public void onPrepareOptionsMenu(Menu menu) { public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu); super.onPrepareOptionsMenu(menu);
if (itemsLoaded) {
MenuItem menuItem = menu.findItem(R.id.clear_history_item); MenuItem menuItem = menu.findItem(R.id.clear_history_item);
if (menuItem != null) { if (menuItem != null) {
menuItem.setVisible(playbackHistory != null && !playbackHistory.isEmpty()); menuItem.setVisible(playbackHistory != null && !playbackHistory.isEmpty());
} }
} }
}
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
@ -211,14 +177,6 @@ public class PlaybackHistoryFragment extends ListFragment {
}; };
private void onFragmentLoaded() { private void onFragmentLoaded() {
if (adapter == null) {
// played items shoudln't be transparent for this fragment since, *all* items
// in this fragment will, by definition, be played. So it serves no purpose and can make
// it harder to read.
adapter = new FeedItemlistAdapter(getActivity(), itemAccess, true, false);
setListAdapter(adapter);
}
setListShown(true);
adapter.notifyDataSetChanged(); adapter.notifyDataSetChanged();
getActivity().supportInvalidateOptionsMenu(); getActivity().supportInvalidateOptionsMenu();
} }
@ -277,11 +235,8 @@ public class PlaybackHistoryFragment extends ListFragment {
.subscribe(result -> { .subscribe(result -> {
if (result != null) { if (result != null) {
playbackHistory = result; playbackHistory = result;
itemsLoaded = true;
if (viewsCreated) {
onFragmentLoaded(); onFragmentLoaded();
} }
}
}, error -> Log.e(TAG, Log.getStackTraceString(error))); }, error -> Log.e(TAG, Log.getStackTraceString(error)));
} }
@ -291,5 +246,4 @@ public class PlaybackHistoryFragment extends ListFragment {
DBReader.loadAdditionalFeedItemListData(history); DBReader.loadAdditionalFeedItemListData(history);
return history; return history;
} }
} }