Skip to content

Commit

Permalink
refactoring VehicleParametersFragment
Browse files Browse the repository at this point in the history
  • Loading branch information
KnollFrank committed Dec 18, 2024
1 parent d979cf4 commit d018a9d
Showing 1 changed file with 39 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static net.osmand.plus.settings.backend.OsmandSettings.ROUTING_PREFERENCE_PREFIX;
import static net.osmand.plus.settings.fragments.RouteParametersFragment.createRoutingParameterPref;
import static net.osmand.plus.settings.fragments.search.PreferenceDialogs.showDialogForPreference;
import static net.osmand.router.GeneralRouter.DEFAULT_SPEED;
import static net.osmand.router.GeneralRouter.MAX_AXLE_LOAD;
import static net.osmand.router.GeneralRouter.MOTOR_TYPE;
Expand Down Expand Up @@ -37,7 +38,8 @@
import net.osmand.plus.settings.bottomsheets.VehicleParametersBottomSheet;
import net.osmand.plus.settings.enums.DrivingRegion;
import net.osmand.plus.settings.fragments.search.SearchablePreferenceDialog;
import net.osmand.plus.settings.fragments.search.SearchablePreferenceDialogProvider;
import net.osmand.plus.settings.fragments.search.ShowableSearchablePreferenceDialog;
import net.osmand.plus.settings.fragments.search.ShowableSearchablePreferenceDialogProvider;
import net.osmand.plus.settings.preferences.ListPreferenceEx;
import net.osmand.plus.settings.preferences.SizePreference;
import net.osmand.plus.settings.vehiclesize.SizeType;
Expand All @@ -53,9 +55,7 @@
import java.util.Map;
import java.util.Optional;

import de.KnollFrank.lib.settingssearch.provider.PreferenceDialogAndSearchableInfoByPreferenceDialogProvider;

public class VehicleParametersFragment extends BaseSettingsFragment implements SearchablePreferenceDialogProvider {
public class VehicleParametersFragment extends BaseSettingsFragment implements ShowableSearchablePreferenceDialogProvider {

public static final String TAG = VehicleParametersFragment.class.getSimpleName();

Expand Down Expand Up @@ -270,41 +270,37 @@ public boolean onPreferenceClick(Preference preference) {
}

@Override
public void onDisplayPreferenceDialog(Preference preference) {
this
.createPreferenceDialog(preference, this, false)
.map(SearchablePreferenceDialogFragmentHolder::searchablePreferenceDialogFragment)
.ifPresentOrElse(
this::show,
() -> super.onDisplayPreferenceDialog(preference));
}

private void show(final SearchablePreferenceDialog dialog) {
final FragmentManager fragmentManager = getFragmentManager();
if (fragmentManager != null) {
dialog.show(fragmentManager, app);
public void onDisplayPreferenceDialog(final Preference preference) {
final boolean shown = showDialogForPreference(preference, this);
if (!shown) {
super.onDisplayPreferenceDialog(preference);
}
}

private Optional<SearchablePreferenceDialogFragmentHolder<?>> createPreferenceDialog(
final Preference preference,
final VehicleParametersFragment target,
final boolean configureSettingsSearch) {
@Override
public Optional<ShowableSearchablePreferenceDialog<?>> getShowableSearchablePreferenceDialog(final Preference preference, final Fragment target) {
final boolean configureSettingsSearch = target == null;
if (preference instanceof SizePreference) {
return Optional.of(
SearchablePreferenceDialogFragmentHolder.of(
new ShowableSearchablePreferenceDialog<>(
VehicleParametersBottomSheet
.createInstance(
preference,
target,
false,
getSelectedAppMode(),
configureSettingsSearch)));
configureSettingsSearch)) {

@Override
protected void show(final SearchablePreferenceDialog searchablePreferenceDialog) {
VehicleParametersFragment.this.show(searchablePreferenceDialog);
}
});
}
if (MOTOR_TYPE_PREF_ID.equals(preference.getKey())) {
final ListPreferenceEx pref = (ListPreferenceEx) preference;
return Optional.of(
SearchablePreferenceDialogFragmentHolder.of(
new ShowableSearchablePreferenceDialog<>(
SimpleSingleSelectionBottomSheet.createInstance(
target,
preference.getKey(),
Expand All @@ -314,36 +310,38 @@ private Optional<SearchablePreferenceDialogFragmentHolder<?>> createPreferenceDi
false,
pref.getEntries(),
pref.getEntryValues(),
pref.getValueIndex())));
pref.getValueIndex())) {

@Override
protected void show(final SearchablePreferenceDialog searchablePreferenceDialog) {
VehicleParametersFragment.this.show(searchablePreferenceDialog);
}
});
}
if (settings.FUEL_TANK_CAPACITY.getId().equals(preference.getKey())) {
return Optional.of(
SearchablePreferenceDialogFragmentHolder.of(
new ShowableSearchablePreferenceDialog<>(
FuelTankCapacityBottomSheet.createInstance(
preference,
target,
false,
getSelectedAppMode(),
configureSettingsSearch)));
configureSettingsSearch)) {

@Override
protected void show(final SearchablePreferenceDialog searchablePreferenceDialog) {
VehicleParametersFragment.this.show(searchablePreferenceDialog);
}
});
}
return Optional.empty();
}

@Override
public Optional<PreferenceDialogAndSearchableInfoByPreferenceDialogProvider<?>> getPreferenceDialogAndSearchableInfoByPreferenceDialogProvider(final Preference preference) {
if (DEFAULT_SPEED.equals(preference.getKey())) {
return Optional.of(
new PreferenceDialogAndSearchableInfoByPreferenceDialogProvider<>(
new Fragment(),
_preferenceDialog -> new VehicleSpeedHelper(app, getSelectedAppMode()).getSearchableInfo()));
private void show(final SearchablePreferenceDialog dialog) {
final FragmentManager fragmentManager = getFragmentManager();
if (fragmentManager != null) {
dialog.show(fragmentManager, app);
}
return this
.createPreferenceDialog(preference, null, true)
.map(SearchablePreferenceDialogFragmentHolder::searchablePreferenceDialogFragment)
.map(preferenceDialog ->
new PreferenceDialogAndSearchableInfoByPreferenceDialogProvider<>(
preferenceDialog,
_preferenceDialog -> preferenceDialog.getSearchableInfo()));
}

@Override
Expand Down

0 comments on commit d018a9d

Please sign in to comment.