|
| 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 | +} |
0 commit comments