Merge pull request #9206 from osmandapp/Vechicle_pref_numeric_dialog

Fix vechicle pref numeric dialog
This commit is contained in:
vshcherb 2020-06-19 11:26:07 +02:00 committed by GitHub
commit 514a596886
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 104 additions and 19 deletions

View file

@ -4,6 +4,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:orientation="vertical">
<net.osmand.plus.widgets.TextViewEx
@ -18,8 +19,7 @@
android:paddingRight="@dimen/content_padding"
android:paddingTop="@dimen/content_padding_small"
android:paddingBottom="@dimen/content_padding_small"
android:textAppearance="@style/TextAppearance.ListItemTitle"
osmand:typeface="@string/font_roboto_medium"
android:textAppearance="@style/TextAppearance.ListItemCategoryTitle"
tools:text="Some Title"
android:paddingStart="@dimen/content_padding"
android:paddingEnd="@dimen/content_padding" />
@ -43,7 +43,7 @@
android:paddingBottom="@dimen/content_padding"
android:textColor="?android:textColorSecondary"
android:textSize="@dimen/default_list_text_size"
android:lineSpacingMultiplier="@dimen/bottom_sheet_info_spacing_multiplier"
android:lineSpacingMultiplier="@dimen/bottom_sheet_text_spacing_multiplier"
osmand:typeface="@string/font_roboto_regular"
tools:text="@string/weight_limit_description"
android:paddingEnd="@dimen/content_padding"
@ -52,15 +52,14 @@
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/content_padding_half"
android:layout_marginLeft="@dimen/content_padding"
android:layout_marginRight="@dimen/content_padding">
<LinearLayout
android:id="@+id/text"
android:paddingTop="@dimen/content_padding_half"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:baselineAligned="false">
@ -70,20 +69,19 @@
android:layout_width="0dp"
android:layout_height="wrap_content"
osmand:hintEnabled="false"
android:gravity="start"
android:layout_weight="1"
osmand:boxBackgroundColor="@color/material_text_input_layout_bg">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/text_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
android:layout_height="@dimen/favorites_list_item_height"
android:gravity="end|center_vertical"
android:inputType="numberDecimal"
android:maxLines="1"
android:maxLength="5"
android:minHeight="@dimen/favorites_list_item_height"
android:paddingBottom="@dimen/card_content_padding_large"
android:paddingTop="0dp"
android:paddingBottom="0dp"
android:paddingStart="@dimen/content_padding_small"
android:paddingLeft="@dimen/content_padding_small"
android:paddingRight="@dimen/content_padding_small"
@ -98,7 +96,7 @@
android:id="@+id/metric"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_gravity="start|center_vertical"
android:paddingStart="@dimen/content_padding_small"
android:paddingRight="@dimen/content_padding_small"
android:paddingEnd="@dimen/content_padding_small"
@ -121,5 +119,4 @@
osmand:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:itemCount="3"
tools:listitem="@layout/point_editor_icon_category_item" />
</LinearLayout>

View file

@ -2,6 +2,7 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/buttons_container"
android:layout_width="wrap_content"
android:minWidth="@dimen/favorites_icon_outline_size"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:gravity="center"

View file

@ -106,12 +106,16 @@
<item name="colorPrimary">@color/active_color_primary_light</item>
<item name="dashboard_divider">@color/divider_color_light</item>
<item name="divider_color">@color/divider_color</item>
<item name="android:textColorPrimary">@color/text_color_primary_light</item>
<item name="android:textColorSecondary">@color/text_color_secondary_light</item>
</style>
<style name="OsmandMaterialDarkTheme" parent="Theme.MaterialComponents">
<item name="colorPrimary">@color/active_color_primary_dark</item>
<item name="dashboard_divider">@color/divider_color_dark</item>
<item name="divider_color">@color/divider_color_dark</item>
<item name="android:textColorPrimary">@color/text_color_primary_dark</item>
<item name="android:textColorSecondary">@color/text_color_secondary_dark</item>
</style>
<!-- MaterialComponents Widgets override -->

View file

@ -57,7 +57,8 @@ public class HorizontalSelectionAdapter extends RecyclerView.Adapter<HorizontalS
ContextCompat.getColor(app, activeColorResId)));
textView.setTextColor(ContextCompat.getColor(app, R.color.color_white));
} else {
textView.setTextColor(ContextCompat.getColor(app, R.color.preference_category_title));
textView.setTextColor(ContextCompat.getColor(app,
nightMode ? R.color.active_color_primary_dark : R.color.preference_category_title));
GradientDrawable buttonBackground = (GradientDrawable) AppCompatResources.getDrawable(app,
R.drawable.bg_select_icon_group_button).mutate();
buttonBackground.setStroke(AndroidUtils.dpToPx(app, 1), ContextCompat.getColor(app,
@ -67,6 +68,7 @@ public class HorizontalSelectionAdapter extends RecyclerView.Adapter<HorizontalS
AndroidUtils.setBackground(holder.button, buttonBackground);
}
textView.setText(capitalizeFirstLetter(item));
textView.requestLayout();
holder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

View file

@ -1,10 +1,19 @@
package net.osmand.plus.settings.bottomsheets;
import android.annotation.SuppressLint;
import android.graphics.Rect;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;
import androidx.annotation.NonNull;
@ -13,6 +22,7 @@ import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.RecyclerView;
import net.osmand.AndroidUtils;
import net.osmand.PlatformUtil;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
@ -35,6 +45,8 @@ public class VehicleParametersBottomSheet extends BasePreferenceBottomSheet {
public static final String TAG = VehicleParametersBottomSheet.class.getSimpleName();
private String selectedItem;
private float currentValue;
private int contentHeightPrevious = 0;
private EditText text;
@Override
public void createMenuItems(Bundle savedInstanceState) {
@ -45,9 +57,10 @@ public class VehicleParametersBottomSheet extends BasePreferenceBottomSheet {
items.add(createBottomSheetItem(app));
}
@SuppressLint("ClickableViewAccessibility")
private BaseBottomSheetItem createBottomSheetItem(OsmandApplication app) {
final SizePreference preference = (SizePreference) getPreference();
View mainView = UiUtilities.getMaterialInflater(app, nightMode)
View mainView = UiUtilities.getMaterialInflater(getContext(), nightMode)
.inflate(R.layout.bottom_sheet_item_edit_with_recyclerview, null);
TextView title = mainView.findViewById(R.id.title);
title.setText(preference.getTitle().toString());
@ -62,7 +75,8 @@ public class VehicleParametersBottomSheet extends BasePreferenceBottomSheet {
final HorizontalSelectionAdapter adapter = new HorizontalSelectionAdapter(app, nightMode);
final TextView metric = mainView.findViewById(R.id.metric);
metric.setText(app.getString(preference.getAssets().getMetricRes()));
final TextView text = mainView.findViewById(R.id.text_edit);
final RecyclerView recyclerView = mainView.findViewById(R.id.recycler_view);
text = mainView.findViewById(R.id.text_edit);
try {
currentValue = Float.parseFloat(preference.getValue());
} catch (NumberFormatException e) {
@ -72,6 +86,15 @@ public class VehicleParametersBottomSheet extends BasePreferenceBottomSheet {
String currentValueStr = currentValue == 0.0f ? "" : String.valueOf(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) {
@ -94,6 +117,10 @@ public class VehicleParametersBottomSheet extends BasePreferenceBottomSheet {
}
selectedItem = preference.getEntryFromValue(String.valueOf(currentValue));
adapter.setSelectedItem(selectedItem);
int itemPosition = adapter.getItemPosition(selectedItem);
if (itemPosition >= 0) {
recyclerView.smoothScrollToPosition(itemPosition);
}
}
});
@ -105,11 +132,12 @@ public class VehicleParametersBottomSheet extends BasePreferenceBottomSheet {
currentValue = preference.getValueFromEntries(selectedItem);
String currentValueStr = currentValue == 0.0f ? "" : String.valueOf(currentValue + 0.01f);
text.setText(currentValueStr);
if (text.hasFocus()) {
text.setSelection(text.getText().length());
}
adapter.notifyDataSetChanged();
}
});
RecyclerView recyclerView = mainView.findViewById(R.id.recycler_view);
recyclerView.setAdapter(adapter);
adapter.setSelectedItem(selectedItem);
return new BaseBottomSheetItem.Builder()
@ -117,6 +145,45 @@ public class VehicleParametersBottomSheet extends BasePreferenceBottomSheet {
.create();
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
final View view = super.onCreateView(inflater, parent, savedInstanceState);
view.getViewTreeObserver().addOnGlobalLayoutListener(getOnGlobalLayoutListener());
return view;
}
private ViewTreeObserver.OnGlobalLayoutListener getOnGlobalLayoutListener() {
final int buttonsHeight = getResources().getDimensionPixelSize(R.dimen.dialog_button_ex_height);
final int shadowHeight = AndroidUtils.dpToPx(getContext(), 8);
final int statusBarHeight = AndroidUtils.getStatusBarHeight(getContext());
return new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect visibleDisplayFrame = new Rect();
final ScrollView scrollView = getView().findViewById(R.id.scroll_view);
scrollView.getWindowVisibleDisplayFrame(visibleDisplayFrame);
int contentHeight = visibleDisplayFrame.bottom - visibleDisplayFrame.top - buttonsHeight
- shadowHeight - statusBarHeight;
if (contentHeightPrevious != contentHeight) {
if (scrollView.getHeight() > contentHeight) {
scrollView.getLayoutParams().height = contentHeight;
} else {
scrollView.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
}
scrollView.requestLayout();
int delay = Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP ? 300 : 1000;
scrollView.postDelayed(new Runnable() {
public void run() {
scrollView.scrollTo(0, scrollView.getHeight());
}
}, delay);
contentHeightPrevious = contentHeight;
}
}
};
}
@Override
protected int getRightBottomButtonTextId() {
return R.string.shared_string_apply;

View file

@ -20,6 +20,7 @@ import net.osmand.plus.settings.bottomsheets.VehicleSizeAssets;
import net.osmand.plus.settings.preferences.ListPreferenceEx;
import net.osmand.plus.settings.preferences.SizePreference;
import net.osmand.router.GeneralRouter;
import net.osmand.util.Algorithms;
import net.osmand.router.GeneralRouter.GeneralRouterProfile;
import java.util.Map;
@ -94,6 +95,11 @@ public class VehicleParametersFragment extends BaseSettingsFragment implements O
}
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), entriesStr[i].substring(firstCharIndex));
}
Context ctx = getContext();
if (ctx == null) {
@ -137,6 +143,14 @@ public class VehicleParametersFragment extends BaseSettingsFragment implements O
&& !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);
}
}
}

View file

@ -81,7 +81,7 @@ public class SizePreference extends DialogPreference {
@Override
public CharSequence getSummary() {
String summary = "-";
String summary = entries[0];
String persistedString = getValue();
if (!persistedString.equals(defaultValue)) {
try {
@ -89,7 +89,7 @@ public class SizePreference extends DialogPreference {
summary = String.format(getContext().getString(R.string.ltr_or_rtl_combine_via_space),
persistedString, getContext().getString(assets.getMetricShortRes()));
} catch (NumberFormatException e) {
summary = "-";
summary = entries[0];
}
}
return summary;