Reorganize DownloadLogFragment lifecycle
This commit is contained in:
parent
bbcec5d0aa
commit
e22adbe451
|
@ -14,6 +14,7 @@ import android.view.View;
|
|||
import android.widget.ListView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import de.danoeh.antennapod.R;
|
||||
|
@ -37,18 +38,13 @@ public class DownloadLogFragment extends ListFragment {
|
|||
|
||||
private static final String TAG = "DownloadLogFragment";
|
||||
|
||||
private List<DownloadStatus> downloadLog;
|
||||
private List<DownloadStatus> downloadLog = new ArrayList<>();
|
||||
private DownloadLogAdapter adapter;
|
||||
|
||||
private boolean viewsCreated = false;
|
||||
private boolean itemsLoaded = false;
|
||||
|
||||
private Disposable disposable;
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
super.onStart();
|
||||
setHasOptionsMenu(true);
|
||||
EventDistributor.getInstance().register(contentUpdate);
|
||||
loadItems();
|
||||
}
|
||||
|
@ -57,7 +53,7 @@ public class DownloadLogFragment extends ListFragment {
|
|||
public void onStop() {
|
||||
super.onStop();
|
||||
EventDistributor.getInstance().unregister(contentUpdate);
|
||||
if(disposable != null) {
|
||||
if (disposable != null) {
|
||||
disposable.dispose();
|
||||
}
|
||||
}
|
||||
|
@ -65,6 +61,7 @@ public class DownloadLogFragment extends ListFragment {
|
|||
@Override
|
||||
public void onViewCreated(View view, Bundle savedInstanceState) {
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
setHasOptionsMenu(true);
|
||||
|
||||
// add padding
|
||||
final ListView lv = getListView();
|
||||
|
@ -72,23 +69,16 @@ public class DownloadLogFragment extends ListFragment {
|
|||
final int vertPadding = getResources().getDimensionPixelSize(R.dimen.list_vertical_padding);
|
||||
lv.setPadding(0, vertPadding, 0, vertPadding);
|
||||
|
||||
viewsCreated = true;
|
||||
if (itemsLoaded) {
|
||||
onFragmentLoaded();
|
||||
}
|
||||
|
||||
EmptyViewHandler emptyView = new EmptyViewHandler(getActivity());
|
||||
emptyView.setTitle(R.string.no_log_downloads_head_label);
|
||||
emptyView.setMessage(R.string.no_log_downloads_label);
|
||||
emptyView.attachToListView(getListView());
|
||||
|
||||
adapter = new DownloadLogAdapter(getActivity(), itemAccess);
|
||||
setListAdapter(adapter);
|
||||
}
|
||||
|
||||
private void onFragmentLoaded() {
|
||||
if (adapter == null) {
|
||||
adapter = new DownloadLogAdapter(getActivity(), itemAccess);
|
||||
setListAdapter(adapter);
|
||||
}
|
||||
setListShown(true);
|
||||
adapter.notifyDataSetChanged();
|
||||
getActivity().supportInvalidateOptionsMenu();
|
||||
|
@ -129,12 +119,12 @@ public class DownloadLogFragment extends ListFragment {
|
|||
|
||||
@Override
|
||||
public int getCount() {
|
||||
return (downloadLog != null) ? downloadLog.size() : 0;
|
||||
return downloadLog.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public DownloadStatus getItem(int position) {
|
||||
if (downloadLog != null && 0 <= position && position < downloadLog.size()) {
|
||||
if (0 <= position && position < downloadLog.size()) {
|
||||
return downloadLog.get(position);
|
||||
} else {
|
||||
return null;
|
||||
|
@ -154,27 +144,23 @@ public class DownloadLogFragment extends ListFragment {
|
|||
|
||||
@Override
|
||||
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
||||
if(!isAdded()) {
|
||||
if (!isAdded()) {
|
||||
return;
|
||||
}
|
||||
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);
|
||||
MenuItemCompat.setShowAsAction(clearHistory, MenuItemCompat.SHOW_AS_ACTION_IF_ROOM);
|
||||
TypedArray drawables = getActivity().obtainStyledAttributes(new int[]{R.attr.content_discard});
|
||||
clearHistory.setIcon(drawables.getDrawable(0));
|
||||
drawables.recycle();
|
||||
}
|
||||
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);
|
||||
TypedArray drawables = getActivity().obtainStyledAttributes(new int[]{R.attr.content_discard});
|
||||
clearHistory.setIcon(drawables.getDrawable(0));
|
||||
drawables.recycle();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPrepareOptionsMenu(Menu menu) {
|
||||
super.onPrepareOptionsMenu(menu);
|
||||
if (itemsLoaded) {
|
||||
MenuItem menuItem = menu.findItem(R.id.clear_history_item);
|
||||
if(menuItem != null) {
|
||||
menuItem.setVisible(downloadLog != null && !downloadLog.isEmpty());
|
||||
}
|
||||
MenuItem menuItem = menu.findItem(R.id.clear_history_item);
|
||||
if (menuItem != null) {
|
||||
menuItem.setVisible(!downloadLog.isEmpty());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -194,7 +180,7 @@ public class DownloadLogFragment extends ListFragment {
|
|||
}
|
||||
|
||||
private void loadItems() {
|
||||
if(disposable != null) {
|
||||
if (disposable != null) {
|
||||
disposable.dispose();
|
||||
}
|
||||
disposable = Observable.fromCallable(DBReader::getDownloadLog)
|
||||
|
@ -203,12 +189,8 @@ public class DownloadLogFragment extends ListFragment {
|
|||
.subscribe(result -> {
|
||||
if (result != null) {
|
||||
downloadLog = result;
|
||||
itemsLoaded = true;
|
||||
if (viewsCreated) {
|
||||
onFragmentLoaded();
|
||||
}
|
||||
onFragmentLoaded();
|
||||
}
|
||||
}, error -> Log.e(TAG, Log.getStackTraceString(error)));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue