Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

Commit

Permalink
fix: Negative value handling
Browse files Browse the repository at this point in the history
  • Loading branch information
reasje committed Sep 7, 2023
1 parent 17e755f commit 49d0df0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ class SendCryptoPage extends HookConsumerWidget {
? () {
FocusManager.instance.primaryFocus?.unfocus();

if (!ref.watch(state).formKey.currentState!.validate())
if (!ref.watch(state).formKey.currentState!.validate()) {
ref.watch(presenter).onValidChange();
return;
}

ref.read(presenter).transactionProcess();
}
Expand Down Expand Up @@ -108,11 +110,24 @@ class SendCryptoPage extends HookConsumerWidget {
controller: ref.read(presenter).amountController,
keyboardType: TextInputType.number,
action: TextInputAction.next,
validator: (v) => Validation.notEmpty(
context,
v,
translate('x_not_empty')
.replaceFirst('{0}', translate('amount'))),
validator: (v) {
final res = Validation.notEmpty(
context,
v,
translate('x_not_empty')
.replaceFirst('{0}', translate('amount')));
if (res != null) {
return res;
}
try {
if (int.parse(v!).isNegative) {
return translate('invalid_format');
}
return null;
} catch (e) {
return translate('invalid_format');
}
},
hint: 'e.g 100',
suffixText: token.symbol,
suffixButton: MxcTextFieldButton.text(
Expand Down Expand Up @@ -155,11 +170,14 @@ class SendCryptoPage extends HookConsumerWidget {
controller: ref.read(presenter).recipientController,
action: TextInputAction.done,
validator: (v) {
Validation.notEmpty(
final res = Validation.notEmpty(
context,
v,
translate('x_not_empty')
.replaceFirst('{0}', translate('recipient')));
if (res != null) {
return res;
}
if (v!.startsWith('0x')) {
return Validation.checkEthereumAddress(context, v);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ class SendCryptoPresenter extends CompletePresenter<SendCryptoState> {
}
});

amountController.addListener(_onValidChange);
recipientController.addListener(_onValidChange);
amountController.addListener(onValidChange);
recipientController.addListener(onValidChange);

recipientController.text = state.qrCode ?? '';
}
Expand All @@ -86,7 +86,7 @@ class SendCryptoPresenter extends CompletePresenter<SendCryptoState> {
notify(() => state.discount = value);
}

void _onValidChange() {
void onValidChange() {
final result = state.formKey.currentState!.validate();
notify(() => state.valid = result);
}
Expand Down Expand Up @@ -190,7 +190,7 @@ class SendCryptoPresenter extends CompletePresenter<SendCryptoState> {
Future<void> dispose() async {
super.dispose();

amountController.removeListener(_onValidChange);
recipientController.removeListener(_onValidChange);
amountController.removeListener(onValidChange);
recipientController.removeListener(onValidChange);
}
}

0 comments on commit 49d0df0

Please sign in to comment.