|
35 | 35 | import androidx.core.text.HtmlCompat; |
36 | 36 | import androidx.fragment.app.DialogFragment; |
37 | 37 | import androidx.fragment.app.FragmentManager; |
| 38 | +import androidx.lifecycle.ViewModelProvider; |
38 | 39 | import androidx.preference.PreferenceManager; |
39 | 40 | import androidx.recyclerview.widget.ItemTouchHelper; |
40 | 41 | import androidx.recyclerview.widget.RecyclerView; |
|
43 | 44 | import org.schabi.newpipe.R; |
44 | 45 | import org.schabi.newpipe.databinding.FragmentSearchBinding; |
45 | 46 | import org.schabi.newpipe.error.ErrorInfo; |
46 | | -import org.schabi.newpipe.error.ErrorUtil; |
47 | 47 | import org.schabi.newpipe.error.ReCaptchaActivity; |
48 | 48 | import org.schabi.newpipe.error.UserAction; |
49 | 49 | import org.schabi.newpipe.extractor.InfoItem; |
|
52 | 52 | import org.schabi.newpipe.extractor.NewPipe; |
53 | 53 | import org.schabi.newpipe.extractor.Page; |
54 | 54 | import org.schabi.newpipe.extractor.StreamingService; |
55 | | -import org.schabi.newpipe.extractor.exceptions.ExtractionException; |
56 | 55 | import org.schabi.newpipe.extractor.search.SearchExtractor; |
57 | 56 | import org.schabi.newpipe.extractor.search.SearchInfo; |
58 | 57 | import org.schabi.newpipe.extractor.search.filter.FilterItem; |
59 | 58 | import org.schabi.newpipe.fragments.BackPressable; |
60 | 59 | import org.schabi.newpipe.fragments.list.BaseListFragment; |
61 | | -import org.schabi.newpipe.fragments.list.search.filter.BaseSearchFilterDialogFragment; |
62 | 60 | import org.schabi.newpipe.fragments.list.search.filter.SearchFilterDialogFragment; |
63 | 61 | import org.schabi.newpipe.fragments.list.search.filter.SearchFilterLogic; |
64 | 62 | import org.schabi.newpipe.fragments.list.search.filter.SearchFilterOptionMenuAlikeDialogFragment; |
|
90 | 88 | import io.reactivex.rxjava3.subjects.PublishSubject; |
91 | 89 |
|
92 | 90 | public class SearchFragment extends BaseListFragment<SearchInfo, ListExtractor.InfoItemsPage<?>> |
93 | | - implements BackPressable, SearchFilterLogic.Callback, |
94 | | - BaseSearchFilterDialogFragment.Listener { |
| 91 | + implements BackPressable { |
95 | 92 | /*////////////////////////////////////////////////////////////////////////// |
96 | 93 | // Search |
97 | 94 | //////////////////////////////////////////////////////////////////////////*/ |
@@ -169,6 +166,11 @@ public class SearchFragment extends BaseListFragment<SearchInfo, ListExtractor.I |
169 | 166 | @State |
170 | 167 | ArrayList<Integer> userSelectedSortFilterList = null; |
171 | 168 |
|
| 169 | + protected SearchViewModel searchViewModel; |
| 170 | + protected SearchFilterLogic.Factory.Variant logicVariant = |
| 171 | + SearchFilterLogic.Factory.Variant.SEARCH_FILTER_LOGIC_DEFAULT; |
| 172 | + |
| 173 | + |
172 | 174 | public static SearchFragment getInstance(final int serviceId, final String searchString) { |
173 | 175 | final SearchFragment searchFragment; |
174 | 176 | final App app = App.getApp(); |
@@ -227,30 +229,40 @@ public View onCreateView(@NonNull final LayoutInflater inflater, |
227 | 229 | if (userSelectedSortFilterList == null) { |
228 | 230 | userSelectedSortFilterList = new ArrayList<>(); |
229 | 231 | } |
230 | | - initializeFilterData(); |
| 232 | + |
| 233 | + initViewModel(); |
| 234 | + |
| 235 | + // observe the content/sort filter items lists |
| 236 | + searchViewModel.getSelectedContentFilterItemListLiveData().observe( |
| 237 | + getViewLifecycleOwner(), filterItems -> selectedContentFilter = filterItems); |
| 238 | + searchViewModel.getSelectedSortFilterItemListLiveData().observe( |
| 239 | + getViewLifecycleOwner(), filterItems -> selectedSortFilter = filterItems); |
| 240 | + |
| 241 | + // the content/sort filters ids lists are only |
| 242 | + // observed here to store them via Icepick |
| 243 | + searchViewModel.getUserSelectedContentFilterListLiveData().observe( |
| 244 | + getViewLifecycleOwner(), filterIds -> userSelectedContentFilterList = filterIds); |
| 245 | + searchViewModel.getUserSelectedSortFilterListLiveData().observe( |
| 246 | + getViewLifecycleOwner(), filterIds -> userSelectedSortFilterList = filterIds); |
| 247 | + |
| 248 | + searchViewModel.getDoSearchLiveData().observe( |
| 249 | + getViewLifecycleOwner(), doSearch -> { |
| 250 | + if (doSearch) { |
| 251 | + selectedFilters(selectedContentFilter, selectedSortFilter); |
| 252 | + searchViewModel.weConsumedDoSearchLiveData(); |
| 253 | + } |
| 254 | + }); |
| 255 | + |
231 | 256 | return inflater.inflate(R.layout.fragment_search, container, false); |
232 | 257 | } |
233 | 258 |
|
234 | | - /** |
235 | | - * initialize the initial content and sort filter Lists with defaults |
236 | | - * or previously with Icepick stored data. |
237 | | - */ |
238 | | - protected void initializeFilterData() { |
239 | | - try { |
240 | | - final SearchFilterLogic logic = |
241 | | - new SearchFilterLogic(NewPipe.getService(serviceId).getSearchQHFactory(), null); |
242 | | - logic.restorePreviouslySelectedFilters( |
243 | | - userSelectedContentFilterList, |
244 | | - userSelectedSortFilterList); |
245 | | - |
246 | | - userSelectedContentFilterList = logic.getSelectedContentFilters(); |
247 | | - userSelectedSortFilterList = logic.getSelectedSortFilters(); |
248 | | - selectedContentFilter = logic.getSelectedContentFilterItems(); |
249 | | - selectedSortFilter = logic.getSelectedSortFiltersItems(); |
250 | | - } catch (final ExtractionException e) { |
251 | | - ErrorUtil.showUiErrorSnackbar(this, |
252 | | - "No filtering possible. Getting service for id " + serviceId, e); |
253 | | - } |
| 259 | + protected void initViewModel() { |
| 260 | + searchViewModel = new ViewModelProvider(this, SearchViewModel.Companion |
| 261 | + .getFactory(serviceId, |
| 262 | + logicVariant, |
| 263 | + userSelectedContentFilterList, |
| 264 | + userSelectedSortFilterList)) |
| 265 | + .get(SearchViewModel.class); |
254 | 266 | } |
255 | 267 |
|
256 | 268 | @Override |
@@ -905,7 +917,6 @@ private void onItemError(final Throwable exception) { |
905 | 917 | // Utils |
906 | 918 | //////////////////////////////////////////////////////////////////////////*/ |
907 | 919 |
|
908 | | - @Override |
909 | 920 | public void selectedFilters(@NonNull final List<FilterItem> theSelectedContentFilter, |
910 | 921 | @NonNull final List<FilterItem> theSelectedSortFilter) { |
911 | 922 |
|
@@ -1072,32 +1083,18 @@ public void onSuggestionItemSwiped(@NonNull final RecyclerView.ViewHolder viewHo |
1072 | 1083 | } |
1073 | 1084 |
|
1074 | 1085 | private void showSelectFiltersDialog() { |
1075 | | - final FragmentManager fragmentManager = getParentFragmentManager(); |
| 1086 | + final FragmentManager fragmentManager = getChildFragmentManager(); |
1076 | 1087 | final DialogFragment searchFilterUiDialog; |
1077 | 1088 |
|
1078 | 1089 | final String searchUi = PreferenceManager.getDefaultSharedPreferences(App.getApp()) |
1079 | 1090 | .getString(getString(R.string.search_filter_ui_key), |
1080 | 1091 | getString(R.string.search_filter_ui_value)); |
1081 | | - if (getString(R.string.search_filter_ui_dialog_key).equals(searchUi)) { |
1082 | | - searchFilterUiDialog = |
1083 | | - SearchFilterDialogFragment.newInstance( |
1084 | | - serviceId, userSelectedContentFilterList, userSelectedSortFilterList); |
1085 | | - } else { |
1086 | | - searchFilterUiDialog = |
1087 | | - SearchFilterOptionMenuAlikeDialogFragment.newInstance( |
1088 | | - serviceId, userSelectedContentFilterList, userSelectedSortFilterList); |
| 1092 | + if (getString(R.string.search_filter_ui_option_menu_style_key).equals(searchUi)) { |
| 1093 | + searchFilterUiDialog = new SearchFilterOptionMenuAlikeDialogFragment(); |
| 1094 | + } else { // default dialog |
| 1095 | + searchFilterUiDialog = new SearchFilterDialogFragment(); |
1089 | 1096 | } |
1090 | 1097 |
|
1091 | | - searchFilterUiDialog.setTargetFragment(SearchFragment.this, 300); |
1092 | 1098 | searchFilterUiDialog.show(fragmentManager, "fragment_search"); |
1093 | 1099 | } |
1094 | | - |
1095 | | - @SuppressWarnings("checkstyle:HiddenField") |
1096 | | - @Override |
1097 | | - public void onFinishSearchFilterDialog(final List<Integer> userSelectedContentFilterList, |
1098 | | - final List<Integer> userSelectedSortFilterList, |
1099 | | - final List<FilterItem> selectedContentFilters, |
1100 | | - final List<FilterItem> selectedSortFilters) { |
1101 | | - selectedFilters(selectedContentFilters, selectedSortFilters); |
1102 | | - } |
1103 | 1100 | } |
0 commit comments