|
| 1 | +// Created by evermind-zz 2022, licensed GNU GPL version 3 or later |
| 2 | + |
| 3 | +package org.schabi.newpipe.fragments.list.search.filter; |
| 4 | + |
| 5 | +import android.os.Bundle; |
| 6 | +import android.view.LayoutInflater; |
| 7 | +import android.view.View; |
| 8 | +import android.view.ViewGroup; |
| 9 | + |
| 10 | +import org.schabi.newpipe.R; |
| 11 | +import org.schabi.newpipe.extractor.NewPipe; |
| 12 | +import org.schabi.newpipe.extractor.StreamingService; |
| 13 | +import org.schabi.newpipe.extractor.exceptions.ExtractionException; |
| 14 | +import org.schabi.newpipe.extractor.search.filter.FilterItem; |
| 15 | + |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.List; |
| 18 | + |
| 19 | +import androidx.annotation.NonNull; |
| 20 | +import androidx.appcompat.widget.Toolbar; |
| 21 | +import androidx.fragment.app.DialogFragment; |
| 22 | +import icepick.Icepick; |
| 23 | +import icepick.State; |
| 24 | + |
| 25 | +/** |
| 26 | + * Base dialog class for {@link DialogFragment} based search filter dialogs. |
| 27 | + */ |
| 28 | +public abstract class BaseSearchFilterDialogFragment extends DialogFragment { |
| 29 | + |
| 30 | + private static final String CONTENT_FILTERS = "CONTENT_FILTERS"; |
| 31 | + private static final String SORT_FILTERS = "SORT_FILTERS"; |
| 32 | + private static final String SERVICE_ID = "SERVICE_ID"; |
| 33 | + |
| 34 | + @State |
| 35 | + public ArrayList<Integer> userSelectedContentFilterList; |
| 36 | + protected List<FilterItem> selectedContentFilters; |
| 37 | + protected List<FilterItem> selectedSortFilters; |
| 38 | + protected BaseSearchFilterUiGenerator dialogGenerator; |
| 39 | + @State |
| 40 | + ArrayList<Integer> userSelectedSortFilterList = null; |
| 41 | + |
| 42 | + protected static DialogFragment initDialogArguments( |
| 43 | + final DialogFragment dialogFragment, |
| 44 | + final int serviceId, |
| 45 | + final ArrayList<Integer> userSelectedContentFilter, |
| 46 | + final ArrayList<Integer> userSelectedSortFilter) { |
| 47 | + final Bundle bundle = new Bundle(1); |
| 48 | + bundle.putInt(SERVICE_ID, serviceId); |
| 49 | + bundle.putIntegerArrayList(CONTENT_FILTERS, userSelectedContentFilter); |
| 50 | + bundle.putIntegerArrayList(SORT_FILTERS, userSelectedSortFilter); |
| 51 | + dialogFragment.setArguments(bundle); |
| 52 | + |
| 53 | + return dialogFragment; |
| 54 | + } |
| 55 | + |
| 56 | + private void initializeFilterData() { |
| 57 | + |
| 58 | + assert getArguments() != null; |
| 59 | + final int serviceId = getArguments().getInt(SERVICE_ID); |
| 60 | + final ArrayList<Integer> contentFilters = |
| 61 | + getArguments().getIntegerArrayList(CONTENT_FILTERS); |
| 62 | + final ArrayList<Integer> sortFilters = |
| 63 | + getArguments().getIntegerArrayList(SORT_FILTERS); |
| 64 | + |
| 65 | + final StreamingService service; |
| 66 | + try { |
| 67 | + service = NewPipe.getService(serviceId); |
| 68 | + } catch (final ExtractionException e) { |
| 69 | + throw new RuntimeException(e); |
| 70 | + } |
| 71 | + |
| 72 | + dialogGenerator = createSearchFilterDialogGenerator(service, |
| 73 | + (userSelectedContentFilter, userSelectedSortFilter) -> { |
| 74 | + selectedContentFilters = userSelectedContentFilter; |
| 75 | + selectedSortFilters = userSelectedSortFilter; |
| 76 | + sendDataToParentFragment(); |
| 77 | + }); |
| 78 | + |
| 79 | + userSelectedContentFilterList = contentFilters; |
| 80 | + userSelectedSortFilterList = sortFilters; |
| 81 | + |
| 82 | + dialogGenerator.restorePreviouslySelectedFilters( |
| 83 | + userSelectedContentFilterList, |
| 84 | + userSelectedSortFilterList); |
| 85 | + |
| 86 | + dialogGenerator.createSearchUI(); |
| 87 | + } |
| 88 | + |
| 89 | + protected abstract BaseSearchFilterUiGenerator createSearchFilterDialogGenerator( |
| 90 | + StreamingService service, |
| 91 | + SearchFilterLogic.Callback callback); |
| 92 | + |
| 93 | + /** |
| 94 | + * As we have different bindings we need to get this sorted in a method. |
| 95 | + * |
| 96 | + * @return the {@link Toolbar} |
| 97 | + */ |
| 98 | + protected abstract Toolbar getToolbar(); |
| 99 | + |
| 100 | + protected abstract View getRootView(LayoutInflater inflater, |
| 101 | + ViewGroup container); |
| 102 | + |
| 103 | + @Override |
| 104 | + public View onCreateView(@NonNull final LayoutInflater inflater, |
| 105 | + final ViewGroup container, |
| 106 | + final Bundle savedInstanceState) { |
| 107 | + final View rootView = getRootView(inflater, container); |
| 108 | + initializeFilterData(); |
| 109 | + return rootView; |
| 110 | + } |
| 111 | + |
| 112 | + |
| 113 | + @Override |
| 114 | + public void onResume() { |
| 115 | + super.onResume(); |
| 116 | + dialogGenerator.onResume(); |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public void onStop() { |
| 121 | + dialogGenerator.onPause(); |
| 122 | + super.onStop(); |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public void onViewCreated(@NonNull final View view, final Bundle savedInstanceState) { |
| 127 | + super.onViewCreated(view, savedInstanceState); |
| 128 | + Icepick.restoreInstanceState(this, savedInstanceState); |
| 129 | + |
| 130 | + initToolbar(getToolbar()); |
| 131 | + } |
| 132 | + |
| 133 | + protected void initToolbar(final Toolbar toolbar) { |
| 134 | + toolbar.setTitle(R.string.filter); |
| 135 | + toolbar.setNavigationIcon(R.drawable.ic_arrow_back); |
| 136 | + toolbar.inflateMenu(R.menu.menu_search_filter_dialog_fragment); |
| 137 | + toolbar.setNavigationOnClickListener(v -> dismiss()); |
| 138 | + toolbar.setNavigationContentDescription(R.string.cancel); |
| 139 | + |
| 140 | + final View okButton = toolbar.findViewById(R.id.search); |
| 141 | + okButton.setEnabled(true); |
| 142 | + |
| 143 | + final View resetButton = toolbar.findViewById(R.id.reset); |
| 144 | + resetButton.setEnabled(true); |
| 145 | + |
| 146 | + toolbar.setOnMenuItemClickListener(item -> { |
| 147 | + if (item.getItemId() == R.id.search) { |
| 148 | + dialogGenerator.prepareForSearch(); |
| 149 | + return true; |
| 150 | + } else if (item.getItemId() == R.id.reset) { |
| 151 | + dialogGenerator.reset(); |
| 152 | + return true; |
| 153 | + } |
| 154 | + return false; |
| 155 | + }); |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + public void onSaveInstanceState(@NonNull final Bundle outState) { |
| 160 | + super.onSaveInstanceState(outState); |
| 161 | + // get data to save its state via Icepick |
| 162 | + userSelectedContentFilterList = dialogGenerator.getSelectedContentFilters(); |
| 163 | + userSelectedSortFilterList = dialogGenerator.getSelectedSortFilters(); |
| 164 | + |
| 165 | + Icepick.saveInstanceState(this, outState); |
| 166 | + } |
| 167 | + |
| 168 | + private void sendDataToParentFragment() { |
| 169 | + final Listener listener = (Listener) getTargetFragment(); |
| 170 | + if (listener != null) { |
| 171 | + listener.onFinishSearchFilterDialog( |
| 172 | + userSelectedContentFilterList, userSelectedSortFilterList, |
| 173 | + selectedContentFilters, selectedSortFilters); |
| 174 | + } |
| 175 | + dismiss(); |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * Listener to be implemented by the parent Fragment so it can receive data. |
| 180 | + */ |
| 181 | + public interface Listener { |
| 182 | + |
| 183 | + void onFinishSearchFilterDialog(ArrayList<Integer> userSelectedContentFilterList, |
| 184 | + ArrayList<Integer> userSelectedSortFilterList, |
| 185 | + List<FilterItem> selectedContentFilters, |
| 186 | + List<FilterItem> selectedSortFilters); |
| 187 | + } |
| 188 | +} |
0 commit comments