diff --git a/OsmAnd/res/layout/bottom_sheet_item_edit_with_recyclerview.xml b/OsmAnd/res/layout/bottom_sheet_item_edit_with_recyclerview.xml index 39c3cc29e7..53ada3c475 100644 --- a/OsmAnd/res/layout/bottom_sheet_item_edit_with_recyclerview.xml +++ b/OsmAnd/res/layout/bottom_sheet_item_edit_with_recyclerview.xml @@ -1,11 +1,54 @@ + + + + + + + osmand:boxBackgroundColor="@color/material_text_input_layout_bg"> diff --git a/OsmAnd/src/net/osmand/plus/settings/bottomsheets/VehicleParametersBottomSheet.java b/OsmAnd/src/net/osmand/plus/settings/bottomsheets/VehicleParametersBottomSheet.java new file mode 100644 index 0000000000..8700798c23 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/settings/bottomsheets/VehicleParametersBottomSheet.java @@ -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); + } + } +} diff --git a/OsmAnd/src/net/osmand/plus/settings/bottomsheets/VehicleParametersNumericBottomSheet.java b/OsmAnd/src/net/osmand/plus/settings/bottomsheets/VehicleParametersNumericBottomSheet.java deleted file mode 100644 index dad8528f5f..0000000000 --- a/OsmAnd/src/net/osmand/plus/settings/bottomsheets/VehicleParametersNumericBottomSheet.java +++ /dev/null @@ -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; - } - } -} diff --git a/OsmAnd/src/net/osmand/plus/settings/bottomsheets/VehicleSizeAssets.java b/OsmAnd/src/net/osmand/plus/settings/bottomsheets/VehicleSizeAssets.java new file mode 100644 index 0000000000..c40e99867c --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/settings/bottomsheets/VehicleSizeAssets.java @@ -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; + } +} diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/VehicleParametersFragment.java b/OsmAnd/src/net/osmand/plus/settings/fragments/VehicleParametersFragment.java index a832f4539d..daaa3fd244 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/VehicleParametersFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/VehicleParametersFragment.java @@ -15,7 +15,8 @@ import net.osmand.plus.routing.RouteProvider.RouteService; import net.osmand.plus.routing.RoutingHelper; import net.osmand.plus.settings.backend.ApplicationMode; 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.SizePreference; import net.osmand.router.GeneralRouter; @@ -95,7 +96,7 @@ public class VehicleParametersFragment extends BaseSettingsFragment implements O } SizePreference vehicleSizePref = new SizePreference(ctx); vehicleSizePref.setKey(pref.getId()); - vehicleSizePref.setAssets(VehicleParametersNumericBottomSheet.VehicleSizeAssets.getAssets(parameterId)); + vehicleSizePref.setAssets(VehicleSizeAssets.getAssets(parameterId)); vehicleSizePref.setDefaultValue(defValue); vehicleSizePref.setTitle(title); vehicleSizePref.setEntries(entriesStr); @@ -150,7 +151,7 @@ public class VehicleParametersFragment extends BaseSettingsFragment implements O if (preference instanceof SizePreference) { FragmentManager fragmentManager = getFragmentManager(); if (fragmentManager != null) { - VehicleParametersNumericBottomSheet.showInstance(fragmentManager, preference.getKey(), + VehicleParametersBottomSheet.showInstance(fragmentManager, preference.getKey(), this, false, getSelectedAppMode()); } } else { diff --git a/OsmAnd/src/net/osmand/plus/settings/preferences/SizePreference.java b/OsmAnd/src/net/osmand/plus/settings/preferences/SizePreference.java index edeb8063b4..f48e078b92 100644 --- a/OsmAnd/src/net/osmand/plus/settings/preferences/SizePreference.java +++ b/OsmAnd/src/net/osmand/plus/settings/preferences/SizePreference.java @@ -5,8 +5,7 @@ import android.content.Context; import androidx.preference.DialogPreference; import net.osmand.plus.R; - -import static net.osmand.plus.settings.bottomsheets.VehicleParametersNumericBottomSheet.*; +import net.osmand.plus.settings.bottomsheets.VehicleSizeAssets; public class SizePreference extends DialogPreference {