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 4aae3fb commit 673c901
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.osmand.plus.plugins.development;

import static net.osmand.plus.settings.bottomsheets.ConfirmationBottomSheet.showResetSettingsDialog;
import static net.osmand.plus.settings.fragments.search.PreferenceDialogs.showDialogForPreference;
import static net.osmand.plus.simulation.OsmAndLocationSimulation.LocationSimulationListener;

import android.content.Intent;
Expand Down Expand Up @@ -318,7 +319,7 @@ private void setupResetToDefaultButton() {
@Override
public boolean onPreferenceClick(Preference preference) {
final String prefId = preference.getKey();
if (showDialogForPreference(preference)) {
if (showDialogForPreference(preference, this)) {
return true;
} else if (SIMULATE_INITIAL_STARTUP.equals(prefId)) {
app.getAppInitializer().resetFirstTimeRun();
Expand Down Expand Up @@ -348,15 +349,6 @@ public boolean onPreferenceClick(Preference preference) {
return super.onPreferenceClick(preference);
}

private boolean showDialogForPreference(final Preference preference) {
final Optional<ShowableSearchablePreferenceDialog<?>> preferenceDialog = getShowableSearchablePreferenceDialog(preference, this);
if (preferenceDialog.isPresent()) {
preferenceDialog.get().show();
return true;
}
return false;
}

@Override
public Optional<ShowableSearchablePreferenceDialog<?>> getShowableSearchablePreferenceDialog(final Preference preference, final Fragment target) {
if (SIMULATE_YOUR_LOCATION.equals(preference.getKey())) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.osmand.plus.settings.fragments;

import static net.osmand.plus.settings.fragments.search.PreferenceDialogs.showDialogForPreference;
import static net.osmand.plus.settings.fragments.search.PreferenceMarker.markPreferenceAsConnectedToPlugin;
import static net.osmand.plus.utils.UiUtilities.CompoundButtonType.TOOLBAR;

Expand All @@ -21,6 +22,7 @@
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.SwitchCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.preference.Preference;
Expand Down Expand Up @@ -50,7 +52,9 @@
import net.osmand.plus.settings.bottomsheets.ResetProfilePrefsBottomSheet.ResetAppModePrefsListener;
import net.osmand.plus.settings.fragments.configureitems.ConfigureMenuRootFragment;
import net.osmand.plus.settings.fragments.profileappearance.ProfileAppearanceFragment;
import net.osmand.plus.settings.fragments.search.SearchablePreferenceDialogProvider;
import net.osmand.plus.settings.fragments.search.SearchablePreferenceDialog;
import net.osmand.plus.settings.fragments.search.ShowableSearchablePreferenceDialog;
import net.osmand.plus.settings.fragments.search.ShowableSearchablePreferenceDialogProvider;
import net.osmand.plus.utils.AndroidUtils;
import net.osmand.plus.utils.ColorUtilities;
import net.osmand.plus.utils.FileUtils;
Expand All @@ -66,9 +70,7 @@
import java.util.List;
import java.util.Optional;

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

public class ConfigureProfileFragment extends BaseSettingsFragment implements CopyAppModePrefsListener, ResetAppModePrefsListener, PluginStateListener, SearchablePreferenceDialogProvider {
public class ConfigureProfileFragment extends BaseSettingsFragment implements CopyAppModePrefsListener, ResetAppModePrefsListener, PluginStateListener, ShowableSearchablePreferenceDialogProvider {

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

Expand Down Expand Up @@ -438,24 +440,19 @@ private void setupUiCustomizationPref() {
}

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

private Optional<SearchablePreferenceDialogFragmentHolder<?>> createSearchablePreferenceDialogFragment(
final Preference preference,
final ConfigureProfileFragment target) {
public Optional<ShowableSearchablePreferenceDialog<?>> getShowableSearchablePreferenceDialog(final Preference preference, final Fragment target) {
if (RESET_TO_DEFAULT.equals(preference.getKey())) {
return Optional.of(
SearchablePreferenceDialogFragmentHolder.of(
new ShowableSearchablePreferenceDialog<>(
ResetProfilePrefsBottomSheet.createInstance(
getSelectedAppMode(),
target)));
target)) {

@Override
protected void show(final SearchablePreferenceDialog searchablePreferenceDialog) {
searchablePreferenceDialog.show(getFragmentManager(), app);
}
});
}
return Optional.empty();
}
Expand All @@ -465,10 +462,7 @@ public boolean onPreferenceClick(Preference preference) {
final MapActivity mapActivity = getMapActivity();
final FragmentManager fragmentManager = getFragmentManager();
if (mapActivity != null && fragmentManager != null) {
final Optional<SearchablePreferenceDialogFragmentHolder<?>> searchablePreferenceDialogFragment =
createSearchablePreferenceDialogFragment(preference, this);
if (searchablePreferenceDialogFragment.isPresent()) {
searchablePreferenceDialogFragment.get().searchablePreferenceDialogFragment().show(fragmentManager, app);
if (showDialogForPreference(preference, this)) {
return true;
}
final String prefId = preference.getKey();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package net.osmand.plus.settings.fragments.search;

import androidx.fragment.app.Fragment;
import androidx.preference.Preference;

import java.util.Optional;

public class PreferenceDialogs {

public static <T extends Fragment & ShowableSearchablePreferenceDialogProvider> boolean showDialogForPreference(
final Preference preference,
final T target) {
final Optional<ShowableSearchablePreferenceDialog<?>> preferenceDialog = target.getShowableSearchablePreferenceDialog(preference, target);
if (preferenceDialog.isPresent()) {
preferenceDialog.get().show();
return true;
}
return false;
}
}

0 comments on commit 673c901

Please sign in to comment.