From 0e8ab87ebdbb1498a25504e4fe3494c25803c7c8 Mon Sep 17 00:00:00 2001 From: frankknoll Date: Sat, 14 Dec 2024 11:04:10 +0100 Subject: [PATCH] refactoring --- .../search/SearchableInfoProvider.java | 91 +------------------ .../search/SettingsSearchButtonHelper.java | 9 +- .../preferences/EditTextPreferenceEx.java | 19 +++- .../preferences/ListPreferenceEx.java | 26 +++++- .../MultiSelectBooleanPreference.java | 16 +++- .../settings/preferences/SizePreference.java | 19 +++- .../preferences/SwitchPreferenceEx.java | 20 +++- 7 files changed, 105 insertions(+), 95 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/search/SearchableInfoProvider.java b/OsmAnd/src/net/osmand/plus/settings/fragments/search/SearchableInfoProvider.java index 6c1d39c2a49..a6efca71129 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/search/SearchableInfoProvider.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/search/SearchableInfoProvider.java @@ -1,92 +1,7 @@ package net.osmand.plus.settings.fragments.search; -import androidx.preference.Preference; +@FunctionalInterface +public interface SearchableInfoProvider { -import net.osmand.plus.settings.preferences.EditTextPreferenceEx; -import net.osmand.plus.settings.preferences.ListPreferenceEx; -import net.osmand.plus.settings.preferences.MultiSelectBooleanPreference; -import net.osmand.plus.settings.preferences.SizePreference; -import net.osmand.plus.settings.preferences.SwitchPreferenceEx; - -import java.util.Arrays; -import java.util.List; -import java.util.Optional; - -import de.KnollFrank.lib.settingssearch.common.Lists; - -class SearchableInfoProvider implements de.KnollFrank.lib.settingssearch.search.provider.SearchableInfoProvider { - - @Override - public Optional getSearchableInfo(final Preference preference) { - if (preference instanceof final ListPreferenceEx listPreferenceEx) { - return Optional.of(getSearchableInfo(listPreferenceEx)); - } - if (preference instanceof final SwitchPreferenceEx switchPreferenceEx) { - return Optional.of(getSearchableInfo(switchPreferenceEx)); - } - if (preference instanceof final MultiSelectBooleanPreference multiSelectBooleanPreference) { - return Optional.of(getSearchableInfo(multiSelectBooleanPreference)); - } - if (preference instanceof final EditTextPreferenceEx editTextPreferenceEx) { - return Optional.of(getSearchableInfo(editTextPreferenceEx)); - } - if (preference instanceof final SizePreference sizePreference) { - return Optional.of(getSizePreferenceSearchableInfo(sizePreference)); - } - return Optional.empty(); - } - - private static String getSearchableInfo(final ListPreferenceEx preference) { - return String.join( - ", ", - concat( - Optional.ofNullable(preference.getDialogTitle()), - Optional.ofNullable(preference.getDescription()), - Optional.ofNullable(preference.getEntries()))); - } - - private static String getSearchableInfo(final SwitchPreferenceEx preference) { - return String.join( - ", ", - Lists.getPresentElements( - Arrays.asList( - Optional.ofNullable(preference.getSummaryOff()), - Optional.ofNullable(preference.getSummaryOn()), - Optional.ofNullable(preference.getDescription())))); - } - - private static String getSearchableInfo(final MultiSelectBooleanPreference preference) { - return String.join( - ", ", - concat( - Optional.ofNullable(preference.getDialogTitle()), - Optional.ofNullable(preference.getDescription()), - Optional.ofNullable(preference.getEntries()))); - } - - private static String getSearchableInfo(final EditTextPreferenceEx preference) { - return String.join( - ", ", - Lists.getPresentElements( - Arrays.asList( - Optional.ofNullable(preference.getText()), - Optional.ofNullable(preference.getDescription())))); - } - - private static String getSizePreferenceSearchableInfo(final SizePreference preference) { - return String.join( - ", ", - Lists.getPresentElements( - Arrays.asList( - Optional.ofNullable(preference.getDialogTitle()), - Optional.ofNullable(preference.getSummary())))); - } - - private static List concat(final Optional dialogTitle, - final Optional description, - final Optional entries) { - final List result = Lists.getPresentElements(Arrays.asList(dialogTitle, description)); - result.addAll(Lists.asList(entries)); - return result; - } + String getSearchableInfo(); } diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/search/SettingsSearchButtonHelper.java b/OsmAnd/src/net/osmand/plus/settings/fragments/search/SettingsSearchButtonHelper.java index ac8b138000b..c7c5550bb01 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/search/SettingsSearchButtonHelper.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/search/SettingsSearchButtonHelper.java @@ -5,6 +5,7 @@ import androidx.annotation.IdRes; import androidx.fragment.app.FragmentActivity; +import androidx.preference.Preference; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; @@ -62,7 +63,7 @@ public static SearchPreferenceFragments createSearchPreferenceFragments( new SearchDatabaseConfigBuilder() .withFragmentFactory(new FragmentFactory()) .withPreferenceFragmentConnected2PreferenceProvider(new PreferenceFragmentConnected2PreferenceProvider()) - .withSearchableInfoProvider(new SearchableInfoProvider()) + .withSearchableInfoProvider(SettingsSearchButtonHelper::getSearchableInfo) .withPreferenceDialogAndSearchableInfoProvider(new PreferenceDialogAndSearchableInfoProvider()) .withPreferenceSearchablePredicate(new PreferenceSearchablePredicate()) .build()) @@ -78,6 +79,12 @@ public static SearchPreferenceFragments createSearchPreferenceFragments( .build(); } + private static Optional getSearchableInfo(final Preference preference) { + return preference instanceof final SearchableInfoProvider searchableInfoProvider ? + Optional.of(searchableInfoProvider.getSearchableInfo()) : + Optional.empty(); + } + private void onClickShowSearchPreferenceFragment(final ImageView searchPreferenceButton) { final SearchPreferenceFragments searchPreferenceFragments = createSearchPreferenceFragments( diff --git a/OsmAnd/src/net/osmand/plus/settings/preferences/EditTextPreferenceEx.java b/OsmAnd/src/net/osmand/plus/settings/preferences/EditTextPreferenceEx.java index 3677c73452b..5dd6c9a371d 100644 --- a/OsmAnd/src/net/osmand/plus/settings/preferences/EditTextPreferenceEx.java +++ b/OsmAnd/src/net/osmand/plus/settings/preferences/EditTextPreferenceEx.java @@ -5,7 +5,14 @@ import androidx.preference.EditTextPreference; -public class EditTextPreferenceEx extends EditTextPreference { +import net.osmand.plus.settings.fragments.search.SearchableInfoProvider; + +import java.util.List; +import java.util.Optional; + +import de.KnollFrank.lib.settingssearch.common.Lists; + +public class EditTextPreferenceEx extends EditTextPreference implements SearchableInfoProvider { private String description; @@ -36,4 +43,14 @@ public void setDescription(String description) { public void setDescription(int descriptionResId) { setDescription(getContext().getString(descriptionResId)); } + + @Override + public String getSearchableInfo() { + return String.join( + ", ", + Lists.getPresentElements( + List.of( + Optional.ofNullable(getText()), + Optional.ofNullable(getDescription())))); + } } diff --git a/OsmAnd/src/net/osmand/plus/settings/preferences/ListPreferenceEx.java b/OsmAnd/src/net/osmand/plus/settings/preferences/ListPreferenceEx.java index aeb9c74ebde..090f9d4b535 100644 --- a/OsmAnd/src/net/osmand/plus/settings/preferences/ListPreferenceEx.java +++ b/OsmAnd/src/net/osmand/plus/settings/preferences/ListPreferenceEx.java @@ -9,8 +9,14 @@ import net.osmand.plus.settings.backend.ApplicationMode; import net.osmand.plus.settings.backend.OsmAndPreferencesDataStore; +import net.osmand.plus.settings.fragments.search.SearchableInfoProvider; -public class ListPreferenceEx extends DialogPreference { +import java.util.List; +import java.util.Optional; + +import de.KnollFrank.lib.settingssearch.common.Lists; + +public class ListPreferenceEx extends DialogPreference implements SearchableInfoProvider { private String[] entries; private Object[] entryValues; @@ -148,4 +154,22 @@ private void persistValue(Object value) { ((OsmAndPreferencesDataStore) dataStore).putValue(getKey(), value); } } + + @Override + public String getSearchableInfo() { + return String.join( + ", ", + concat( + Optional.ofNullable(getDialogTitle()), + Optional.ofNullable(getDescription()), + Optional.ofNullable(getEntries()))); + } + + static List concat(final Optional dialogTitle, + final Optional description, + final Optional entries) { + final List result = Lists.getPresentElements(List.of(dialogTitle, description)); + result.addAll(Lists.asList(entries)); + return result; + } } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/settings/preferences/MultiSelectBooleanPreference.java b/OsmAnd/src/net/osmand/plus/settings/preferences/MultiSelectBooleanPreference.java index 1f69cd09891..74d4f1c2b59 100644 --- a/OsmAnd/src/net/osmand/plus/settings/preferences/MultiSelectBooleanPreference.java +++ b/OsmAnd/src/net/osmand/plus/settings/preferences/MultiSelectBooleanPreference.java @@ -1,5 +1,7 @@ package net.osmand.plus.settings.preferences; +import static net.osmand.plus.settings.preferences.ListPreferenceEx.concat; + import android.content.Context; import android.util.AttributeSet; @@ -7,11 +9,13 @@ import androidx.preference.PreferenceDataStore; import net.osmand.plus.settings.backend.OsmAndPreferencesDataStore; +import net.osmand.plus.settings.fragments.search.SearchableInfoProvider; import java.util.HashSet; +import java.util.Optional; import java.util.Set; -public class MultiSelectBooleanPreference extends MultiSelectListPreference { +public class MultiSelectBooleanPreference extends MultiSelectListPreference implements SearchableInfoProvider { private String description; @@ -109,4 +113,14 @@ public Set getPersistedBooleanPrefsIds(Set defaultReturnValue) { return enabledPrefs; } + + @Override + public String getSearchableInfo() { + return String.join( + ", ", + concat( + Optional.ofNullable(getDialogTitle()), + Optional.ofNullable(getDescription()), + Optional.ofNullable(getEntries()))); + } } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/settings/preferences/SizePreference.java b/OsmAnd/src/net/osmand/plus/settings/preferences/SizePreference.java index d9289b9332f..191aa434f94 100644 --- a/OsmAnd/src/net/osmand/plus/settings/preferences/SizePreference.java +++ b/OsmAnd/src/net/osmand/plus/settings/preferences/SizePreference.java @@ -6,15 +6,20 @@ import androidx.preference.DialogPreference; import net.osmand.plus.R; +import net.osmand.plus.settings.fragments.search.SearchableInfoProvider; import net.osmand.plus.settings.vehiclesize.SizeType; import net.osmand.plus.settings.vehiclesize.VehicleSizes; import net.osmand.plus.settings.vehiclesize.containers.Metric; import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; +import java.util.List; import java.util.Locale; +import java.util.Optional; -public class SizePreference extends DialogPreference { +import de.KnollFrank.lib.settingssearch.common.Lists; + +public class SizePreference extends DialogPreference implements SearchableInfoProvider { private SizeType sizeType; private Metric metric; @@ -78,7 +83,17 @@ private String getString(int stringId) { return getContext().getString(stringId); } - public String getValue () { + public String getValue() { return getPersistedString(defaultValue); } + + @Override + public String getSearchableInfo() { + return String.join( + ", ", + Lists.getPresentElements( + List.of( + Optional.ofNullable(getDialogTitle()), + Optional.ofNullable(getSummary())))); + } } diff --git a/OsmAnd/src/net/osmand/plus/settings/preferences/SwitchPreferenceEx.java b/OsmAnd/src/net/osmand/plus/settings/preferences/SwitchPreferenceEx.java index 75902f4778c..e24cfce410f 100644 --- a/OsmAnd/src/net/osmand/plus/settings/preferences/SwitchPreferenceEx.java +++ b/OsmAnd/src/net/osmand/plus/settings/preferences/SwitchPreferenceEx.java @@ -5,7 +5,14 @@ import androidx.preference.SwitchPreferenceCompat; -public class SwitchPreferenceEx extends SwitchPreferenceCompat { +import net.osmand.plus.settings.fragments.search.SearchableInfoProvider; + +import java.util.List; +import java.util.Optional; + +import de.KnollFrank.lib.settingssearch.common.Lists; + +public class SwitchPreferenceEx extends SwitchPreferenceCompat implements SearchableInfoProvider { private String description; @@ -43,4 +50,15 @@ protected void onClick() { getPreferenceManager().showDialog(this); } } + + @Override + public String getSearchableInfo() { + return String.join( + ", ", + Lists.getPresentElements( + List.of( + Optional.ofNullable(getSummaryOff()), + Optional.ofNullable(getSummaryOn()), + Optional.ofNullable(getDescription())))); + } } \ No newline at end of file