From b8d901bf24a5b39ea9099500f0e25bb0e9e99746 Mon Sep 17 00:00:00 2001 From: frankknoll Date: Wed, 18 Dec 2024 15:45:03 +0100 Subject: [PATCH] refactoring --- .../fragments/RouteParametersFragment.java | 30 +++++----- ...rchablePreferenceDialogFragmentHolder.java | 3 +- .../fragments/VehicleParametersFragment.java | 55 ++++++++++--------- .../voice/VoiceAnnouncesFragment.java | 19 ++++--- 4 files changed, 58 insertions(+), 49 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/RouteParametersFragment.java b/OsmAnd/src/net/osmand/plus/settings/fragments/RouteParametersFragment.java index eb09b990baa..c4ca038b6bd 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/RouteParametersFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/RouteParametersFragment.java @@ -66,7 +66,6 @@ import net.osmand.plus.settings.enums.RoutingType; 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.preferences.ListParameters; import net.osmand.plus.settings.preferences.ListPreferenceEx; @@ -639,29 +638,32 @@ public boolean showPreferenceFragment(final PreferenceFragmentCompat preferenceF public Optional> getPreferenceDialogAndSearchableInfoByPreferenceDialogProvider(final Preference preference) { return this .getSearchablePreferenceDialog(preference) + .map(SearchablePreferenceDialogFragmentHolder::searchablePreferenceDialogFragment) .map(preferenceDialog -> new PreferenceDialogAndSearchableInfoByPreferenceDialogProvider<>( - (Fragment) preferenceDialog, + preferenceDialog, _preferenceDialog -> preferenceDialog.getSearchableInfo())); } - private Optional getSearchablePreferenceDialog(final Preference preference) { + private Optional> getSearchablePreferenceDialog(final Preference preference) { if (settings.ROUTE_RECALCULATION_DISTANCE.getId().equals(preference.getKey())) { return Optional.of( - RecalculateRouteInDeviationBottomSheet.createInstance( - preference, - null, - false, - getSelectedAppMode())); + SearchablePreferenceDialogFragmentHolder.of( + RecalculateRouteInDeviationBottomSheet.createInstance( + preference, + null, + false, + getSelectedAppMode()))); } if (GOODS_RESTRICTIONS_PREFERENCE.equals(preference.getKey())) { return Optional.of( - GoodsRestrictionsBottomSheet.createInstance( - null, - preference, - getSelectedAppMode(), - false, - getGoodsRestrictionPreference().getModeValue(getSelectedAppMode()))); + SearchablePreferenceDialogFragmentHolder.of( + GoodsRestrictionsBottomSheet.createInstance( + null, + preference, + getSelectedAppMode(), + false, + getGoodsRestrictionPreference().getModeValue(getSelectedAppMode())))); } return Optional.empty(); } diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/SearchablePreferenceDialogFragmentHolder.java b/OsmAnd/src/net/osmand/plus/settings/fragments/SearchablePreferenceDialogFragmentHolder.java index 7bc5278f938..3cb1190c5af 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/SearchablePreferenceDialogFragmentHolder.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/SearchablePreferenceDialogFragmentHolder.java @@ -4,8 +4,7 @@ import net.osmand.plus.settings.fragments.search.SearchablePreferenceDialog; -// FK-TODO: use everywhere like in ConfigureProfileFragment -record SearchablePreferenceDialogFragmentHolder( +public record SearchablePreferenceDialogFragmentHolder( T searchablePreferenceDialogFragment) { public static SearchablePreferenceDialogFragmentHolder of(final T searchablePreferenceDialogFragment) { diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/VehicleParametersFragment.java b/OsmAnd/src/net/osmand/plus/settings/fragments/VehicleParametersFragment.java index ad5acd96f34..0c54608b5bd 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/VehicleParametersFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/VehicleParametersFragment.java @@ -273,6 +273,7 @@ public boolean onPreferenceClick(Preference preference) { public void onDisplayPreferenceDialog(Preference preference) { this .createPreferenceDialog(preference, this, false) + .map(SearchablePreferenceDialogFragmentHolder::searchablePreferenceDialogFragment) .ifPresentOrElse( this::show, () -> super.onDisplayPreferenceDialog(preference)); @@ -285,42 +286,45 @@ private void show(final SearchablePreferenceDialog dialog) { } } - private Optional createPreferenceDialog( + private Optional> createPreferenceDialog( final Preference preference, final VehicleParametersFragment target, final boolean configureSettingsSearch) { if (preference instanceof SizePreference) { return Optional.of( - VehicleParametersBottomSheet - .createInstance( - preference, - target, - false, - getSelectedAppMode(), - configureSettingsSearch)); + SearchablePreferenceDialogFragmentHolder.of( + VehicleParametersBottomSheet + .createInstance( + preference, + target, + false, + getSelectedAppMode(), + configureSettingsSearch))); } if (MOTOR_TYPE_PREF_ID.equals(preference.getKey())) { final ListPreferenceEx pref = (ListPreferenceEx) preference; return Optional.of( - SimpleSingleSelectionBottomSheet.createInstance( - target, - preference.getKey(), - pref.getTitle().toString(), - pref.getDescription(), - getSelectedAppMode(), - false, - pref.getEntries(), - pref.getEntryValues(), - pref.getValueIndex())); + SearchablePreferenceDialogFragmentHolder.of( + SimpleSingleSelectionBottomSheet.createInstance( + target, + preference.getKey(), + pref.getTitle().toString(), + pref.getDescription(), + getSelectedAppMode(), + false, + pref.getEntries(), + pref.getEntryValues(), + pref.getValueIndex()))); } if (settings.FUEL_TANK_CAPACITY.getId().equals(preference.getKey())) { return Optional.of( - FuelTankCapacityBottomSheet.createInstance( - preference, - target, - false, - getSelectedAppMode(), - configureSettingsSearch)); + SearchablePreferenceDialogFragmentHolder.of( + FuelTankCapacityBottomSheet.createInstance( + preference, + target, + false, + getSelectedAppMode(), + configureSettingsSearch))); } return Optional.empty(); } @@ -335,9 +339,10 @@ public Optional> } return this .createPreferenceDialog(preference, null, true) + .map(SearchablePreferenceDialogFragmentHolder::searchablePreferenceDialogFragment) .map(preferenceDialog -> new PreferenceDialogAndSearchableInfoByPreferenceDialogProvider<>( - (Fragment) preferenceDialog, + preferenceDialog, _preferenceDialog -> preferenceDialog.getSearchableInfo())); } diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/voice/VoiceAnnouncesFragment.java b/OsmAnd/src/net/osmand/plus/settings/fragments/voice/VoiceAnnouncesFragment.java index 820c0de8352..a639a185331 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/voice/VoiceAnnouncesFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/voice/VoiceAnnouncesFragment.java @@ -12,7 +12,6 @@ import androidx.annotation.NonNull; import androidx.appcompat.widget.SwitchCompat; import androidx.core.content.ContextCompat; -import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; import androidx.preference.Preference; import androidx.preference.PreferenceViewHolder; @@ -28,6 +27,7 @@ import net.osmand.plus.settings.bottomsheets.SpeedLimitBottomSheet; import net.osmand.plus.settings.fragments.ApplyQueryType; import net.osmand.plus.settings.fragments.BaseSettingsFragment; +import net.osmand.plus.settings.fragments.SearchablePreferenceDialogFragmentHolder; import net.osmand.plus.settings.fragments.search.SearchablePreferenceDialog; import net.osmand.plus.settings.fragments.search.SearchablePreferenceDialogProvider; import net.osmand.plus.settings.preferences.ListPreferenceEx; @@ -257,6 +257,7 @@ public boolean onPreferenceClick(Preference preference) { public void onDisplayPreferenceDialog(Preference preference) { this .createSearchablePreferenceDialog(preference, this) + .map(SearchablePreferenceDialogFragmentHolder::searchablePreferenceDialogFragment) .ifPresentOrElse( this::show, () -> { @@ -268,16 +269,17 @@ public void onDisplayPreferenceDialog(Preference preference) { }); } - private Optional createSearchablePreferenceDialog( + private Optional> createSearchablePreferenceDialog( final Preference preference, final VoiceAnnouncesFragment target) { return settings.ARRIVAL_DISTANCE_FACTOR.getId().equals(preference.getKey()) ? Optional.of( - AnnouncementTimeBottomSheet.createInstance( - preference, - target, - getSelectedAppMode(), - false)) : + SearchablePreferenceDialogFragmentHolder.of( + AnnouncementTimeBottomSheet.createInstance( + preference, + target, + getSelectedAppMode(), + false))) : Optional.empty(); } @@ -307,9 +309,10 @@ private void setupSpeakCamerasPref() { public Optional> getPreferenceDialogAndSearchableInfoByPreferenceDialogProvider(final Preference preference) { return this .createSearchablePreferenceDialog(preference, null) + .map(SearchablePreferenceDialogFragmentHolder::searchablePreferenceDialogFragment) .map(preferenceDialog -> new PreferenceDialogAndSearchableInfoByPreferenceDialogProvider<>( - (Fragment) preferenceDialog, + preferenceDialog, _preferenceDialog -> preferenceDialog.getSearchableInfo())); } }