From 41561f0ea4560423a5ccd77c5a0520e7d71c56b9 Mon Sep 17 00:00:00 2001 From: Dishebh Bhayana <43489708+Dishebh@users.noreply.github.com> Date: Sat, 21 Sep 2024 02:13:58 +0530 Subject: [PATCH] fix: add support for negative values via keyboard (#2550) Co-authored-by: Marc Nause --- .../io/pslab/activity/PowerSourceActivity.java | 15 +++++++++------ .../res/layout/activity_powersource_layout.xml | 8 +++++--- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/io/pslab/activity/PowerSourceActivity.java b/app/src/main/java/io/pslab/activity/PowerSourceActivity.java index 819759501..f99cc5de7 100644 --- a/app/src/main/java/io/pslab/activity/PowerSourceActivity.java +++ b/app/src/main/java/io/pslab/activity/PowerSourceActivity.java @@ -44,7 +44,6 @@ import butterknife.ButterKnife; import io.pslab.R; import io.pslab.activity.guide.GuideActivity; -import io.pslab.communication.CommunicationHandler; import io.pslab.communication.ScienceLab; import io.pslab.items.SquareImageButton; import io.pslab.models.PowerSourceData; @@ -88,7 +87,7 @@ public class PowerSourceActivity extends GuideActivity { private static final Range PV3_VOLTAGE_RANGE = Range.create(0.0f, 3.30f); private static final Range PCS_CURRENT_RANGE = Range.create(0.0f, 3.30f); - private final NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.getDefault()); + private final NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.ROOT); /** * Step of one tap on an up or down button. @@ -214,7 +213,8 @@ public void onClick(View v) { public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_DONE) { final String voltageValue = remove(displayPV1.getText(), "V", "\\+").trim(); - final float voltage = PV1_VOLTAGE_RANGE.clamp(parseFloat(voltageValue, PV1_VOLTAGE_RANGE.getLower())); + final String decimalVoltageValue = voltageValue.replace(",", "."); + final float voltage = PV1_VOLTAGE_RANGE.clamp(parseFloat(decimalVoltageValue, PV1_VOLTAGE_RANGE.getLower())); setText(displayPV1, VOLTAGE_FORMAT, voltage); controllerPV1.setProgress(mapPowerToProgress(voltage, PV1_CONTROLLER_MAX, PV1_VOLTAGE_RANGE.getUpper(), PV1_VOLTAGE_RANGE.getLower())); @@ -228,7 +228,8 @@ public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_DONE) { final String voltageValue = remove(displayPV2.getText(), "V", "\\+").trim(); - final float voltage = PV2_VOLTAGE_RANGE.clamp(parseFloat(voltageValue, PV2_VOLTAGE_RANGE.getLower())); + final String decimalVoltageValue = voltageValue.replace(",", "."); + final float voltage = PV2_VOLTAGE_RANGE.clamp(parseFloat(decimalVoltageValue, PV2_VOLTAGE_RANGE.getLower())); setText(displayPV2, VOLTAGE_FORMAT, voltage); controllerPV2.setProgress(mapPowerToProgress(voltage, PV2_CONTROLLER_MAX, PV2_VOLTAGE_RANGE.getUpper(), PV2_VOLTAGE_RANGE.getLower())); @@ -242,7 +243,8 @@ public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_DONE) { final String voltageValue = remove(displayPV3.getText(), "V", "\\+").trim(); - final float voltage = PV3_VOLTAGE_RANGE.clamp(parseFloat(voltageValue, PV3_VOLTAGE_RANGE.getLower())); + final String decimalVoltageValue = voltageValue.replace(",", "."); + final float voltage = PV3_VOLTAGE_RANGE.clamp(parseFloat(decimalVoltageValue, PV3_VOLTAGE_RANGE.getLower())); setText(displayPV3, VOLTAGE_FORMAT, voltage); controllerPV3.setProgress(mapPowerToProgress(voltage, PV3_CONTROLLER_MAX, PV3_VOLTAGE_RANGE.getUpper(), PV3_VOLTAGE_RANGE.getLower())); @@ -256,7 +258,8 @@ public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_DONE) { final String currentValue = remove(displayPCS.getText(), "mA", "\\+").trim(); - final float current = PCS_CURRENT_RANGE.clamp(parseFloat(currentValue, PCS_CURRENT_RANGE.getLower())); + final String decimalCurrentValue = currentValue.replace(",", "."); + final float current = PCS_CURRENT_RANGE.clamp(parseFloat(decimalCurrentValue, PCS_CURRENT_RANGE.getLower())); setText(displayPV3, CURRENT_FORMAT, current); controllerPCS.setProgress(mapPowerToProgress(current, PCS_CONTROLLER_MAX, PCS_CURRENT_RANGE.getUpper(), PCS_CURRENT_RANGE.getLower())); diff --git a/app/src/main/res/layout/activity_powersource_layout.xml b/app/src/main/res/layout/activity_powersource_layout.xml index b94302126..8db4d2264 100644 --- a/app/src/main/res/layout/activity_powersource_layout.xml +++ b/app/src/main/res/layout/activity_powersource_layout.xml @@ -67,7 +67,8 @@ android:cursorVisible="false" android:gravity="center" android:imeOptions="actionDone" - android:inputType="numberDecimal" + android:inputType="numberDecimal|numberSigned" + android:digits="0123456789-.," android:padding="@dimen/power_card_medium_margin" android:singleLine="true" android:textSize="@dimen/power_card_display_text" @@ -192,7 +193,8 @@ android:cursorVisible="false" android:gravity="center" android:imeOptions="actionDone" - android:inputType="numberDecimal" + android:inputType="numberDecimal|numberSigned" + android:digits="0123456789-.," android:padding="@dimen/power_card_medium_margin" android:singleLine="true" android:textSize="@dimen/power_card_display_text" @@ -317,8 +319,8 @@ android:background="@drawable/control_edittext" android:cursorVisible="false" android:gravity="center" - android:imeOptions="actionDone" android:inputType="numberDecimal" + android:imeOptions="actionDone" android:padding="@dimen/power_card_medium_margin" android:singleLine="true" android:textSize="@dimen/power_card_display_text"