Code cleanup + indexing improvements

* Removed unused method
* Only index all settings once -> Saves performance
* Fixed some SonarLint reported problems
This commit is contained in:
litetex 2021-12-26 23:46:22 +01:00
parent 52542e04e8
commit 0f45c69388
6 changed files with 29 additions and 51 deletions

View File

@ -26,11 +26,13 @@ import org.schabi.newpipe.CheckForNewAppVersion;
import org.schabi.newpipe.MainActivity; import org.schabi.newpipe.MainActivity;
import org.schabi.newpipe.R; import org.schabi.newpipe.R;
import org.schabi.newpipe.databinding.SettingsLayoutBinding; 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.PreferenceSearchConfiguration;
import org.schabi.newpipe.settings.preferencesearch.PreferenceSearchFragment; import org.schabi.newpipe.settings.preferencesearch.PreferenceSearchFragment;
import org.schabi.newpipe.settings.preferencesearch.PreferenceSearchItem; import org.schabi.newpipe.settings.preferencesearch.PreferenceSearchItem;
import org.schabi.newpipe.settings.preferencesearch.PreferenceSearchResultHighlighter; import org.schabi.newpipe.settings.preferencesearch.PreferenceSearchResultHighlighter;
import org.schabi.newpipe.settings.preferencesearch.PreferenceSearchResultListener; 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.DeviceUtils;
import org.schabi.newpipe.util.KeyboardUtil; import org.schabi.newpipe.util.KeyboardUtil;
import org.schabi.newpipe.util.ThemeHelper; import org.schabi.newpipe.util.ThemeHelper;
@ -58,9 +60,8 @@ import java.util.concurrent.TimeUnit;
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>. * along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
*/ */
public class SettingsActivity extends AppCompatActivity public class SettingsActivity extends AppCompatActivity implements
implements PreferenceFragmentCompat.OnPreferenceStartFragmentCallback,
BasePreferenceFragment.OnPreferenceStartFragmentCallback,
PreferenceSearchResultListener { PreferenceSearchResultListener {
private static final String TAG = "SettingsActivity"; private static final String TAG = "SettingsActivity";
private static final boolean DEBUG = MainActivity.DEBUG; private static final boolean DEBUG = MainActivity.DEBUG;
@ -165,6 +166,7 @@ public class SettingsActivity extends AppCompatActivity
@Override @Override
protected void onDestroy() { protected void onDestroy() {
setMenuSearchItem(null); setMenuSearchItem(null);
searchFragment = null;
super.onDestroy(); super.onDestroy();
} }
@ -183,26 +185,33 @@ public class SettingsActivity extends AppCompatActivity
RxTextView.textChanges(searchEditText) RxTextView.textChanges(searchEditText)
// Wait some time after the last input before actually searching // Wait some time after the last input before actually searching
.debounce(200, TimeUnit.MILLISECONDS) .debounce(200, TimeUnit.MILLISECONDS)
.subscribe(v -> runOnUiThread(() -> onSearchChanged())); .subscribe(v -> runOnUiThread(this::onSearchChanged));
// Configure clear button // Configure clear button
searchContainer.findViewById(R.id.toolbar_search_clear) searchContainer.findViewById(R.id.toolbar_search_clear)
.setOnClickListener(ev -> resetSearchText()); .setOnClickListener(ev -> resetSearchText());
// Build search configuration using SettingsResourceRegistry
prepareSearchConfig(); prepareSearchConfig();
// Build search configuration using SettingsResourceRegistry
final PreferenceSearchConfiguration config = new PreferenceSearchConfiguration(); final PreferenceSearchConfiguration config = new PreferenceSearchConfiguration();
SettingsResourceRegistry.getInstance().getAllEntries().stream() SettingsResourceRegistry.getInstance().getAllEntries().stream()
.filter(SettingsResourceRegistry.SettingRegistryEntry::isSearchable) .filter(SettingsResourceRegistry.SettingRegistryEntry::isSearchable)
.map(SettingsResourceRegistry.SettingRegistryEntry::getPreferencesResId) .map(SettingsResourceRegistry.SettingRegistryEntry::getPreferencesResId)
.forEach(config::index); .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() { private void prepareSearchConfig() {
// Check if the update settings should be available // Check if the update settings are available
if (!CheckForNewAppVersion.isReleaseApk(App.getApp())) { if (!CheckForNewAppVersion.isReleaseApk(App.getApp())) {
SettingsResourceRegistry.getInstance() SettingsResourceRegistry.getInstance()
.getEntryByPreferencesResId(R.xml.update_settings) .getEntryByPreferencesResId(R.xml.update_settings)

View File

@ -2,8 +2,6 @@ package org.schabi.newpipe.settings.preferencesearch;
import android.text.TextUtils; import android.text.TextUtils;
import androidx.annotation.NonNull;
import org.schabi.newpipe.settings.preferencesearch.similarity.FuzzyScore; import org.schabi.newpipe.settings.preferencesearch.similarity.FuzzyScore;
import java.util.Comparator; import java.util.Comparator;
@ -22,7 +20,7 @@ public class PreferenceFuzzySearchFunction
final Stream<PreferenceSearchItem> allAvailable, final Stream<PreferenceSearchItem> allAvailable,
final String keyword 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 return allAvailable
// General search // General search
@ -39,14 +37,6 @@ public class PreferenceFuzzySearchFunction
.limit(20); .limit(20);
} }
private float computeFuzzyScore(
@NonNull final PreferenceSearchItem item,
@NonNull final Function<PreferenceSearchItem, String> resolver,
@NonNull final String keyword
) {
return FUZZY_SCORE.fuzzyScore(resolver.apply(item), keyword);
}
static class FuzzySearchGeneralDTO { static class FuzzySearchGeneralDTO {
private final PreferenceSearchItem item; private final PreferenceSearchItem item;
private final float score; private final float score;

View File

@ -18,7 +18,7 @@ import java.util.Objects;
/** /**
* Parses the corresponding preference-file(s). * Parses the corresponding preference-file(s).
*/ */
class PreferenceParser { public class PreferenceParser {
private static final String TAG = "PreferenceParser"; private static final String TAG = "PreferenceParser";
private static final String NS_ANDROID = "http://schemas.android.com/apk/res/android"; private static final String NS_ANDROID = "http://schemas.android.com/apk/res/android";
@ -28,7 +28,7 @@ class PreferenceParser {
private final Map<String, ?> allPreferences; private final Map<String, ?> allPreferences;
private final PreferenceSearchConfiguration searchConfiguration; private final PreferenceSearchConfiguration searchConfiguration;
PreferenceParser( public PreferenceParser(
final Context context, final Context context,
final PreferenceSearchConfiguration searchConfiguration final PreferenceSearchConfiguration searchConfiguration
) { ) {

View File

@ -50,7 +50,7 @@ public class PreferenceSearchConfiguration {
return item; return item;
} }
List<SearchIndexItem> getFiles() { public List<SearchIndexItem> getFiles() {
return itemsToIndex; return itemsToIndex;
} }

View File

@ -24,29 +24,15 @@ import java.util.Objects;
public class PreferenceSearchFragment extends Fragment { public class PreferenceSearchFragment extends Fragment {
public static final String NAME = PreferenceSearchFragment.class.getSimpleName(); public static final String NAME = PreferenceSearchFragment.class.getSimpleName();
private final PreferenceSearchConfiguration searchConfiguration;
private final PreferenceSearcher searcher; private final PreferenceSearcher searcher;
private SearchViewHolder viewHolder; private SearchViewHolder viewHolder;
private PreferenceSearchAdapter adapter; private PreferenceSearchAdapter adapter;
public PreferenceSearchFragment(final PreferenceSearchConfiguration searchConfiguration) { public PreferenceSearchFragment(
this.searchConfiguration = searchConfiguration; final PreferenceSearcher searcher
this.searcher = new PreferenceSearcher(searchConfiguration); ) {
} this.searcher = searcher;
@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);
} }
@Nullable @Nullable
@ -98,12 +84,6 @@ public class PreferenceSearchFragment extends Fragment {
((PreferenceSearchResultListener) getActivity()).onSearchResultClicked(item); ((PreferenceSearchResultListener) getActivity()).onSearchResultClicked(item);
} }
@Override
public void onDestroy() {
searcher.close();
super.onDestroy();
}
private static class SearchViewHolder { private static class SearchViewHolder {
private final RecyclerView recyclerView; private final RecyclerView recyclerView;
private final View emptyStateView; private final View emptyStateView;

View File

@ -6,16 +6,16 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
class PreferenceSearcher implements AutoCloseable { public class PreferenceSearcher {
private final List<PreferenceSearchItem> allEntries = new ArrayList<>(); private final List<PreferenceSearchItem> allEntries = new ArrayList<>();
private final PreferenceSearchConfiguration configuration; private final PreferenceSearchConfiguration configuration;
PreferenceSearcher(final PreferenceSearchConfiguration configuration) { public PreferenceSearcher(final PreferenceSearchConfiguration configuration) {
this.configuration = configuration; this.configuration = configuration;
} }
void add(final List<PreferenceSearchItem> items) { public void add(final List<PreferenceSearchItem> items) {
allEntries.addAll(items); allEntries.addAll(items);
} }
@ -29,8 +29,7 @@ class PreferenceSearcher implements AutoCloseable {
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
@Override public void clear() {
public void close() {
allEntries.clear(); allEntries.clear();
} }
} }