Reorganize DownloadLogFragment lifecycle

This commit is contained in:
Anderson Mesquita 2019-05-26 08:54:34 -04:00
parent bbcec5d0aa
commit e22adbe451
1 changed files with 19 additions and 37 deletions

View File

@ -14,6 +14,7 @@ import android.view.View;
import android.widget.ListView; import android.widget.ListView;
import android.widget.TextView; import android.widget.TextView;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import de.danoeh.antennapod.R; import de.danoeh.antennapod.R;
@ -37,18 +38,13 @@ public class DownloadLogFragment extends ListFragment {
private static final String TAG = "DownloadLogFragment"; private static final String TAG = "DownloadLogFragment";
private List<DownloadStatus> downloadLog; private List<DownloadStatus> downloadLog = new ArrayList<>();
private DownloadLogAdapter adapter; private DownloadLogAdapter adapter;
private boolean viewsCreated = false;
private boolean itemsLoaded = false;
private Disposable disposable; private Disposable disposable;
@Override @Override
public void onStart() { public void onStart() {
super.onStart(); super.onStart();
setHasOptionsMenu(true);
EventDistributor.getInstance().register(contentUpdate); EventDistributor.getInstance().register(contentUpdate);
loadItems(); loadItems();
} }
@ -57,7 +53,7 @@ public class DownloadLogFragment extends ListFragment {
public void onStop() { public void onStop() {
super.onStop(); super.onStop();
EventDistributor.getInstance().unregister(contentUpdate); EventDistributor.getInstance().unregister(contentUpdate);
if(disposable != null) { if (disposable != null) {
disposable.dispose(); disposable.dispose();
} }
} }
@ -65,6 +61,7 @@ public class DownloadLogFragment extends ListFragment {
@Override @Override
public void onViewCreated(View view, Bundle savedInstanceState) { public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState); super.onViewCreated(view, savedInstanceState);
setHasOptionsMenu(true);
// add padding // add padding
final ListView lv = getListView(); final ListView lv = getListView();
@ -72,23 +69,16 @@ public class DownloadLogFragment 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_log_downloads_head_label); emptyView.setTitle(R.string.no_log_downloads_head_label);
emptyView.setMessage(R.string.no_log_downloads_label); emptyView.setMessage(R.string.no_log_downloads_label);
emptyView.attachToListView(getListView()); emptyView.attachToListView(getListView());
adapter = new DownloadLogAdapter(getActivity(), itemAccess);
setListAdapter(adapter);
} }
private void onFragmentLoaded() { private void onFragmentLoaded() {
if (adapter == null) {
adapter = new DownloadLogAdapter(getActivity(), itemAccess);
setListAdapter(adapter);
}
setListShown(true); setListShown(true);
adapter.notifyDataSetChanged(); adapter.notifyDataSetChanged();
getActivity().supportInvalidateOptionsMenu(); getActivity().supportInvalidateOptionsMenu();
@ -129,12 +119,12 @@ public class DownloadLogFragment extends ListFragment {
@Override @Override
public int getCount() { public int getCount() {
return (downloadLog != null) ? downloadLog.size() : 0; return downloadLog.size();
} }
@Override @Override
public DownloadStatus getItem(int position) { public DownloadStatus getItem(int position) {
if (downloadLog != null && 0 <= position && position < downloadLog.size()) { if (0 <= position && position < downloadLog.size()) {
return downloadLog.get(position); return downloadLog.get(position);
} else { } else {
return null; return null;
@ -154,27 +144,23 @@ public class DownloadLogFragment 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(!downloadLog.isEmpty());
menuItem.setVisible(downloadLog != null && !downloadLog.isEmpty());
}
} }
} }
@ -194,7 +180,7 @@ public class DownloadLogFragment extends ListFragment {
} }
private void loadItems() { private void loadItems() {
if(disposable != null) { if (disposable != null) {
disposable.dispose(); disposable.dispose();
} }
disposable = Observable.fromCallable(DBReader::getDownloadLog) disposable = Observable.fromCallable(DBReader::getDownloadLog)
@ -203,12 +189,8 @@ public class DownloadLogFragment extends ListFragment {
.subscribe(result -> { .subscribe(result -> {
if (result != null) { if (result != null) {
downloadLog = result; downloadLog = result;
itemsLoaded = true; onFragmentLoaded();
if (viewsCreated) {
onFragmentLoaded();
}
} }
}, error -> Log.e(TAG, Log.getStackTraceString(error))); }, error -> Log.e(TAG, Log.getStackTraceString(error)));
} }
} }