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();
}
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);
}
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);
}
}
}
Aggregations