Skip to content

Commit

Permalink
refactoring GeneralProfileSettingsFragment
Browse files Browse the repository at this point in the history
  • Loading branch information
KnollFrank committed Dec 17, 2024
1 parent 19dbdb2 commit dc5c3ab
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -337,10 +337,10 @@ public final boolean onConfirmPreferenceChange(String prefId, Object newValue, A
@Override
public boolean onPreferenceClick(final Preference preference) {
if (this instanceof final PreferenceFragmentHandlerProvider preferenceFragmentHandlerProvider) {
return preferenceFragmentHandlerProvider
.getPreferenceFragmentHandler(preference)
.map(this::showPreferenceFragment)
.orElse(false);
final Optional<PreferenceFragmentHandler> preferenceFragmentHandler = preferenceFragmentHandlerProvider.getPreferenceFragmentHandler(preference);
if (preferenceFragmentHandler.isPresent()) {
return showPreferenceFragment(preferenceFragmentHandler.get());
}
}
if (this instanceof ShowableSearchablePreferenceDialogProvider showableSearchablePreferenceDialogProvider) {
final Optional<ShowableSearchablePreferenceDialog<?>> preferenceDialog = showableSearchablePreferenceDialogProvider.getShowableSearchablePreferenceDialog(preference, this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
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.fragments.search.ShowableSearchablePreferenceDialog;
import net.osmand.plus.settings.fragments.search.ShowableSearchablePreferenceDialogProvider;
import net.osmand.plus.settings.preferences.ListPreferenceEx;
import net.osmand.plus.settings.preferences.SwitchPreferenceEx;
import net.osmand.plus.utils.UiUtilities;
Expand All @@ -54,9 +55,8 @@
import java.util.List;
import java.util.Optional;

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

public class GeneralProfileSettingsFragment extends BaseSettingsFragment implements SearchablePreferenceDialogProvider, PreferenceFragmentHandlerProvider {
// FK-TODO: combine ShowableSearchablePreferenceDialogProvider and PreferenceFragmentHandlerProvider?
public class GeneralProfileSettingsFragment extends BaseSettingsFragment implements ShowableSearchablePreferenceDialogProvider, PreferenceFragmentHandlerProvider {

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

Expand Down Expand Up @@ -423,29 +423,17 @@ public void onApplyPreferenceChange(String prefId, boolean applyToAllProfiles, O

@Override
public boolean onPreferenceClick(Preference preference) {
{
final Optional<SearchablePreferenceDialog> preferenceDialog =
createPreferenceDialog(
preference,
this);
if (preferenceDialog.isPresent()) {
show(preferenceDialog.get());
return true;
}
String key = preference.getKey();
ApplicationMode appMode = getSelectedAppMode();
if (settings.DRIVING_REGION.getId().equals(key)) {
showDrivingRegionDialog();
return true;
}
{
String key = preference.getKey();
ApplicationMode appMode = getSelectedAppMode();
if (settings.DRIVING_REGION.getId().equals(key)) {
showDrivingRegionDialog();
return true;
}
if (settings.ROTATE_MAP.getId().equals(key)) {
CompassModeDialogController controller = new CompassModeDialogController(app, appMode);
showSingleSelectionDialog(CompassModeDialogController.PROCESS_ID, controller);
controller.setCallback(this);
return true;
}
if (settings.ROTATE_MAP.getId().equals(key)) {
CompassModeDialogController controller = new CompassModeDialogController(app, appMode);
showSingleSelectionDialog(CompassModeDialogController.PROCESS_ID, controller);
controller.setCallback(this);
return true;
}
return super.onPreferenceClick(preference);
}
Expand Down Expand Up @@ -484,34 +472,27 @@ public boolean showPreferenceFragment(final PreferenceFragmentCompat preferenceF
return Optional.empty();
}

private Optional<SearchablePreferenceDialog> createPreferenceDialog(final Preference preference,
final GeneralProfileSettingsFragment target) {
@Override
public Optional<ShowableSearchablePreferenceDialog<?>> getShowableSearchablePreferenceDialog(final Preference preference, final Fragment target) {
if (settings.PRECISE_DISTANCE_NUMBERS.getId().equals(preference.getKey())) {
return Optional.of(
DistanceDuringNavigationBottomSheet.createInstance(
preference,
target,
getSelectedAppMode(),
false));
}
return Optional.empty();
}
new ShowableSearchablePreferenceDialog<>(
DistanceDuringNavigationBottomSheet.createInstance(
preference,
target,
getSelectedAppMode(),
false)) {

private void show(final SearchablePreferenceDialog dialog) {
final FragmentManager fragmentManager = getFragmentManager();
if (fragmentManager != null) {
dialog.show(fragmentManager, app);
@Override
protected void show(final SearchablePreferenceDialog searchablePreferenceDialog) {
final FragmentManager fragmentManager = getFragmentManager();
if (fragmentManager != null) {
searchablePreferenceDialog.show(fragmentManager, app);
}
}
});
}
}

@Override
public Optional<PreferenceDialogAndSearchableInfoByPreferenceDialogProvider<?>> getPreferenceDialogAndSearchableInfoByPreferenceDialogProvider(final Preference preference) {
return this
.createPreferenceDialog(preference, null)
.map(preferenceDialog ->
new PreferenceDialogAndSearchableInfoByPreferenceDialogProvider<>(
(Fragment) preferenceDialog,
_preferenceDialog -> preferenceDialog.getSearchableInfo()));
return Optional.empty();
}

private void updateDialogControllerCallbacks() {
Expand Down

0 comments on commit dc5c3ab

Please sign in to comment.