Skip to content

Commit

Permalink
adding SettingsSearchTest.test_search_within_FuelTankCapacityBottomSh…
Browse files Browse the repository at this point in the history
…eet_description()
  • Loading branch information
KnollFrank committed Dec 15, 2024
1 parent 0e8ab87 commit be4c7eb
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.preference.Preference;

import net.osmand.PlatformUtil;
import net.osmand.plus.OsmandApplication;
Expand All @@ -19,6 +20,7 @@
import net.osmand.plus.settings.enums.VolumeUnit;
import net.osmand.plus.settings.fragments.ApplyQueryType;
import net.osmand.plus.settings.fragments.OnConfirmPreferenceChange;
import net.osmand.plus.settings.fragments.search.SearchablePreferenceDialog;
import net.osmand.plus.utils.OsmAndFormatter;
import net.osmand.plus.widgets.chips.ChipItem;
import net.osmand.plus.widgets.dialogbutton.DialogButtonType;
Expand All @@ -33,7 +35,7 @@
import java.util.List;
import java.util.Locale;

public class FuelTankCapacityBottomSheet extends BaseTextFieldBottomSheet {
public class FuelTankCapacityBottomSheet extends BaseTextFieldBottomSheet implements SearchablePreferenceDialog {
private static final Log LOG = PlatformUtil.getLog(VehicleParametersBottomSheet.class);
public static final String TAG = FuelTankCapacityBottomSheet.class.getSimpleName();

Expand Down Expand Up @@ -77,7 +79,7 @@ public void afterTextChanged(Editable s) {

@NonNull
public List<ChipItem> collectChipItems(@NonNull OsmandApplication app,
@NonNull VolumeUnit volumeUnit) {
@NonNull VolumeUnit volumeUnit) {
List<ChipItem> chips = new ArrayList<>();
String none = app.getString(R.string.shared_string_none);
ChipItem chip = new ChipItem(none);
Expand Down Expand Up @@ -129,21 +131,42 @@ protected String formatInputValue(float input) {
return formatter.format(input);
}

public static void showInstance(@NonNull FragmentManager fm, String key, Fragment target,
boolean usedOnMap, @Nullable ApplicationMode appMode) {
@NonNull
public static FuelTankCapacityBottomSheet createInstance(final Preference preference,
final Fragment target,
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;
}

@Override
public void show(final FragmentManager fragmentManager, final OsmandApplication app) {
try {
if (!fm.isStateSaved()) {
Bundle args = new Bundle();
args.putString(PREFERENCE_ID, key);
FuelTankCapacityBottomSheet fragment = new FuelTankCapacityBottomSheet();
fragment.setArguments(args);
fragment.setUsedOnMap(usedOnMap);
fragment.setAppMode(appMode);
fragment.setTargetFragment(target, 0);
fragment.show(fm, TAG);
if (!fragmentManager.isStateSaved()) {
show(fragmentManager, TAG);
}
} catch (RuntimeException e) {
} catch (final RuntimeException e) {
LOG.error("showInstance", e);
}
}

@Override
public String getSearchableInfo() {
return String.join(
", ",
title.getText(),
tvDescription.getText());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import java.text.DecimalFormatSymbols;
import java.util.List;
import java.util.Locale;
import java.util.Optional;

public class VehicleParametersBottomSheet extends BaseTextFieldBottomSheet implements SearchablePreferenceDialog {
private static final Log LOG = PlatformUtil.getLog(VehicleParametersBottomSheet.class);
Expand Down Expand Up @@ -135,21 +134,22 @@ protected String formatInputValue(float input) {
return formatter.format(input);
}

public static @NonNull VehicleParametersBottomSheet createInstance(final String key,
final Fragment target,
final boolean usedOnMap,
final @Nullable ApplicationMode appMode,
final boolean configureSettingsSearch,
final Optional<Preference> preference) {
@NonNull
public static VehicleParametersBottomSheet createInstance(final Preference preference,
final Fragment target,
final boolean usedOnMap,
final @Nullable ApplicationMode appMode,
final boolean configureSettingsSearch) {
final Bundle args = new Bundle();
args.putString(PREFERENCE_ID, key);

args.putString(PREFERENCE_ID, preference.getKey());
final VehicleParametersBottomSheet fragment = new VehicleParametersBottomSheet();
fragment.setArguments(args);
fragment.setUsedOnMap(usedOnMap);
fragment.setAppMode(appMode);
fragment.setTargetFragment(target, 0);
preference.ifPresent(fragment::setPreference);
if (target == null) {
fragment.setPreference(preference);
}
fragment.setConfigureSettingsSearch(configureSettingsSearch);
return fragment;
}
Expand All @@ -160,7 +160,7 @@ public void show(final FragmentManager fragmentManager, final OsmandApplication
if (!fragmentManager.isStateSaved()) {
show(fragmentManager, TAG);
}
} catch (RuntimeException e) {
} catch (final RuntimeException e) {
LOG.error("showInstance", e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -490,13 +490,12 @@ private Optional<SearchablePreferenceDialog> createPreferenceDialog(final Prefer
final Optional<Preference> preferenceParam) {
if (settings.PRECISE_DISTANCE_NUMBERS.getId().equals(preference.getKey())) {
return Optional.of(
DistanceDuringNavigationBottomSheet
.createInstance(
preference.getKey(),
target,
getSelectedAppMode(),
false,
preferenceParam));
DistanceDuringNavigationBottomSheet.createInstance(
preference.getKey(),
target,
getSelectedAppMode(),
false,
preferenceParam));
}
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,13 @@ public boolean onPreferenceClick(Preference preference) {
}
return true;
} else if (settings.FUEL_TANK_CAPACITY.getId().equals(key)) {
FragmentManager fragmentManager = getFragmentManager();
if (fragmentManager != null) {
FuelTankCapacityBottomSheet.showInstance(fragmentManager, preference.getKey(),
this, false, getSelectedAppMode());
}
final Optional<SearchablePreferenceDialog> preferenceDialog =
createPreferenceDialog(
preference,
this,
false);
show(preferenceDialog.get());
return true;
}
return super.onPreferenceClick(preference);
}
Expand All @@ -278,8 +280,7 @@ public void onDisplayPreferenceDialog(Preference preference) {
createPreferenceDialog(
preference,
this,
false,
Optional.empty());
false);
if (preferenceDialog.isPresent()) {
show(preferenceDialog.get());
} else {
Expand All @@ -297,33 +298,39 @@ private void show(final SearchablePreferenceDialog dialog) {
private Optional<SearchablePreferenceDialog> createPreferenceDialog(
final Preference preference,
final VehicleParametersFragment target,
final boolean configureSettingsSearch,
final Optional<Preference> preferenceParam) {
final boolean configureSettingsSearch) {
if (preference instanceof SizePreference) {
return Optional.of(
VehicleParametersBottomSheet
.createInstance(
preference.getKey(),
preference,
target,
false,
getSelectedAppMode(),
configureSettingsSearch,
preferenceParam));
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()));
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));
}
return Optional.empty();
}
Expand All @@ -337,7 +344,7 @@ public Optional<PreferenceDialogAndSearchableInfoByPreferenceDialogProvider<?>>
_preferenceDialog -> new VehicleSpeedHelper(app, getSelectedAppMode()).getSearchableInfo()));
}
return this
.createPreferenceDialog(preference, null, true, Optional.of(preference))
.createPreferenceDialog(preference, null, true)
.map(preferenceDialog ->
new PreferenceDialogAndSearchableInfoByPreferenceDialogProvider<>(
(Fragment) preferenceDialog,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ public void test_search_within_AnnouncementTimeBottomSheet_description() {
testSearchAndFind(R.string.announcement_time_descr);
}

@Test
public void test_search_within_FuelTankCapacityBottomSheet_description() {
testSearchAndFind(R.string.fuel_tank_capacity_description);
}

private void testSearchAndFind(final int id) {
testSearchAndFind(app.getResources().getString(id));
}
Expand Down

0 comments on commit be4c7eb

Please sign in to comment.