Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support exact search #11509

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.schabi.newpipe.fragments.list.search;

import static androidx.recyclerview.widget.ItemTouchHelper.Callback.makeMovementFlags;
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.ALL;
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.EXACT;
import static org.schabi.newpipe.extractor.utils.Utils.isBlank;
import static org.schabi.newpipe.ktx.ViewUtils.animate;
import static org.schabi.newpipe.util.ExtractorHelper.showMetaInfoInTextView;
Expand Down Expand Up @@ -845,7 +847,24 @@ private void search(@NonNull final String theSearchString,

// prepare search
lastSearchedString = this.searchString;
this.searchString = theSearchString;
searchString = theSearchString;

// Always remove EXACT search initially so it doesn't persist over multiple searches
final List<String> tempContentFilter = new ArrayList<>(Arrays.asList(contentFilter));
tempContentFilter.remove(EXACT);
contentFilter = tempContentFilter.toArray(new String[0]);

// If quotes are around the search string EXACT search is enabled
// Overrides ALL and is otherwise appended to existing filters
if (searchString.charAt(0) == '\"'
&& searchString.charAt(searchString.length() - 1) == '\"') {
if (contentFilter.length == 0 || contentFilter[0].equals(ALL)) {
contentFilter = new String[]{EXACT};
} else {
contentFilter = new String[]{contentFilter[0], EXACT};
}
}

infoListAdapter.clearStreamItemList();
hideSuggestionsPanel();
showMetaInfoInTextView(null, searchBinding.searchMetaInfoTextView,
Expand All @@ -867,6 +886,15 @@ private void search(@NonNull final String theSearchString,
startLoading(false);
}

/**
* Adjusts the search string from EXACT searches for queries (removes the surrounding quotes).
* @return The adjusted search string
*/
private String getExactSearchAdjustedString() {
return Arrays.asList(contentFilter).contains(EXACT)
? searchString.substring(1, searchString.length() - 1) : searchString;
}

@Override
public void startLoading(final boolean forceLoad) {
super.startLoading(forceLoad);
Expand All @@ -875,14 +903,13 @@ public void startLoading(final boolean forceLoad) {
searchDisposable.dispose();
}
searchDisposable = ExtractorHelper.searchFor(serviceId,
searchString,
getExactSearchAdjustedString(),
Arrays.asList(contentFilter),
sortFilter)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnEvent((searchResult, throwable) -> isLoading.set(false))
.subscribe(this::handleResult, this::onItemError);

}

@Override
Expand All @@ -897,7 +924,7 @@ protected void loadMoreItems() {
}
searchDisposable = ExtractorHelper.getMoreSearchItems(
serviceId,
searchString,
getExactSearchAdjustedString(),
asList(contentFilter),
sortFilter,
nextPage)
Expand Down
Loading