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

Display search suggestion: did you mean & showing result for #3471

Merged
merged 6 commits into from
Jul 7, 2020
Merged
Show file tree
Hide file tree
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
Expand Up @@ -7,6 +7,7 @@
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.Editable;
import android.text.Html;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
Expand Down Expand Up @@ -71,6 +72,7 @@
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subjects.PublishSubject;

import static android.text.Html.escapeHtml;
import static androidx.recyclerview.widget.ItemTouchHelper.Callback.makeMovementFlags;
import static java.util.Arrays.asList;
import static org.schabi.newpipe.util.AnimationUtils.animateView;
Expand Down Expand Up @@ -118,6 +120,12 @@ public class SearchFragment extends BaseListFragment<SearchInfo, ListExtractor.I
@State
String lastSearchedString;

@State
String searchSuggestion;

@State
boolean isCorrectedSearch;

@State
boolean wasSearchFocused = false;

Expand All @@ -143,6 +151,8 @@ public class SearchFragment extends BaseListFragment<SearchInfo, ListExtractor.I
private EditText searchEditText;
private View searchClear;

private TextView correctSuggestion;

private View suggestionsPanel;
private RecyclerView suggestionsRecyclerView;

Expand Down Expand Up @@ -257,6 +267,8 @@ public void onResume() {
}
}

handleSearchSuggestion();

if (suggestionDisposable == null || suggestionDisposable.isDisposed()) {
initSuggestionObserver();
}
Expand Down Expand Up @@ -345,6 +357,8 @@ public void onSwiped(@NonNull final RecyclerView.ViewHolder viewHolder, final in
searchToolbarContainer = activity.findViewById(R.id.toolbar_search_container);
searchEditText = searchToolbarContainer.findViewById(R.id.toolbar_search_edit_text);
searchClear = searchToolbarContainer.findViewById(R.id.toolbar_search_clear);

correctSuggestion = rootView.findViewById(R.id.correct_suggestion);
}

/*//////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -497,6 +511,8 @@ private void initSearchListeners() {
return;
}

correctSuggestion.setVisibility(View.GONE);

searchEditText.setText("");
suggestionListAdapter.setItems(new ArrayList<>());
showKeyboardSearch();
Expand Down Expand Up @@ -554,11 +570,13 @@ public void onSuggestionItemLongClick(final SuggestionItem item) {
textWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(final CharSequence s, final int start,
final int count, final int after) { }
final int count, final int after) {
}

@Override
public void onTextChanged(final CharSequence s, final int start,
final int before, final int count) { }
final int before, final int count) {
}

@Override
public void afterTextChanged(final Editable s) {
Expand Down Expand Up @@ -688,10 +706,6 @@ public boolean onBackPressed() {
return false;
}

public void giveSearchEditTextFocus() {
showKeyboardSearch();
}

private void initSuggestionObserver() {
if (DEBUG) {
Log.d(TAG, "initSuggestionObserver() called");
Expand Down Expand Up @@ -961,6 +975,11 @@ public void handleResult(@NonNull final SearchInfo result) {
NewPipe.getNameOfService(serviceId), searchString, 0);
}

searchSuggestion = result.getSearchSuggestion();
isCorrectedSearch = result.isCorrectedSearch();
wb9688 marked this conversation as resolved.
Show resolved Hide resolved

handleSearchSuggestion();

lastSearchedString = searchString;
nextPageUrl = result.getNextPageUrl();
currentPageUrl = result.getUrl();
Expand All @@ -978,6 +997,37 @@ public void handleResult(@NonNull final SearchInfo result) {
super.handleResult(result);
}

private void handleSearchSuggestion() {
if (TextUtils.isEmpty(searchSuggestion)) {
correctSuggestion.setVisibility(View.GONE);
} else {
final String helperText = getString(isCorrectedSearch
? R.string.search_showing_result_for
: R.string.did_you_mean);

final String highlightedSearchSuggestion =
"<b><i>" + escapeHtml(searchSuggestion) + "</i></b>";
correctSuggestion.setText(
Html.fromHtml(String.format(helperText, highlightedSearchSuggestion)));


correctSuggestion.setOnClickListener(v -> {
correctSuggestion.setVisibility(View.GONE);
search(searchSuggestion, contentFilter, sortFilter);
searchEditText.setText(searchSuggestion);
});

correctSuggestion.setOnLongClickListener(v -> {
searchEditText.setText(searchSuggestion);
searchEditText.setSelection(searchSuggestion.length());
showKeyboardSearch();
return true;
});

correctSuggestion.setVisibility(View.VISIBLE);
}
}

@Override
public void handleNextItems(final ListExtractor.InfoItemsPage result) {
showListFooter(false);
Expand Down
17 changes: 15 additions & 2 deletions app/src/main/res/layout/fragment_search.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,25 @@
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/correct_suggestion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="@id/error_panel"
android:background="?attr/selectableItemBackground"
android:padding="10dp"
android:textColor="@color/background_title_color"
android:textSize="@dimen/search_suggestion_text_size"
tools:text="Showing results for lorem ipsum dolor sit amet consectetur adipisci elit" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/items_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/correct_suggestion"
android:scrollbars="vertical"
app:layoutManager="LinearLayoutManager"
tools:listitem="@layout/list_stream_item"/>
tools:listitem="@layout/list_stream_item" />

<ProgressBar
android:id="@+id/loading_progress_bar"
Expand Down Expand Up @@ -58,6 +70,7 @@
android:background="?android:attr/windowBackground"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical"
android:visibility="gone"
tools:background="@android:color/transparent"
tools:visibility="visible">
Expand All @@ -68,7 +81,7 @@
android:layout_height="match_parent"
android:scrollbars="vertical"
app:layoutManager="LinearLayoutManager"
tools:listitem="@layout/item_search_suggestion"/>
tools:listitem="@layout/item_search_suggestion" />
</LinearLayout>

<!--ERROR PANEL-->
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-land/dimens.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,6 @@

<!-- File picker dimensions -->
<dimen name="file_picker_items_text_size">16sp</dimen>

<dimen name="search_suggestion_text_size">14sp</dimen>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-sw600dp/dimens.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@
<!-- Paddings & Margins -->
<dimen name="video_item_detail_like_margin">10dp</dimen>

<dimen name="search_suggestion_text_size">14sp</dimen>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,5 @@
<dimen name="feed_group_carousel_top_bottom_margin">2dp</dimen>
<dimen name="feed_group_carousel_between_items_margin">4dp</dimen>

<dimen name="search_suggestion_text_size">16sp</dimen>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<string name="search">Search</string>
<string name="settings">Settings</string>
<string name="did_you_mean">Did you mean: %1$s?</string>
<string name="search_showing_result_for">Showing results for: %s</string>
<string name="share_dialog_title">Share with</string>
<string name="choose_browser">Choose browser</string>
<string name="screen_rotation">rotation</string>
Expand Down