From a1deaaf8fdb777eb990ce3447678df6750c2ea81 Mon Sep 17 00:00:00 2001 From: frankknoll Date: Thu, 19 Dec 2024 08:18:33 +0100 Subject: [PATCH] refactoring ProfileAppearanceFragment --- ...ustomizableSingleSelectionBottomSheet.java | 34 ++++----- .../CompassModeWidgetDialogController.java | 6 +- .../controllers/MapFocusDialogController.java | 13 ++-- .../fragments/BaseSettingsFragment.java | 4 +- .../ProfileAppearanceFragment.java | 72 ++++++++++--------- 5 files changed, 66 insertions(+), 63 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/settings/bottomsheets/CustomizableSingleSelectionBottomSheet.java b/OsmAnd/src/net/osmand/plus/settings/bottomsheets/CustomizableSingleSelectionBottomSheet.java index 8792ac5a42a..68793a170e9 100644 --- a/OsmAnd/src/net/osmand/plus/settings/bottomsheets/CustomizableSingleSelectionBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/settings/bottomsheets/CustomizableSingleSelectionBottomSheet.java @@ -11,6 +11,7 @@ import androidx.annotation.NonNull; import androidx.fragment.app.FragmentManager; +import net.osmand.plus.OsmandApplication; import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem; import net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithCompoundButton; import net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithCompoundButton.Builder; @@ -18,6 +19,7 @@ import net.osmand.plus.base.bottomsheetmenu.simpleitems.LongDescriptionItem; import net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem; import net.osmand.plus.base.dialog.data.DisplayItem; +import net.osmand.plus.settings.fragments.search.SearchablePreferenceDialog; import net.osmand.plus.utils.UiUtilities; import java.util.List; @@ -28,7 +30,7 @@ * When choosing one of the options, the selected option is passed to the controller * and dialog automatically closed without the need for confirmation by the user. */ -public class CustomizableSingleSelectionBottomSheet extends CustomizableBottomSheet { +public class CustomizableSingleSelectionBottomSheet extends CustomizableBottomSheet implements SearchablePreferenceDialog { public static final String TAG = CustomizableSingleSelectionBottomSheet.class.getSimpleName(); @@ -88,11 +90,6 @@ protected boolean hideButtonsContainer() { return show == null || !show; } - public static boolean showInstance(@NonNull FragmentManager fragmentManager, - @NonNull String processId, boolean usedOnMap) { - return createInstance(processId, usedOnMap).show(fragmentManager); - } - public static @NonNull CustomizableSingleSelectionBottomSheet createInstance(final @NonNull String processId, final boolean usedOnMap) { final CustomizableSingleSelectionBottomSheet fragment = new CustomizableSingleSelectionBottomSheet(); fragment.setProcessId(processId); @@ -100,13 +97,17 @@ public static boolean showInstance(@NonNull FragmentManager fragmentManager, return fragment; } - public boolean show(@NonNull final FragmentManager fragmentManager) { - try { - show(fragmentManager, TAG); - return true; - } catch (final RuntimeException e) { - return false; - } + @Override + public void show(final FragmentManager fragmentManager, final OsmandApplication app) { + show(fragmentManager, TAG); + } + + @Override + public String getSearchableInfo() { + return CustomizableSingleSelectionBottomSheetSearchableInfoProvider.getSearchableInfo( + getTitle(), + getDescription(), + getDisplayItems()); } private String getTitle() { @@ -120,11 +121,4 @@ private String getDescription() { private List getDisplayItems() { return displayData.getDisplayItems(); } - - public String getSearchableInfo() { - return CustomizableSingleSelectionBottomSheetSearchableInfoProvider.getSearchableInfo( - getTitle(), - getDescription(), - getDisplayItems()); - } } diff --git a/OsmAnd/src/net/osmand/plus/settings/controllers/CompassModeWidgetDialogController.java b/OsmAnd/src/net/osmand/plus/settings/controllers/CompassModeWidgetDialogController.java index de9bd9cc512..1ac19e95936 100644 --- a/OsmAnd/src/net/osmand/plus/settings/controllers/CompassModeWidgetDialogController.java +++ b/OsmAnd/src/net/osmand/plus/settings/controllers/CompassModeWidgetDialogController.java @@ -1,7 +1,6 @@ package net.osmand.plus.settings.controllers; import androidx.annotation.NonNull; -import androidx.fragment.app.FragmentManager; import net.osmand.plus.OsmandApplication; import net.osmand.plus.activities.MapActivity; @@ -39,7 +38,8 @@ public static void showDialog(@NonNull MapActivity mapActivity) { DialogManager dialogManager = app.getDialogManager(); dialogManager.register(PROCESS_ID, controller); - FragmentManager manager = mapActivity.getSupportFragmentManager(); - CustomizableSingleSelectionBottomSheet.showInstance(manager, PROCESS_ID, true); + CustomizableSingleSelectionBottomSheet + .createInstance(PROCESS_ID, true) + .show(mapActivity.getSupportFragmentManager(), app); } } diff --git a/OsmAnd/src/net/osmand/plus/settings/controllers/MapFocusDialogController.java b/OsmAnd/src/net/osmand/plus/settings/controllers/MapFocusDialogController.java index 8a4d8c6501b..1d9397d5076 100644 --- a/OsmAnd/src/net/osmand/plus/settings/controllers/MapFocusDialogController.java +++ b/OsmAnd/src/net/osmand/plus/settings/controllers/MapFocusDialogController.java @@ -6,7 +6,6 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import androidx.fragment.app.FragmentManager; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; @@ -15,8 +14,8 @@ import net.osmand.plus.base.dialog.DialogManager; import net.osmand.plus.base.dialog.data.DisplayData; import net.osmand.plus.base.dialog.data.DisplayItem; -import net.osmand.plus.base.dialog.interfaces.controller.IDisplayDataProvider; import net.osmand.plus.base.dialog.interfaces.controller.IDialogItemSelected; +import net.osmand.plus.base.dialog.interfaces.controller.IDisplayDataProvider; import net.osmand.plus.settings.backend.ApplicationMode; import net.osmand.plus.settings.backend.OsmandSettings; import net.osmand.plus.settings.bottomsheets.CustomizableSingleSelectionBottomSheet; @@ -33,13 +32,14 @@ public class MapFocusDialogController extends BaseDialogController private final OsmandSettings settings; public MapFocusDialogController(@NonNull OsmandApplication app, - @NonNull ApplicationMode appMode) { + @NonNull ApplicationMode appMode) { super(app); this.appMode = appMode; this.settings = app.getSettings(); } - @NonNull @Override + @NonNull + @Override public String getProcessId() { return PROCESS_ID; } @@ -96,7 +96,8 @@ public static void showDialog(@NonNull MapActivity mapActivity, @NonNull Applica DialogManager dialogManager = app.getDialogManager(); dialogManager.register(PROCESS_ID, controller); - FragmentManager manager = mapActivity.getSupportFragmentManager(); - CustomizableSingleSelectionBottomSheet.showInstance(manager, PROCESS_ID, true); + CustomizableSingleSelectionBottomSheet + .createInstance(PROCESS_ID, true) + .show(mapActivity.getSupportFragmentManager(), app); } } diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/BaseSettingsFragment.java b/OsmAnd/src/net/osmand/plus/settings/fragments/BaseSettingsFragment.java index dd92c422f1e..338b21b33d2 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/BaseSettingsFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/BaseSettingsFragment.java @@ -848,7 +848,9 @@ protected void showSingleSelectionDialog(@NonNull String processId, DialogManager dialogManager = app.getDialogManager(); dialogManager.register(processId, controller); FragmentManager fm = activity.getSupportFragmentManager(); - CustomizableSingleSelectionBottomSheet.showInstance(fm, processId, false); + CustomizableSingleSelectionBottomSheet + .createInstance(processId, false) + .show(fm, app); } } diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/profileappearance/ProfileAppearanceFragment.java b/OsmAnd/src/net/osmand/plus/settings/fragments/profileappearance/ProfileAppearanceFragment.java index e8f4facd3ba..bd5c135b3a2 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/profileappearance/ProfileAppearanceFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/profileappearance/ProfileAppearanceFragment.java @@ -1,6 +1,7 @@ package net.osmand.plus.settings.fragments.profileappearance; import static net.osmand.aidlapi.OsmAndCustomizationConstants.DRAWER_SETTINGS_ID; +import static net.osmand.plus.settings.fragments.search.PreferenceDialogs.showDialogForPreference; import static net.osmand.plus.utils.ColorUtilities.getListBgColorId; import android.annotation.SuppressLint; @@ -38,10 +39,11 @@ import net.osmand.plus.card.icon.IconsPaletteCard; import net.osmand.plus.helpers.AndroidUiHelper; import net.osmand.plus.routepreparationmenu.cards.BaseCard; -import net.osmand.plus.settings.bottomsheets.CustomizableSingleSelectionBottomSheet; import net.osmand.plus.settings.fragments.BaseSettingsFragment; import net.osmand.plus.settings.fragments.SettingsScreenType; -import net.osmand.plus.settings.fragments.search.SearchablePreferenceDialogProvider; +import net.osmand.plus.settings.fragments.search.SearchablePreferenceDialog; +import net.osmand.plus.settings.fragments.search.ShowableSearchablePreferenceDialog; +import net.osmand.plus.settings.fragments.search.ShowableSearchablePreferenceDialogProvider; import net.osmand.plus.utils.AndroidUtils; import net.osmand.plus.utils.ColorUtilities; import net.osmand.plus.utils.UiUtilities; @@ -54,9 +56,7 @@ import java.util.Optional; -import de.KnollFrank.lib.settingssearch.provider.PreferenceDialogAndSearchableInfoByPreferenceDialogProvider; - -public class ProfileAppearanceFragment extends BaseSettingsFragment implements IProfileAppearanceScreen, SearchablePreferenceDialogProvider { +public class ProfileAppearanceFragment extends BaseSettingsFragment implements IProfileAppearanceScreen, ShowableSearchablePreferenceDialogProvider { private static final Log LOG = PlatformUtil.getLog(ProfileAppearanceFragment.class); @@ -230,48 +230,54 @@ public void afterTextChanged(Editable s) { } @Override - public boolean onPreferenceClick(Preference preference) { - MapActivity mapActivity = getMapActivity(); - if (mapActivity != null) { - this - .createPreferenceDialog(preference) - .ifPresent(dialog -> dialog.show(mapActivity.getSupportFragmentManager())); + public boolean onPreferenceClick(final Preference preference) { + if (showDialogForPreference(preference, this)) { + return true; } return super.onPreferenceClick(preference); } @Override - public Optional> getPreferenceDialogAndSearchableInfoByPreferenceDialogProvider(final Preference preference) { - return this - .createPreferenceDialog(preference) - .map(preferenceDialog -> - new PreferenceDialogAndSearchableInfoByPreferenceDialogProvider<>( - preferenceDialog, - CustomizableSingleSelectionBottomSheet::getSearchableInfo)); - } - - private Optional createPreferenceDialog(final Preference preference) { + public Optional> getShowableSearchablePreferenceDialog(final Preference preference, final Fragment target) { if (settings.VIEW_ANGLE_VISIBILITY.getId().equals(preference.getKey())) { return Optional.of( - screenController - .getProfileOptionController() - .createDialog( - app.getString(R.string.view_angle), - app.getString(R.string.view_angle_description), - settings.VIEW_ANGLE_VISIBILITY)); + new ShowableSearchablePreferenceDialog<>( + screenController + .getProfileOptionController() + .createDialog( + app.getString(R.string.view_angle), + app.getString(R.string.view_angle_description), + settings.VIEW_ANGLE_VISIBILITY)) { + + @Override + protected void show(final SearchablePreferenceDialog searchablePreferenceDialog) { + ProfileAppearanceFragment.this.show(searchablePreferenceDialog); + } + }); } if (settings.LOCATION_RADIUS_VISIBILITY.getId().equals(preference.getKey())) { return Optional.of( - screenController - .getProfileOptionController() - .createDialog( - app.getString(R.string.location_radius), - app.getString(R.string.location_radius_description), - settings.LOCATION_RADIUS_VISIBILITY)); + new ShowableSearchablePreferenceDialog<>( + screenController + .getProfileOptionController() + .createDialog( + app.getString(R.string.location_radius), + app.getString(R.string.location_radius_description), + settings.LOCATION_RADIUS_VISIBILITY)) { + + @Override + protected void show(final SearchablePreferenceDialog searchablePreferenceDialog) { + ProfileAppearanceFragment.this.show(searchablePreferenceDialog); + } + }); } return Optional.empty(); } + private void show(final SearchablePreferenceDialog searchablePreferenceDialog) { + searchablePreferenceDialog.show(requireActivity().getSupportFragmentManager(), app); + } + private void bindCard(@NonNull PreferenceViewHolder holder, @NonNull BaseCard card) { ViewGroup container = (ViewGroup) holder.itemView; container.removeAllViews();