Skip to content

Commit

Permalink
refactoring ProfileAppearanceFragment
Browse files Browse the repository at this point in the history
  • Loading branch information
KnollFrank committed Dec 19, 2024
1 parent 49629d3 commit a1deaaf
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentManager;

import net.osmand.plus.OsmandApplication;
import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem;
import net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithCompoundButton;
import net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithCompoundButton.Builder;
import net.osmand.plus.base.bottomsheetmenu.simpleitems.DividerItem;
import net.osmand.plus.base.bottomsheetmenu.simpleitems.LongDescriptionItem;
import net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem;
import net.osmand.plus.base.dialog.data.DisplayItem;
import net.osmand.plus.settings.fragments.search.SearchablePreferenceDialog;
import net.osmand.plus.utils.UiUtilities;

import java.util.List;
Expand All @@ -28,7 +30,7 @@
* When choosing one of the options, the selected option is passed to the controller
* and dialog automatically closed without the need for confirmation by the user.
*/
public class CustomizableSingleSelectionBottomSheet extends CustomizableBottomSheet {
public class CustomizableSingleSelectionBottomSheet extends CustomizableBottomSheet implements SearchablePreferenceDialog {

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

Expand Down Expand Up @@ -88,25 +90,24 @@ protected boolean hideButtonsContainer() {
return show == null || !show;
}

public static boolean showInstance(@NonNull FragmentManager fragmentManager,
@NonNull String processId, boolean usedOnMap) {
return createInstance(processId, usedOnMap).show(fragmentManager);
}

public static @NonNull CustomizableSingleSelectionBottomSheet createInstance(final @NonNull String processId, final boolean usedOnMap) {
final CustomizableSingleSelectionBottomSheet fragment = new CustomizableSingleSelectionBottomSheet();
fragment.setProcessId(processId);
fragment.setUsedOnMap(usedOnMap);
return fragment;
}

public boolean show(@NonNull final FragmentManager fragmentManager) {
try {
show(fragmentManager, TAG);
return true;
} catch (final RuntimeException e) {
return false;
}
@Override
public void show(final FragmentManager fragmentManager, final OsmandApplication app) {
show(fragmentManager, TAG);
}

@Override
public String getSearchableInfo() {
return CustomizableSingleSelectionBottomSheetSearchableInfoProvider.getSearchableInfo(
getTitle(),
getDescription(),
getDisplayItems());
}

private String getTitle() {
Expand All @@ -120,11 +121,4 @@ private String getDescription() {
private List<DisplayItem> getDisplayItems() {
return displayData.getDisplayItems();
}

public String getSearchableInfo() {
return CustomizableSingleSelectionBottomSheetSearchableInfoProvider.getSearchableInfo(
getTitle(),
getDescription(),
getDisplayItems());
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package net.osmand.plus.settings.controllers;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentManager;

import net.osmand.plus.OsmandApplication;
import net.osmand.plus.activities.MapActivity;
Expand Down Expand Up @@ -39,7 +38,8 @@ public static void showDialog(@NonNull MapActivity mapActivity) {
DialogManager dialogManager = app.getDialogManager();
dialogManager.register(PROCESS_ID, controller);

FragmentManager manager = mapActivity.getSupportFragmentManager();
CustomizableSingleSelectionBottomSheet.showInstance(manager, PROCESS_ID, true);
CustomizableSingleSelectionBottomSheet
.createInstance(PROCESS_ID, true)
.show(mapActivity.getSupportFragmentManager(), app);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentManager;

import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
Expand All @@ -15,8 +14,8 @@
import net.osmand.plus.base.dialog.DialogManager;
import net.osmand.plus.base.dialog.data.DisplayData;
import net.osmand.plus.base.dialog.data.DisplayItem;
import net.osmand.plus.base.dialog.interfaces.controller.IDisplayDataProvider;
import net.osmand.plus.base.dialog.interfaces.controller.IDialogItemSelected;
import net.osmand.plus.base.dialog.interfaces.controller.IDisplayDataProvider;
import net.osmand.plus.settings.backend.ApplicationMode;
import net.osmand.plus.settings.backend.OsmandSettings;
import net.osmand.plus.settings.bottomsheets.CustomizableSingleSelectionBottomSheet;
Expand All @@ -33,13 +32,14 @@ public class MapFocusDialogController extends BaseDialogController
private final OsmandSettings settings;

public MapFocusDialogController(@NonNull OsmandApplication app,
@NonNull ApplicationMode appMode) {
@NonNull ApplicationMode appMode) {
super(app);
this.appMode = appMode;
this.settings = app.getSettings();
}

@NonNull @Override
@NonNull
@Override
public String getProcessId() {
return PROCESS_ID;
}
Expand Down Expand Up @@ -96,7 +96,8 @@ public static void showDialog(@NonNull MapActivity mapActivity, @NonNull Applica
DialogManager dialogManager = app.getDialogManager();
dialogManager.register(PROCESS_ID, controller);

FragmentManager manager = mapActivity.getSupportFragmentManager();
CustomizableSingleSelectionBottomSheet.showInstance(manager, PROCESS_ID, true);
CustomizableSingleSelectionBottomSheet
.createInstance(PROCESS_ID, true)
.show(mapActivity.getSupportFragmentManager(), app);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,9 @@ protected void showSingleSelectionDialog(@NonNull String processId,
DialogManager dialogManager = app.getDialogManager();
dialogManager.register(processId, controller);
FragmentManager fm = activity.getSupportFragmentManager();
CustomizableSingleSelectionBottomSheet.showInstance(fm, processId, false);
CustomizableSingleSelectionBottomSheet
.createInstance(processId, false)
.show(fm, app);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.osmand.plus.settings.fragments.profileappearance;

import static net.osmand.aidlapi.OsmAndCustomizationConstants.DRAWER_SETTINGS_ID;
import static net.osmand.plus.settings.fragments.search.PreferenceDialogs.showDialogForPreference;
import static net.osmand.plus.utils.ColorUtilities.getListBgColorId;

import android.annotation.SuppressLint;
Expand Down Expand Up @@ -38,10 +39,11 @@
import net.osmand.plus.card.icon.IconsPaletteCard;
import net.osmand.plus.helpers.AndroidUiHelper;
import net.osmand.plus.routepreparationmenu.cards.BaseCard;
import net.osmand.plus.settings.bottomsheets.CustomizableSingleSelectionBottomSheet;
import net.osmand.plus.settings.fragments.BaseSettingsFragment;
import net.osmand.plus.settings.fragments.SettingsScreenType;
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.UiUtilities;
Expand All @@ -54,9 +56,7 @@

import java.util.Optional;

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

public class ProfileAppearanceFragment extends BaseSettingsFragment implements IProfileAppearanceScreen, SearchablePreferenceDialogProvider {
public class ProfileAppearanceFragment extends BaseSettingsFragment implements IProfileAppearanceScreen, ShowableSearchablePreferenceDialogProvider {

private static final Log LOG = PlatformUtil.getLog(ProfileAppearanceFragment.class);

Expand Down Expand Up @@ -230,48 +230,54 @@ public void afterTextChanged(Editable s) {
}

@Override
public boolean onPreferenceClick(Preference preference) {
MapActivity mapActivity = getMapActivity();
if (mapActivity != null) {
this
.createPreferenceDialog(preference)
.ifPresent(dialog -> dialog.show(mapActivity.getSupportFragmentManager()));
public boolean onPreferenceClick(final Preference preference) {
if (showDialogForPreference(preference, this)) {
return true;
}
return super.onPreferenceClick(preference);
}

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

private Optional<CustomizableSingleSelectionBottomSheet> createPreferenceDialog(final Preference preference) {
public Optional<ShowableSearchablePreferenceDialog<?>> getShowableSearchablePreferenceDialog(final Preference preference, final Fragment target) {
if (settings.VIEW_ANGLE_VISIBILITY.getId().equals(preference.getKey())) {
return Optional.of(
screenController
.getProfileOptionController()
.createDialog(
app.getString(R.string.view_angle),
app.getString(R.string.view_angle_description),
settings.VIEW_ANGLE_VISIBILITY));
new ShowableSearchablePreferenceDialog<>(
screenController
.getProfileOptionController()
.createDialog(
app.getString(R.string.view_angle),
app.getString(R.string.view_angle_description),
settings.VIEW_ANGLE_VISIBILITY)) {

@Override
protected void show(final SearchablePreferenceDialog searchablePreferenceDialog) {
ProfileAppearanceFragment.this.show(searchablePreferenceDialog);
}
});
}
if (settings.LOCATION_RADIUS_VISIBILITY.getId().equals(preference.getKey())) {
return Optional.of(
screenController
.getProfileOptionController()
.createDialog(
app.getString(R.string.location_radius),
app.getString(R.string.location_radius_description),
settings.LOCATION_RADIUS_VISIBILITY));
new ShowableSearchablePreferenceDialog<>(
screenController
.getProfileOptionController()
.createDialog(
app.getString(R.string.location_radius),
app.getString(R.string.location_radius_description),
settings.LOCATION_RADIUS_VISIBILITY)) {

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

private void show(final SearchablePreferenceDialog searchablePreferenceDialog) {
searchablePreferenceDialog.show(requireActivity().getSupportFragmentManager(), app);
}

private void bindCard(@NonNull PreferenceViewHolder holder, @NonNull BaseCard card) {
ViewGroup container = (ViewGroup) holder.itemView;
container.removeAllViews();
Expand Down

0 comments on commit a1deaaf

Please sign in to comment.