Fix review

This commit is contained in:
Dima-1 2020-06-03 18:21:36 +03:00
parent e4cb37aca3
commit fc5ab038c7
6 changed files with 268 additions and 219 deletions

View file

@ -1,11 +1,54 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:osmand="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical"> android:orientation="vertical">
<net.osmand.plus.widgets.TextViewEx
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:gravity="center_vertical"
android:maxLines="5"
android:minHeight="@dimen/bottom_sheet_title_height"
android:paddingLeft="@dimen/content_padding"
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"
tools:text="Some Title"
android:paddingStart="@dimen/content_padding"
android:paddingEnd="@dimen/content_padding" />
<ImageView
android:id="@+id/image_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/content_padding_half"
android:contentDescription="@string/shared_string_icon" />
<net.osmand.plus.widgets.TextViewEx
android:id="@+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:letterSpacing="@dimen/text_button_letter_spacing"
android:minHeight="@dimen/bottom_sheet_list_item_height"
android:paddingLeft="@dimen/content_padding"
android:paddingTop="@dimen/content_padding"
android:paddingRight="@dimen/content_padding"
android:paddingBottom="@dimen/content_padding"
android:textColor="?android:textColorSecondary"
android:textSize="@dimen/default_list_text_size"
android:lineSpacingMultiplier="@dimen/bottom_sheet_info_spacing_multiplier"
osmand:typeface="@string/font_roboto_regular"
tools:text="@string/weight_limit_description"
android:paddingEnd="@dimen/content_padding"
android:paddingStart="@dimen/content_padding" />
<FrameLayout <FrameLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
@ -14,6 +57,7 @@
<LinearLayout <LinearLayout
android:id="@+id/text" android:id="@+id/text"
android:paddingTop="@dimen/content_padding_half"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:gravity="center_vertical" android:gravity="center_vertical"
@ -25,10 +69,10 @@
android:id="@+id/text_caption" android:id="@+id/text_caption"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
app:hintEnabled="false" osmand:hintEnabled="false"
android:gravity="start" android:gravity="start"
android:layout_weight="1" android:layout_weight="1"
app:boxBackgroundColor="@color/material_text_input_layout_bg"> osmand:boxBackgroundColor="@color/material_text_input_layout_bg">
<com.google.android.material.textfield.TextInputEditText <com.google.android.material.textfield.TextInputEditText
android:id="@+id/text_edit" android:id="@+id/text_edit"
@ -74,7 +118,7 @@
android:paddingEnd="@dimen/content_padding" android:paddingEnd="@dimen/content_padding"
android:clipToPadding="false" android:clipToPadding="false"
android:orientation="horizontal" android:orientation="horizontal"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" osmand:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:itemCount="3" tools:itemCount="3"
tools:listitem="@layout/point_editor_icon_category_item" /> tools:listitem="@layout/point_editor_icon_category_item" />

View file

@ -0,0 +1,156 @@
package net.osmand.plus.settings.bottomsheets;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.RecyclerView;
import net.osmand.PlatformUtil;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.UiUtilities;
import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem;
import net.osmand.plus.mapcontextmenu.other.HorizontalSelectionAdapter;
import net.osmand.plus.settings.backend.ApplicationMode;
import net.osmand.plus.settings.fragments.ApplyQueryType;
import net.osmand.plus.settings.fragments.OnConfirmPreferenceChange;
import net.osmand.plus.settings.preferences.SizePreference;
import net.osmand.util.Algorithms;
import org.apache.commons.logging.Log;
import java.util.Arrays;
public class VehicleParametersBottomSheet extends BasePreferenceBottomSheet {
private static final Log LOG = PlatformUtil.getLog(VehicleParametersBottomSheet.class);
public static final String TAG = VehicleParametersBottomSheet.class.getSimpleName();
private String selectedItem;
private float currentValue;
@Override
public void createMenuItems(Bundle savedInstanceState) {
OsmandApplication app = getMyApplication();
if (app == null) {
return;
}
items.add(createBottomSheetItem(app));
}
private BaseBottomSheetItem createBottomSheetItem(OsmandApplication app) {
final SizePreference preference = (SizePreference) getPreference();
View mainView = UiUtilities.getMaterialInflater(app, nightMode)
.inflate(R.layout.bottom_sheet_item_edit_with_recyclerview, null);
String key = preference.getKey();
TextView title = mainView.findViewById(R.id.title);
title.setText(preference.getTitle().toString());
String parameterName = key.substring(key.lastIndexOf("_") + 1);
VehicleSizeAssets vehicleSizeAssets = VehicleSizeAssets.getAssets(parameterName);
if (vehicleSizeAssets != null) {
ImageView imageView = mainView.findViewById(R.id.image_view);
imageView.setImageDrawable(ContextCompat.getDrawable(app,
!nightMode ? vehicleSizeAssets.getDayIconId() : vehicleSizeAssets.getNightIconId()));
TextView description = mainView.findViewById(R.id.description);
description.setText(app.getString(vehicleSizeAssets.getDescriptionRes()));
}
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);
try {
currentValue = Float.parseFloat(preference.getValue());
} catch (NumberFormatException e) {
currentValue = 0.0f;
}
selectedItem = preference.getEntryFromValue(preference.getValue());
String currentValueStr = currentValue == 0.0f ? "" : String.valueOf(currentValue + 0.01f);
text.setText(currentValueStr);
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));
adapter.setSelectedItem(selectedItem);
}
});
adapter.setItems(Arrays.asList(preference.getEntries()));
adapter.setListener(new HorizontalSelectionAdapter.HorizontalSelectionAdapterListener() {
@Override
public void onItemSelected(String item) {
selectedItem = item;
currentValue = preference.getValueFromEntries(selectedItem);
String currentValueStr = currentValue == 0.0f ? "" : String.valueOf(currentValue + 0.01f);
text.setText(currentValueStr);
adapter.notifyDataSetChanged();
}
});
RecyclerView recyclerView = mainView.findViewById(R.id.recycler_view);
recyclerView.setAdapter(adapter);
adapter.setSelectedItem(selectedItem);
return new BaseBottomSheetItem.Builder()
.setCustomView(mainView)
.create();
}
@Override
protected int getRightBottomButtonTextId() {
return R.string.shared_string_apply;
}
@Override
protected void onRightBottomButtonClick() {
Fragment target = getTargetFragment();
if (target instanceof OnConfirmPreferenceChange) {
((OnConfirmPreferenceChange) target).onConfirmPreferenceChange(
getPreference().getKey(), String.valueOf(currentValue), ApplyQueryType.SNACK_BAR);
}
dismiss();
}
public static void showInstance(@NonNull FragmentManager fm, String key, Fragment target,
boolean usedOnMap, @Nullable ApplicationMode appMode) {
try {
if (!fm.isStateSaved()) {
Bundle args = new Bundle();
args.putString(PREFERENCE_ID, key);
VehicleParametersBottomSheet fragment = new VehicleParametersBottomSheet();
fragment.setArguments(args);
fragment.setUsedOnMap(usedOnMap);
fragment.setAppMode(appMode);
fragment.setTargetFragment(target, 0);
fragment.show(fm, TAG);
}
} catch (RuntimeException e) {
LOG.error("showInstance", e);
}
}
}

View file

@ -1,210 +0,0 @@
package net.osmand.plus.settings.bottomsheets;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.RecyclerView;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.UiUtilities;
import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem;
import net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithDescription;
import net.osmand.plus.base.bottomsheetmenu.SimpleBottomSheetItem;
import net.osmand.plus.base.bottomsheetmenu.simpleitems.DividerSpaceItem;
import net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem;
import net.osmand.plus.mapcontextmenu.other.HorizontalSelectionAdapter;
import net.osmand.plus.settings.backend.ApplicationMode;
import net.osmand.plus.settings.fragments.ApplyQueryType;
import net.osmand.plus.settings.fragments.OnConfirmPreferenceChange;
import net.osmand.plus.settings.preferences.SizePreference;
import net.osmand.router.GeneralRouter;
import net.osmand.util.Algorithms;
import java.util.Arrays;
public class VehicleParametersNumericBottomSheet extends BasePreferenceBottomSheet {
public enum VehicleSizeAssets {
WIDTH(GeneralRouter.VEHICLE_WIDTH, R.drawable.img_help_width_limit_day, R.drawable.img_help_width_limit_night,
R.string.width_limit_description, R.string.shared_string_meters, R.string.m),
HEIGHT(GeneralRouter.VEHICLE_HEIGHT, R.drawable.img_help_height_limit_day, R.drawable.img_help_height_limit_night,
R.string.height_limit_description, R.string.shared_string_meters, R.string.m),
WEIGHT(GeneralRouter.VEHICLE_WEIGHT, R.drawable.img_help_weight_limit_day, R.drawable.img_help_weight_limit_night,
R.string.weight_limit_description, R.string.shared_string_tones, R.string.metric_ton);
String routerParameterName;
int dayIconId;
int nightIconId;
int descriptionRes;
int metricRes;
int metricShortRes;
VehicleSizeAssets(String routerParameterName, int dayIconId, int nightIconId, int descriptionRes, int metricRes,
int metricShortRes) {
this.routerParameterName = routerParameterName;
this.dayIconId = dayIconId;
this.nightIconId = nightIconId;
this.descriptionRes = descriptionRes;
this.metricRes = metricRes;
this.metricShortRes = metricShortRes;
}
public static VehicleSizeAssets getAssets(String parameterName) {
for (VehicleSizeAssets type : VehicleSizeAssets.values()) {
if (type.routerParameterName.equals(parameterName)) {
return type;
}
}
return null;
}
public int getDayIconId() {
return dayIconId;
}
public int getNightIconId() {
return nightIconId;
}
public int getDescriptionRes() {
return descriptionRes;
}
public int getMetricRes() {
return metricRes;
}
public int getMetricShortRes() {
return metricShortRes;
}
}
public static final String TAG = VehicleParametersNumericBottomSheet.class.getSimpleName();
private String selectedItem;
private float currentValue;
@Override
public void createMenuItems(Bundle savedInstanceState) {
OsmandApplication app = getMyApplication();
if (app == null) {
return;
}
SizePreference preference = (SizePreference) getPreference();
String key = preference.getKey();
String parameterName = key.substring(key.lastIndexOf("_") + 1);
VehicleSizeAssets vehicleSizeAssets = VehicleSizeAssets.getAssets(parameterName);
if (vehicleSizeAssets == null) {
return;
}
items.add(new TitleItem(preference.getTitle().toString()));
ImageView imageView = new ImageView(getContext());
imageView.setImageDrawable(ContextCompat.getDrawable(app,
!nightMode ? vehicleSizeAssets.getDayIconId() : vehicleSizeAssets.getNightIconId()));
items.add(new SimpleBottomSheetItem.Builder().setCustomView(imageView).create());
items.add(new DividerSpaceItem(app, getResources().getDimensionPixelSize(R.dimen.bottom_sheet_content_margin_small)));
BaseBottomSheetItem description = new BottomSheetItemWithDescription.Builder()
.setDescription(app.getString(vehicleSizeAssets.getDescriptionRes()))
.setLayoutId(R.layout.bottom_sheet_item_preference_info)
.create();
items.add(description);
items.add(new DividerSpaceItem(app, getResources().getDimensionPixelSize(R.dimen.bottom_sheet_content_margin_small)));
items.add(createComboView(app, preference));
}
private BaseBottomSheetItem createComboView(OsmandApplication app, final SizePreference preference) {
View mainView = UiUtilities.getMaterialInflater(app, nightMode)
.inflate(R.layout.bottom_sheet_item_edit_with_recyclerview, null);
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);
currentValue = Float.parseFloat(preference.getValue());
selectedItem = preference.getEntryFromValue(preference.getValue());
String currentValueStr = currentValue == 0.0f ? "" : String.valueOf(currentValue + 0.01f);
text.setText(currentValueStr);
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)) {
currentValue = Float.parseFloat(s.toString()) - 0.01f;
} else {
currentValue = 0.0f;
}
selectedItem = preference.getEntryFromValue(String.valueOf(currentValue));
adapter.setSelectedItem(selectedItem);
}
});
adapter.setItems(Arrays.asList(preference.getEntries()));
adapter.setListener(new HorizontalSelectionAdapter.HorizontalSelectionAdapterListener() {
@Override
public void onItemSelected(String item) {
selectedItem = item;
currentValue = preference.getValueFromEntries(selectedItem);
String currentValueStr = currentValue == 0.0f ? "" : String.valueOf(currentValue + 0.01f);
text.setText(currentValueStr);
adapter.notifyDataSetChanged();
}
});
RecyclerView recyclerView = mainView.findViewById(R.id.recycler_view);
recyclerView.setAdapter(adapter);
adapter.setSelectedItem(selectedItem);
return new BaseBottomSheetItem.Builder()
.setCustomView(mainView)
.create();
}
@Override
protected int getRightBottomButtonTextId() {
return R.string.shared_string_apply;
}
@Override
protected void onRightBottomButtonClick() {
Fragment target = getTargetFragment();
if (target instanceof OnConfirmPreferenceChange) {
((OnConfirmPreferenceChange) target).onConfirmPreferenceChange(
getPreference().getKey(), String.valueOf(currentValue), ApplyQueryType.SNACK_BAR);
}
dismiss();
}
public static boolean showInstance(@NonNull FragmentManager fragmentManager, String key, Fragment target,
boolean usedOnMap, @Nullable ApplicationMode appMode) {
try {
Bundle args = new Bundle();
args.putString(PREFERENCE_ID, key);
VehicleParametersNumericBottomSheet fragment = new VehicleParametersNumericBottomSheet();
fragment.setArguments(args);
fragment.setUsedOnMap(usedOnMap);
fragment.setAppMode(appMode);
fragment.setTargetFragment(target, 0);
fragment.show(fragmentManager, TAG);
return true;
} catch (RuntimeException e) {
return false;
}
}
}

View file

@ -0,0 +1,59 @@
package net.osmand.plus.settings.bottomsheets;
import net.osmand.plus.R;
import net.osmand.router.GeneralRouter;
public enum VehicleSizeAssets {
WIDTH(GeneralRouter.VEHICLE_WIDTH, R.drawable.img_help_width_limit_day, R.drawable.img_help_width_limit_night,
R.string.width_limit_description, R.string.shared_string_meters, R.string.m),
HEIGHT(GeneralRouter.VEHICLE_HEIGHT, R.drawable.img_help_height_limit_day, R.drawable.img_help_height_limit_night,
R.string.height_limit_description, R.string.shared_string_meters, R.string.m),
WEIGHT(GeneralRouter.VEHICLE_WEIGHT, R.drawable.img_help_weight_limit_day, R.drawable.img_help_weight_limit_night,
R.string.weight_limit_description, R.string.shared_string_tones, R.string.metric_ton);
String routerParameterName;
int dayIconId;
int nightIconId;
int descriptionRes;
int metricRes;
int metricShortRes;
VehicleSizeAssets(String routerParameterName, int dayIconId, int nightIconId, int descriptionRes, int metricRes,
int metricShortRes) {
this.routerParameterName = routerParameterName;
this.dayIconId = dayIconId;
this.nightIconId = nightIconId;
this.descriptionRes = descriptionRes;
this.metricRes = metricRes;
this.metricShortRes = metricShortRes;
}
public static VehicleSizeAssets getAssets(String parameterName) {
for (VehicleSizeAssets type : VehicleSizeAssets.values()) {
if (type.routerParameterName.equals(parameterName)) {
return type;
}
}
return null;
}
public int getDayIconId() {
return dayIconId;
}
public int getNightIconId() {
return nightIconId;
}
public int getDescriptionRes() {
return descriptionRes;
}
public int getMetricRes() {
return metricRes;
}
public int getMetricShortRes() {
return metricShortRes;
}
}

View file

@ -15,7 +15,8 @@ import net.osmand.plus.routing.RouteProvider.RouteService;
import net.osmand.plus.routing.RoutingHelper; import net.osmand.plus.routing.RoutingHelper;
import net.osmand.plus.settings.backend.ApplicationMode; import net.osmand.plus.settings.backend.ApplicationMode;
import net.osmand.plus.settings.backend.OsmandSettings; import net.osmand.plus.settings.backend.OsmandSettings;
import net.osmand.plus.settings.bottomsheets.VehicleParametersNumericBottomSheet; import net.osmand.plus.settings.bottomsheets.VehicleParametersBottomSheet;
import net.osmand.plus.settings.bottomsheets.VehicleSizeAssets;
import net.osmand.plus.settings.preferences.ListPreferenceEx; import net.osmand.plus.settings.preferences.ListPreferenceEx;
import net.osmand.plus.settings.preferences.SizePreference; import net.osmand.plus.settings.preferences.SizePreference;
import net.osmand.router.GeneralRouter; import net.osmand.router.GeneralRouter;
@ -95,7 +96,7 @@ public class VehicleParametersFragment extends BaseSettingsFragment implements O
} }
SizePreference vehicleSizePref = new SizePreference(ctx); SizePreference vehicleSizePref = new SizePreference(ctx);
vehicleSizePref.setKey(pref.getId()); vehicleSizePref.setKey(pref.getId());
vehicleSizePref.setAssets(VehicleParametersNumericBottomSheet.VehicleSizeAssets.getAssets(parameterId)); vehicleSizePref.setAssets(VehicleSizeAssets.getAssets(parameterId));
vehicleSizePref.setDefaultValue(defValue); vehicleSizePref.setDefaultValue(defValue);
vehicleSizePref.setTitle(title); vehicleSizePref.setTitle(title);
vehicleSizePref.setEntries(entriesStr); vehicleSizePref.setEntries(entriesStr);
@ -150,7 +151,7 @@ public class VehicleParametersFragment extends BaseSettingsFragment implements O
if (preference instanceof SizePreference) { if (preference instanceof SizePreference) {
FragmentManager fragmentManager = getFragmentManager(); FragmentManager fragmentManager = getFragmentManager();
if (fragmentManager != null) { if (fragmentManager != null) {
VehicleParametersNumericBottomSheet.showInstance(fragmentManager, preference.getKey(), VehicleParametersBottomSheet.showInstance(fragmentManager, preference.getKey(),
this, false, getSelectedAppMode()); this, false, getSelectedAppMode());
} }
} else { } else {

View file

@ -5,8 +5,7 @@ import android.content.Context;
import androidx.preference.DialogPreference; import androidx.preference.DialogPreference;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.settings.bottomsheets.VehicleSizeAssets;
import static net.osmand.plus.settings.bottomsheets.VehicleParametersNumericBottomSheet.*;
public class SizePreference extends DialogPreference { public class SizePreference extends DialogPreference {