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