Skip to content

Commit

Permalink
making SelectNavProfileBottomSheet searchable
Browse files Browse the repository at this point in the history
  • Loading branch information
KnollFrank committed Dec 20, 2024
1 parent 52c00ba commit 6f97670
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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";
Expand All @@ -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
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,20 +29,23 @@ public T with(final Optional<Preference> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";
Expand Down Expand Up @@ -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<ShowableSearchablePreferenceDialog<?>> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ public static Iterable<Object[]> 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)}
});
}

Expand Down

0 comments on commit 6f97670

Please sign in to comment.