Skip to content

Commit 41561f0

Browse files
Dishebhmarcnause
andauthored
fix: add support for negative values via keyboard (#2550)
Co-authored-by: Marc Nause <[email protected]>
1 parent 2fb72d2 commit 41561f0

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

app/src/main/java/io/pslab/activity/PowerSourceActivity.java

+9-6
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
import butterknife.ButterKnife;
4545
import io.pslab.R;
4646
import io.pslab.activity.guide.GuideActivity;
47-
import io.pslab.communication.CommunicationHandler;
4847
import io.pslab.communication.ScienceLab;
4948
import io.pslab.items.SquareImageButton;
5049
import io.pslab.models.PowerSourceData;
@@ -88,7 +87,7 @@ public class PowerSourceActivity extends GuideActivity {
8887
private static final Range<Float> PV3_VOLTAGE_RANGE = Range.create(0.0f, 3.30f);
8988
private static final Range<Float> PCS_CURRENT_RANGE = Range.create(0.0f, 3.30f);
9089

91-
private final NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.getDefault());
90+
private final NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.ROOT);
9291

9392
/**
9493
* Step of one tap on an up or down button.
@@ -214,7 +213,8 @@ public void onClick(View v) {
214213
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
215214
if (actionId == EditorInfo.IME_ACTION_DONE) {
216215
final String voltageValue = remove(displayPV1.getText(), "V", "\\+").trim();
217-
final float voltage = PV1_VOLTAGE_RANGE.clamp(parseFloat(voltageValue, PV1_VOLTAGE_RANGE.getLower()));
216+
final String decimalVoltageValue = voltageValue.replace(",", ".");
217+
final float voltage = PV1_VOLTAGE_RANGE.clamp(parseFloat(decimalVoltageValue, PV1_VOLTAGE_RANGE.getLower()));
218218
setText(displayPV1, VOLTAGE_FORMAT, voltage);
219219
controllerPV1.setProgress(mapPowerToProgress(voltage, PV1_CONTROLLER_MAX,
220220
PV1_VOLTAGE_RANGE.getUpper(), PV1_VOLTAGE_RANGE.getLower()));
@@ -228,7 +228,8 @@ public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
228228
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
229229
if (actionId == EditorInfo.IME_ACTION_DONE) {
230230
final String voltageValue = remove(displayPV2.getText(), "V", "\\+").trim();
231-
final float voltage = PV2_VOLTAGE_RANGE.clamp(parseFloat(voltageValue, PV2_VOLTAGE_RANGE.getLower()));
231+
final String decimalVoltageValue = voltageValue.replace(",", ".");
232+
final float voltage = PV2_VOLTAGE_RANGE.clamp(parseFloat(decimalVoltageValue, PV2_VOLTAGE_RANGE.getLower()));
232233
setText(displayPV2, VOLTAGE_FORMAT, voltage);
233234
controllerPV2.setProgress(mapPowerToProgress(voltage, PV2_CONTROLLER_MAX,
234235
PV2_VOLTAGE_RANGE.getUpper(), PV2_VOLTAGE_RANGE.getLower()));
@@ -242,7 +243,8 @@ public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
242243
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
243244
if (actionId == EditorInfo.IME_ACTION_DONE) {
244245
final String voltageValue = remove(displayPV3.getText(), "V", "\\+").trim();
245-
final float voltage = PV3_VOLTAGE_RANGE.clamp(parseFloat(voltageValue, PV3_VOLTAGE_RANGE.getLower()));
246+
final String decimalVoltageValue = voltageValue.replace(",", ".");
247+
final float voltage = PV3_VOLTAGE_RANGE.clamp(parseFloat(decimalVoltageValue, PV3_VOLTAGE_RANGE.getLower()));
246248
setText(displayPV3, VOLTAGE_FORMAT, voltage);
247249
controllerPV3.setProgress(mapPowerToProgress(voltage, PV3_CONTROLLER_MAX,
248250
PV3_VOLTAGE_RANGE.getUpper(), PV3_VOLTAGE_RANGE.getLower()));
@@ -256,7 +258,8 @@ public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
256258
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
257259
if (actionId == EditorInfo.IME_ACTION_DONE) {
258260
final String currentValue = remove(displayPCS.getText(), "mA", "\\+").trim();
259-
final float current = PCS_CURRENT_RANGE.clamp(parseFloat(currentValue, PCS_CURRENT_RANGE.getLower()));
261+
final String decimalCurrentValue = currentValue.replace(",", ".");
262+
final float current = PCS_CURRENT_RANGE.clamp(parseFloat(decimalCurrentValue, PCS_CURRENT_RANGE.getLower()));
260263
setText(displayPV3, CURRENT_FORMAT, current);
261264
controllerPCS.setProgress(mapPowerToProgress(current, PCS_CONTROLLER_MAX,
262265
PCS_CURRENT_RANGE.getUpper(), PCS_CURRENT_RANGE.getLower()));

app/src/main/res/layout/activity_powersource_layout.xml

+5-3
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@
6767
android:cursorVisible="false"
6868
android:gravity="center"
6969
android:imeOptions="actionDone"
70-
android:inputType="numberDecimal"
70+
android:inputType="numberDecimal|numberSigned"
71+
android:digits="0123456789-.,"
7172
android:padding="@dimen/power_card_medium_margin"
7273
android:singleLine="true"
7374
android:textSize="@dimen/power_card_display_text"
@@ -192,7 +193,8 @@
192193
android:cursorVisible="false"
193194
android:gravity="center"
194195
android:imeOptions="actionDone"
195-
android:inputType="numberDecimal"
196+
android:inputType="numberDecimal|numberSigned"
197+
android:digits="0123456789-.,"
196198
android:padding="@dimen/power_card_medium_margin"
197199
android:singleLine="true"
198200
android:textSize="@dimen/power_card_display_text"
@@ -317,8 +319,8 @@
317319
android:background="@drawable/control_edittext"
318320
android:cursorVisible="false"
319321
android:gravity="center"
320-
android:imeOptions="actionDone"
321322
android:inputType="numberDecimal"
323+
android:imeOptions="actionDone"
322324
android:padding="@dimen/power_card_medium_margin"
323325
android:singleLine="true"
324326
android:textSize="@dimen/power_card_display_text"

0 commit comments

Comments
 (0)