mirror of
https://github.com/TeamNewPipe/NewPipe
synced 2024-12-23 00:20:32 +00:00
Code improvements
* Renamed methods so that they are more understandable * Removed ``SearchIndexItem``
This commit is contained in:
parent
b16e972710
commit
e2f449f0c8
@ -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.
|
||||
* <br/>
|
||||
* 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()
|
||||
|
@ -39,27 +39,22 @@ public class PreferenceParser {
|
||||
}
|
||||
|
||||
public List<PreferenceSearchItem> parse(
|
||||
final PreferenceSearchConfiguration.SearchIndexItem item
|
||||
@XmlRes final int resId
|
||||
) {
|
||||
Objects.requireNonNull(item, "item can't be null");
|
||||
|
||||
final List<PreferenceSearchItem> 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<String> 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;
|
||||
}
|
||||
|
@ -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<SearchIndexItem> itemsToIndex = new ArrayList<>();
|
||||
|
||||
private BinaryOperator<String> 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<SearchIndexItem> getFiles() {
|
||||
return itemsToIndex;
|
||||
}
|
||||
|
||||
public BinaryOperator<String> 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<SearchIndexItem> 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<PreferenceSearchItem> search(
|
||||
|
@ -24,7 +24,7 @@ public class PreferenceSearcher {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
return configuration.getSearchMatcher()
|
||||
return configuration.getSearcher()
|
||||
.search(allEntries.stream(), keyword)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user