diff --git a/app/src/main/java/com/utsmm/kbz/ui/core_viewmodel/InputAmountViewModel.java b/app/src/main/java/com/utsmm/kbz/ui/core_viewmodel/InputAmountViewModel.java index a291968..8c31a80 100644 --- a/app/src/main/java/com/utsmm/kbz/ui/core_viewmodel/InputAmountViewModel.java +++ b/app/src/main/java/com/utsmm/kbz/ui/core_viewmodel/InputAmountViewModel.java @@ -13,112 +13,109 @@ import java.util.Objects; public class InputAmountViewModel extends ViewModel { - private static final String TAG = InputAmountViewModel.class.getSimpleName(); - public MutableLiveData inputAmountView = new MutableLiveData<>(); - public SingleLiveEvent invalidAmountMsg = new SingleLiveEvent<>(); - public SingleLiveEvent availableAmount = new SingleLiveEvent<>(); + private static final int MAX_INT_DIGITS = 10; + private static final int MAX_DECIMAL_DIGITS = 2; public InputAmountViewModel() { inputAmountView.setValue("0"); } - private int countDecimalDigits(double number) { - String numberStr = Double.toString(number); - int decimalIndex = numberStr.indexOf('.'); - return decimalIndex >= 0 ? numberStr.length() - decimalIndex - 1 : 0; + private String formatInteger(String value) { + if (TextUtils.isEmpty(value)) return "0"; + double amt = Double.parseDouble(value); + DecimalFormat formatter = new DecimalFormat("#,###"); + return formatter.format(amt); } - - - public NumberKeyboard.KeyClickCallback onKeyClick() { return new NumberKeyboard.KeyClickCallback() { + @Override public void onNumClick(int keyNum) { - if (inputAmountView.getValue().length() == 15) { - return; - } + String current = inputAmountView.getValue(); + if (current == null) return; - if (TextUtils.equals(inputAmountView.getValue(), "0")) { - inputAmountView.setValue(keyNum + ""); - } else if (inputAmountView.getValue().contains(".")) { - StringBuilder inputAmount = new StringBuilder(); - inputAmount.append(inputAmountView.getValue()); + String raw = current.replace(",", ""); - int dotIndex = inputAmount.indexOf("."); + if (raw.contains(".")) { - if(inputAmount.substring(dotIndex).length() > 0 && inputAmount.substring(dotIndex+1).length() == 2) { + String[] parts = raw.split("\\."); + String intPart = parts[0]; + String decimalPart = parts.length > 1 ? parts[1] : ""; + + if (decimalPart.length() >= MAX_DECIMAL_DIGITS) { return; } - if (inputAmountView.getValue().contains(",")) { - inputAmountView.setValue(inputAmountView.getValue().replace(",", "")); - } - double amt = Double.parseDouble(inputAmountView.getValue()); - if (countDecimalDigits(amt) < 2) { - inputAmountView.setValue(inputAmountView.getValue() + keyNum); - } + raw = raw + keyNum; } else { - if (inputAmountView.getValue().contains(",")) { - inputAmountView.setValue(inputAmountView.getValue().replace(",", "")); + + if (raw.length() >= MAX_INT_DIGITS) { + return; } - inputAmountView.setValue(inputAmountView.getValue() + keyNum); - double amt = Double.parseDouble(inputAmountView.getValue()); - DecimalFormat formatter = new DecimalFormat("#,###"); - inputAmountView.setValue(formatter.format(amt)); + + if (TextUtils.equals(raw, "0")) { + raw = String.valueOf(keyNum); + } else { + raw = raw + keyNum; + } + } + + if (raw.contains(".")) { + String[] parts = raw.split("\\."); + String formattedInt = formatInteger(parts[0]); + String decimal = parts.length > 1 ? parts[1] : ""; + inputAmountView.setValue(formattedInt + "." + decimal); + } else { + inputAmountView.setValue(formatInteger(raw)); } } @Override public void onDelClick() { - if (Objects.requireNonNull(inputAmountView.getValue()).contains(".")) { - return; - } - if (inputAmountView.getValue().length() == 15) { - return; - } - if (TextUtils.equals(inputAmountView.getValue(), "0")) { + + String current = inputAmountView.getValue(); + if (current == null) return; + + if (current.contains(".")) return; + + if (TextUtils.equals(current, "0")) { inputAmountView.setValue("0."); } else { - - inputAmountView.setValue(inputAmountView.getValue() + "."); + inputAmountView.setValue(current + "."); } - } @Override public void onCleanClick() { - if (inputAmountView.getValue() != null) { - if (inputAmountView.getValue().contains(".")) { - StringBuilder inputAmount = new StringBuilder(); - inputAmount.append(inputAmountView.getValue()); - inputAmount.deleteCharAt(inputAmount.length() - 1); - inputAmountView.setValue(inputAmount.toString()); - } else if (!inputAmountView.getValue().equals("0")) { - String currentValue = inputAmountView.getValue(); - String rawNumber = currentValue.replace(",", ""); + String current = inputAmountView.getValue(); + if (current == null) return; - if (rawNumber.length() > 1) { - rawNumber = rawNumber.substring(0, rawNumber.length() - 1); + String raw = current.replace(",", ""); - double amt = Double.parseDouble(rawNumber); - DecimalFormat formatter = new DecimalFormat("#,###"); - inputAmountView.setValue(formatter.format(amt)); - } else { - inputAmountView.setValue("0"); - } - } + if (raw.length() <= 1) { + inputAmountView.setValue("0"); + return; + } + + raw = raw.substring(0, raw.length() - 1); + + if (raw.contains(".")) { + String[] parts = raw.split("\\."); + String formattedInt = formatInteger(parts[0]); + String decimal = parts.length > 1 ? parts[1] : ""; + inputAmountView.setValue(formattedInt + "." + decimal); + } else { + inputAmountView.setValue(formatInteger(raw)); } } }; } - - -} +} \ No newline at end of file diff --git a/baselib/src/main/java/com/utsmyanmar/baselib/viewModel/EmvBaseViewModel.java b/baselib/src/main/java/com/utsmyanmar/baselib/viewModel/EmvBaseViewModel.java index 31ea53d..cc4b8d0 100644 --- a/baselib/src/main/java/com/utsmyanmar/baselib/viewModel/EmvBaseViewModel.java +++ b/baselib/src/main/java/com/utsmyanmar/baselib/viewModel/EmvBaseViewModel.java @@ -675,8 +675,8 @@ public abstract class EmvBaseViewModel extends BaseViewModel { } else { //contact terminal capability ; if different card brand(depend on aid) have different terminal capability if (ByteUtils.byteArray2HexString(aid).toUpperCase().contains("A000000004")) { - emvHandler.setTlv(new byte[]{(byte) 0x9F, (byte) 0x33}, new byte[]{(byte) 0xE0, (byte) 0x28, (byte) 0xC8}); - emvHandler.setTlv(new byte[]{(byte) 0x9F, (byte) 0x1D}, ByteUtils.hexString2ByteArray("6C00800000000000"));//terminal risk + emvHandler.setTlv(new byte[]{(byte) 0x9F, (byte) 0x33}, ByteUtil.hexStr2Bytes(terminalCapability)); + emvHandler.setTlv(new byte[]{(byte) 0xE0, (byte) 0x1D}, ByteUtils.hexString2ByteArray("6C00800000000000"));//terminal risk } }