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