Skip to content

Commit 5b0f4eb

Browse files
committed
searchfilters: dynamically generate UIs and evaluate selected sort and content filters
SearchFilterLogic.java: ======================= This class handles all the user interaction with the content and sort filters of NewPipeExtractor. The class works standalone to just get the default selected filters eg. during init phase. See in SearchFragment#initializeFilterData() BaseSearchFilterUiGenerator.java: ================================= It extends SearchFilterLogic and is used as a base class to implement the UI interface for content and sort filter dialogs eg. SearchFilterDialogGenerator or SearchFilterOptionMenuAlikeDialogGenerator.
1 parent aad5e26 commit 5b0f4eb

2 files changed

Lines changed: 849 additions & 0 deletions

File tree

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
6+
import android.content.Context;
7+
import android.util.TypedValue;
8+
9+
import org.schabi.newpipe.R;
10+
import org.schabi.newpipe.extractor.linkhandler.SearchQueryHandlerFactory;
11+
12+
/**
13+
* Base for any search filter UI.
14+
* <p>
15+
* It extends SearchFilterLogic and is used as a base class to implement the UI interface
16+
* for content and sort filter dialogs eg. {@link SearchFilterDialogGenerator}
17+
* or {@link SearchFilterOptionMenuAlikeDialogGenerator}.
18+
*/
19+
public abstract class BaseSearchFilterUiGenerator extends SearchFilterLogic {
20+
protected final ICreateUiForFiltersWorker contentFilterWorker;
21+
protected final ICreateUiForFiltersWorker sortFilterWorker;
22+
protected final Context context;
23+
24+
protected BaseSearchFilterUiGenerator(final SearchQueryHandlerFactory linkHandlerFactory,
25+
final Callback callback,
26+
final Context context) {
27+
super(linkHandlerFactory, callback);
28+
this.context = context;
29+
this.contentFilterWorker = createContentFilterWorker();
30+
this.sortFilterWorker = createSortFilterWorker();
31+
}
32+
33+
/**
34+
* {@link ICreateUiForFiltersWorker}.
35+
*
36+
* @return the class that implements the UI for the content filters.
37+
*/
38+
protected abstract ICreateUiForFiltersWorker createContentFilterWorker();
39+
40+
/**
41+
* {@link ICreateUiForFiltersWorker}.
42+
*
43+
* @return the class that implements the UI for the sort filters.
44+
*/
45+
protected abstract ICreateUiForFiltersWorker createSortFilterWorker();
46+
47+
protected int getSeparatorLineColorFromTheme() {
48+
final TypedValue value = new TypedValue();
49+
context.getTheme().resolveAttribute(R.attr.colorAccent, value, true);
50+
return value.data;
51+
}
52+
53+
/**
54+
* Create the complete UI for the search filter dialog and make sure the initial
55+
* visibility of the UI elements is done.
56+
*/
57+
public void createSearchUI() {
58+
initContentFiltersUi(contentFilterWorker);
59+
initSortFiltersUi(sortFilterWorker);
60+
doMeasurementsIfNeeded();
61+
// make sure that only sort filters relevant to the selected content filter are shown
62+
showSortFilterContainerUI();
63+
}
64+
65+
protected void doMeasurementsIfNeeded() {
66+
// nothing to measure here, if you want to measure something override this method
67+
}
68+
69+
/**
70+
* If UI is implemented within an fragment/activity this method has to be called from
71+
* its corresponding lifecyle method manually.
72+
*/
73+
public abstract void onResume();
74+
75+
/**
76+
* If UI is implemented within an fragment/activity this method has to be called from
77+
* its corresponding lifecyle method manually.
78+
*/
79+
public abstract void onPause();
80+
81+
/**
82+
* Helper interface used as 'function pointer'.
83+
*/
84+
protected interface UiWrapperMapDelegate {
85+
void put(int identifier, IUiItemWrapper menuItemUiWrapper);
86+
}
87+
88+
/**
89+
* Helper interface used as 'function pointer'.
90+
*/
91+
protected interface UiSelectorDelegate {
92+
void selectFilter(int identifier);
93+
}
94+
}

0 commit comments

Comments
 (0)