Search in sources :

Example 1 with SizePreference

use of net.osmand.plus.settings.preferences.SizePreference in project Osmand by osmandapp.

the class VehicleParametersBottomSheet method createBottomSheetItem.

@SuppressLint("ClickableViewAccessibility")
private BaseBottomSheetItem createBottomSheetItem(OsmandApplication app) {
    final SizePreference preference = (SizePreference) getPreference();
    View mainView = UiUtilities.getInflater(getContext(), nightMode).inflate(R.layout.bottom_sheet_item_edit_with_chips_view, null);
    TextView title = mainView.findViewById(R.id.title);
    title.setText(preference.getTitle().toString());
    VehicleSizeAssets vehicleSizeAssets = preference.getAssets();
    if (vehicleSizeAssets != null) {
        ImageView imageView = mainView.findViewById(R.id.image_view);
        imageView.setImageDrawable(app.getUIUtilities().getIcon(!nightMode ? vehicleSizeAssets.getDayIconId() : vehicleSizeAssets.getNightIconId()));
        TextView description = mainView.findViewById(R.id.description);
        description.setText(app.getString(vehicleSizeAssets.getDescriptionRes()));
    }
    final HorizontalChipsView chipsView = mainView.findViewById(R.id.chips_view);
    final TextView metric = mainView.findViewById(R.id.metric);
    metric.setText(app.getString(preference.getAssets().getMetricRes()));
    final DecimalFormat df = new DecimalFormat("#.####", new DecimalFormatSymbols(Locale.US));
    text = mainView.findViewById(R.id.text_edit);
    try {
        currentValue = Float.parseFloat(preference.getValue());
    } catch (NumberFormatException e) {
        currentValue = 0.0f;
    }
    selectedItem = preference.getEntryFromValue(preference.getValue());
    String currentValueStr = currentValue == 0.0f ? "" : df.format(currentValue + 0.01f);
    text.setText(currentValueStr);
    text.clearFocus();
    text.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            text.onTouchEvent(event);
            text.setSelection(text.getText().length());
            return true;
        }
    });
    text.addTextChangedListener(new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            if (!Algorithms.isEmpty(s)) {
                try {
                    currentValue = Float.parseFloat(s.toString()) - 0.01f;
                } catch (NumberFormatException e) {
                    currentValue = 0.0f;
                }
            } else {
                currentValue = 0.0f;
            }
            selectedItem = preference.getEntryFromValue(String.valueOf(currentValue));
            ChipItem selected = chipsView.getChipById(selectedItem);
            chipsView.setSelected(selected);
            chipsView.smoothScrollTo(selected);
        }
    });
    List<ChipItem> chips = new ArrayList<>();
    for (String entry : preference.getEntries()) {
        ChipItem chip = new ChipItem(entry);
        chip.title = entry;
        chips.add(chip);
    }
    chipsView.setItems(chips);
    chipsView.setOnSelectChipListener(chip -> {
        selectedItem = chip.id;
        currentValue = preference.getValueFromEntries(selectedItem);
        String currentValueStr1 = currentValue == 0.0f ? "" : df.format(currentValue + 0.01f);
        text.setText(currentValueStr1);
        if (text.hasFocus()) {
            text.setSelection(text.getText().length());
        }
        return true;
    });
    ChipItem selected = chipsView.getChipById(selectedItem);
    chipsView.setSelected(selected);
    return new BaseBottomSheetItem.Builder().setCustomView(mainView).create();
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols) DecimalFormat(java.text.DecimalFormat) ArrayList(java.util.ArrayList) SizePreference(net.osmand.plus.settings.preferences.SizePreference) ImageView(android.widget.ImageView) View(android.view.View) HorizontalChipsView(net.osmand.plus.widgets.chips.HorizontalChipsView) TextView(android.widget.TextView) ScrollView(android.widget.ScrollView) SuppressLint(android.annotation.SuppressLint) MotionEvent(android.view.MotionEvent) ChipItem(net.osmand.plus.widgets.chips.ChipItem) HorizontalChipsView(net.osmand.plus.widgets.chips.HorizontalChipsView) TextWatcher(android.text.TextWatcher) Editable(android.text.Editable) TextView(android.widget.TextView) ImageView(android.widget.ImageView) SuppressLint(android.annotation.SuppressLint)

Example 2 with SizePreference

use of net.osmand.plus.settings.preferences.SizePreference in project Osmand by osmandapp.

the class VehicleParametersFragment method setupCustomRoutingPropertyPref.

private void setupCustomRoutingPropertyPref(@Nullable RoutingParameter parameter, GeneralRouterProfile routerProfile) {
    if (parameter == null) {
        return;
    }
    String parameterId = parameter.getId();
    String title = AndroidUtils.getRoutingStringPropertyName(app, parameterId, parameter.getName());
    String description = AndroidUtils.getRoutingStringPropertyDescription(app, parameterId, parameter.getDescription());
    String defValue = parameter.getType() == RoutingParameterType.NUMERIC ? ROUTING_PARAMETER_NUMERIC_DEFAULT : ROUTING_PARAMETER_SYMBOLIC_DEFAULT;
    StringPreference pref = (StringPreference) app.getSettings().getCustomRoutingProperty(parameterId, defValue);
    VehicleSizeAssets assets = VehicleSizeAssets.getAssets(parameterId, routerProfile);
    Object[] values = parameter.getPossibleValues();
    String[] valuesStr = new String[values.length];
    for (int i = 0; i < values.length; i++) {
        valuesStr[i] = values[i].toString();
    }
    String[] entriesStr = parameter.getPossibleValueDescriptions().clone();
    entriesStr[0] = app.getString(R.string.shared_string_none);
    for (int i = 1; i < entriesStr.length; i++) {
        int firstCharIndex = Algorithms.findFirstNumberEndIndex(entriesStr[i]);
        entriesStr[i] = String.format(app.getString(R.string.ltr_or_rtl_combine_via_space), entriesStr[i].substring(0, firstCharIndex), getString(assets.getMetricShortRes()));
    }
    Context ctx = getContext();
    if (ctx == null) {
        return;
    }
    SizePreference vehicleSizePref = new SizePreference(ctx);
    vehicleSizePref.setKey(pref.getId());
    vehicleSizePref.setAssets(assets);
    vehicleSizePref.setDefaultValue(defValue);
    vehicleSizePref.setTitle(title);
    vehicleSizePref.setEntries(entriesStr);
    vehicleSizePref.setEntryValues(valuesStr);
    vehicleSizePref.setSummary(description);
    vehicleSizePref.setIcon(getPreferenceIcon(parameterId));
    vehicleSizePref.setLayoutResource(R.layout.preference_with_descr);
    getPreferenceScreen().addPreference(vehicleSizePref);
}
Also used : Context(android.content.Context) VehicleSizeAssets(net.osmand.plus.settings.bottomsheets.VehicleSizeAssets) SizePreference(net.osmand.plus.settings.preferences.SizePreference) StringPreference(net.osmand.plus.settings.backend.preferences.StringPreference)

Example 3 with SizePreference

use of net.osmand.plus.settings.preferences.SizePreference in project Osmand by osmandapp.

the class VehicleParametersFragment method onBindPreferenceViewHolder.

@Override
protected void onBindPreferenceViewHolder(Preference preference, PreferenceViewHolder holder) {
    super.onBindPreferenceViewHolder(preference, holder);
    if (!DEFAULT_SPEED.equals(preference.getKey()) && preference instanceof ListPreferenceEx) {
        ImageView imageView = (ImageView) holder.findViewById(android.R.id.icon);
        if (imageView != null) {
            Object currentValue = ((ListPreferenceEx) preference).getValue();
            boolean enabled = preference.isEnabled() && !ROUTING_PARAMETER_NUMERIC_DEFAULT.equals(currentValue) && !ROUTING_PARAMETER_SYMBOLIC_DEFAULT.equals(currentValue);
            imageView.setEnabled(enabled);
        }
    } else if (preference instanceof SizePreference) {
        ImageView imageView = (ImageView) holder.findViewById(android.R.id.icon);
        if (imageView != null) {
            Object currentValue = ((SizePreference) preference).getValue();
            boolean enabled = preference.isEnabled() && !ROUTING_PARAMETER_NUMERIC_DEFAULT.equals(currentValue) && !ROUTING_PARAMETER_SYMBOLIC_DEFAULT.equals(currentValue);
            imageView.setEnabled(enabled);
        }
    }
}
Also used : ListPreferenceEx(net.osmand.plus.settings.preferences.ListPreferenceEx) ImageView(android.widget.ImageView) SizePreference(net.osmand.plus.settings.preferences.SizePreference)

Aggregations

SizePreference (net.osmand.plus.settings.preferences.SizePreference)3 ImageView (android.widget.ImageView)2 SuppressLint (android.annotation.SuppressLint)1 Context (android.content.Context)1 Editable (android.text.Editable)1 TextWatcher (android.text.TextWatcher)1 MotionEvent (android.view.MotionEvent)1 View (android.view.View)1 ScrollView (android.widget.ScrollView)1 TextView (android.widget.TextView)1 DecimalFormat (java.text.DecimalFormat)1 DecimalFormatSymbols (java.text.DecimalFormatSymbols)1 ArrayList (java.util.ArrayList)1 StringPreference (net.osmand.plus.settings.backend.preferences.StringPreference)1 VehicleSizeAssets (net.osmand.plus.settings.bottomsheets.VehicleSizeAssets)1 ListPreferenceEx (net.osmand.plus.settings.preferences.ListPreferenceEx)1 ChipItem (net.osmand.plus.widgets.chips.ChipItem)1 HorizontalChipsView (net.osmand.plus.widgets.chips.HorizontalChipsView)1