From e2f449f0c8782e19ae2a9cf64936931b9e03a578 Mon Sep 17 00:00:00 2001
From: litetex <40789489+litetex@users.noreply.github.com>
Date: Sun, 9 Jan 2022 15:22:05 +0100
Subject: [PATCH] Code improvements
* Renamed methods so that they are more understandable
* Removed ``SearchIndexItem``
---
.../newpipe/settings/SettingsActivity.java | 25 +++--
.../preferencesearch/PreferenceParser.java | 13 +--
.../PreferenceSearchConfiguration.java | 99 +------------------
.../preferencesearch/PreferenceSearcher.java | 2 +-
4 files changed, 24 insertions(+), 115 deletions(-)
diff --git a/app/src/main/java/org/schabi/newpipe/settings/SettingsActivity.java b/app/src/main/java/org/schabi/newpipe/settings/SettingsActivity.java
index 4dfbf6c08..7d56863eb 100644
--- a/app/src/main/java/org/schabi/newpipe/settings/SettingsActivity.java
+++ b/app/src/main/java/org/schabi/newpipe/settings/SettingsActivity.java
@@ -171,6 +171,7 @@ public class SettingsActivity extends AppCompatActivity implements
@Override
public boolean onPreferenceStartFragment(final PreferenceFragmentCompat caller,
final Preference preference) {
+ preference.getExtras()
showSettingsFragment(instantiateFragment(preference.getFragment()));
return true;
}
@@ -221,20 +222,24 @@ public class SettingsActivity extends AppCompatActivity implements
searchContainer.findViewById(R.id.toolbar_search_clear)
.setOnClickListener(ev -> resetSearchText());
- prepareSearchConfig();
+ ensureSearchRepresentsApplicationState();
// Build search configuration using SettingsResourceRegistry
final PreferenceSearchConfiguration config = new PreferenceSearchConfiguration();
- SettingsResourceRegistry.getInstance().getAllEntries().stream()
- .filter(SettingsResourceRegistry.SettingRegistryEntry::isSearchable)
- .map(SettingsResourceRegistry.SettingRegistryEntry::getPreferencesResId)
- .forEach(config::index);
+
// Build search items
final PreferenceParser parser = new PreferenceParser(getApplicationContext(), config);
final PreferenceSearcher searcher = new PreferenceSearcher(config);
- config.getFiles().stream()
+
+ // Find all searchable SettingsResourceRegistry fragments
+ SettingsResourceRegistry.getInstance().getAllEntries().stream()
+ .filter(SettingsResourceRegistry.SettingRegistryEntry::isSearchable)
+ // Get the resId
+ .map(SettingsResourceRegistry.SettingRegistryEntry::getPreferencesResId)
+ // Parse
.map(parser::parse)
+ // Add it to the searcher
.forEach(searcher::add);
if (restored) {
@@ -252,7 +257,13 @@ public class SettingsActivity extends AppCompatActivity implements
searchFragment.setSearcher(searcher);
}
- private void prepareSearchConfig() {
+ /**
+ * Ensures that the search shows the correct/available search results.
+ *
+ * Some features are e.g. only available for debug builds, these should not
+ * be found when searching inside a release.
+ */
+ private void ensureSearchRepresentsApplicationState() {
// Check if the update settings are available
if (!CheckForNewAppVersion.isReleaseApk(App.getApp())) {
SettingsResourceRegistry.getInstance()
diff --git a/app/src/main/java/org/schabi/newpipe/settings/preferencesearch/PreferenceParser.java b/app/src/main/java/org/schabi/newpipe/settings/preferencesearch/PreferenceParser.java
index afd2c1b4f..ccddee97b 100644
--- a/app/src/main/java/org/schabi/newpipe/settings/preferencesearch/PreferenceParser.java
+++ b/app/src/main/java/org/schabi/newpipe/settings/preferencesearch/PreferenceParser.java
@@ -39,27 +39,22 @@ public class PreferenceParser {
}
public List parse(
- final PreferenceSearchConfiguration.SearchIndexItem item
+ @XmlRes final int resId
) {
- Objects.requireNonNull(item, "item can't be null");
-
final List results = new ArrayList<>();
- final XmlPullParser xpp = context.getResources().getXml(item.getResId());
+ final XmlPullParser xpp = context.getResources().getXml(resId);
try {
xpp.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
xpp.setFeature(XmlPullParser.FEATURE_REPORT_NAMESPACE_ATTRIBUTES, true);
final List breadcrumbs = new ArrayList<>();
- if (!TextUtils.isEmpty(item.getBreadcrumb())) {
- breadcrumbs.add(item.getBreadcrumb());
- }
while (xpp.getEventType() != XmlPullParser.END_DOCUMENT) {
if (xpp.getEventType() == XmlPullParser.START_TAG) {
final PreferenceSearchItem result = parseSearchResult(
xpp,
joinBreadcrumbs(breadcrumbs),
- item.getResId()
+ resId
);
if (!searchConfiguration.getParserIgnoreElements().contains(xpp.getName())
@@ -79,7 +74,7 @@ public class PreferenceParser {
xpp.next();
}
} catch (final Exception e) {
- Log.w(TAG, "Failed to parse resid=" + item.getResId(), e);
+ Log.w(TAG, "Failed to parse resid=" + resId, e);
}
return results;
}
diff --git a/app/src/main/java/org/schabi/newpipe/settings/preferencesearch/PreferenceSearchConfiguration.java b/app/src/main/java/org/schabi/newpipe/settings/preferencesearch/PreferenceSearchConfiguration.java
index 1c9dc99aa..3d49140fb 100644
--- a/app/src/main/java/org/schabi/newpipe/settings/preferencesearch/PreferenceSearchConfiguration.java
+++ b/app/src/main/java/org/schabi/newpipe/settings/preferencesearch/PreferenceSearchConfiguration.java
@@ -1,14 +1,10 @@
package org.schabi.newpipe.settings.preferencesearch;
-import android.os.Parcel;
-import android.os.Parcelable;
import android.text.TextUtils;
-import androidx.annotation.XmlRes;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceScreen;
-import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
@@ -16,8 +12,6 @@ import java.util.function.BinaryOperator;
import java.util.stream.Stream;
public class PreferenceSearchConfiguration {
- private final ArrayList itemsToIndex = new ArrayList<>();
-
private BinaryOperator breadcrumbConcat =
(s1, s2) -> TextUtils.isEmpty(s1) ? s2 : (s1 + " > " + s2);
@@ -38,27 +32,11 @@ public class PreferenceSearchConfiguration {
this.searcher = Objects.requireNonNull(searcher);
}
- /**
- * Adds a new file to the index.
- *
- * @param resId The preference file to index
- * @return SearchIndexItem
- */
- public SearchIndexItem index(@XmlRes final int resId) {
- final SearchIndexItem item = new SearchIndexItem(resId, this);
- itemsToIndex.add(item);
- return item;
- }
-
- public List getFiles() {
- return itemsToIndex;
- }
-
public BinaryOperator getBreadcrumbConcat() {
return breadcrumbConcat;
}
- public PreferenceSearchFunction getSearchMatcher() {
+ public PreferenceSearchFunction getSearcher() {
return searcher;
}
@@ -70,81 +48,6 @@ public class PreferenceSearchConfiguration {
return parserContainerElements;
}
- /**
- * Adds a given R.xml resource to the search index.
- */
- public static final class SearchIndexItem implements Parcelable {
- private String breadcrumb = "";
- @XmlRes
- private final int resId;
- private final PreferenceSearchConfiguration searchConfiguration;
-
- /**
- * Includes the given R.xml resource in the index.
- *
- * @param resId The resource to index
- * @param searchConfiguration The configuration for the search
- */
- private SearchIndexItem(
- @XmlRes final int resId,
- final PreferenceSearchConfiguration searchConfiguration
- ) {
- this.resId = resId;
- this.searchConfiguration = searchConfiguration;
- }
-
- /**
- * Adds a breadcrumb.
- *
- * @param breadcrumb The breadcrumb to add
- * @return For chaining
- */
- @SuppressWarnings("HiddenField")
- public SearchIndexItem withBreadcrumb(final String breadcrumb) {
- this.breadcrumb =
- searchConfiguration.getBreadcrumbConcat().apply(this.breadcrumb, breadcrumb);
- return this;
- }
-
- @XmlRes
- int getResId() {
- return resId;
- }
-
- String getBreadcrumb() {
- return breadcrumb;
- }
-
- public static final Creator CREATOR = new Creator<>() {
- @Override
- public SearchIndexItem createFromParcel(final Parcel in) {
- return new SearchIndexItem(in);
- }
-
- @Override
- public SearchIndexItem[] newArray(final int size) {
- return new SearchIndexItem[size];
- }
- };
-
- private SearchIndexItem(final Parcel parcel) {
- this.breadcrumb = parcel.readString();
- this.resId = parcel.readInt();
- this.searchConfiguration = null;
- }
-
- @Override
- public void writeToParcel(final Parcel dest, final int flags) {
- dest.writeString(this.breadcrumb);
- dest.writeInt(this.resId);
- }
-
- @Override
- public int describeContents() {
- return 0;
- }
- }
-
@FunctionalInterface
public interface PreferenceSearchFunction {
Stream search(
diff --git a/app/src/main/java/org/schabi/newpipe/settings/preferencesearch/PreferenceSearcher.java b/app/src/main/java/org/schabi/newpipe/settings/preferencesearch/PreferenceSearcher.java
index 87dd11693..176dc5d14 100644
--- a/app/src/main/java/org/schabi/newpipe/settings/preferencesearch/PreferenceSearcher.java
+++ b/app/src/main/java/org/schabi/newpipe/settings/preferencesearch/PreferenceSearcher.java
@@ -24,7 +24,7 @@ public class PreferenceSearcher {
return new ArrayList<>();
}
- return configuration.getSearchMatcher()
+ return configuration.getSearcher()
.search(allEntries.stream(), keyword)
.collect(Collectors.toList());
}