1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2024-12-23 08:30:44 +00:00

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.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 <http://www.gnu.org/licenses/>.
*/
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)

View File

@ -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<PreferenceSearchItem> 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<PreferenceSearchItem, String> resolver,
@NonNull final String keyword
) {
return FUZZY_SCORE.fuzzyScore(resolver.apply(item), keyword);
}
static class FuzzySearchGeneralDTO {
private final PreferenceSearchItem item;
private final float score;

View File

@ -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<String, ?> allPreferences;
private final PreferenceSearchConfiguration searchConfiguration;
PreferenceParser(
public PreferenceParser(
final Context context,
final PreferenceSearchConfiguration searchConfiguration
) {

View File

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

View File

@ -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;

View File

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