Skip to content

Commit

Permalink
refactoring GlobalSettingsFragment
Browse files Browse the repository at this point in the history
  • Loading branch information
KnollFrank committed Dec 18, 2024
1 parent e929d75 commit d979cf4
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 36 deletions.
10 changes: 7 additions & 3 deletions OsmAnd/src/net/osmand/plus/activities/MapActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,9 @@ protected void onResume() {
if (showWelcomeScreen && FirstUsageWizardFragment.showFragment(this)) {
SecondSplashScreenFragment.SHOW = false;
} else if (SendAnalyticsBottomSheetDialogFragment.shouldShowDialog(app)) {
SendAnalyticsBottomSheetDialogFragment.showInstance(app, fragmentManager, null);
SendAnalyticsBottomSheetDialogFragment
.createInstance(null)
.show(fragmentManager, app);
}
if (fragmentsHelper.isFirstScreenShowing() && (!settings.SHOW_OSMAND_WELCOME_SCREEN.get() || !showOsmAndWelcomeScreen)) {
fragmentsHelper.disableFirstUsageFragment();
Expand Down Expand Up @@ -1353,7 +1355,9 @@ public MapContextMenu getContextMenu() {
}

@NonNull
public MapRouteInfoMenu getMapRouteInfoMenu() { return mapRouteInfoMenu; }
public MapRouteInfoMenu getMapRouteInfoMenu() {
return mapRouteInfoMenu;
}

@NonNull
public TrackDetailsMenu getTrackDetailsMenu() {
Expand Down Expand Up @@ -1579,7 +1583,7 @@ public void newRouteIsCalculated(boolean newRoute, ValueHolder<Boolean> showToas
sim.startStopRouteAnimation(this);
}
}
for (OsmandPlugin plugin: PluginsHelper.getEnabledPlugins()) {
for (OsmandPlugin plugin : PluginsHelper.getEnabledPlugins()) {
plugin.newRouteIsCalculated(newRoute);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,6 @@ public static boolean shouldShowDialog(@NonNull OsmandApplication app) {
return false;
}

public static void showInstance(@NonNull OsmandApplication app, @NonNull FragmentManager fm, @Nullable Fragment target) {
createInstance(target).show(fm, app);
}

public static @NonNull SendAnalyticsBottomSheetDialogFragment createInstance(final @Nullable Fragment target) {
final SendAnalyticsBottomSheetDialogFragment fragment = new SendAnalyticsBottomSheetDialogFragment();
fragment.setTargetFragment(target, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static net.osmand.plus.profiles.SelectProfileBottomSheet.PROFILE_KEY_ARG;
import static net.osmand.plus.profiles.SelectProfileBottomSheet.USE_LAST_PROFILE_ARG;
import static net.osmand.plus.settings.fragments.search.PreferenceDialogs.showDialogForPreference;

import android.app.Activity;
import android.app.backup.BackupManager;
Expand All @@ -11,6 +12,7 @@
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.preference.Preference;
import androidx.preference.PreferenceViewHolder;
Expand All @@ -33,18 +35,17 @@
import net.osmand.plus.settings.datastorage.item.StorageItem;
import net.osmand.plus.settings.enums.LocationSource;
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 java.util.Map;
import java.util.Optional;

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


public class GlobalSettingsFragment extends BaseSettingsFragment
implements OnSendAnalyticsPrefsUpdate, OnSelectProfileCallback, SearchablePreferenceDialogProvider {
implements OnSendAnalyticsPrefsUpdate, OnSelectProfileCallback, ShowableSearchablePreferenceDialogProvider {

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

Expand Down Expand Up @@ -73,20 +74,24 @@ protected void setupPreferences() {
}

@Override
public void onDisplayPreferenceDialog(Preference preference) {
this
.createPreferenceDialog(preference, this)
.map(SearchablePreferenceDialogFragmentHolder::searchablePreferenceDialogFragment)
.ifPresentOrElse(
this::show,
() -> super.onDisplayPreferenceDialog(preference));
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 GlobalSettingsFragment target) {
@Override
public Optional<ShowableSearchablePreferenceDialog<?>> getShowableSearchablePreferenceDialog(final Preference preference, final Fragment target) {
return SEND_ANONYMOUS_DATA_PREF_ID.equals(preference.getKey()) ?
Optional.of(SearchablePreferenceDialogFragmentHolder.of(SendAnalyticsBottomSheetDialogFragment.createInstance(target))) :
Optional.of(
new ShowableSearchablePreferenceDialog<>(SendAnalyticsBottomSheetDialogFragment.createInstance(target)) {

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

Expand All @@ -97,17 +102,6 @@ private void show(final SearchablePreferenceDialog dialog) {
}
}

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

@Override
protected void onBindPreferenceViewHolder(@NonNull Preference preference,
@NonNull PreferenceViewHolder holder) {
Expand Down Expand Up @@ -156,7 +150,9 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
if (enabled) {
FragmentManager fragmentManager = getFragmentManager();
if (fragmentManager != null) {
SendAnalyticsBottomSheetDialogFragment.showInstance(app, fragmentManager, this);
SendAnalyticsBottomSheetDialogFragment
.createInstance(this)
.show(fragmentManager, app);
}
} else {
settings.SEND_ANONYMOUS_MAP_DOWNLOADS_DATA.set(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ 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);
final T preferenceDialogProviderAndTarget) {
final Optional<ShowableSearchablePreferenceDialog<?>> preferenceDialog = preferenceDialogProviderAndTarget.getShowableSearchablePreferenceDialog(preference, preferenceDialogProviderAndTarget);
if (preferenceDialog.isPresent()) {
preferenceDialog.get().show();
return true;
Expand Down

0 comments on commit d979cf4

Please sign in to comment.