1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2024-12-23 16:40:32 +00:00

Fix deprecations, warnings and useless null checks in SearchFragment

This commit is contained in:
Stypox 2020-07-31 11:40:29 +02:00
parent f8de72f59f
commit 0062ff9cfa
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23

View File

@ -79,7 +79,7 @@ import static androidx.recyclerview.widget.ItemTouchHelper.Callback.makeMovement
import static java.util.Arrays.asList; import static java.util.Arrays.asList;
import static org.schabi.newpipe.util.AnimationUtils.animateView; import static org.schabi.newpipe.util.AnimationUtils.animateView;
public class SearchFragment extends BaseListFragment<SearchInfo, ListExtractor.InfoItemsPage> public class SearchFragment extends BaseListFragment<SearchInfo, ListExtractor.InfoItemsPage<?>>
implements BackPressable { implements BackPressable {
/*////////////////////////////////////////////////////////////////////////// /*//////////////////////////////////////////////////////////////////////////
// Search // Search
@ -134,7 +134,6 @@ public class SearchFragment extends BaseListFragment<SearchInfo, ListExtractor.I
private Map<Integer, String> menuItemToFilterName; private Map<Integer, String> menuItemToFilterName;
private StreamingService service; private StreamingService service;
private Page nextPage; private Page nextPage;
private String contentCountry;
private boolean isSuggestionsEnabled = true; private boolean isSuggestionsEnabled = true;
private Disposable searchDisposable; private Disposable searchDisposable;
@ -206,8 +205,6 @@ public class SearchFragment extends BaseListFragment<SearchInfo, ListExtractor.I
= PreferenceManager.getDefaultSharedPreferences(activity); = PreferenceManager.getDefaultSharedPreferences(activity);
isSuggestionsEnabled = preferences isSuggestionsEnabled = preferences
.getBoolean(getString(R.string.show_search_suggestions_key), true); .getBoolean(getString(R.string.show_search_suggestions_key), true);
contentCountry = preferences.getString(getString(R.string.content_country_key),
getString(R.string.default_localization_key));
} }
@Override @Override
@ -235,9 +232,7 @@ public class SearchFragment extends BaseListFragment<SearchInfo, ListExtractor.I
if (suggestionDisposable != null) { if (suggestionDisposable != null) {
suggestionDisposable.dispose(); suggestionDisposable.dispose();
} }
if (disposables != null) { disposables.clear();
disposables.clear();
}
hideKeyboardSearch(); hideKeyboardSearch();
} }
@ -251,8 +246,8 @@ public class SearchFragment extends BaseListFragment<SearchInfo, ListExtractor.I
try { try {
service = NewPipe.getService(serviceId); service = NewPipe.getService(serviceId);
} catch (final Exception e) { } catch (final Exception e) {
ErrorActivity.reportError(getActivity(), e, getActivity().getClass(), ErrorActivity.reportError(getActivity(), e, requireActivity().getClass(),
getActivity().findViewById(android.R.id.content), requireActivity().findViewById(android.R.id.content),
ErrorActivity.ErrorInfo.make(UserAction.UI_ERROR, ErrorActivity.ErrorInfo.make(UserAction.UI_ERROR,
"", "",
"", R.string.general_error)); "", R.string.general_error));
@ -305,26 +300,20 @@ public class SearchFragment extends BaseListFragment<SearchInfo, ListExtractor.I
if (suggestionDisposable != null) { if (suggestionDisposable != null) {
suggestionDisposable.dispose(); suggestionDisposable.dispose();
} }
if (disposables != null) { disposables.clear();
disposables.clear();
}
} }
@Override @Override
public void onActivityResult(final int requestCode, final int resultCode, final Intent data) { public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
switch (requestCode) { if (requestCode == ReCaptchaActivity.RECAPTCHA_REQUEST) {
case ReCaptchaActivity.RECAPTCHA_REQUEST: if (resultCode == Activity.RESULT_OK
if (resultCode == Activity.RESULT_OK && !TextUtils.isEmpty(searchString)) {
&& !TextUtils.isEmpty(searchString)) { search(searchString, contentFilter, sortFilter);
search(searchString, contentFilter, sortFilter); } else {
} else { Log.e(TAG, "ReCaptcha failed");
Log.e(TAG, "ReCaptcha failed"); }
} } else {
break; Log.e(TAG, "Request code from activity not supported [" + requestCode + "]");
default:
Log.e(TAG, "Request code from activity not supported [" + requestCode + "]");
break;
} }
} }
@ -342,7 +331,7 @@ public class SearchFragment extends BaseListFragment<SearchInfo, ListExtractor.I
@Override @Override
public int getMovementFlags(@NonNull final RecyclerView recyclerView, public int getMovementFlags(@NonNull final RecyclerView recyclerView,
@NonNull final RecyclerView.ViewHolder viewHolder) { @NonNull final RecyclerView.ViewHolder viewHolder) {
return getSuggestionMovementFlags(recyclerView, viewHolder); return getSuggestionMovementFlags(viewHolder);
} }
@Override @Override
@ -354,7 +343,7 @@ public class SearchFragment extends BaseListFragment<SearchInfo, ListExtractor.I
@Override @Override
public void onSwiped(@NonNull final RecyclerView.ViewHolder viewHolder, final int i) { public void onSwiped(@NonNull final RecyclerView.ViewHolder viewHolder, final int i) {
onSuggestionItemSwiped(viewHolder, i); onSuggestionItemSwiped(viewHolder);
} }
}).attachToRecyclerView(suggestionsRecyclerView); }).attachToRecyclerView(suggestionsRecyclerView);
@ -673,8 +662,7 @@ public class SearchFragment extends BaseListFragment<SearchInfo, ListExtractor.I
} }
private void showDeleteSuggestionDialog(final SuggestionItem item) { private void showDeleteSuggestionDialog(final SuggestionItem item) {
if (activity == null || historyRecordManager == null || suggestionPublisher == null if (activity == null || historyRecordManager == null || searchEditText == null) {
|| searchEditText == null || disposables == null) {
return; return;
} }
final String query = item.query; final String query = item.query;
@ -802,21 +790,23 @@ public class SearchFragment extends BaseListFragment<SearchInfo, ListExtractor.I
// no-op // no-op
} }
private void search(final String ss, final String[] cf, final String sf) { private void search(final String theSearchString,
final String[] theContentFilter,
final String theSortFilter) {
if (DEBUG) { if (DEBUG) {
Log.d(TAG, "search() called with: query = [" + ss + "]"); Log.d(TAG, "search() called with: query = [" + theSearchString + "]");
} }
if (ss.isEmpty()) { if (theSearchString.isEmpty()) {
return; return;
} }
try { try {
final StreamingService streamingService = NewPipe.getServiceByUrl(ss); final StreamingService streamingService = NewPipe.getServiceByUrl(theSearchString);
if (streamingService != null) { if (streamingService != null) {
showLoading(); showLoading();
disposables.add(Observable disposables.add(Observable
.fromCallable(() -> .fromCallable(() -> NavigationHelper.getIntentByLink(activity,
NavigationHelper.getIntentByLink(activity, streamingService, ss)) streamingService, theSearchString))
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe(intent -> { .subscribe(intent -> {
@ -831,29 +821,27 @@ public class SearchFragment extends BaseListFragment<SearchInfo, ListExtractor.I
} }
lastSearchedString = this.searchString; lastSearchedString = this.searchString;
this.searchString = ss; this.searchString = theSearchString;
infoListAdapter.clearStreamItemList(); infoListAdapter.clearStreamItemList();
hideSuggestionsPanel(); hideSuggestionsPanel();
hideKeyboardSearch(); hideKeyboardSearch();
historyRecordManager.onSearched(serviceId, ss) disposables.add(historyRecordManager.onSearched(serviceId, theSearchString)
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe( .subscribe(
ignored -> { ignored -> {
}, },
error -> showSnackBarError(error, UserAction.SEARCHED, error -> showSnackBarError(error, UserAction.SEARCHED,
NewPipe.getNameOfService(serviceId), ss, 0) NewPipe.getNameOfService(serviceId), theSearchString, 0)
); ));
suggestionPublisher.onNext(ss); suggestionPublisher.onNext(theSearchString);
startLoading(false); startLoading(false);
} }
@Override @Override
public void startLoading(final boolean forceLoad) { public void startLoading(final boolean forceLoad) {
super.startLoading(forceLoad); super.startLoading(forceLoad);
if (disposables != null) { disposables.clear();
disposables.clear();
}
if (searchDisposable != null) { if (searchDisposable != null) {
searchDisposable.dispose(); searchDisposable.dispose();
} }
@ -892,8 +880,7 @@ public class SearchFragment extends BaseListFragment<SearchInfo, ListExtractor.I
@Override @Override
protected boolean hasMoreItems() { protected boolean hasMoreItems() {
// TODO: No way to tell if search has more items in the moment return Page.isValid(nextPage);
return true;
} }
@Override @Override
@ -906,22 +893,25 @@ public class SearchFragment extends BaseListFragment<SearchInfo, ListExtractor.I
// Utils // Utils
//////////////////////////////////////////////////////////////////////////*/ //////////////////////////////////////////////////////////////////////////*/
private void changeContentFilter(final MenuItem item, final List<String> cf) { private void changeContentFilter(final MenuItem item, final List<String> theContentFilter) {
this.filterItemCheckedId = item.getItemId(); filterItemCheckedId = item.getItemId();
item.setChecked(true); item.setChecked(true);
this.contentFilter = new String[]{cf.get(0)}; contentFilter = new String[]{theContentFilter.get(0)};
if (!TextUtils.isEmpty(searchString)) { if (!TextUtils.isEmpty(searchString)) {
search(searchString, this.contentFilter, sortFilter); search(searchString, contentFilter, sortFilter);
} }
} }
private void setQuery(final int sid, final String ss, final String[] cf, final String sf) { private void setQuery(final int theServiceId,
this.serviceId = sid; final String theSearchString,
this.searchString = searchString; final String[] theContentFilter,
this.contentFilter = cf; final String theSortFilter) {
this.sortFilter = sf; serviceId = theServiceId;
searchString = theSearchString;
contentFilter = theContentFilter;
sortFilter = theSortFilter;
} }
/*////////////////////////////////////////////////////////////////////////// /*//////////////////////////////////////////////////////////////////////////
@ -1038,7 +1028,7 @@ public class SearchFragment extends BaseListFragment<SearchInfo, ListExtractor.I
} }
@Override @Override
public void handleNextItems(final ListExtractor.InfoItemsPage result) { public void handleNextItems(final ListExtractor.InfoItemsPage<?> result) {
showListFooter(false); showListFooter(false);
infoListAdapter.addInfoItemList(result.getItems()); infoListAdapter.addInfoItemList(result.getItems());
nextPage = result.getNextPage(); nextPage = result.getNextPage();
@ -1077,8 +1067,7 @@ public class SearchFragment extends BaseListFragment<SearchInfo, ListExtractor.I
// Suggestion item touch helper // Suggestion item touch helper
//////////////////////////////////////////////////////////////////////////*/ //////////////////////////////////////////////////////////////////////////*/
public int getSuggestionMovementFlags(@NonNull final RecyclerView recyclerView, public int getSuggestionMovementFlags(@NonNull final RecyclerView.ViewHolder viewHolder) {
@NonNull final RecyclerView.ViewHolder viewHolder) {
final int position = viewHolder.getAdapterPosition(); final int position = viewHolder.getAdapterPosition();
if (position == RecyclerView.NO_POSITION) { if (position == RecyclerView.NO_POSITION) {
return 0; return 0;
@ -1089,8 +1078,7 @@ public class SearchFragment extends BaseListFragment<SearchInfo, ListExtractor.I
ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) : 0; ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) : 0;
} }
public void onSuggestionItemSwiped(@NonNull final RecyclerView.ViewHolder viewHolder, public void onSuggestionItemSwiped(@NonNull final RecyclerView.ViewHolder viewHolder) {
final int i) {
final int position = viewHolder.getAdapterPosition(); final int position = viewHolder.getAdapterPosition();
final String query = suggestionListAdapter.getItem(position).query; final String query = suggestionListAdapter.getItem(position).query;
final Disposable onDelete = historyRecordManager.deleteSearchHistory(query) final Disposable onDelete = historyRecordManager.deleteSearchHistory(query)