Skip to content

Commit

Permalink
searchfilters: integrate 3rd UI SearchFragment/SearchFragmentLegacy
Browse files Browse the repository at this point in the history
  • Loading branch information
evermind-zz committed Oct 11, 2022
1 parent 5c6201c commit 1c7ca57
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public class SearchFragment extends BaseListFragment<SearchInfo, ListExtractor.I

private FragmentSearchBinding searchBinding;

private View searchToolbarContainer;
protected View searchToolbarContainer;
private EditText searchEditText;
private View searchClear;

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

public static SearchFragment getInstance(final int serviceId, final String searchString) {
final SearchFragment searchFragment = new SearchFragment();
final SearchFragment searchFragment;
final App app = App.getApp();


final String searchUi = PreferenceManager.getDefaultSharedPreferences(app)
.getString(app.getString(R.string.search_filter_ui_key),
app.getString(R.string.search_filter_ui_value));
if (app.getString(R.string.search_filter_ui_option_menu_legacy_key).equals(searchUi)) {
searchFragment = new SearchFragmentLegacy();
} else {
searchFragment = new SearchFragment();
}

searchFragment.setQuery(serviceId, searchString, null, null);

if (!TextUtils.isEmpty(searchString)) {
Expand Down Expand Up @@ -665,7 +677,7 @@ private void showKeyboardSearch() {
KeyboardUtil.showKeyboard(activity, searchEditText);
}

private void hideKeyboardSearch() {
protected void hideKeyboardSearch() {
if (DEBUG) {
Log.d(TAG, "hideKeyboardSearch() called");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Created by evermind-zz 2022, licensed GNU GPL version 3 or later

package org.schabi.newpipe.fragments.list.search;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;

import org.schabi.newpipe.R;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.StreamingService;
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.fragments.list.search.filter.SearchFilterUIOptionMenu;

import javax.annotation.Nonnull;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import icepick.State;

/**
* UI that hosts the options menu based filter 'dialog'.
*
* Called ..Legacy because of this was the way NewPipe had implemented the search filter dialog.
* The new UI was implemented using a {@link androidx.fragment.app.DialogFragment}
* {@link SearchFragment}
*/
public class SearchFragmentLegacy extends SearchFragment {

private SearchFilterUIOptionMenu searchFilterUi;

@State
protected int countOnPrepareOptionsMenuCalls = 0;

@Override
protected void initializeFilterData() {
try {
final StreamingService service = NewPipe.getService(serviceId);

searchFilterUi = new SearchFilterUIOptionMenu(service, this, getContext());
searchFilterUi.restorePreviouslySelectedFilters(
userSelectedContentFilterList,
userSelectedSortFilterList);

userSelectedContentFilterList = searchFilterUi.getSelectedContentFilters();
userSelectedSortFilterList = searchFilterUi.getSelectedSortFilters();
selectedContentFilter = searchFilterUi.getSelectedContentFilterItems();
selectedSortFilter = searchFilterUi.getSelectedSortFiltersItems();
} catch (final ExtractionException e) {
throw new RuntimeException(e);
}
}

public void onSaveInstanceState(@NonNull final Bundle bundle) {
// get data to save its state via Icepick
userSelectedContentFilterList = searchFilterUi.getSelectedContentFilters();
userSelectedSortFilterList = searchFilterUi.getSelectedSortFilters();

super.onSaveInstanceState(bundle);
}

@Override
protected void createMenu(@NonNull final Menu menu,
@NonNull final MenuInflater inflater) {
searchFilterUi.createSearchUI(menu);
}

public boolean onOptionsItemSelected(@NonNull final MenuItem item) {
return searchFilterUi.onOptionsItemSelected(item);
}

@Override
protected void initViews(final View rootView,
final Bundle savedInstanceState) {
super.initViews(rootView, savedInstanceState);
final Toolbar toolbar = (Toolbar) searchToolbarContainer.getParent();
toolbar.setOverflowIcon(ContextCompat.getDrawable(requireContext(),
R.drawable.ic_sort));
}

@Override
public void onPrepareOptionsMenu(@NonNull @Nonnull final Menu menu) {
super.onPrepareOptionsMenu(menu);
// workaround: we want to hide the keyboard in case we open the options
// menu. As somehow this method gets triggered twice but only the 2nd
// time is relevant as the options menu is selected by the user.
if (++countOnPrepareOptionsMenuCalls > 1) {
hideKeyboardSearch();
}
}
}
3 changes: 3 additions & 0 deletions app/src/main/res/values/settings_keys.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1274,15 +1274,18 @@

<string name="search_filter_ui_dialog_key">dialog</string>
<string name="search_filter_ui_option_menu_style_key">style</string>
<string name="search_filter_ui_option_menu_legacy_key">legacy</string>

<string-array name="search_filter_ui_values">
<item>@string/search_filter_ui_dialog_key</item>
<item>@string/search_filter_ui_option_menu_style_key</item>
<item>@string/search_filter_ui_option_menu_legacy_key</item>
</string-array>

<string-array name="search_filter_ui_description">
<item>@string/search_filter_ui_dialog</item>
<item>@string/search_filter_ui_style</item>
<item>@string/search_filter_ui_legacy</item>
</string-array>

<string name="tablet_mode_key">tablet_mode</string>
Expand Down
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 @@ -549,6 +549,7 @@
<string name="search_filter_ui">Select Search Filter UI</string>
<string name="search_filter_ui_dialog">Simple Dialog (default)</string>
<string name="search_filter_ui_style">Action Menu styled Dialog</string>
<string name="search_filter_ui_legacy">Action Menu (legacy)</string>
<!-- Seekbar Preview Thumbnail-->
<string name="seekbar_preview_thumbnail_title">Seekbar thumbnail preview</string>
<string name="high_quality_larger">High quality (larger)</string>
Expand Down

0 comments on commit 1c7ca57

Please sign in to comment.