amount fix
This commit is contained in:
parent
89252a6732
commit
d8d1a5c1d5
@ -13,112 +13,109 @@ import java.util.Objects;
|
|||||||
|
|
||||||
public class InputAmountViewModel extends ViewModel {
|
public class InputAmountViewModel extends ViewModel {
|
||||||
|
|
||||||
private static final String TAG = InputAmountViewModel.class.getSimpleName();
|
|
||||||
|
|
||||||
public MutableLiveData<String> inputAmountView = new MutableLiveData<>();
|
public MutableLiveData<String> inputAmountView = new MutableLiveData<>();
|
||||||
|
|
||||||
public SingleLiveEvent<String> invalidAmountMsg = new SingleLiveEvent<>();
|
public SingleLiveEvent<String> invalidAmountMsg = new SingleLiveEvent<>();
|
||||||
|
|
||||||
public SingleLiveEvent<String> availableAmount = new SingleLiveEvent<>();
|
public SingleLiveEvent<String> availableAmount = new SingleLiveEvent<>();
|
||||||
|
|
||||||
|
private static final int MAX_INT_DIGITS = 10;
|
||||||
|
private static final int MAX_DECIMAL_DIGITS = 2;
|
||||||
|
|
||||||
public InputAmountViewModel() {
|
public InputAmountViewModel() {
|
||||||
inputAmountView.setValue("0");
|
inputAmountView.setValue("0");
|
||||||
}
|
}
|
||||||
|
|
||||||
private int countDecimalDigits(double number) {
|
private String formatInteger(String value) {
|
||||||
String numberStr = Double.toString(number);
|
if (TextUtils.isEmpty(value)) return "0";
|
||||||
int decimalIndex = numberStr.indexOf('.');
|
double amt = Double.parseDouble(value);
|
||||||
return decimalIndex >= 0 ? numberStr.length() - decimalIndex - 1 : 0;
|
DecimalFormat formatter = new DecimalFormat("#,###");
|
||||||
|
return formatter.format(amt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public NumberKeyboard.KeyClickCallback onKeyClick() {
|
public NumberKeyboard.KeyClickCallback onKeyClick() {
|
||||||
return new NumberKeyboard.KeyClickCallback() {
|
return new NumberKeyboard.KeyClickCallback() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onNumClick(int keyNum) {
|
public void onNumClick(int keyNum) {
|
||||||
|
|
||||||
if (inputAmountView.getValue().length() == 15) {
|
String current = inputAmountView.getValue();
|
||||||
return;
|
if (current == null) return;
|
||||||
}
|
|
||||||
|
|
||||||
if (TextUtils.equals(inputAmountView.getValue(), "0")) {
|
String raw = current.replace(",", "");
|
||||||
inputAmountView.setValue(keyNum + "");
|
|
||||||
} else if (inputAmountView.getValue().contains(".")) {
|
|
||||||
StringBuilder inputAmount = new StringBuilder();
|
|
||||||
inputAmount.append(inputAmountView.getValue());
|
|
||||||
|
|
||||||
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (inputAmountView.getValue().contains(",")) {
|
raw = raw + keyNum;
|
||||||
inputAmountView.setValue(inputAmountView.getValue().replace(",", ""));
|
|
||||||
}
|
|
||||||
double amt = Double.parseDouble(inputAmountView.getValue());
|
|
||||||
if (countDecimalDigits(amt) < 2) {
|
|
||||||
inputAmountView.setValue(inputAmountView.getValue() + keyNum);
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
} 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());
|
if (TextUtils.equals(raw, "0")) {
|
||||||
DecimalFormat formatter = new DecimalFormat("#,###");
|
raw = String.valueOf(keyNum);
|
||||||
inputAmountView.setValue(formatter.format(amt));
|
} 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
|
@Override
|
||||||
public void onDelClick() {
|
public void onDelClick() {
|
||||||
if (Objects.requireNonNull(inputAmountView.getValue()).contains(".")) {
|
|
||||||
return;
|
String current = inputAmountView.getValue();
|
||||||
}
|
if (current == null) return;
|
||||||
if (inputAmountView.getValue().length() == 15) {
|
|
||||||
return;
|
if (current.contains(".")) return;
|
||||||
}
|
|
||||||
if (TextUtils.equals(inputAmountView.getValue(), "0")) {
|
if (TextUtils.equals(current, "0")) {
|
||||||
inputAmountView.setValue("0.");
|
inputAmountView.setValue("0.");
|
||||||
} else {
|
} else {
|
||||||
|
inputAmountView.setValue(current + ".");
|
||||||
inputAmountView.setValue(inputAmountView.getValue() + ".");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCleanClick() {
|
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) {
|
String raw = current.replace(",", "");
|
||||||
rawNumber = rawNumber.substring(0, rawNumber.length() - 1);
|
|
||||||
|
|
||||||
double amt = Double.parseDouble(rawNumber);
|
if (raw.length() <= 1) {
|
||||||
DecimalFormat formatter = new DecimalFormat("#,###");
|
inputAmountView.setValue("0");
|
||||||
inputAmountView.setValue(formatter.format(amt));
|
return;
|
||||||
} else {
|
}
|
||||||
inputAmountView.setValue("0");
|
|
||||||
}
|
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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -675,8 +675,8 @@ public abstract class EmvBaseViewModel extends BaseViewModel {
|
|||||||
} else {
|
} else {
|
||||||
//contact terminal capability ; if different card brand(depend on aid) have different terminal capability
|
//contact terminal capability ; if different card brand(depend on aid) have different terminal capability
|
||||||
if (ByteUtils.byteArray2HexString(aid).toUpperCase().contains("A000000004")) {
|
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) 0x33}, ByteUtil.hexStr2Bytes(terminalCapability));
|
||||||
emvHandler.setTlv(new byte[]{(byte) 0x9F, (byte) 0x1D}, ByteUtils.hexString2ByteArray("6C00800000000000"));//terminal risk
|
emvHandler.setTlv(new byte[]{(byte) 0xE0, (byte) 0x1D}, ByteUtils.hexString2ByteArray("6C00800000000000"));//terminal risk
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user