Skip to content

Commit

Permalink
refactoring GlobalSettingsFragment
Browse files Browse the repository at this point in the history
  • Loading branch information
KnollFrank committed Dec 17, 2024
1 parent dc5c3ab commit 5f870a9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,14 @@ protected void displayPreferenceDialog(@NonNull String prefKey) {

@Override
public void onDisplayPreferenceDialog(Preference preference) {
if (this instanceof ShowableSearchablePreferenceDialogProvider showableSearchablePreferenceDialogProvider) {
final Optional<ShowableSearchablePreferenceDialog<?>> preferenceDialog = showableSearchablePreferenceDialogProvider.getShowableSearchablePreferenceDialog(preference, this);
if (preferenceDialog.isPresent()) {
preferenceDialog.get().show();
return;
}
}

FragmentManager fragmentManager = getFragmentManager();
if (fragmentManager == null) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,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 @@ -74,38 +73,22 @@ protected void setupPreferences() {
}

@Override
public void onDisplayPreferenceDialog(Preference preference) {
this
.createPreferenceDialog(preference, this)
.ifPresentOrElse(
this::show,
() -> super.onDisplayPreferenceDialog(preference));
}

private Optional<SearchablePreferenceDialog> createPreferenceDialog(final Preference preference,
final GlobalSettingsFragment target) {
public Optional<ShowableSearchablePreferenceDialog<?>> getShowableSearchablePreferenceDialog(final Preference preference, final Fragment target) {
return SEND_ANONYMOUS_DATA_PREF_ID.equals(preference.getKey()) ?
Optional.of(SendAnalyticsBottomSheetDialogFragment.createInstance(target)) :
Optional.of(
new ShowableSearchablePreferenceDialog<>(SendAnalyticsBottomSheetDialogFragment.createInstance(target)) {

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

private void show(final SearchablePreferenceDialog dialog) {
final FragmentManager fragmentManager = getFragmentManager();
if (fragmentManager != null) {
dialog.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()));
}

@Override
protected void onBindPreferenceViewHolder(@NonNull Preference preference,
@NonNull PreferenceViewHolder holder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,6 @@ public void setDescription(int titleResId) {
setDescription(getContext().getString(titleResId));
}

@Override
protected void onClick() {
if (getFragment() == null && getIntent() == null) {
getPreferenceManager().showDialog(this);
}
}

@Override
public String getSearchableInfo() {
return Optionals
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public static Iterable<Object[]> data() {
{"GoodsRestrictionsBottomSheet: goods_delivery_desc_3", R.string.goods_delivery_desc_3},
{"GoodsRestrictionsBottomSheet: goods_delivery_desc_4", R.string.goods_delivery_desc_4},

{"SendAnalyticsBottomSheetDialogFragment: description", R.string.make_osmand_better_descr},

// FK-TODO: enable development plugin for these two test cases
// {"LocationInterpolationBottomSheet: title", R.string.location_interpolation_percent},
// {"LocationInterpolationBottomSheet: description", R.string.location_interpolation_percent_desc}
Expand Down

0 comments on commit 5f870a9

Please sign in to comment.