Skip to content

Commit

Permalink
refactoring GeneralProfileSettingsFragment
Browse files Browse the repository at this point in the history
  • Loading branch information
KnollFrank committed Dec 19, 2024
1 parent d018a9d commit 49629d3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static net.osmand.plus.settings.bottomsheets.DistanceDuringNavigationBottomSheet.DistanceDuringNavigationMode;
import static net.osmand.plus.settings.fragments.SettingsScreenType.EXTERNAL_INPUT_DEVICE;
import static net.osmand.plus.settings.fragments.search.PreferenceDialogs.showDialogForPreference;

import android.content.Context;
import android.content.pm.ActivityInfo;
Expand Down Expand Up @@ -41,7 +42,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 +56,7 @@
import java.util.List;
import java.util.Optional;

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

public class GeneralProfileSettingsFragment extends BaseSettingsFragment implements SearchablePreferenceDialogProvider, PreferenceFragmentHandlerProvider {
public class GeneralProfileSettingsFragment extends BaseSettingsFragment implements PreferenceFragmentHandlerProvider, ShowableSearchablePreferenceDialogProvider {

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

Expand Down Expand Up @@ -422,32 +422,19 @@ public void onApplyPreferenceChange(String prefId, boolean applyToAllProfiles, O
}

@Override
public boolean onPreferenceClick(Preference preference) {
{
final Optional<SearchablePreferenceDialog> preferenceDialog =
this
.createPreferenceDialog(
preference,
this)
.map(SearchablePreferenceDialogFragmentHolder::searchablePreferenceDialogFragment);
if (preferenceDialog.isPresent()) {
show(preferenceDialog.get());
return true;
}
public boolean onPreferenceClick(final Preference preference) {
if (showDialogForPreference(preference, this)) {
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.DRIVING_REGION.getId().equals(preference.getKey())) {
showDrivingRegionDialog();
return true;
}
if (settings.ROTATE_MAP.getId().equals(preference.getKey())) {
CompassModeDialogController controller = new CompassModeDialogController(app, getSelectedAppMode());
showSingleSelectionDialog(CompassModeDialogController.PROCESS_ID, controller);
controller.setCallback(this);
return true;
}
return super.onPreferenceClick(preference);
}
Expand Down Expand Up @@ -486,17 +473,24 @@ public boolean showPreferenceFragment(final PreferenceFragmentCompat preferenceF
return Optional.empty();
}

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

@Override
protected void show(final SearchablePreferenceDialog searchablePreferenceDialog) {
GeneralProfileSettingsFragment.this.show(searchablePreferenceDialog);
}
});
}
return Optional.empty();
}
Expand All @@ -508,17 +502,6 @@ private void show(final SearchablePreferenceDialog dialog) {
}
}

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

private void updateDialogControllerCallbacks() {
IDialogController controller;
DialogManager dialogManager = app.getDialogManager();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

// FK-TODO: remove
public record SearchablePreferenceDialogFragmentHolder<T extends Fragment & SearchablePreferenceDialog>(
T searchablePreferenceDialogFragment) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ public static Iterable<Object[]> data() {
{"ResetProfilePrefsBottomSheet: description", searchQuery(R.string.reset_all_profile_settings_descr)},
{"ResetProfilePrefsBottomSheet: reset_confirmation_descr", searchQuery("Tapping Reset discards all your changes")},

{"GeneralProfileSettingsFragment", searchQuery(R.string.distance_during_navigation)}
{"GeneralProfileSettingsFragment", searchQuery(R.string.distance_during_navigation)},

{"DistanceDuringNavigationBottomSheet: description", searchQuery("Choose how distance information is displayed in navigation widgets")}
});
}

Expand Down

0 comments on commit 49629d3

Please sign in to comment.