Reorganize search fragment lifecycle
This commit is contained in:
parent
5e5dfa5584
commit
fd07a10f03
|
@ -14,6 +14,7 @@ import android.view.MenuItem;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.ListView;
|
import android.widget.ListView;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import de.danoeh.antennapod.R;
|
import de.danoeh.antennapod.R;
|
||||||
|
@ -40,11 +41,7 @@ public class SearchFragment extends ListFragment {
|
||||||
private static final String ARG_FEED = "feed";
|
private static final String ARG_FEED = "feed";
|
||||||
|
|
||||||
private SearchlistAdapter searchAdapter;
|
private SearchlistAdapter searchAdapter;
|
||||||
private List<SearchResult> searchResults;
|
private List<SearchResult> searchResults = new ArrayList<>();
|
||||||
|
|
||||||
private boolean viewCreated = false;
|
|
||||||
private boolean itemsLoaded = false;
|
|
||||||
|
|
||||||
private Disposable disposable;
|
private Disposable disposable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -74,13 +71,13 @@ public class SearchFragment extends ListFragment {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setRetainInstance(true);
|
setRetainInstance(true);
|
||||||
setHasOptionsMenu(true);
|
setHasOptionsMenu(true);
|
||||||
search();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onStart() {
|
public void onStart() {
|
||||||
super.onStart();
|
super.onStart();
|
||||||
EventDistributor.getInstance().register(contentUpdate);
|
EventDistributor.getInstance().register(contentUpdate);
|
||||||
|
search();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -92,21 +89,6 @@ public class SearchFragment extends ListFragment {
|
||||||
EventDistributor.getInstance().unregister(contentUpdate);
|
EventDistributor.getInstance().unregister(contentUpdate);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onDetach() {
|
|
||||||
super.onDetach();
|
|
||||||
if(disposable != null) {
|
|
||||||
disposable.dispose();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onDestroyView() {
|
|
||||||
super.onDestroyView();
|
|
||||||
searchAdapter = null;
|
|
||||||
viewCreated = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onViewCreated(View view, Bundle savedInstanceState) {
|
public void onViewCreated(View view, Bundle savedInstanceState) {
|
||||||
super.onViewCreated(view, savedInstanceState);
|
super.onViewCreated(view, savedInstanceState);
|
||||||
|
@ -118,10 +100,9 @@ public class SearchFragment extends ListFragment {
|
||||||
lv.setPadding(0, vertPadding, 0, vertPadding);
|
lv.setPadding(0, vertPadding, 0, vertPadding);
|
||||||
|
|
||||||
((AppCompatActivity) getActivity()).getSupportActionBar().setTitle(R.string.search_label);
|
((AppCompatActivity) getActivity()).getSupportActionBar().setTitle(R.string.search_label);
|
||||||
viewCreated = true;
|
|
||||||
if (itemsLoaded) {
|
searchAdapter = new SearchlistAdapter(getActivity(), itemAccess);
|
||||||
onFragmentLoaded();
|
setListAdapter(searchAdapter);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -142,28 +123,26 @@ public class SearchFragment extends ListFragment {
|
||||||
@Override
|
@Override
|
||||||
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
||||||
super.onCreateOptionsMenu(menu, inflater);
|
super.onCreateOptionsMenu(menu, inflater);
|
||||||
if (itemsLoaded) {
|
MenuItem item = menu.add(Menu.NONE, R.id.search_item, Menu.NONE, R.string.search_label);
|
||||||
MenuItem item = menu.add(Menu.NONE, R.id.search_item, Menu.NONE, R.string.search_label);
|
MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_IF_ROOM);
|
||||||
MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_IF_ROOM);
|
final SearchView sv = new SearchView(getActivity());
|
||||||
final SearchView sv = new SearchView(getActivity());
|
sv.setQueryHint(getString(R.string.search_hint));
|
||||||
sv.setQueryHint(getString(R.string.search_hint));
|
sv.setQuery(getArguments().getString(ARG_QUERY), false);
|
||||||
sv.setQuery(getArguments().getString(ARG_QUERY), false);
|
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
|
||||||
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
|
@Override
|
||||||
@Override
|
public boolean onQueryTextSubmit(String s) {
|
||||||
public boolean onQueryTextSubmit(String s) {
|
sv.clearFocus();
|
||||||
getArguments().putString(ARG_QUERY, s);
|
getArguments().putString(ARG_QUERY, s);
|
||||||
itemsLoaded = false;
|
search();
|
||||||
search();
|
return true;
|
||||||
return true;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onQueryTextChange(String s) {
|
public boolean onQueryTextChange(String s) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
MenuItemCompat.setActionView(item, sv);
|
MenuItemCompat.setActionView(item, sv);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private final EventDistributor.EventListener contentUpdate = new EventDistributor.EventListener() {
|
private final EventDistributor.EventListener contentUpdate = new EventDistributor.EventListener() {
|
||||||
|
@ -176,14 +155,9 @@ public class SearchFragment extends ListFragment {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
private void onFragmentLoaded() {
|
private void onSearchResults(List<SearchResult> results) {
|
||||||
if (searchAdapter == null) {
|
searchResults = results;
|
||||||
searchAdapter = new SearchlistAdapter(getActivity(), itemAccess);
|
|
||||||
setListAdapter(searchAdapter);
|
|
||||||
}
|
|
||||||
searchAdapter.notifyDataSetChanged();
|
searchAdapter.notifyDataSetChanged();
|
||||||
setListShown(true);
|
|
||||||
|
|
||||||
String query = getArguments().getString(ARG_QUERY);
|
String query = getArguments().getString(ARG_QUERY);
|
||||||
setEmptyText(getString(R.string.no_results_for_query, query));
|
setEmptyText(getString(R.string.no_results_for_query, query));
|
||||||
}
|
}
|
||||||
|
@ -191,12 +165,12 @@ public class SearchFragment extends ListFragment {
|
||||||
private final SearchlistAdapter.ItemAccess itemAccess = new SearchlistAdapter.ItemAccess() {
|
private final SearchlistAdapter.ItemAccess itemAccess = new SearchlistAdapter.ItemAccess() {
|
||||||
@Override
|
@Override
|
||||||
public int getCount() {
|
public int getCount() {
|
||||||
return (searchResults != null) ? searchResults.size() : 0;
|
return searchResults.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public SearchResult getItem(int position) {
|
public SearchResult getItem(int position) {
|
||||||
if (searchResults != null && 0 <= position && position < searchResults.size()) {
|
if (0 <= position && position < searchResults.size()) {
|
||||||
return searchResults.get(position);
|
return searchResults.get(position);
|
||||||
} else {
|
} else {
|
||||||
return null;
|
return null;
|
||||||
|
@ -204,24 +178,14 @@ public class SearchFragment extends ListFragment {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
private void search() {
|
private void search() {
|
||||||
if(disposable != null) {
|
if(disposable != null) {
|
||||||
disposable.dispose();
|
disposable.dispose();
|
||||||
}
|
}
|
||||||
if (viewCreated && !itemsLoaded) {
|
|
||||||
setListShown(false);
|
|
||||||
}
|
|
||||||
disposable = Observable.fromCallable(this::performSearch)
|
disposable = Observable.fromCallable(this::performSearch)
|
||||||
.subscribeOn(Schedulers.io())
|
.subscribeOn(Schedulers.io())
|
||||||
.observeOn(AndroidSchedulers.mainThread())
|
.observeOn(AndroidSchedulers.mainThread())
|
||||||
.subscribe(result -> {
|
.subscribe(this::onSearchResults, error -> Log.e(TAG, Log.getStackTraceString(error)));
|
||||||
itemsLoaded = true;
|
|
||||||
searchResults = result;
|
|
||||||
if (viewCreated) {
|
|
||||||
onFragmentLoaded();
|
|
||||||
}
|
|
||||||
}, error -> Log.e(TAG, Log.getStackTraceString(error)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
|
@ -232,5 +196,4 @@ public class SearchFragment extends ListFragment {
|
||||||
Context context = getActivity();
|
Context context = getActivity();
|
||||||
return FeedSearcher.performSearch(context, query, feed);
|
return FeedSearcher.performSearch(context, query, feed);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue