Skip to content

Commit

Permalink
update acala input validators
Browse files Browse the repository at this point in the history
  • Loading branch information
RomeroYang committed Aug 17, 2020
1 parent 62ce502 commit b2b3852
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 20 deletions.
37 changes: 30 additions & 7 deletions lib/page-acala/earn/addLiquidityPage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ class _AddLiquidityPageState extends State<AddLiquidityPage> {

Future<void> _onSupplyAmountChange(String v, double swapRatio) async {
String supply = v.trim();
if (supply.isEmpty) {
try {
if (supply.isEmpty || double.parse(supply) == 0) {
return;
}
} catch (err) {
return;
}
setState(() {
Expand All @@ -60,7 +64,11 @@ class _AddLiquidityPageState extends State<AddLiquidityPage> {

Future<void> _onTargetAmountChange(String v, double swapRatio) async {
String target = v.trim();
if (target.isEmpty) {
try {
if (target.isEmpty || double.parse(target) == 0) {
return;
}
} catch (err) {
return;
}
setState(() {
Expand Down Expand Up @@ -153,9 +161,14 @@ class _AddLiquidityPageState extends State<AddLiquidityPage> {
amountTokenUser = amountToken * userShare;

String input = _amountTokenCtrl.text.trim();
double amountInput = double.parse(input.isEmpty ? '0' : input);
userShareNew =
(amountInput + amountTokenUser) / (amountInput + amountToken);
try {
final double amountInput =
double.parse(input.isEmpty ? '0' : input);
userShareNew =
(amountInput + amountTokenUser) / (amountInput + amountToken);
} catch (_) {
// parse double failed
}
}

double swapRatio =
Expand Down Expand Up @@ -228,7 +241,12 @@ class _AddLiquidityPageState extends State<AddLiquidityPage> {
keyboardType: TextInputType.numberWithOptions(
decimal: true),
validator: (v) {
if (v.isEmpty) {
try {
if (v.trim().isEmpty ||
double.parse(v.trim()) == 0) {
return dicAssets['amount.error'];
}
} catch (err) {
return dicAssets['amount.error'];
}
if (Fmt.tokenInt(v.trim(),
Expand Down Expand Up @@ -268,7 +286,12 @@ class _AddLiquidityPageState extends State<AddLiquidityPage> {
keyboardType: TextInputType.numberWithOptions(
decimal: true),
validator: (v) {
if (v.isEmpty) {
try {
if (v.trim().isEmpty ||
double.parse(v.trim()) == 0) {
return dicAssets['amount.error'];
}
} catch (err) {
return dicAssets['amount.error'];
}
if (Fmt.tokenInt(v.trim(),
Expand Down
7 changes: 6 additions & 1 deletion lib/page-acala/earn/withdrawLiquidityPage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,12 @@ class _WithdrawLiquidityPageState extends State<WithdrawLiquidityPage> {
keyboardType:
TextInputType.numberWithOptions(decimal: true),
validator: (v) {
if (v.isEmpty) {
try {
if (v.trim().isEmpty ||
double.parse(v.trim()) == 0) {
return dicAssets['amount.error'];
}
} catch (err) {
return dicAssets['amount.error'];
}
if (_shareInput > shareInt) {
Expand Down
7 changes: 6 additions & 1 deletion lib/page-acala/homa/mintPage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,12 @@ class _MintPageState extends State<MintPage> {
TextInputType.numberWithOptions(
decimal: true),
validator: (v) {
if (v.isEmpty) {
try {
if (v.isEmpty ||
double.parse(v) == 0) {
return dicAssets['amount.error'];
}
} catch (err) {
return dicAssets['amount.error'];
}
if (double.parse(v.trim()) >=
Expand Down
10 changes: 8 additions & 2 deletions lib/page-acala/homa/redeemPage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,16 @@ class _HomaRedeemPageState extends State<HomaRedeemPage> {
TextInputType.numberWithOptions(
decimal: true),
validator: (v) {
if (v.isEmpty) {
double amt;
try {
amt = double.parse(v.trim());
if (v.trim().isEmpty || amt == 0) {
return dicAssets['amount.error'];
}
} catch (err) {
return dicAssets['amount.error'];
}
if (double.parse(v.trim()) >=
if (amt >=
Fmt.bigIntToDouble(balance,
decimals: decimals)) {
return dicAssets['amount.low'];
Expand Down
14 changes: 8 additions & 6 deletions lib/page-acala/loan/loanAdjustPage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:polka_wallet/common/components/roundedButton.dart';
import 'package:polka_wallet/common/consts/settings.dart';
import 'package:polka_wallet/common/regInputFormatter.dart';
import 'package:polka_wallet/page-acala/loan/loanInfoPanel.dart';
import 'package:polka_wallet/page/account/txConfirmPage.dart';
import 'package:polka_wallet/store/acala/types/loanType.dart';
Expand Down Expand Up @@ -164,7 +163,11 @@ class _LoanAdjustPageState extends State<LoanAdjustPage> {
final Map assetDic = I18n.of(context).assets;

String v = value.trim();
if (v.isEmpty || double.parse(v) == 0) {
try {
if (v.isEmpty || double.parse(v) == 0) {
return assetDic['amount.error'];
}
} catch (err) {
return assetDic['amount.error'];
}
if (_amountCollateral > available) {
Expand All @@ -184,11 +187,10 @@ class _LoanAdjustPageState extends State<LoanAdjustPage> {
final Map dic = I18n.of(context).acala;

String v = value.trim();
if (v.isEmpty) {
return assetDic['amount.error'];
}
try {
final double amt = double.parse(v);
if (v.isEmpty || double.parse(v) == 0) {
return assetDic['amount.error'];
}
} catch (err) {
return assetDic['amount.error'];
}
Expand Down
6 changes: 5 additions & 1 deletion lib/page-acala/loan/loanCreatePage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,11 @@ class _LoanCreatePageState extends State<LoanCreatePage> {
final Map dic = I18n.of(context).acala;

String v = value.trim();
if (v.isEmpty) {
try {
if (v.isEmpty || double.parse(v) < 1) {
return assetDic['amount.error'];
}
} catch (err) {
return assetDic['amount.error'];
}
BigInt debits = Fmt.tokenInt(v, decimals: acala_token_decimals);
Expand Down
16 changes: 14 additions & 2 deletions lib/page-acala/swap/swapPage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,13 @@ class _SwapPageState extends State<SwapPage> {
TextInputType.numberWithOptions(
decimal: true),
validator: (v) {
if (v.isEmpty) {
try {
if (v.isEmpty ||
double.parse(v) == 0) {
return dicAssets[
'amount.error'];
}
} catch (err) {
return dicAssets[
'amount.error'];
}
Expand Down Expand Up @@ -384,7 +390,13 @@ class _SwapPageState extends State<SwapPage> {
TextInputType.numberWithOptions(
decimal: true),
validator: (v) {
if (v.isEmpty) {
try {
if (v.isEmpty ||
double.parse(v) == 0) {
return dicAssets[
'amount.error'];
}
} catch (err) {
return dicAssets[
'amount.error'];
}
Expand Down

0 comments on commit b2b3852

Please sign in to comment.