Search in sources :

Example 1 with OnConfirmPreferenceChange

use of net.osmand.plus.settings.fragments.OnConfirmPreferenceChange in project Osmand by osmandapp.

the class BooleanPreferenceBottomSheet method createMenuItems.

@Override
public void createMenuItems(Bundle savedInstanceState) {
    final OsmandApplication app = getMyApplication();
    if (app == null) {
        return;
    }
    final SwitchPreferenceEx switchPreference = getSwitchPreferenceEx();
    if (switchPreference == null) {
        return;
    }
    OsmandPreference preference = app.getSettings().getPreference(switchPreference.getKey());
    if (!(preference instanceof BooleanPreference)) {
        return;
    }
    Context themedCtx = UiUtilities.getThemedContext(app, nightMode);
    String title = switchPreference.getTitle().toString();
    items.add(new TitleItem(title));
    final BooleanPreference pref = (BooleanPreference) preference;
    CharSequence summaryOn = switchPreference.getSummaryOn();
    CharSequence summaryOff = switchPreference.getSummaryOff();
    final String on = summaryOn == null || summaryOn.toString().isEmpty() ? getString(R.string.shared_string_enabled) : summaryOn.toString();
    final String off = summaryOff == null || summaryOff.toString().isEmpty() ? getString(R.string.shared_string_disabled) : summaryOff.toString();
    final int activeColor = AndroidUtils.resolveAttribute(themedCtx, R.attr.active_color_basic);
    final int disabledColor = AndroidUtils.resolveAttribute(themedCtx, android.R.attr.textColorSecondary);
    boolean checked = switchPreference.isChecked();
    final BottomSheetItemWithCompoundButton[] preferenceBtn = new BottomSheetItemWithCompoundButton[1];
    preferenceBtn[0] = (BottomSheetItemWithCompoundButton) new BottomSheetItemWithCompoundButton.Builder().setChecked(checked).setTitle(checked ? on : off).setTitleColorId(checked ? activeColor : disabledColor).setCustomView(getCustomButtonView(app, getAppMode(), checked, nightMode)).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            boolean newValue = !switchPreference.isChecked();
            Fragment targetFragment = getTargetFragment();
            if (targetFragment instanceof OnConfirmPreferenceChange) {
                ApplyQueryType applyQueryType = getApplyQueryType();
                if (applyQueryType == ApplyQueryType.SNACK_BAR) {
                    applyQueryType = ApplyQueryType.NONE;
                }
                OnConfirmPreferenceChange confirmationInterface = (OnConfirmPreferenceChange) targetFragment;
                if (confirmationInterface.onConfirmPreferenceChange(switchPreference.getKey(), newValue, applyQueryType)) {
                    switchPreference.setChecked(newValue);
                    preferenceBtn[0].setTitle(newValue ? on : off);
                    preferenceBtn[0].setChecked(newValue);
                    preferenceBtn[0].setTitleColorId(newValue ? activeColor : disabledColor);
                    updateCustomButtonView(app, getAppMode(), v, newValue, nightMode);
                    if (targetFragment instanceof OnPreferenceChanged) {
                        ((OnPreferenceChanged) targetFragment).onPreferenceChanged(switchPreference.getKey());
                    }
                }
            }
        }
    }).create();
    if (isProfileDependent()) {
        preferenceBtn[0].setCompoundButtonColor(getAppMode().getProfileColor(nightMode));
    }
    items.add(preferenceBtn[0]);
    String description = switchPreference.getDescription();
    if (description != null) {
        BaseBottomSheetItem preferenceDescription = new BottomSheetItemWithDescription.Builder().setDescription(description).setLayoutId(R.layout.bottom_sheet_item_descr).create();
        items.add(preferenceDescription);
    }
}
Also used : Context(android.content.Context) BaseBottomSheetItem(net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem) OsmandApplication(net.osmand.plus.OsmandApplication) OsmandPreference(net.osmand.plus.settings.backend.preferences.OsmandPreference) TitleItem(net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem) SwitchPreferenceEx(net.osmand.plus.settings.preferences.SwitchPreferenceEx) OnConfirmPreferenceChange(net.osmand.plus.settings.fragments.OnConfirmPreferenceChange) View(android.view.View) Fragment(androidx.fragment.app.Fragment) BottomSheetItemWithDescription(net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithDescription) BooleanPreference(net.osmand.plus.settings.backend.preferences.BooleanPreference) ApplyQueryType(net.osmand.plus.settings.fragments.ApplyQueryType) BottomSheetItemWithCompoundButton(net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithCompoundButton) OnPreferenceChanged(net.osmand.plus.settings.fragments.OnPreferenceChanged)

Example 2 with OnConfirmPreferenceChange

use of net.osmand.plus.settings.fragments.OnConfirmPreferenceChange in project Osmand by osmandapp.

the class ElevationDateBottomSheet method createUseHeightButton.

private void createUseHeightButton(Context context) {
    boolean checked = useHeightPref.getModeValue(appMode);
    useHeightButton = (BottomSheetItemWithCompoundButton) new BottomSheetItemWithCompoundButton.Builder().setCompoundButtonColor(appModeColor).setChecked(checked).setTitle(checked ? on : off).setTitleColorId(checked ? activeColor : disabledColor).setCustomView(getCustomButtonView(app, appMode, checked, nightMode)).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            boolean newValue = !useHeightPref.getModeValue(appMode);
            Fragment target = getTargetFragment();
            if (target instanceof OnConfirmPreferenceChange) {
                OnConfirmPreferenceChange confirmInterface = (OnConfirmPreferenceChange) target;
                if (confirmInterface.onConfirmPreferenceChange(useHeightPref.getId(), newValue, ApplyQueryType.NONE)) {
                    updateUseHeightButton(useHeightButton, newValue);
                    if (target instanceof BaseSettingsFragment) {
                        ((BaseSettingsFragment) target).updateSetting(useHeightPref.getId());
                    }
                }
            } else {
                useHeightPref.setModeValue(appMode, newValue);
                updateUseHeightButton(useHeightButton, newValue);
            }
        }
    }).create();
    items.add(useHeightButton);
}
Also used : OnConfirmPreferenceChange(net.osmand.plus.settings.fragments.OnConfirmPreferenceChange) BaseSettingsFragment(net.osmand.plus.settings.fragments.BaseSettingsFragment) View(android.view.View) BooleanPreferenceBottomSheet.updateCustomButtonView(net.osmand.plus.settings.bottomsheets.BooleanPreferenceBottomSheet.updateCustomButtonView) TextView(android.widget.TextView) BooleanPreferenceBottomSheet.getCustomButtonView(net.osmand.plus.settings.bottomsheets.BooleanPreferenceBottomSheet.getCustomButtonView) Fragment(androidx.fragment.app.Fragment) BaseSettingsFragment(net.osmand.plus.settings.fragments.BaseSettingsFragment) MenuBottomSheetDialogFragment(net.osmand.plus.base.MenuBottomSheetDialogFragment) BottomSheetItemWithCompoundButton(net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithCompoundButton)

Example 3 with OnConfirmPreferenceChange

use of net.osmand.plus.settings.fragments.OnConfirmPreferenceChange in project Osmand by osmandapp.

the class VehicleParametersBottomSheet method onRightBottomButtonClick.

@Override
protected void onRightBottomButtonClick() {
    Fragment target = getTargetFragment();
    if (target instanceof OnConfirmPreferenceChange) {
        ((OnConfirmPreferenceChange) target).onConfirmPreferenceChange(getPreference().getKey(), String.valueOf(currentValue), ApplyQueryType.SNACK_BAR);
    }
    dismiss();
}
Also used : OnConfirmPreferenceChange(net.osmand.plus.settings.fragments.OnConfirmPreferenceChange) Fragment(androidx.fragment.app.Fragment)

Example 4 with OnConfirmPreferenceChange

use of net.osmand.plus.settings.fragments.OnConfirmPreferenceChange in project Osmand by osmandapp.

the class RecalculateRouteInDeviationBottomSheet method createMenuItems.

@Override
public void createMenuItems(Bundle savedInstanceState) {
    app = requiredMyApplication();
    settings = app.getSettings();
    appMode = getAppMode();
    preference = settings.ROUTE_RECALCULATION_DISTANCE;
    Context themedCtx = UiUtilities.getThemedContext(app, nightMode);
    getPreferenceStateAndValue();
    final SwitchPreferenceEx switchPref = (SwitchPreferenceEx) getPreference();
    if (switchPref == null) {
        return;
    }
    if (savedInstanceState != null && savedInstanceState.containsKey(CURRENT_VALUE)) {
        currentValue = savedInstanceState.getFloat(CURRENT_VALUE);
    }
    int contentPaddingSmall = app.getResources().getDimensionPixelSize(R.dimen.content_padding_small);
    int contentPadding = app.getResources().getDimensionPixelSize(R.dimen.content_padding);
    MetricsConstants mc = settings.METRIC_SYSTEM.get();
    if (mc == MetricsConstants.KILOMETERS_AND_METERS) {
        entryValues = new Float[] { 10.f, 20.0f, 30.0f, 50.0f, 100.0f, 200.0f, 500.0f, 1000.0f, 1500.0f };
    } else {
        entryValues = new Float[] { 9.1f, 18.3f, 30.5f, 45.7f, 91.5f, 183.0f, 482.0f, 965.0f, 1609.0f };
    }
    final int appModeColor = appMode.getProfileColor(nightMode);
    final int activeColor = AndroidUtils.resolveAttribute(themedCtx, R.attr.active_color_basic);
    final int disabledColor = AndroidUtils.resolveAttribute(themedCtx, android.R.attr.textColorSecondary);
    String title = getString(R.string.recalculate_route_in_deviation);
    items.add(new TitleItem(title));
    final View sliderView = UiUtilities.getInflater(getContext(), nightMode).inflate(R.layout.bottom_sheet_item_slider_with_two_text, null);
    slider = sliderView.findViewById(R.id.slider);
    tvSliderTitle = sliderView.findViewById(android.R.id.title);
    tvSliderTitle.setText(getString(R.string.distance));
    tvSliderSummary = sliderView.findViewById(android.R.id.summary);
    slider.setValueFrom(0);
    slider.setValueTo(entryValues.length - 1);
    slider.setStepSize(1);
    updateSliderView();
    final String on = getString(R.string.shared_string_enabled);
    final String off = getString(R.string.shared_string_disabled);
    final BottomSheetItemWithCompoundButton[] preferenceBtn = new BottomSheetItemWithCompoundButton[1];
    preferenceBtn[0] = (BottomSheetItemWithCompoundButton) new BottomSheetItemWithCompoundButton.Builder().setChecked(enabled).setCompoundButtonColor(appModeColor).setTitle(enabled ? on : off).setTitleColorId(enabled ? activeColor : disabledColor).setCustomView(getCustomButtonView(app, getAppMode(), enabled, nightMode)).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            enabled = !enabled;
            sliderPositionChanged = false;
            switchPref.setChecked(enabled);
            preferenceBtn[0].setTitle(enabled ? on : off);
            preferenceBtn[0].setTitleColorId(enabled ? activeColor : disabledColor);
            preferenceBtn[0].setChecked(enabled);
            getDefaultValue();
            updateSliderView();
            updateCustomButtonView(app, getAppMode(), v, enabled, nightMode);
            Fragment target = getTargetFragment();
            float newValue = enabled ? DEFAULT_MODE : DISABLE_MODE;
            if (target instanceof OnConfirmPreferenceChange) {
                ((OnConfirmPreferenceChange) target).onConfirmPreferenceChange(switchPref.getKey(), newValue, ApplyQueryType.NONE);
            }
        }
    }).create();
    items.add(preferenceBtn[0]);
    items.add(new DividerSpaceItem(app, contentPaddingSmall));
    items.add(new LongDescriptionItem(getString(R.string.select_distance_route_will_recalc)));
    items.add(new DividerSpaceItem(app, contentPadding));
    slider.addOnChangeListener(new Slider.OnChangeListener() {

        @Override
        public void onValueChange(@NonNull Slider slider, float value, boolean fromUser) {
            sliderPositionChanged = true;
            if (fromUser) {
                currentValue = entryValues[(int) slider.getValue()];
                tvSliderSummary.setText(getFormattedDistance(app, currentValue));
            }
        }
    });
    UiUtilities.setupSlider(slider, nightMode, appModeColor, true);
    items.add(new BaseBottomSheetItem.Builder().setCustomView(sliderView).create());
    items.add(new SubtitmeListDividerItem(getContext()));
    items.add(new DividerSpaceItem(app, contentPaddingSmall));
    items.add(new LongDescriptionItem(getString(R.string.recalculate_route_distance_promo)));
}
Also used : Context(android.content.Context) BaseBottomSheetItem(net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem) Slider(com.google.android.material.slider.Slider) LongDescriptionItem(net.osmand.plus.base.bottomsheetmenu.simpleitems.LongDescriptionItem) TitleItem(net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem) SwitchPreferenceEx(net.osmand.plus.settings.preferences.SwitchPreferenceEx) OnConfirmPreferenceChange(net.osmand.plus.settings.fragments.OnConfirmPreferenceChange) View(android.view.View) TextView(android.widget.TextView) Fragment(androidx.fragment.app.Fragment) MetricsConstants(net.osmand.plus.settings.enums.MetricsConstants) SubtitmeListDividerItem(net.osmand.plus.base.bottomsheetmenu.simpleitems.SubtitmeListDividerItem) DividerSpaceItem(net.osmand.plus.base.bottomsheetmenu.simpleitems.DividerSpaceItem) BottomSheetItemWithCompoundButton(net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithCompoundButton)

Aggregations

Fragment (androidx.fragment.app.Fragment)4 OnConfirmPreferenceChange (net.osmand.plus.settings.fragments.OnConfirmPreferenceChange)4 View (android.view.View)3 BottomSheetItemWithCompoundButton (net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithCompoundButton)3 Context (android.content.Context)2 TextView (android.widget.TextView)2 BaseBottomSheetItem (net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem)2 TitleItem (net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem)2 SwitchPreferenceEx (net.osmand.plus.settings.preferences.SwitchPreferenceEx)2 Slider (com.google.android.material.slider.Slider)1 OsmandApplication (net.osmand.plus.OsmandApplication)1 MenuBottomSheetDialogFragment (net.osmand.plus.base.MenuBottomSheetDialogFragment)1 BottomSheetItemWithDescription (net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithDescription)1 DividerSpaceItem (net.osmand.plus.base.bottomsheetmenu.simpleitems.DividerSpaceItem)1 LongDescriptionItem (net.osmand.plus.base.bottomsheetmenu.simpleitems.LongDescriptionItem)1 SubtitmeListDividerItem (net.osmand.plus.base.bottomsheetmenu.simpleitems.SubtitmeListDividerItem)1 BooleanPreference (net.osmand.plus.settings.backend.preferences.BooleanPreference)1 OsmandPreference (net.osmand.plus.settings.backend.preferences.OsmandPreference)1 BooleanPreferenceBottomSheet.getCustomButtonView (net.osmand.plus.settings.bottomsheets.BooleanPreferenceBottomSheet.getCustomButtonView)1 BooleanPreferenceBottomSheet.updateCustomButtonView (net.osmand.plus.settings.bottomsheets.BooleanPreferenceBottomSheet.updateCustomButtonView)1