Skip to content

Commit bf063f3

Browse files
committed
searchfilters: integrate 3rd UI SearchFragment/SearchFragmentLegacy
1 parent 9e658c1 commit bf063f3

4 files changed

Lines changed: 110 additions & 3 deletions

File tree

app/src/main/java/org/schabi/newpipe/fragments/list/search/SearchFragment.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public class SearchFragment extends BaseListFragment<SearchInfo, ListExtractor.I
153153

154154
private FragmentSearchBinding searchBinding;
155155

156-
private View searchToolbarContainer;
156+
protected View searchToolbarContainer;
157157
private EditText searchEditText;
158158
private View searchClear;
159159

@@ -170,7 +170,19 @@ public class SearchFragment extends BaseListFragment<SearchInfo, ListExtractor.I
170170
ArrayList<Integer> userSelectedSortFilterList = null;
171171

172172
public static SearchFragment getInstance(final int serviceId, final String searchString) {
173-
final SearchFragment searchFragment = new SearchFragment();
173+
final SearchFragment searchFragment;
174+
final App app = App.getApp();
175+
176+
177+
final String searchUi = PreferenceManager.getDefaultSharedPreferences(app)
178+
.getString(app.getString(R.string.search_filter_ui_key),
179+
app.getString(R.string.search_filter_ui_value));
180+
if (app.getString(R.string.search_filter_ui_option_menu_legacy_key).equals(searchUi)) {
181+
searchFragment = new SearchFragmentLegacy();
182+
} else {
183+
searchFragment = new SearchFragment();
184+
}
185+
174186
searchFragment.setQuery(serviceId, searchString, null, null);
175187

176188
if (!TextUtils.isEmpty(searchString)) {
@@ -665,7 +677,7 @@ private void showKeyboardSearch() {
665677
KeyboardUtil.showKeyboard(activity, searchEditText);
666678
}
667679

668-
private void hideKeyboardSearch() {
680+
protected void hideKeyboardSearch() {
669681
if (DEBUG) {
670682
Log.d(TAG, "hideKeyboardSearch() called");
671683
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Created by evermind-zz 2022, licensed GNU GPL version 3 or later
2+
3+
package org.schabi.newpipe.fragments.list.search;
4+
5+
import android.os.Bundle;
6+
import android.view.Menu;
7+
import android.view.MenuInflater;
8+
import android.view.MenuItem;
9+
import android.view.View;
10+
11+
import org.schabi.newpipe.R;
12+
import org.schabi.newpipe.extractor.NewPipe;
13+
import org.schabi.newpipe.extractor.StreamingService;
14+
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
15+
import org.schabi.newpipe.fragments.list.search.filter.SearchFilterUIOptionMenu;
16+
17+
import androidx.annotation.NonNull;
18+
import androidx.appcompat.widget.Toolbar;
19+
import androidx.core.content.ContextCompat;
20+
import icepick.State;
21+
22+
/**
23+
* UI that hosts the options menu based filter 'dialog'.
24+
* <p>
25+
* Called ..Legacy because of this was the way NewPipe had implemented the search filter dialog.
26+
* The new UI was implemented using a {@link androidx.fragment.app.DialogFragment}
27+
* {@link SearchFragment}
28+
*/
29+
public class SearchFragmentLegacy extends SearchFragment {
30+
31+
@State
32+
protected int countOnPrepareOptionsMenuCalls = 0;
33+
private SearchFilterUIOptionMenu searchFilterUi;
34+
35+
@Override
36+
protected void initializeFilterData() {
37+
try {
38+
final StreamingService service = NewPipe.getService(serviceId);
39+
40+
searchFilterUi = new SearchFilterUIOptionMenu(service, this, getContext());
41+
searchFilterUi.restorePreviouslySelectedFilters(
42+
userSelectedContentFilterList,
43+
userSelectedSortFilterList);
44+
45+
userSelectedContentFilterList = searchFilterUi.getSelectedContentFilters();
46+
userSelectedSortFilterList = searchFilterUi.getSelectedSortFilters();
47+
selectedContentFilter = searchFilterUi.getSelectedContentFilterItems();
48+
selectedSortFilter = searchFilterUi.getSelectedSortFiltersItems();
49+
} catch (final ExtractionException e) {
50+
throw new RuntimeException(e);
51+
}
52+
}
53+
54+
public void onSaveInstanceState(@NonNull final Bundle bundle) {
55+
// get data to save its state via Icepick
56+
userSelectedContentFilterList = searchFilterUi.getSelectedContentFilters();
57+
userSelectedSortFilterList = searchFilterUi.getSelectedSortFilters();
58+
59+
super.onSaveInstanceState(bundle);
60+
}
61+
62+
@Override
63+
protected void createMenu(@NonNull final Menu menu,
64+
@NonNull final MenuInflater inflater) {
65+
searchFilterUi.createSearchUI(menu);
66+
}
67+
68+
public boolean onOptionsItemSelected(@NonNull final MenuItem item) {
69+
return searchFilterUi.onOptionsItemSelected(item);
70+
}
71+
72+
@Override
73+
protected void initViews(final View rootView,
74+
final Bundle savedInstanceState) {
75+
super.initViews(rootView, savedInstanceState);
76+
final Toolbar toolbar = (Toolbar) searchToolbarContainer.getParent();
77+
toolbar.setOverflowIcon(ContextCompat.getDrawable(requireContext(),
78+
R.drawable.ic_sort));
79+
}
80+
81+
@Override
82+
public void onPrepareOptionsMenu(@NonNull final Menu menu) {
83+
super.onPrepareOptionsMenu(menu);
84+
// workaround: we want to hide the keyboard in case we open the options
85+
// menu. As somehow this method gets triggered twice but only the 2nd
86+
// time is relevant as the options menu is selected by the user.
87+
if (++countOnPrepareOptionsMenuCalls > 1) {
88+
hideKeyboardSearch();
89+
}
90+
}
91+
}

app/src/main/res/values/settings_keys.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,15 +1274,18 @@
12741274

12751275
<string name="search_filter_ui_dialog_key">dialog</string>
12761276
<string name="search_filter_ui_option_menu_style_key">style</string>
1277+
<string name="search_filter_ui_option_menu_legacy_key">legacy</string>
12771278

12781279
<string-array name="search_filter_ui_values">
12791280
<item>@string/search_filter_ui_dialog_key</item>
12801281
<item>@string/search_filter_ui_option_menu_style_key</item>
1282+
<item>@string/search_filter_ui_option_menu_legacy_key</item>
12811283
</string-array>
12821284

12831285
<string-array name="search_filter_ui_description">
12841286
<item>@string/search_filter_ui_dialog</item>
12851287
<item>@string/search_filter_ui_style</item>
1288+
<item>@string/search_filter_ui_legacy</item>
12861289
</string-array>
12871290

12881291
<string name="tablet_mode_key">tablet_mode</string>

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,7 @@
549549
<string name="search_filter_ui">Select Search Filter UI</string>
550550
<string name="search_filter_ui_dialog">Simple Dialog (default)</string>
551551
<string name="search_filter_ui_style">Action Menu styled Dialog</string>
552+
<string name="search_filter_ui_legacy">Action Menu (legacy)</string>
552553
<!-- Seekbar Preview Thumbnail-->
553554
<string name="seekbar_preview_thumbnail_title">Seekbar thumbnail preview</string>
554555
<string name="high_quality_larger">High quality (larger)</string>

0 commit comments

Comments
 (0)