diff --git a/app/src/main/java/org/schabi/newpipe/fragments/list/search/SearchFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/list/search/SearchFragment.java index 49054820522..0dd13de9d01 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/list/search/SearchFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/list/search/SearchFragment.java @@ -153,7 +153,7 @@ public class SearchFragment extends BaseListFragment 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)) { @@ -665,7 +677,7 @@ private void showKeyboardSearch() { KeyboardUtil.showKeyboard(activity, searchEditText); } - private void hideKeyboardSearch() { + protected void hideKeyboardSearch() { if (DEBUG) { Log.d(TAG, "hideKeyboardSearch() called"); } diff --git a/app/src/main/java/org/schabi/newpipe/fragments/list/search/SearchFragmentLegacy.java b/app/src/main/java/org/schabi/newpipe/fragments/list/search/SearchFragmentLegacy.java new file mode 100644 index 00000000000..8f60e40fb88 --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/fragments/list/search/SearchFragmentLegacy.java @@ -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(); + } + } +} diff --git a/app/src/main/res/values/settings_keys.xml b/app/src/main/res/values/settings_keys.xml index bed2916b0b5..320fb23fcc7 100644 --- a/app/src/main/res/values/settings_keys.xml +++ b/app/src/main/res/values/settings_keys.xml @@ -1274,15 +1274,18 @@ dialog style + legacy @string/search_filter_ui_dialog_key @string/search_filter_ui_option_menu_style_key + @string/search_filter_ui_option_menu_legacy_key @string/search_filter_ui_dialog @string/search_filter_ui_style + @string/search_filter_ui_legacy tablet_mode diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index d9dc83cc663..071816d02e3 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -549,6 +549,7 @@ Select Search Filter UI Simple Dialog (default) Action Menu styled Dialog + Action Menu (legacy) Seekbar thumbnail preview High quality (larger)