Skip to content

Commit e2f449f

Browse files
committed
Code improvements
* Renamed methods so that they are more understandable * Removed ``SearchIndexItem``
1 parent b16e972 commit e2f449f

4 files changed

Lines changed: 24 additions & 115 deletions

File tree

app/src/main/java/org/schabi/newpipe/settings/SettingsActivity.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ public boolean onOptionsItemSelected(final MenuItem item) {
171171
@Override
172172
public boolean onPreferenceStartFragment(final PreferenceFragmentCompat caller,
173173
final Preference preference) {
174+
preference.getExtras()
174175
showSettingsFragment(instantiateFragment(preference.getFragment()));
175176
return true;
176177
}
@@ -221,20 +222,24 @@ private void initSearch(
221222
searchContainer.findViewById(R.id.toolbar_search_clear)
222223
.setOnClickListener(ev -> resetSearchText());
223224

224-
prepareSearchConfig();
225+
ensureSearchRepresentsApplicationState();
225226

226227
// Build search configuration using SettingsResourceRegistry
227228
final PreferenceSearchConfiguration config = new PreferenceSearchConfiguration();
228-
SettingsResourceRegistry.getInstance().getAllEntries().stream()
229-
.filter(SettingsResourceRegistry.SettingRegistryEntry::isSearchable)
230-
.map(SettingsResourceRegistry.SettingRegistryEntry::getPreferencesResId)
231-
.forEach(config::index);
229+
232230

233231
// Build search items
234232
final PreferenceParser parser = new PreferenceParser(getApplicationContext(), config);
235233
final PreferenceSearcher searcher = new PreferenceSearcher(config);
236-
config.getFiles().stream()
234+
235+
// Find all searchable SettingsResourceRegistry fragments
236+
SettingsResourceRegistry.getInstance().getAllEntries().stream()
237+
.filter(SettingsResourceRegistry.SettingRegistryEntry::isSearchable)
238+
// Get the resId
239+
.map(SettingsResourceRegistry.SettingRegistryEntry::getPreferencesResId)
240+
// Parse
237241
.map(parser::parse)
242+
// Add it to the searcher
238243
.forEach(searcher::add);
239244

240245
if (restored) {
@@ -252,7 +257,13 @@ private void initSearch(
252257
searchFragment.setSearcher(searcher);
253258
}
254259

255-
private void prepareSearchConfig() {
260+
/**
261+
* Ensures that the search shows the correct/available search results.
262+
* <br/>
263+
* Some features are e.g. only available for debug builds, these should not
264+
* be found when searching inside a release.
265+
*/
266+
private void ensureSearchRepresentsApplicationState() {
256267
// Check if the update settings are available
257268
if (!CheckForNewAppVersion.isReleaseApk(App.getApp())) {
258269
SettingsResourceRegistry.getInstance()

app/src/main/java/org/schabi/newpipe/settings/preferencesearch/PreferenceParser.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,27 +39,22 @@ public PreferenceParser(
3939
}
4040

4141
public List<PreferenceSearchItem> parse(
42-
final PreferenceSearchConfiguration.SearchIndexItem item
42+
@XmlRes final int resId
4343
) {
44-
Objects.requireNonNull(item, "item can't be null");
45-
4644
final List<PreferenceSearchItem> results = new ArrayList<>();
47-
final XmlPullParser xpp = context.getResources().getXml(item.getResId());
45+
final XmlPullParser xpp = context.getResources().getXml(resId);
4846

4947
try {
5048
xpp.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
5149
xpp.setFeature(XmlPullParser.FEATURE_REPORT_NAMESPACE_ATTRIBUTES, true);
5250

5351
final List<String> breadcrumbs = new ArrayList<>();
54-
if (!TextUtils.isEmpty(item.getBreadcrumb())) {
55-
breadcrumbs.add(item.getBreadcrumb());
56-
}
5752
while (xpp.getEventType() != XmlPullParser.END_DOCUMENT) {
5853
if (xpp.getEventType() == XmlPullParser.START_TAG) {
5954
final PreferenceSearchItem result = parseSearchResult(
6055
xpp,
6156
joinBreadcrumbs(breadcrumbs),
62-
item.getResId()
57+
resId
6358
);
6459

6560
if (!searchConfiguration.getParserIgnoreElements().contains(xpp.getName())
@@ -79,7 +74,7 @@ public List<PreferenceSearchItem> parse(
7974
xpp.next();
8075
}
8176
} catch (final Exception e) {
82-
Log.w(TAG, "Failed to parse resid=" + item.getResId(), e);
77+
Log.w(TAG, "Failed to parse resid=" + resId, e);
8378
}
8479
return results;
8580
}

app/src/main/java/org/schabi/newpipe/settings/preferencesearch/PreferenceSearchConfiguration.java

Lines changed: 1 addition & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
11
package org.schabi.newpipe.settings.preferencesearch;
22

3-
import android.os.Parcel;
4-
import android.os.Parcelable;
53
import android.text.TextUtils;
64

7-
import androidx.annotation.XmlRes;
85
import androidx.preference.PreferenceCategory;
96
import androidx.preference.PreferenceScreen;
107

11-
import java.util.ArrayList;
128
import java.util.Arrays;
139
import java.util.List;
1410
import java.util.Objects;
1511
import java.util.function.BinaryOperator;
1612
import java.util.stream.Stream;
1713

1814
public class PreferenceSearchConfiguration {
19-
private final ArrayList<SearchIndexItem> itemsToIndex = new ArrayList<>();
20-
2115
private BinaryOperator<String> breadcrumbConcat =
2216
(s1, s2) -> TextUtils.isEmpty(s1) ? s2 : (s1 + " > " + s2);
2317

@@ -38,27 +32,11 @@ public void setSearcher(final PreferenceSearchFunction searcher) {
3832
this.searcher = Objects.requireNonNull(searcher);
3933
}
4034

41-
/**
42-
* Adds a new file to the index.
43-
*
44-
* @param resId The preference file to index
45-
* @return SearchIndexItem
46-
*/
47-
public SearchIndexItem index(@XmlRes final int resId) {
48-
final SearchIndexItem item = new SearchIndexItem(resId, this);
49-
itemsToIndex.add(item);
50-
return item;
51-
}
52-
53-
public List<SearchIndexItem> getFiles() {
54-
return itemsToIndex;
55-
}
56-
5735
public BinaryOperator<String> getBreadcrumbConcat() {
5836
return breadcrumbConcat;
5937
}
6038

61-
public PreferenceSearchFunction getSearchMatcher() {
39+
public PreferenceSearchFunction getSearcher() {
6240
return searcher;
6341
}
6442

@@ -70,81 +48,6 @@ public List<String> getParserContainerElements() {
7048
return parserContainerElements;
7149
}
7250

73-
/**
74-
* Adds a given R.xml resource to the search index.
75-
*/
76-
public static final class SearchIndexItem implements Parcelable {
77-
private String breadcrumb = "";
78-
@XmlRes
79-
private final int resId;
80-
private final PreferenceSearchConfiguration searchConfiguration;
81-
82-
/**
83-
* Includes the given R.xml resource in the index.
84-
*
85-
* @param resId The resource to index
86-
* @param searchConfiguration The configuration for the search
87-
*/
88-
private SearchIndexItem(
89-
@XmlRes final int resId,
90-
final PreferenceSearchConfiguration searchConfiguration
91-
) {
92-
this.resId = resId;
93-
this.searchConfiguration = searchConfiguration;
94-
}
95-
96-
/**
97-
* Adds a breadcrumb.
98-
*
99-
* @param breadcrumb The breadcrumb to add
100-
* @return For chaining
101-
*/
102-
@SuppressWarnings("HiddenField")
103-
public SearchIndexItem withBreadcrumb(final String breadcrumb) {
104-
this.breadcrumb =
105-
searchConfiguration.getBreadcrumbConcat().apply(this.breadcrumb, breadcrumb);
106-
return this;
107-
}
108-
109-
@XmlRes
110-
int getResId() {
111-
return resId;
112-
}
113-
114-
String getBreadcrumb() {
115-
return breadcrumb;
116-
}
117-
118-
public static final Creator<SearchIndexItem> CREATOR = new Creator<>() {
119-
@Override
120-
public SearchIndexItem createFromParcel(final Parcel in) {
121-
return new SearchIndexItem(in);
122-
}
123-
124-
@Override
125-
public SearchIndexItem[] newArray(final int size) {
126-
return new SearchIndexItem[size];
127-
}
128-
};
129-
130-
private SearchIndexItem(final Parcel parcel) {
131-
this.breadcrumb = parcel.readString();
132-
this.resId = parcel.readInt();
133-
this.searchConfiguration = null;
134-
}
135-
136-
@Override
137-
public void writeToParcel(final Parcel dest, final int flags) {
138-
dest.writeString(this.breadcrumb);
139-
dest.writeInt(this.resId);
140-
}
141-
142-
@Override
143-
public int describeContents() {
144-
return 0;
145-
}
146-
}
147-
14851
@FunctionalInterface
14952
public interface PreferenceSearchFunction {
15053
Stream<PreferenceSearchItem> search(

app/src/main/java/org/schabi/newpipe/settings/preferencesearch/PreferenceSearcher.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ List<PreferenceSearchItem> searchFor(final String keyword) {
2424
return new ArrayList<>();
2525
}
2626

27-
return configuration.getSearchMatcher()
27+
return configuration.getSearcher()
2828
.search(allEntries.stream(), keyword)
2929
.collect(Collectors.toList());
3030
}

0 commit comments

Comments
 (0)