Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
KnollFrank committed Dec 18, 2024
1 parent ee4d38c commit b8d901b
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
import net.osmand.plus.settings.enums.RoutingType;
import net.osmand.plus.settings.fragments.search.PreferenceFragmentHandler;
import net.osmand.plus.settings.fragments.search.PreferenceFragmentHandlerProvider;
import net.osmand.plus.settings.fragments.search.SearchablePreferenceDialog;
import net.osmand.plus.settings.fragments.search.SearchablePreferenceDialogProvider;
import net.osmand.plus.settings.preferences.ListParameters;
import net.osmand.plus.settings.preferences.ListPreferenceEx;
Expand Down Expand Up @@ -639,29 +638,32 @@ public boolean showPreferenceFragment(final PreferenceFragmentCompat preferenceF
public Optional<PreferenceDialogAndSearchableInfoByPreferenceDialogProvider<?>> getPreferenceDialogAndSearchableInfoByPreferenceDialogProvider(final Preference preference) {
return this
.getSearchablePreferenceDialog(preference)
.map(SearchablePreferenceDialogFragmentHolder::searchablePreferenceDialogFragment)
.map(preferenceDialog ->
new PreferenceDialogAndSearchableInfoByPreferenceDialogProvider<>(
(Fragment) preferenceDialog,
preferenceDialog,
_preferenceDialog -> preferenceDialog.getSearchableInfo()));
}

private Optional<SearchablePreferenceDialog> getSearchablePreferenceDialog(final Preference preference) {
private Optional<SearchablePreferenceDialogFragmentHolder<?>> getSearchablePreferenceDialog(final Preference preference) {
if (settings.ROUTE_RECALCULATION_DISTANCE.getId().equals(preference.getKey())) {
return Optional.of(
RecalculateRouteInDeviationBottomSheet.createInstance(
preference,
null,
false,
getSelectedAppMode()));
SearchablePreferenceDialogFragmentHolder.of(
RecalculateRouteInDeviationBottomSheet.createInstance(
preference,
null,
false,
getSelectedAppMode())));
}
if (GOODS_RESTRICTIONS_PREFERENCE.equals(preference.getKey())) {
return Optional.of(
GoodsRestrictionsBottomSheet.createInstance(
null,
preference,
getSelectedAppMode(),
false,
getGoodsRestrictionPreference().getModeValue(getSelectedAppMode())));
SearchablePreferenceDialogFragmentHolder.of(
GoodsRestrictionsBottomSheet.createInstance(
null,
preference,
getSelectedAppMode(),
false,
getGoodsRestrictionPreference().getModeValue(getSelectedAppMode()))));
}
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

import net.osmand.plus.settings.fragments.search.SearchablePreferenceDialog;

// FK-TODO: use everywhere like in ConfigureProfileFragment
record SearchablePreferenceDialogFragmentHolder<T extends Fragment & SearchablePreferenceDialog>(
public record SearchablePreferenceDialogFragmentHolder<T extends Fragment & SearchablePreferenceDialog>(
T searchablePreferenceDialogFragment) {

public static <T extends Fragment & SearchablePreferenceDialog> SearchablePreferenceDialogFragmentHolder<T> of(final T searchablePreferenceDialogFragment) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ public boolean onPreferenceClick(Preference preference) {
public void onDisplayPreferenceDialog(Preference preference) {
this
.createPreferenceDialog(preference, this, false)
.map(SearchablePreferenceDialogFragmentHolder::searchablePreferenceDialogFragment)
.ifPresentOrElse(
this::show,
() -> super.onDisplayPreferenceDialog(preference));
Expand All @@ -285,42 +286,45 @@ private void show(final SearchablePreferenceDialog dialog) {
}
}

private Optional<SearchablePreferenceDialog> createPreferenceDialog(
private Optional<SearchablePreferenceDialogFragmentHolder<?>> createPreferenceDialog(
final Preference preference,
final VehicleParametersFragment target,
final boolean configureSettingsSearch) {
if (preference instanceof SizePreference) {
return Optional.of(
VehicleParametersBottomSheet
.createInstance(
preference,
target,
false,
getSelectedAppMode(),
configureSettingsSearch));
SearchablePreferenceDialogFragmentHolder.of(
VehicleParametersBottomSheet
.createInstance(
preference,
target,
false,
getSelectedAppMode(),
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()));
SearchablePreferenceDialogFragmentHolder.of(
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));
SearchablePreferenceDialogFragmentHolder.of(
FuelTankCapacityBottomSheet.createInstance(
preference,
target,
false,
getSelectedAppMode(),
configureSettingsSearch)));
}
return Optional.empty();
}
Expand All @@ -335,9 +339,10 @@ public Optional<PreferenceDialogAndSearchableInfoByPreferenceDialogProvider<?>>
}
return this
.createPreferenceDialog(preference, null, true)
.map(SearchablePreferenceDialogFragmentHolder::searchablePreferenceDialogFragment)
.map(preferenceDialog ->
new PreferenceDialogAndSearchableInfoByPreferenceDialogProvider<>(
(Fragment) preferenceDialog,
preferenceDialog,
_preferenceDialog -> preferenceDialog.getSearchableInfo()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import androidx.annotation.NonNull;
import androidx.appcompat.widget.SwitchCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.preference.Preference;
import androidx.preference.PreferenceViewHolder;
Expand All @@ -28,6 +27,7 @@
import net.osmand.plus.settings.bottomsheets.SpeedLimitBottomSheet;
import net.osmand.plus.settings.fragments.ApplyQueryType;
import net.osmand.plus.settings.fragments.BaseSettingsFragment;
import net.osmand.plus.settings.fragments.SearchablePreferenceDialogFragmentHolder;
import net.osmand.plus.settings.fragments.search.SearchablePreferenceDialog;
import net.osmand.plus.settings.fragments.search.SearchablePreferenceDialogProvider;
import net.osmand.plus.settings.preferences.ListPreferenceEx;
Expand Down Expand Up @@ -257,6 +257,7 @@ public boolean onPreferenceClick(Preference preference) {
public void onDisplayPreferenceDialog(Preference preference) {
this
.createSearchablePreferenceDialog(preference, this)
.map(SearchablePreferenceDialogFragmentHolder::searchablePreferenceDialogFragment)
.ifPresentOrElse(
this::show,
() -> {
Expand All @@ -268,16 +269,17 @@ public void onDisplayPreferenceDialog(Preference preference) {
});
}

private Optional<SearchablePreferenceDialog> createSearchablePreferenceDialog(
private Optional<SearchablePreferenceDialogFragmentHolder<?>> createSearchablePreferenceDialog(
final Preference preference,
final VoiceAnnouncesFragment target) {
return settings.ARRIVAL_DISTANCE_FACTOR.getId().equals(preference.getKey()) ?
Optional.of(
AnnouncementTimeBottomSheet.createInstance(
preference,
target,
getSelectedAppMode(),
false)) :
SearchablePreferenceDialogFragmentHolder.of(
AnnouncementTimeBottomSheet.createInstance(
preference,
target,
getSelectedAppMode(),
false))) :
Optional.empty();
}

Expand Down Expand Up @@ -307,9 +309,10 @@ private void setupSpeakCamerasPref() {
public Optional<PreferenceDialogAndSearchableInfoByPreferenceDialogProvider<?>> getPreferenceDialogAndSearchableInfoByPreferenceDialogProvider(final Preference preference) {
return this
.createSearchablePreferenceDialog(preference, null)
.map(SearchablePreferenceDialogFragmentHolder::searchablePreferenceDialogFragment)
.map(preferenceDialog ->
new PreferenceDialogAndSearchableInfoByPreferenceDialogProvider<>(
(Fragment) preferenceDialog,
preferenceDialog,
_preferenceDialog -> preferenceDialog.getSearchableInfo()));
}
}

0 comments on commit b8d901b

Please sign in to comment.