From 49629d371614269cb7018ca06e6f897736468d8e Mon Sep 17 00:00:00 2001 From: frankknoll Date: Thu, 19 Dec 2024 07:25:59 +0100 Subject: [PATCH] refactoring GeneralProfileSettingsFragment --- .../GeneralProfileSettingsFragment.java | 71 +++++++------------ ...rchablePreferenceDialogFragmentHolder.java | 1 + .../SettingsSearchParameterizedTest.java | 4 +- 3 files changed, 31 insertions(+), 45 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/GeneralProfileSettingsFragment.java b/OsmAnd/src/net/osmand/plus/settings/fragments/GeneralProfileSettingsFragment.java index 2bb4b509c62..625189e950c 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/GeneralProfileSettingsFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/GeneralProfileSettingsFragment.java @@ -2,6 +2,7 @@ import static net.osmand.plus.settings.bottomsheets.DistanceDuringNavigationBottomSheet.DistanceDuringNavigationMode; import static net.osmand.plus.settings.fragments.SettingsScreenType.EXTERNAL_INPUT_DEVICE; +import static net.osmand.plus.settings.fragments.search.PreferenceDialogs.showDialogForPreference; import android.content.Context; import android.content.pm.ActivityInfo; @@ -41,7 +42,8 @@ import net.osmand.plus.settings.fragments.search.PreferenceFragmentHandler; import net.osmand.plus.settings.fragments.search.PreferenceFragmentHandlerProvider; import net.osmand.plus.settings.fragments.search.SearchablePreferenceDialog; -import net.osmand.plus.settings.fragments.search.SearchablePreferenceDialogProvider; +import net.osmand.plus.settings.fragments.search.ShowableSearchablePreferenceDialog; +import net.osmand.plus.settings.fragments.search.ShowableSearchablePreferenceDialogProvider; import net.osmand.plus.settings.preferences.ListPreferenceEx; import net.osmand.plus.settings.preferences.SwitchPreferenceEx; import net.osmand.plus.utils.UiUtilities; @@ -54,9 +56,7 @@ import java.util.List; import java.util.Optional; -import de.KnollFrank.lib.settingssearch.provider.PreferenceDialogAndSearchableInfoByPreferenceDialogProvider; - -public class GeneralProfileSettingsFragment extends BaseSettingsFragment implements SearchablePreferenceDialogProvider, PreferenceFragmentHandlerProvider { +public class GeneralProfileSettingsFragment extends BaseSettingsFragment implements PreferenceFragmentHandlerProvider, ShowableSearchablePreferenceDialogProvider { public static final String TAG = GeneralProfileSettingsFragment.class.getSimpleName(); @@ -422,32 +422,19 @@ public void onApplyPreferenceChange(String prefId, boolean applyToAllProfiles, O } @Override - public boolean onPreferenceClick(Preference preference) { - { - final Optional preferenceDialog = - this - .createPreferenceDialog( - preference, - this) - .map(SearchablePreferenceDialogFragmentHolder::searchablePreferenceDialogFragment); - if (preferenceDialog.isPresent()) { - show(preferenceDialog.get()); - return true; - } + public boolean onPreferenceClick(final Preference preference) { + if (showDialogForPreference(preference, this)) { + return true; } - { - String key = preference.getKey(); - ApplicationMode appMode = getSelectedAppMode(); - if (settings.DRIVING_REGION.getId().equals(key)) { - showDrivingRegionDialog(); - return true; - } - if (settings.ROTATE_MAP.getId().equals(key)) { - CompassModeDialogController controller = new CompassModeDialogController(app, appMode); - showSingleSelectionDialog(CompassModeDialogController.PROCESS_ID, controller); - controller.setCallback(this); - return true; - } + if (settings.DRIVING_REGION.getId().equals(preference.getKey())) { + showDrivingRegionDialog(); + return true; + } + if (settings.ROTATE_MAP.getId().equals(preference.getKey())) { + CompassModeDialogController controller = new CompassModeDialogController(app, getSelectedAppMode()); + showSingleSelectionDialog(CompassModeDialogController.PROCESS_ID, controller); + controller.setCallback(this); + return true; } return super.onPreferenceClick(preference); } @@ -486,17 +473,24 @@ public boolean showPreferenceFragment(final PreferenceFragmentCompat preferenceF return Optional.empty(); } - private Optional> createPreferenceDialog( + @Override + public Optional> getShowableSearchablePreferenceDialog( final Preference preference, - final GeneralProfileSettingsFragment target) { + final Fragment target) { if (settings.PRECISE_DISTANCE_NUMBERS.getId().equals(preference.getKey())) { return Optional.of( - SearchablePreferenceDialogFragmentHolder.of( + new ShowableSearchablePreferenceDialog<>( DistanceDuringNavigationBottomSheet.createInstance( preference, target, getSelectedAppMode(), - false))); + false)) { + + @Override + protected void show(final SearchablePreferenceDialog searchablePreferenceDialog) { + GeneralProfileSettingsFragment.this.show(searchablePreferenceDialog); + } + }); } return Optional.empty(); } @@ -508,17 +502,6 @@ private void show(final SearchablePreferenceDialog dialog) { } } - @Override - public Optional> getPreferenceDialogAndSearchableInfoByPreferenceDialogProvider(final Preference preference) { - return this - .createPreferenceDialog(preference, null) - .map(SearchablePreferenceDialogFragmentHolder::searchablePreferenceDialogFragment) - .map(searchablePreferenceDialog -> - new PreferenceDialogAndSearchableInfoByPreferenceDialogProvider<>( - searchablePreferenceDialog, - _preferenceDialog -> searchablePreferenceDialog.getSearchableInfo())); - } - private void updateDialogControllerCallbacks() { IDialogController controller; DialogManager dialogManager = app.getDialogManager(); diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/SearchablePreferenceDialogFragmentHolder.java b/OsmAnd/src/net/osmand/plus/settings/fragments/SearchablePreferenceDialogFragmentHolder.java index 3cb1190c5af..b026a273026 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/SearchablePreferenceDialogFragmentHolder.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/SearchablePreferenceDialogFragmentHolder.java @@ -4,6 +4,7 @@ import net.osmand.plus.settings.fragments.search.SearchablePreferenceDialog; +// FK-TODO: remove public record SearchablePreferenceDialogFragmentHolder( T searchablePreferenceDialogFragment) { diff --git a/OsmAnd/test/java/net/osmand/plus/settings/fragments/search/SettingsSearchParameterizedTest.java b/OsmAnd/test/java/net/osmand/plus/settings/fragments/search/SettingsSearchParameterizedTest.java index 5bab4a71b31..b9693c8f8d2 100644 --- a/OsmAnd/test/java/net/osmand/plus/settings/fragments/search/SettingsSearchParameterizedTest.java +++ b/OsmAnd/test/java/net/osmand/plus/settings/fragments/search/SettingsSearchParameterizedTest.java @@ -73,7 +73,9 @@ public static Iterable data() { {"ResetProfilePrefsBottomSheet: description", searchQuery(R.string.reset_all_profile_settings_descr)}, {"ResetProfilePrefsBottomSheet: reset_confirmation_descr", searchQuery("Tapping Reset discards all your changes")}, - {"GeneralProfileSettingsFragment", searchQuery(R.string.distance_during_navigation)} + {"GeneralProfileSettingsFragment", searchQuery(R.string.distance_during_navigation)}, + + {"DistanceDuringNavigationBottomSheet: description", searchQuery("Choose how distance information is displayed in navigation widgets")} }); }