From e96aea422c817398724c12e7f3be4ad5d43089d7 Mon Sep 17 00:00:00 2001 From: frankknoll Date: Mon, 16 Dec 2024 08:07:25 +0100 Subject: [PATCH] refactoring --- .../AnnouncementTimeBottomSheet.java | 14 ++----- .../BasePreferenceBottomSheetInitializer.java | 41 +++++++++++++++++++ .../DistanceDuringNavigationBottomSheet.java | 15 ++----- .../FuelTankCapacityBottomSheet.java | 14 ++----- ...ecalculateRouteInDeviationBottomSheet.java | 15 ++----- .../VehicleParametersBottomSheet.java | 14 ++----- 6 files changed, 56 insertions(+), 57 deletions(-) create mode 100644 OsmAnd/src/net/osmand/plus/settings/bottomsheets/BasePreferenceBottomSheetInitializer.java diff --git a/OsmAnd/src/net/osmand/plus/settings/bottomsheets/AnnouncementTimeBottomSheet.java b/OsmAnd/src/net/osmand/plus/settings/bottomsheets/AnnouncementTimeBottomSheet.java index c82df3af3fb..ee27ec6117b 100644 --- a/OsmAnd/src/net/osmand/plus/settings/bottomsheets/AnnouncementTimeBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/settings/bottomsheets/AnnouncementTimeBottomSheet.java @@ -166,17 +166,9 @@ public static AnnouncementTimeBottomSheet createInstance(final Preference prefer final Fragment target, final @Nullable ApplicationMode appMode, final boolean usedOnMap) { - final Bundle args = new Bundle(); - args.putString(PREFERENCE_ID, preference.getKey()); - final AnnouncementTimeBottomSheet fragment = new AnnouncementTimeBottomSheet(); - fragment.setArguments(args); - fragment.setAppMode(appMode); - fragment.setUsedOnMap(usedOnMap); - fragment.setTargetFragment(target, 0); - if (target == null) { - fragment.setPreference(preference); - } - return fragment; + return BasePreferenceBottomSheetInitializer + .initialize(new AnnouncementTimeBottomSheet()) + .with(preference, appMode, usedOnMap, target); } @Override diff --git a/OsmAnd/src/net/osmand/plus/settings/bottomsheets/BasePreferenceBottomSheetInitializer.java b/OsmAnd/src/net/osmand/plus/settings/bottomsheets/BasePreferenceBottomSheetInitializer.java new file mode 100644 index 00000000000..d1369c37ef1 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/settings/bottomsheets/BasePreferenceBottomSheetInitializer.java @@ -0,0 +1,41 @@ +package net.osmand.plus.settings.bottomsheets; + +import static net.osmand.plus.settings.bottomsheets.BasePreferenceBottomSheet.PREFERENCE_ID; + +import android.os.Bundle; + +import androidx.annotation.Nullable; +import androidx.fragment.app.Fragment; +import androidx.preference.Preference; + +import net.osmand.plus.settings.backend.ApplicationMode; + +class BasePreferenceBottomSheetInitializer { + + private final T basePreferenceBottomSheet; + + private BasePreferenceBottomSheetInitializer(final T basePreferenceBottomSheet) { + this.basePreferenceBottomSheet = basePreferenceBottomSheet; + } + + public static BasePreferenceBottomSheetInitializer initialize(final T basePreferenceBottomSheet) { + return new BasePreferenceBottomSheetInitializer<>(basePreferenceBottomSheet); + } + + public T with(final Preference preference, + final @Nullable ApplicationMode appMode, + final boolean usedOnMap, + final @Nullable Fragment target) { + final Bundle args = new Bundle(); + args.putString(PREFERENCE_ID, preference.getKey()); + basePreferenceBottomSheet.setArguments(args); + basePreferenceBottomSheet.setUsedOnMap(usedOnMap); + basePreferenceBottomSheet.setAppMode(appMode); + basePreferenceBottomSheet.setTargetFragment(target, 0); + // FK-TODO: find a clearer way to handle this (everywhere): + if (target == null) { + basePreferenceBottomSheet.setPreference(preference); + } + return basePreferenceBottomSheet; + } +} diff --git a/OsmAnd/src/net/osmand/plus/settings/bottomsheets/DistanceDuringNavigationBottomSheet.java b/OsmAnd/src/net/osmand/plus/settings/bottomsheets/DistanceDuringNavigationBottomSheet.java index dd426eaccce..7b741952fba 100644 --- a/OsmAnd/src/net/osmand/plus/settings/bottomsheets/DistanceDuringNavigationBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/settings/bottomsheets/DistanceDuringNavigationBottomSheet.java @@ -127,18 +127,9 @@ protected int getDismissButtonTextId() { final @Nullable Fragment target, final @Nullable ApplicationMode appMode, final boolean usedOnMap) { - final Bundle args = new Bundle(); - args.putString(PREFERENCE_ID, preference.getKey()); - - final DistanceDuringNavigationBottomSheet fragment = new DistanceDuringNavigationBottomSheet(); - fragment.setArguments(args); - fragment.setAppMode(appMode); - fragment.setUsedOnMap(usedOnMap); - fragment.setTargetFragment(target, 0); - if (target == null) { - fragment.setPreference(preference); - } - return fragment; + return BasePreferenceBottomSheetInitializer + .initialize(new DistanceDuringNavigationBottomSheet()) + .with(preference, appMode, usedOnMap, target); } @Override diff --git a/OsmAnd/src/net/osmand/plus/settings/bottomsheets/FuelTankCapacityBottomSheet.java b/OsmAnd/src/net/osmand/plus/settings/bottomsheets/FuelTankCapacityBottomSheet.java index 5d369cf5f62..52520e8aef6 100644 --- a/OsmAnd/src/net/osmand/plus/settings/bottomsheets/FuelTankCapacityBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/settings/bottomsheets/FuelTankCapacityBottomSheet.java @@ -1,7 +1,6 @@ package net.osmand.plus.settings.bottomsheets; import android.annotation.SuppressLint; -import android.os.Bundle; import android.text.Editable; import android.view.View; @@ -137,18 +136,11 @@ public static FuelTankCapacityBottomSheet createInstance(final Preference prefer final boolean usedOnMap, final @Nullable ApplicationMode appMode, final boolean configureSettingsSearch) { - final Bundle args = new Bundle(); - args.putString(PREFERENCE_ID, preference.getKey()); final FuelTankCapacityBottomSheet fragment = new FuelTankCapacityBottomSheet(); - fragment.setArguments(args); - fragment.setUsedOnMap(usedOnMap); - fragment.setAppMode(appMode); - fragment.setTargetFragment(target, 0); - if (target == null) { - fragment.setPreference(preference); - } fragment.setConfigureSettingsSearch(configureSettingsSearch); - return fragment; + return BasePreferenceBottomSheetInitializer + .initialize(fragment) + .with(preference, appMode, usedOnMap, target); } @Override diff --git a/OsmAnd/src/net/osmand/plus/settings/bottomsheets/RecalculateRouteInDeviationBottomSheet.java b/OsmAnd/src/net/osmand/plus/settings/bottomsheets/RecalculateRouteInDeviationBottomSheet.java index d3a70f76f56..a41f2ffcb2e 100644 --- a/OsmAnd/src/net/osmand/plus/settings/bottomsheets/RecalculateRouteInDeviationBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/settings/bottomsheets/RecalculateRouteInDeviationBottomSheet.java @@ -246,18 +246,9 @@ public static RecalculateRouteInDeviationBottomSheet createInstance(final Prefer final Fragment target, final boolean usedOnMap, final @Nullable ApplicationMode appMode) { - final Bundle args = new Bundle(); - args.putString(PREFERENCE_ID, preference.getKey()); - final RecalculateRouteInDeviationBottomSheet fragment = new RecalculateRouteInDeviationBottomSheet(); - fragment.setArguments(args); - fragment.setUsedOnMap(usedOnMap); - fragment.setAppMode(appMode); - fragment.setTargetFragment(target, 0); - // FK-TODO: find a clearer way to handle this (everywhere): - if (target == null) { - fragment.setPreference(preference); - } - return fragment; + return BasePreferenceBottomSheetInitializer + .initialize(new RecalculateRouteInDeviationBottomSheet()) + .with(preference, appMode, usedOnMap, target); } @Override diff --git a/OsmAnd/src/net/osmand/plus/settings/bottomsheets/VehicleParametersBottomSheet.java b/OsmAnd/src/net/osmand/plus/settings/bottomsheets/VehicleParametersBottomSheet.java index a92fa4aabee..6043d88c42b 100644 --- a/OsmAnd/src/net/osmand/plus/settings/bottomsheets/VehicleParametersBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/settings/bottomsheets/VehicleParametersBottomSheet.java @@ -2,7 +2,6 @@ import android.annotation.SuppressLint; import android.graphics.drawable.Drawable; -import android.os.Bundle; import android.text.Editable; import android.view.View; import android.widget.TextView; @@ -140,18 +139,11 @@ public static VehicleParametersBottomSheet createInstance(final Preference prefe final boolean usedOnMap, final @Nullable ApplicationMode appMode, final boolean configureSettingsSearch) { - final Bundle args = new Bundle(); - args.putString(PREFERENCE_ID, preference.getKey()); final VehicleParametersBottomSheet fragment = new VehicleParametersBottomSheet(); - fragment.setArguments(args); - fragment.setUsedOnMap(usedOnMap); - fragment.setAppMode(appMode); - fragment.setTargetFragment(target, 0); - if (target == null) { - fragment.setPreference(preference); - } fragment.setConfigureSettingsSearch(configureSettingsSearch); - return fragment; + return BasePreferenceBottomSheetInitializer + .initialize(fragment) + .with(preference, appMode, usedOnMap, target); } @Override