From 6f97670159ceed92d0129574be7d5931e6d8634b Mon Sep 17 00:00:00 2001 From: frankknoll Date: Fri, 20 Dec 2024 11:31:25 +0100 Subject: [PATCH] making SelectNavProfileBottomSheet searchable --- .../profiles/SelectNavProfileBottomSheet.java | 50 +++++++++++------- .../BasePreferenceBottomSheetInitializer.java | 22 ++++---- .../fragments/NavigationFragment.java | 51 ++++++++++++++----- .../SettingsSearchParameterizedTest.java | 4 +- 4 files changed, 87 insertions(+), 40 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/profiles/SelectNavProfileBottomSheet.java b/OsmAnd/src/net/osmand/plus/profiles/SelectNavProfileBottomSheet.java index 82e560ade2b..e4ba29eb3ca 100644 --- a/OsmAnd/src/net/osmand/plus/profiles/SelectNavProfileBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/profiles/SelectNavProfileBottomSheet.java @@ -17,10 +17,10 @@ import androidx.appcompat.app.AlertDialog; import androidx.core.content.ContextCompat; import androidx.fragment.app.Fragment; -import androidx.fragment.app.FragmentActivity; import androidx.fragment.app.FragmentManager; import net.osmand.IndexConstants; +import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem; @@ -39,7 +39,9 @@ import net.osmand.plus.profiles.data.RoutingDataObject; import net.osmand.plus.profiles.data.RoutingDataUtils; import net.osmand.plus.settings.backend.ApplicationMode; +import net.osmand.plus.settings.bottomsheets.BasePreferenceBottomSheetInitializer; import net.osmand.plus.settings.fragments.NavigationFragment; +import net.osmand.plus.settings.fragments.search.SearchablePreferenceDialog; import net.osmand.plus.utils.AndroidUtils; import net.osmand.plus.utils.UiUtilities; import net.osmand.plus.widgets.multistatetoggle.TextToggleButton.TextRadioItem; @@ -54,8 +56,9 @@ import java.io.File; import java.util.ArrayList; import java.util.List; +import java.util.Optional; -public class SelectNavProfileBottomSheet extends SelectProfileBottomSheet implements ImportTaskListener { +public class SelectNavProfileBottomSheet extends SelectProfileBottomSheet implements ImportTaskListener, SearchablePreferenceDialog { private static final String DOWNLOADED_PREDEFINED_JSON = "downloaded_predefined_json"; private static final String DIALOG_TYPE = "dialog_type"; @@ -79,24 +82,23 @@ public enum DialogMode { int titleId; } - public static void showInstance(@NonNull FragmentActivity activity, - @Nullable Fragment target, - ApplicationMode appMode, - String selectedItemKey, - boolean usedOnMap) { - FragmentManager fragmentManager = activity.getSupportFragmentManager(); - if (!fragmentManager.isStateSaved()) { - SelectNavProfileBottomSheet fragment = new SelectNavProfileBottomSheet(); - Bundle args = new Bundle(); + public static SelectNavProfileBottomSheet createInstance(final @Nullable Fragment target, + final ApplicationMode appMode, + final String selectedItemKey, + final boolean usedOnMap) { + final SelectNavProfileBottomSheet bottomSheet = new SelectNavProfileBottomSheet(); + { + final Bundle args = new Bundle(); args.putString(SELECTED_KEY, selectedItemKey); - fragment.setArguments(args); - fragment.setUsedOnMap(usedOnMap); - fragment.setAppMode(appMode); - fragment.setTargetFragment(target, 0); - boolean isOnline = OnlineRoutingEngine.isOnlineEngineKey(selectedItemKey); - fragment.setDialogMode(isOnline ? DialogMode.ONLINE : DialogMode.OFFLINE); - fragment.show(fragmentManager, TAG); + bottomSheet.setArguments(args); + } + { + final boolean isOnline = OnlineRoutingEngine.isOnlineEngineKey(selectedItemKey); + bottomSheet.setDialogMode(isOnline ? DialogMode.ONLINE : DialogMode.OFFLINE); } + return BasePreferenceBottomSheetInitializer + .initialize(bottomSheet) + .with(Optional.empty(), appMode, usedOnMap, target); } @Override @@ -515,4 +517,16 @@ public void setDialogMode(DialogMode dialogMode) { this.dialogMode = dialogMode; } + @Override + public void show(final FragmentManager fragmentManager, final OsmandApplication app) { + if (!fragmentManager.isStateSaved()) { + show(fragmentManager, TAG); + } + } + + @Override + public String getSearchableInfo() { + // FK-TODO: add online and offline profiles + return getString(R.string.select_nav_profile_dialog_message); + } } diff --git a/OsmAnd/src/net/osmand/plus/settings/bottomsheets/BasePreferenceBottomSheetInitializer.java b/OsmAnd/src/net/osmand/plus/settings/bottomsheets/BasePreferenceBottomSheetInitializer.java index 7c13e41c113..07c1ad8cf25 100644 --- a/OsmAnd/src/net/osmand/plus/settings/bottomsheets/BasePreferenceBottomSheetInitializer.java +++ b/OsmAnd/src/net/osmand/plus/settings/bottomsheets/BasePreferenceBottomSheetInitializer.java @@ -4,6 +4,7 @@ import android.os.Bundle; +import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; import androidx.preference.Preference; @@ -28,20 +29,23 @@ public T with(final Optional preference, final @Nullable ApplicationMode appMode, final boolean usedOnMap, final @Nullable Fragment target) { - preference.ifPresent( - _preference -> { - basePreferenceBottomSheet.setPreference(_preference); - basePreferenceBottomSheet.setArguments(createArguments(PREFERENCE_ID, _preference.getKey())); - }); + preference.ifPresent(this::setPreference); basePreferenceBottomSheet.setUsedOnMap(usedOnMap); basePreferenceBottomSheet.setAppMode(appMode); basePreferenceBottomSheet.setTargetFragment(target, 0); return basePreferenceBottomSheet; } - private static Bundle createArguments(final String key, final String value) { - final Bundle arguments = new Bundle(); - arguments.putString(key, value); - return arguments; + private void setPreference(final Preference preference) { + basePreferenceBottomSheet.setPreference(preference); + getArguments(basePreferenceBottomSheet).putString(PREFERENCE_ID, preference.getKey()); + } + + @NonNull + private static Bundle getArguments(final Fragment fragment) { + if (fragment.getArguments() == null) { + fragment.setArguments(new Bundle()); + } + return fragment.getArguments(); } } diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/NavigationFragment.java b/OsmAnd/src/net/osmand/plus/settings/fragments/NavigationFragment.java index 1cf0818040c..0af78760f25 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/NavigationFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/NavigationFragment.java @@ -6,6 +6,7 @@ import static net.osmand.plus.profiles.SelectProfileBottomSheet.PROFILE_KEY_ARG; import static net.osmand.plus.routepreparationmenu.RouteOptionsBottomSheet.DIALOG_MODE_KEY; import static net.osmand.plus.routing.TransportRoutingHelper.PUBLIC_TRANSPORT_KEY; +import static net.osmand.plus.settings.fragments.search.PreferenceDialogs.showDialogForPreference; import android.graphics.drawable.Drawable; import android.os.Bundle; @@ -32,9 +33,14 @@ import net.osmand.plus.routepreparationmenu.RouteOptionsBottomSheet.DialogMode; import net.osmand.plus.routing.RouteService; import net.osmand.plus.settings.backend.ApplicationMode; +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.util.Algorithms; -public class NavigationFragment extends BaseSettingsFragment implements OnSelectProfileCallback { +import java.util.Optional; + +public class NavigationFragment extends BaseSettingsFragment implements OnSelectProfileCallback, ShowableSearchablePreferenceDialogProvider { public static final String TAG = NavigationFragment.class.getSimpleName(); public static final String NAVIGATION_TYPE = "navigation_type"; @@ -144,18 +150,39 @@ public void onApplyPreferenceChange(String prefId, boolean applyToAllProfiles, O } @Override - public boolean onPreferenceClick(Preference preference) { - String prefId = preference.getKey(); - MapActivity activity = getMapActivity(); + public Optional> getShowableSearchablePreferenceDialog(final Preference preference, final Fragment target) { + return NAVIGATION_TYPE.equals(preference.getKey()) ? + Optional.of( + new ShowableSearchablePreferenceDialog<>( + SelectNavProfileBottomSheet.createInstance( + target, + getSelectedAppMode(), + getSelectedAppMode().getRoutingProfile(), + false)) { + + @Override + protected void show(final SearchablePreferenceDialog searchablePreferenceDialog) { + searchablePreferenceDialog.show( + getMapActivity().getSupportFragmentManager(), + app); + } + }) : + Optional.empty(); + } + + @Override + public boolean onPreferenceClick(final Preference preference) { + if (showDialogForPreference(preference, this)) { + return true; + } + final MapActivity activity = getMapActivity(); if (activity != null) { - ApplicationMode appMode = getSelectedAppMode(); - if (NAVIGATION_TYPE.equals(prefId)) { - String selected = appMode.getRoutingProfile(); - SelectNavProfileBottomSheet.showInstance(activity, this, appMode, selected, false); - } else if (CUSTOMIZE_ROUTE_LINE.equals(prefId)) { - RouteLineAppearanceFragment.showInstance(activity, appMode); - } else if (DETAILED_TRACK_GUIDANCE.equals(prefId)) { - DetailedTrackGuidanceFragment.showInstance(activity, appMode, this); + if (CUSTOMIZE_ROUTE_LINE.equals(preference.getKey())) { + RouteLineAppearanceFragment.showInstance(activity, getSelectedAppMode()); + // TODO: return true because the click was handled? + } else if (DETAILED_TRACK_GUIDANCE.equals(preference.getKey())) { + DetailedTrackGuidanceFragment.showInstance(activity, getSelectedAppMode(), this); + // TODO: return true because the click was handled? } } return false; 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 dc50faf70be..eccd6faf7c8 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 @@ -85,7 +85,9 @@ public static Iterable data() { {"WakeTimeBottomSheet: description", searchQuery(context -> context.getString(R.string.turn_screen_on_wake_time_descr, context.getString(R.string.keep_screen_on)))}, {"WakeTimeBottomSheet: keep_screen_on", searchQuery(R.string.keep_screen_on)}, - {"WakeTimeBottomSheet: timeoutDescription", searchQuery(context -> context.getString(R.string.screen_timeout_descr, context.getString(R.string.system_screen_timeout)))} + {"WakeTimeBottomSheet: timeoutDescription", searchQuery(context -> context.getString(R.string.screen_timeout_descr, context.getString(R.string.system_screen_timeout)))}, + + {"SelectNavProfileBottomSheet: header", searchQuery(R.string.select_nav_profile_dialog_message)} }); }