From 0f45c69388a2da0a465dff19813ec0e09face67f Mon Sep 17 00:00:00 2001
From: litetex <40789489+litetex@users.noreply.github.com>
Date: Sun, 26 Dec 2021 23:46:22 +0100
Subject: [PATCH] Code cleanup + indexing improvements
* Removed unused method
* Only index all settings once -> Saves performance
* Fixed some SonarLint reported problems
---
.../newpipe/settings/SettingsActivity.java | 23 +++++++++-----
.../PreferenceFuzzySearchFunction.java | 12 +-------
.../preferencesearch/PreferenceParser.java | 4 +--
.../PreferenceSearchConfiguration.java | 2 +-
.../PreferenceSearchFragment.java | 30 ++++---------------
.../preferencesearch/PreferenceSearcher.java | 9 +++---
6 files changed, 29 insertions(+), 51 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 1f917e771..92bc21c9d 100644
--- a/app/src/main/java/org/schabi/newpipe/settings/SettingsActivity.java
+++ b/app/src/main/java/org/schabi/newpipe/settings/SettingsActivity.java
@@ -26,11 +26,13 @@ import org.schabi.newpipe.CheckForNewAppVersion;
import org.schabi.newpipe.MainActivity;
import org.schabi.newpipe.R;
import org.schabi.newpipe.databinding.SettingsLayoutBinding;
+import org.schabi.newpipe.settings.preferencesearch.PreferenceParser;
import org.schabi.newpipe.settings.preferencesearch.PreferenceSearchConfiguration;
import org.schabi.newpipe.settings.preferencesearch.PreferenceSearchFragment;
import org.schabi.newpipe.settings.preferencesearch.PreferenceSearchItem;
import org.schabi.newpipe.settings.preferencesearch.PreferenceSearchResultHighlighter;
import org.schabi.newpipe.settings.preferencesearch.PreferenceSearchResultListener;
+import org.schabi.newpipe.settings.preferencesearch.PreferenceSearcher;
import org.schabi.newpipe.util.DeviceUtils;
import org.schabi.newpipe.util.KeyboardUtil;
import org.schabi.newpipe.util.ThemeHelper;
@@ -58,9 +60,8 @@ import java.util.concurrent.TimeUnit;
* along with NewPipe. If not, see .
*/
-public class SettingsActivity extends AppCompatActivity
- implements
- BasePreferenceFragment.OnPreferenceStartFragmentCallback,
+public class SettingsActivity extends AppCompatActivity implements
+ PreferenceFragmentCompat.OnPreferenceStartFragmentCallback,
PreferenceSearchResultListener {
private static final String TAG = "SettingsActivity";
private static final boolean DEBUG = MainActivity.DEBUG;
@@ -165,6 +166,7 @@ public class SettingsActivity extends AppCompatActivity
@Override
protected void onDestroy() {
setMenuSearchItem(null);
+ searchFragment = null;
super.onDestroy();
}
@@ -183,26 +185,33 @@ public class SettingsActivity extends AppCompatActivity
RxTextView.textChanges(searchEditText)
// Wait some time after the last input before actually searching
.debounce(200, TimeUnit.MILLISECONDS)
- .subscribe(v -> runOnUiThread(() -> onSearchChanged()));
+ .subscribe(v -> runOnUiThread(this::onSearchChanged));
// Configure clear button
searchContainer.findViewById(R.id.toolbar_search_clear)
.setOnClickListener(ev -> resetSearchText());
- // Build search configuration using SettingsResourceRegistry
prepareSearchConfig();
+ // 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);
- searchFragment = new PreferenceSearchFragment(config);
+ // Build search items
+ final PreferenceParser parser = new PreferenceParser(getApplicationContext(), config);
+ final PreferenceSearcher searcher = new PreferenceSearcher(config);
+ config.getFiles().stream()
+ .map(parser::parse)
+ .forEach(searcher::add);
+
+ searchFragment = new PreferenceSearchFragment(searcher);
}
private void prepareSearchConfig() {
- // Check if the update settings should be available
+ // Check if the update settings are available
if (!CheckForNewAppVersion.isReleaseApk(App.getApp())) {
SettingsResourceRegistry.getInstance()
.getEntryByPreferencesResId(R.xml.update_settings)
diff --git a/app/src/main/java/org/schabi/newpipe/settings/preferencesearch/PreferenceFuzzySearchFunction.java b/app/src/main/java/org/schabi/newpipe/settings/preferencesearch/PreferenceFuzzySearchFunction.java
index 48f507a41..88ef790c8 100644
--- a/app/src/main/java/org/schabi/newpipe/settings/preferencesearch/PreferenceFuzzySearchFunction.java
+++ b/app/src/main/java/org/schabi/newpipe/settings/preferencesearch/PreferenceFuzzySearchFunction.java
@@ -2,8 +2,6 @@ package org.schabi.newpipe.settings.preferencesearch;
import android.text.TextUtils;
-import androidx.annotation.NonNull;
-
import org.schabi.newpipe.settings.preferencesearch.similarity.FuzzyScore;
import java.util.Comparator;
@@ -22,7 +20,7 @@ public class PreferenceFuzzySearchFunction
final Stream allAvailable,
final String keyword
) {
- final float maxScore = (keyword.length() + 1) * 3 - 2; // First can't get +2 bonus score
+ final int maxScore = (keyword.length() + 1) * 3 - 2; // First can't get +2 bonus score
return allAvailable
// General search
@@ -39,14 +37,6 @@ public class PreferenceFuzzySearchFunction
.limit(20);
}
- private float computeFuzzyScore(
- @NonNull final PreferenceSearchItem item,
- @NonNull final Function resolver,
- @NonNull final String keyword
- ) {
- return FUZZY_SCORE.fuzzyScore(resolver.apply(item), keyword);
- }
-
static class FuzzySearchGeneralDTO {
private final PreferenceSearchItem item;
private final float score;
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 1cf401892..b52daeb5b 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
@@ -18,7 +18,7 @@ import java.util.Objects;
/**
* Parses the corresponding preference-file(s).
*/
-class PreferenceParser {
+public class PreferenceParser {
private static final String TAG = "PreferenceParser";
private static final String NS_ANDROID = "http://schemas.android.com/apk/res/android";
@@ -28,7 +28,7 @@ class PreferenceParser {
private final Map allPreferences;
private final PreferenceSearchConfiguration searchConfiguration;
- PreferenceParser(
+ public PreferenceParser(
final Context context,
final PreferenceSearchConfiguration searchConfiguration
) {
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 37b98035a..1c9dc99aa 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
@@ -50,7 +50,7 @@ public class PreferenceSearchConfiguration {
return item;
}
- List getFiles() {
+ public List getFiles() {
return itemsToIndex;
}
diff --git a/app/src/main/java/org/schabi/newpipe/settings/preferencesearch/PreferenceSearchFragment.java b/app/src/main/java/org/schabi/newpipe/settings/preferencesearch/PreferenceSearchFragment.java
index d9a17da16..1b1d627c1 100644
--- a/app/src/main/java/org/schabi/newpipe/settings/preferencesearch/PreferenceSearchFragment.java
+++ b/app/src/main/java/org/schabi/newpipe/settings/preferencesearch/PreferenceSearchFragment.java
@@ -24,29 +24,15 @@ import java.util.Objects;
public class PreferenceSearchFragment extends Fragment {
public static final String NAME = PreferenceSearchFragment.class.getSimpleName();
- private final PreferenceSearchConfiguration searchConfiguration;
-
private final PreferenceSearcher searcher;
+
private SearchViewHolder viewHolder;
private PreferenceSearchAdapter adapter;
- public PreferenceSearchFragment(final PreferenceSearchConfiguration searchConfiguration) {
- this.searchConfiguration = searchConfiguration;
- this.searcher = new PreferenceSearcher(searchConfiguration);
- }
-
- @Override
- public void onCreate(@Nullable final Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
-
- final PreferenceParser parser =
- new PreferenceParser(
- getContext(),
- searchConfiguration);
-
- searchConfiguration.getFiles().stream()
- .map(parser::parse)
- .forEach(searcher::add);
+ public PreferenceSearchFragment(
+ final PreferenceSearcher searcher
+ ) {
+ this.searcher = searcher;
}
@Nullable
@@ -98,12 +84,6 @@ public class PreferenceSearchFragment extends Fragment {
((PreferenceSearchResultListener) getActivity()).onSearchResultClicked(item);
}
- @Override
- public void onDestroy() {
- searcher.close();
- super.onDestroy();
- }
-
private static class SearchViewHolder {
private final RecyclerView recyclerView;
private final View emptyStateView;
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 f9427a1ca..87dd11693 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
@@ -6,16 +6,16 @@ import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
-class PreferenceSearcher implements AutoCloseable {
+public class PreferenceSearcher {
private final List allEntries = new ArrayList<>();
private final PreferenceSearchConfiguration configuration;
- PreferenceSearcher(final PreferenceSearchConfiguration configuration) {
+ public PreferenceSearcher(final PreferenceSearchConfiguration configuration) {
this.configuration = configuration;
}
- void add(final List items) {
+ public void add(final List items) {
allEntries.addAll(items);
}
@@ -29,8 +29,7 @@ class PreferenceSearcher implements AutoCloseable {
.collect(Collectors.toList());
}
- @Override
- public void close() {
+ public void clear() {
allEntries.clear();
}
}