From dc5c3abca8d7f4457adee1d9c0c798d5e0455d49 Mon Sep 17 00:00:00 2001 From: frankknoll Date: Tue, 17 Dec 2024 09:17:10 +0100 Subject: [PATCH] refactoring GeneralProfileSettingsFragment --- .../fragments/BaseSettingsFragment.java | 8 +- .../GeneralProfileSettingsFragment.java | 81 +++++++------------ 2 files changed, 35 insertions(+), 54 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/BaseSettingsFragment.java b/OsmAnd/src/net/osmand/plus/settings/fragments/BaseSettingsFragment.java index 6cc96711d76..ab4e887a4f0 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/BaseSettingsFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/BaseSettingsFragment.java @@ -337,10 +337,10 @@ public final boolean onConfirmPreferenceChange(String prefId, Object newValue, A @Override public boolean onPreferenceClick(final Preference preference) { if (this instanceof final PreferenceFragmentHandlerProvider preferenceFragmentHandlerProvider) { - return preferenceFragmentHandlerProvider - .getPreferenceFragmentHandler(preference) - .map(this::showPreferenceFragment) - .orElse(false); + final Optional preferenceFragmentHandler = preferenceFragmentHandlerProvider.getPreferenceFragmentHandler(preference); + if (preferenceFragmentHandler.isPresent()) { + return showPreferenceFragment(preferenceFragmentHandler.get()); + } } if (this instanceof ShowableSearchablePreferenceDialogProvider showableSearchablePreferenceDialogProvider) { final Optional> preferenceDialog = showableSearchablePreferenceDialogProvider.getShowableSearchablePreferenceDialog(preference, this); diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/GeneralProfileSettingsFragment.java b/OsmAnd/src/net/osmand/plus/settings/fragments/GeneralProfileSettingsFragment.java index 022d5fc499d..d72a588448c 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/GeneralProfileSettingsFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/GeneralProfileSettingsFragment.java @@ -41,7 +41,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 +55,8 @@ import java.util.List; import java.util.Optional; -import de.KnollFrank.lib.settingssearch.provider.PreferenceDialogAndSearchableInfoByPreferenceDialogProvider; - -public class GeneralProfileSettingsFragment extends BaseSettingsFragment implements SearchablePreferenceDialogProvider, PreferenceFragmentHandlerProvider { +// FK-TODO: combine ShowableSearchablePreferenceDialogProvider and PreferenceFragmentHandlerProvider? +public class GeneralProfileSettingsFragment extends BaseSettingsFragment implements ShowableSearchablePreferenceDialogProvider, PreferenceFragmentHandlerProvider { public static final String TAG = GeneralProfileSettingsFragment.class.getSimpleName(); @@ -423,29 +423,17 @@ public void onApplyPreferenceChange(String prefId, boolean applyToAllProfiles, O @Override public boolean onPreferenceClick(Preference preference) { - { - final Optional preferenceDialog = - createPreferenceDialog( - preference, - this); - if (preferenceDialog.isPresent()) { - show(preferenceDialog.get()); - return true; - } + String key = preference.getKey(); + ApplicationMode appMode = getSelectedAppMode(); + if (settings.DRIVING_REGION.getId().equals(key)) { + showDrivingRegionDialog(); + 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.ROTATE_MAP.getId().equals(key)) { + CompassModeDialogController controller = new CompassModeDialogController(app, appMode); + showSingleSelectionDialog(CompassModeDialogController.PROCESS_ID, controller); + controller.setCallback(this); + return true; } return super.onPreferenceClick(preference); } @@ -484,34 +472,27 @@ public boolean showPreferenceFragment(final PreferenceFragmentCompat preferenceF return Optional.empty(); } - private Optional createPreferenceDialog(final Preference preference, - final GeneralProfileSettingsFragment target) { + @Override + public Optional> getShowableSearchablePreferenceDialog(final Preference preference, final Fragment target) { if (settings.PRECISE_DISTANCE_NUMBERS.getId().equals(preference.getKey())) { return Optional.of( - DistanceDuringNavigationBottomSheet.createInstance( - preference, - target, - getSelectedAppMode(), - false)); - } - return Optional.empty(); - } + new ShowableSearchablePreferenceDialog<>( + DistanceDuringNavigationBottomSheet.createInstance( + preference, + target, + getSelectedAppMode(), + false)) { - private void show(final SearchablePreferenceDialog dialog) { - final FragmentManager fragmentManager = getFragmentManager(); - if (fragmentManager != null) { - dialog.show(fragmentManager, app); + @Override + protected void show(final SearchablePreferenceDialog searchablePreferenceDialog) { + final FragmentManager fragmentManager = getFragmentManager(); + if (fragmentManager != null) { + searchablePreferenceDialog.show(fragmentManager, app); + } + } + }); } - } - - @Override - public Optional> getPreferenceDialogAndSearchableInfoByPreferenceDialogProvider(final Preference preference) { - return this - .createPreferenceDialog(preference, null) - .map(preferenceDialog -> - new PreferenceDialogAndSearchableInfoByPreferenceDialogProvider<>( - (Fragment) preferenceDialog, - _preferenceDialog -> preferenceDialog.getSearchableInfo())); + return Optional.empty(); } private void updateDialogControllerCallbacks() {