CAKE-345 | reworked send page for batch sending; added send_item.dart, send_card.dart, send_template_view_model.dart and parse_address_from_domain_alert.dart to the app; reworked send template page; fixed exchange_trade_page.dart and exchange_trade_view_model.dart
parent
96ea969519
commit
e6cfa2b636
@ -0,0 +1,17 @@
|
|||||||
|
import 'package:cake_wallet/src/widgets/alert_with_one_action.dart';
|
||||||
|
import 'package:cake_wallet/utils/show_pop_up.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:cake_wallet/generated/i18n.dart';
|
||||||
|
|
||||||
|
void showAddressAlert(BuildContext context, String title, String content) async {
|
||||||
|
await showPopUp<void>(
|
||||||
|
context: context,
|
||||||
|
builder: (BuildContext context) {
|
||||||
|
|
||||||
|
return AlertWithOneAction(
|
||||||
|
alertTitle: title,
|
||||||
|
alertContent: content,
|
||||||
|
buttonText: S.of(context).ok,
|
||||||
|
buttonAction: () => Navigator.of(context).pop());
|
||||||
|
});
|
||||||
|
}
|
@ -0,0 +1,521 @@
|
|||||||
|
import 'dart:ui';
|
||||||
|
import 'package:cake_wallet/entities/transaction_priority.dart';
|
||||||
|
import 'package:cake_wallet/src/screens/send/widgets/parse_address_from_domain_alert.dart';
|
||||||
|
import 'package:cake_wallet/src/widgets/keyboard_done_button.dart';
|
||||||
|
import 'package:cake_wallet/src/widgets/picker.dart';
|
||||||
|
import 'package:cake_wallet/view_model/send/send_item.dart';
|
||||||
|
import 'package:cake_wallet/view_model/settings/settings_view_model.dart';
|
||||||
|
import 'package:flutter/cupertino.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter/services.dart';
|
||||||
|
import 'package:flutter_mobx/flutter_mobx.dart';
|
||||||
|
import 'package:mobx/mobx.dart';
|
||||||
|
import 'package:keyboard_actions/keyboard_actions.dart';
|
||||||
|
import 'package:cake_wallet/view_model/send/send_view_model.dart';
|
||||||
|
import 'package:cake_wallet/utils/show_pop_up.dart';
|
||||||
|
import 'package:cake_wallet/src/widgets/address_text_field.dart';
|
||||||
|
import 'package:cake_wallet/generated/i18n.dart';
|
||||||
|
import 'package:cake_wallet/src/widgets/base_text_form_field.dart';
|
||||||
|
|
||||||
|
class SendCard extends StatefulWidget {
|
||||||
|
SendCard({Key key, @required this.item, @required this.sendViewModel}) : super(key: key);
|
||||||
|
|
||||||
|
final SendItem item;
|
||||||
|
final SendViewModel sendViewModel;
|
||||||
|
|
||||||
|
@override
|
||||||
|
SendCardState createState() => SendCardState(
|
||||||
|
item: item,
|
||||||
|
sendViewModel: sendViewModel
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
class SendCardState extends State<SendCard>
|
||||||
|
with AutomaticKeepAliveClientMixin<SendCard> {
|
||||||
|
SendCardState({@required this.item, @required this.sendViewModel})
|
||||||
|
: addressController = TextEditingController(),
|
||||||
|
cryptoAmountController = TextEditingController(),
|
||||||
|
fiatAmountController = TextEditingController(),
|
||||||
|
noteController = TextEditingController(),
|
||||||
|
cryptoAmountFocus = FocusNode(),
|
||||||
|
fiatAmountFocus = FocusNode(),
|
||||||
|
addressFocusNode = FocusNode();
|
||||||
|
|
||||||
|
static const prefixIconWidth = 34.0;
|
||||||
|
static const prefixIconHeight = 34.0;
|
||||||
|
|
||||||
|
final SendItem item;
|
||||||
|
final SendViewModel sendViewModel;
|
||||||
|
|
||||||
|
final TextEditingController addressController;
|
||||||
|
final TextEditingController cryptoAmountController;
|
||||||
|
final TextEditingController fiatAmountController;
|
||||||
|
final TextEditingController noteController;
|
||||||
|
final FocusNode cryptoAmountFocus;
|
||||||
|
final FocusNode fiatAmountFocus;
|
||||||
|
final FocusNode addressFocusNode;
|
||||||
|
|
||||||
|
bool _effectsInstalled = false;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
super.build(context);
|
||||||
|
_setEffects(context);
|
||||||
|
|
||||||
|
return KeyboardActions(
|
||||||
|
config: KeyboardActionsConfig(
|
||||||
|
keyboardActionsPlatform: KeyboardActionsPlatform.IOS,
|
||||||
|
keyboardBarColor: Theme.of(context).accentTextTheme.body2
|
||||||
|
.backgroundColor,
|
||||||
|
nextFocus: false,
|
||||||
|
actions: [
|
||||||
|
KeyboardActionsItem(
|
||||||
|
focusNode: cryptoAmountFocus,
|
||||||
|
toolbarButtons: [(_) => KeyboardDoneButton()],
|
||||||
|
),
|
||||||
|
KeyboardActionsItem(
|
||||||
|
focusNode: fiatAmountFocus,
|
||||||
|
toolbarButtons: [(_) => KeyboardDoneButton()],
|
||||||
|
)
|
||||||
|
]),
|
||||||
|
child: Container(
|
||||||
|
height: 445,
|
||||||
|
decoration: BoxDecoration(
|
||||||
|
borderRadius: BorderRadius.only(
|
||||||
|
bottomLeft: Radius.circular(24),
|
||||||
|
bottomRight: Radius.circular(24)),
|
||||||
|
gradient: LinearGradient(colors: [
|
||||||
|
Theme.of(context).primaryTextTheme.subhead.color,
|
||||||
|
Theme.of(context)
|
||||||
|
.primaryTextTheme
|
||||||
|
.subhead
|
||||||
|
.decorationColor,
|
||||||
|
], begin: Alignment.topLeft, end: Alignment.bottomRight),
|
||||||
|
),
|
||||||
|
child: Padding(
|
||||||
|
padding: EdgeInsets.fromLTRB(24, 80, 24, 32),
|
||||||
|
child: SingleChildScrollView(
|
||||||
|
child: Column(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
children: <Widget>[
|
||||||
|
AddressTextField(
|
||||||
|
focusNode: addressFocusNode,
|
||||||
|
controller: addressController,
|
||||||
|
onURIScanned: (uri) {
|
||||||
|
var address = '';
|
||||||
|
var amount = '';
|
||||||
|
|
||||||
|
if (uri != null) {
|
||||||
|
address = uri.path;
|
||||||
|
amount = uri.queryParameters['tx_amount'] ??
|
||||||
|
uri.queryParameters['amount'];
|
||||||
|
} else {
|
||||||
|
address = uri.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
addressController.text = address;
|
||||||
|
cryptoAmountController.text = amount;
|
||||||
|
},
|
||||||
|
options: [
|
||||||
|
AddressTextFieldOption.paste,
|
||||||
|
AddressTextFieldOption.qrCode,
|
||||||
|
AddressTextFieldOption.addressBook
|
||||||
|
],
|
||||||
|
buttonColor: Theme.of(context)
|
||||||
|
.primaryTextTheme
|
||||||
|
.display1
|
||||||
|
.color,
|
||||||
|
borderColor: Theme.of(context)
|
||||||
|
.primaryTextTheme
|
||||||
|
.headline
|
||||||
|
.color,
|
||||||
|
textStyle: TextStyle(
|
||||||
|
fontSize: 14,
|
||||||
|
fontWeight: FontWeight.w500,
|
||||||
|
color: Colors.white),
|
||||||
|
hintStyle: TextStyle(
|
||||||
|
fontSize: 14,
|
||||||
|
fontWeight: FontWeight.w500,
|
||||||
|
color: Theme.of(context)
|
||||||
|
.primaryTextTheme
|
||||||
|
.headline
|
||||||
|
.decorationColor),
|
||||||
|
validator: sendViewModel.addressValidator,
|
||||||
|
),
|
||||||
|
Observer(
|
||||||
|
builder: (_) => Padding(
|
||||||
|
padding: const EdgeInsets.only(top: 20),
|
||||||
|
child: Stack(
|
||||||
|
children: [
|
||||||
|
BaseTextFormField(
|
||||||
|
focusNode: cryptoAmountFocus,
|
||||||
|
controller: cryptoAmountController,
|
||||||
|
keyboardType:
|
||||||
|
TextInputType.numberWithOptions(
|
||||||
|
signed: false, decimal: true),
|
||||||
|
prefixIcon: Padding(
|
||||||
|
padding: EdgeInsets.only(top: 9),
|
||||||
|
child: Text(
|
||||||
|
sendViewModel.currency.title +
|
||||||
|
':',
|
||||||
|
style: TextStyle(
|
||||||
|
fontSize: 16,
|
||||||
|
fontWeight: FontWeight.w600,
|
||||||
|
color: Colors.white,
|
||||||
|
)),
|
||||||
|
),
|
||||||
|
suffixIcon: SizedBox(
|
||||||
|
width: prefixIconWidth,
|
||||||
|
),
|
||||||
|
hintText: '0.0000',
|
||||||
|
borderColor: Theme.of(context)
|
||||||
|
.primaryTextTheme
|
||||||
|
.headline
|
||||||
|
.color,
|
||||||
|
textStyle: TextStyle(
|
||||||
|
fontSize: 14,
|
||||||
|
fontWeight: FontWeight.w500,
|
||||||
|
color: Colors.white),
|
||||||
|
placeholderTextStyle: TextStyle(
|
||||||
|
color: Theme.of(context)
|
||||||
|
.primaryTextTheme
|
||||||
|
.headline
|
||||||
|
.decorationColor,
|
||||||
|
fontWeight: FontWeight.w500,
|
||||||
|
fontSize: 14),
|
||||||
|
validator: item.sendAll
|
||||||
|
? sendViewModel.allAmountValidator
|
||||||
|
: sendViewModel
|
||||||
|
.amountValidator),
|
||||||
|
Positioned(
|
||||||
|
top: 2,
|
||||||
|
right: 0,
|
||||||
|
child: Container(
|
||||||
|
width: prefixIconWidth,
|
||||||
|
height: prefixIconHeight,
|
||||||
|
child: InkWell(
|
||||||
|
onTap: () async =>
|
||||||
|
item.setSendAll(),
|
||||||
|
child: Container(
|
||||||
|
decoration: BoxDecoration(
|
||||||
|
color: Theme.of(context)
|
||||||
|
.primaryTextTheme
|
||||||
|
.display1
|
||||||
|
.color,
|
||||||
|
borderRadius:
|
||||||
|
BorderRadius.all(
|
||||||
|
Radius.circular(6))),
|
||||||
|
child: Center(
|
||||||
|
child: Text(
|
||||||
|
S.of(context).all,
|
||||||
|
textAlign:
|
||||||
|
TextAlign.center,
|
||||||
|
style: TextStyle(
|
||||||
|
fontSize: 12,
|
||||||
|
fontWeight:
|
||||||
|
FontWeight.bold,
|
||||||
|
color:
|
||||||
|
Theme.of(context)
|
||||||
|
.primaryTextTheme
|
||||||
|
.display1
|
||||||
|
.decorationColor))),
|
||||||
|
))))])
|
||||||
|
)),
|
||||||
|
Observer(
|
||||||
|
builder: (_) => Padding(
|
||||||
|
padding: EdgeInsets.only(top: 10),
|
||||||
|
child: Row(
|
||||||
|
mainAxisSize: MainAxisSize.max,
|
||||||
|
mainAxisAlignment:
|
||||||
|
MainAxisAlignment.spaceBetween,
|
||||||
|
children: <Widget>[
|
||||||
|
Expanded(
|
||||||
|
child: Text(
|
||||||
|
S.of(context).available_balance +
|
||||||
|
':',
|
||||||
|
style: TextStyle(
|
||||||
|
fontSize: 12,
|
||||||
|
fontWeight: FontWeight.w600,
|
||||||
|
color: Theme.of(context)
|
||||||
|
.primaryTextTheme
|
||||||
|
.headline
|
||||||
|
.decorationColor),
|
||||||
|
)),
|
||||||
|
Text(
|
||||||
|
sendViewModel.balance,
|
||||||
|
style: TextStyle(
|
||||||
|
fontSize: 12,
|
||||||
|
fontWeight: FontWeight.w600,
|
||||||
|
color: Theme.of(context)
|
||||||
|
.primaryTextTheme
|
||||||
|
.headline
|
||||||
|
.decorationColor),
|
||||||
|
)
|
||||||
|
],
|
||||||
|
),
|
||||||
|
)),
|
||||||
|
Padding(
|
||||||
|
padding: const EdgeInsets.only(top: 20),
|
||||||
|
child: BaseTextFormField(
|
||||||
|
focusNode: fiatAmountFocus,
|
||||||
|
controller: fiatAmountController,
|
||||||
|
keyboardType:
|
||||||
|
TextInputType.numberWithOptions(
|
||||||
|
signed: false, decimal: true),
|
||||||
|
prefixIcon: Padding(
|
||||||
|
padding: EdgeInsets.only(top: 9),
|
||||||
|
child:
|
||||||
|
Text(sendViewModel.fiat.title + ':',
|
||||||
|
style: TextStyle(
|
||||||
|
fontSize: 16,
|
||||||
|
fontWeight: FontWeight.w600,
|
||||||
|
color: Colors.white,
|
||||||
|
)),
|
||||||
|
),
|
||||||
|
hintText: '0.00',
|
||||||
|
borderColor: Theme.of(context)
|
||||||
|
.primaryTextTheme
|
||||||
|
.headline
|
||||||
|
.color,
|
||||||
|
textStyle: TextStyle(
|
||||||
|
fontSize: 14,
|
||||||
|
fontWeight: FontWeight.w500,
|
||||||
|
color: Colors.white),
|
||||||
|
placeholderTextStyle: TextStyle(
|
||||||
|
color: Theme.of(context)
|
||||||
|
.primaryTextTheme
|
||||||
|
.headline
|
||||||
|
.decorationColor,
|
||||||
|
fontWeight: FontWeight.w500,
|
||||||
|
fontSize: 14),
|
||||||
|
)),
|
||||||
|
Padding(
|
||||||
|
padding: EdgeInsets.only(top: 20),
|
||||||
|
child: BaseTextFormField(
|
||||||
|
controller: noteController,
|
||||||
|
keyboardType: TextInputType.multiline,
|
||||||
|
maxLines: null,
|
||||||
|
borderColor: Theme.of(context)
|
||||||
|
.primaryTextTheme
|
||||||
|
.headline
|
||||||
|
.color,
|
||||||
|
textStyle: TextStyle(
|
||||||
|
fontSize: 14,
|
||||||
|
fontWeight: FontWeight.w500,
|
||||||
|
color: Colors.white),
|
||||||
|
hintText: S.of(context).note_optional,
|
||||||
|
placeholderTextStyle: TextStyle(
|
||||||
|
fontSize: 14,
|
||||||
|
fontWeight: FontWeight.w500,
|
||||||
|
color: Theme.of(context)
|
||||||
|
.primaryTextTheme
|
||||||
|
.headline
|
||||||
|
.decorationColor),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
Observer(
|
||||||
|
builder: (_) => GestureDetector(
|
||||||
|
onTap: () =>
|
||||||
|
_setTransactionPriority(context),
|
||||||
|
child: Container(
|
||||||
|
padding: EdgeInsets.only(top: 24),
|
||||||
|
child: Row(
|
||||||
|
mainAxisAlignment:
|
||||||
|
MainAxisAlignment.spaceBetween,
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
|
children: <Widget>[
|
||||||
|
Text(
|
||||||
|
S
|
||||||
|
.of(context)
|
||||||
|
.send_estimated_fee,
|
||||||
|
style: TextStyle(
|
||||||
|
fontSize: 12,
|
||||||
|
fontWeight:
|
||||||
|
FontWeight.w500,
|
||||||
|
//color: Theme.of(context).primaryTextTheme.display2.color,
|
||||||
|
color: Colors.white)),
|
||||||
|
Container(
|
||||||
|
child: Row(
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
|
children: <Widget>[
|
||||||
|
Column(
|
||||||
|
mainAxisAlignment: MainAxisAlignment.start,
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.end,
|
||||||
|
children: [
|
||||||
|
Text(
|
||||||
|
item
|
||||||
|
.estimatedFee
|
||||||
|
.toString() +
|
||||||
|
' ' +
|
||||||
|
sendViewModel
|
||||||
|
.currency.title,
|
||||||
|
style: TextStyle(
|
||||||
|
fontSize: 12,
|
||||||
|
fontWeight:
|
||||||
|
FontWeight.w600,
|
||||||
|
//color: Theme.of(context).primaryTextTheme.display2.color,
|
||||||
|
color:
|
||||||
|
Colors.white)),
|
||||||
|
Padding(
|
||||||
|
padding:
|
||||||
|
EdgeInsets.only(top: 5),
|
||||||
|
child: Text(
|
||||||
|
item
|
||||||
|
.estimatedFeeFiatAmount
|
||||||
|
+ ' ' +
|
||||||
|
sendViewModel
|
||||||
|
.fiat.title,
|
||||||
|
style: TextStyle(
|
||||||
|
fontSize: 12,
|
||||||
|
fontWeight:
|
||||||
|
FontWeight.w600,
|
||||||
|
color: Theme
|
||||||
|
.of(context)
|
||||||
|
.primaryTextTheme
|
||||||
|
.headline
|
||||||
|
.decorationColor))
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
Padding(
|
||||||
|
padding: EdgeInsets.only(
|
||||||
|
top: 2,
|
||||||
|
left: 5),
|
||||||
|
child: Icon(
|
||||||
|
Icons.arrow_forward_ios,
|
||||||
|
size: 12,
|
||||||
|
color: Colors.white,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
],
|
||||||
|
),
|
||||||
|
)
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
))
|
||||||
|
],
|
||||||
|
)
|
||||||
|
),
|
||||||
|
),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
void _setEffects(BuildContext context) {
|
||||||
|
addressController.text = item.address;
|
||||||
|
cryptoAmountController.text = item.cryptoAmount;
|
||||||
|
fiatAmountController.text = item.fiatAmount;
|
||||||
|
noteController.text = item.note;
|
||||||
|
|
||||||
|
if (_effectsInstalled) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
cryptoAmountController.addListener(() {
|
||||||
|
final amount = cryptoAmountController.text;
|
||||||
|
|
||||||
|
if (item.sendAll && amount != S.current.all) {
|
||||||
|
item.sendAll = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (amount != item.cryptoAmount) {
|
||||||
|
item.setCryptoAmount(amount);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
fiatAmountController.addListener(() {
|
||||||
|
final amount = fiatAmountController.text;
|
||||||
|
|
||||||
|
if (amount != item.fiatAmount) {
|
||||||
|
item.sendAll = false;
|
||||||
|
item.setFiatAmount(amount);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
noteController.addListener(() {
|
||||||
|
final note = noteController.text ?? '';
|
||||||
|
|
||||||
|
if (note != item.note) {
|
||||||
|
item.note = note;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
reaction((_) => item.sendAll, (bool all) {
|
||||||
|
if (all) {
|
||||||
|
cryptoAmountController.text = S.current.all;
|
||||||
|
fiatAmountController.text = null;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
reaction((_) => item.fiatAmount, (String amount) {
|
||||||
|
if (amount != fiatAmountController.text) {
|
||||||
|
fiatAmountController.text = amount;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
reaction((_) => item.cryptoAmount, (String amount) {
|
||||||
|
if (item.sendAll && amount != S.current.all) {
|
||||||
|
item.sendAll = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (amount != cryptoAmountController.text) {
|
||||||
|
cryptoAmountController.text = amount;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
reaction((_) => item.address, (String address) {
|
||||||
|
if (address != addressController.text) {
|
||||||
|
addressController.text = address;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
addressController.addListener(() {
|
||||||
|
final address = addressController.text;
|
||||||
|
|
||||||
|
if (item.address != address) {
|
||||||
|
item.address = address;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
reaction((_) => item.note, (String note) {
|
||||||
|
if (note != noteController.text) {
|
||||||
|
noteController.text = note;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
addressFocusNode.addListener(() async {
|
||||||
|
if (!addressFocusNode.hasFocus && addressController.text.isNotEmpty) {
|
||||||
|
final record = await item.getOpenaliasRecord();
|
||||||
|
|
||||||
|
if (record != null) {
|
||||||
|
showAddressAlert(
|
||||||
|
context,
|
||||||
|
S.current.openalias_alert_title,
|
||||||
|
S.current.openalias_alert_content(record.name));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
_effectsInstalled = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _setTransactionPriority(BuildContext context) async {
|
||||||
|
final items = priorityForWalletType(sendViewModel.walletType);
|
||||||
|
final selectedItem = items.indexOf(sendViewModel.transactionPriority);
|
||||||
|
|
||||||
|
await showPopUp<void>(
|
||||||
|
builder: (_) => Picker(
|
||||||
|
items: items,
|
||||||
|
displayItem: sendViewModel.displayFeeRate,
|
||||||
|
selectedAtIndex: selectedItem,
|
||||||
|
title: S.of(context).please_select,
|
||||||
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
|
onItemSelected: (TransactionPriority priority) =>
|
||||||
|
sendViewModel.setTransactionPriority(priority),
|
||||||
|
),
|
||||||
|
context: context);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool get wantKeepAlive => true;
|
||||||
|
}
|
@ -0,0 +1,198 @@
|
|||||||
|
import 'package:cake_wallet/bitcoin/bitcoin_amount_format.dart';
|
||||||
|
import 'package:cake_wallet/bitcoin/electrum_wallet.dart';
|
||||||
|
import 'package:cake_wallet/entities/calculate_fiat_amount_raw.dart';
|
||||||
|
import 'package:cake_wallet/entities/openalias_record.dart';
|
||||||
|
import 'package:cake_wallet/monero/monero_amount_format.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:intl/intl.dart';
|
||||||
|
import 'package:mobx/mobx.dart';
|
||||||
|
import 'package:cake_wallet/core/wallet_base.dart';
|
||||||
|
import 'package:cake_wallet/monero/monero_wallet.dart';
|
||||||
|
import 'package:cake_wallet/entities/calculate_fiat_amount.dart';
|
||||||
|
import 'package:cake_wallet/entities/wallet_type.dart';
|
||||||
|
import 'package:cake_wallet/store/dashboard/fiat_conversion_store.dart';
|
||||||
|
import 'package:cake_wallet/store/settings_store.dart';
|
||||||
|
import 'package:cake_wallet/generated/i18n.dart';
|
||||||
|
|
||||||
|
part 'send_item.g.dart';
|
||||||
|
|
||||||
|
const String cryptoNumberPattern = '0.0';
|
||||||
|
|
||||||
|
class SendItem = SendItemBase with _$SendItem;
|
||||||
|
|
||||||
|
abstract class SendItemBase with Store {
|
||||||
|
SendItemBase(this._wallet, this._settingsStore, this._fiatConversationStore)
|
||||||
|
:_cryptoNumberFormat = NumberFormat(cryptoNumberPattern) {
|
||||||
|
reset();
|
||||||
|
_setCryptoNumMaximumFractionDigits();
|
||||||
|
key = UniqueKey();
|
||||||
|
}
|
||||||
|
|
||||||
|
Key key;
|
||||||
|
|
||||||
|
@observable
|
||||||
|
String fiatAmount;
|
||||||
|
|
||||||
|
@observable
|
||||||
|
String cryptoAmount;
|
||||||
|
|
||||||
|
@observable
|
||||||
|
String address;
|
||||||
|
|
||||||
|
@observable
|
||||||
|
String note;
|
||||||
|
|
||||||
|
@observable
|
||||||
|
bool sendAll;
|
||||||
|
|
||||||
|
@computed
|
||||||
|
double get estimatedFee {
|
||||||
|
int amount;
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (cryptoAmount?.isNotEmpty ?? false) {
|
||||||
|
final _cryptoAmount = cryptoAmount.replaceAll(',', '.');
|
||||||
|
int _amount = 0;
|
||||||
|
switch (walletType) {
|
||||||
|
case WalletType.monero:
|
||||||
|
_amount = moneroParseAmount(amount: _cryptoAmount);
|
||||||
|
break;
|
||||||
|
case WalletType.bitcoin:
|
||||||
|
_amount = stringDoubleToBitcoinAmount(_cryptoAmount);
|
||||||
|
break;
|
||||||
|
case WalletType.litecoin:
|
||||||
|
_amount = stringDoubleToBitcoinAmount(_cryptoAmount);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_amount > 0) {
|
||||||
|
amount = _amount;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
final fee = _wallet.calculateEstimatedFee(
|
||||||
|
_settingsStore.priority[_wallet.type], amount);
|
||||||
|
|
||||||
|
if (_wallet is ElectrumWallet) {
|
||||||
|
return bitcoinAmountToDouble(amount: fee);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_wallet is MoneroWallet) {
|
||||||
|
return moneroAmountToDouble(amount: fee);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
print(e.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@computed
|
||||||
|
String get estimatedFeeFiatAmount {
|
||||||
|
try {
|
||||||
|
final fiat = calculateFiatAmountRaw(
|
||||||
|
price: _fiatConversationStore.prices[_wallet.currency],
|
||||||
|
cryptoAmount: estimatedFee);
|
||||||
|
return fiat;
|
||||||
|
} catch (_) {
|
||||||
|
return '0.00';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
WalletType get walletType => _wallet.type;
|
||||||
|
final WalletBase _wallet;
|
||||||
|
final SettingsStore _settingsStore;
|
||||||
|
final FiatConversionStore _fiatConversationStore;
|
||||||
|
final NumberFormat _cryptoNumberFormat;
|
||||||
|
|
||||||
|
@action
|
||||||
|
void setSendAll() => sendAll = true;
|
||||||
|
|
||||||
|
@action
|
||||||
|
void reset() {
|
||||||
|
sendAll = false;
|
||||||
|
cryptoAmount = '';
|
||||||
|
fiatAmount = '';
|
||||||
|
address = '';
|
||||||
|
note = '';
|
||||||
|
}
|
||||||
|
|
||||||
|
@action
|
||||||
|
void setCryptoAmount(String amount) {
|
||||||
|
if (amount.toUpperCase() != S.current.all) {
|
||||||
|
sendAll = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
cryptoAmount = amount;
|
||||||
|
_updateFiatAmount();
|
||||||
|
}
|
||||||
|
|
||||||
|
@action
|
||||||
|
void setFiatAmount(String amount) {
|
||||||
|
fiatAmount = amount;
|
||||||
|
_updateCryptoAmount();
|
||||||
|
}
|
||||||
|
|
||||||
|
@action
|
||||||
|
void _updateFiatAmount() {
|
||||||
|
try {
|
||||||
|
final fiat = calculateFiatAmount(
|
||||||
|
price: _fiatConversationStore.prices[_wallet.currency],
|
||||||
|
cryptoAmount: cryptoAmount.replaceAll(',', '.'));
|
||||||
|
if (fiatAmount != fiat) {
|
||||||
|
fiatAmount = fiat;
|
||||||
|
}
|
||||||
|
} catch (_) {
|
||||||
|
fiatAmount = '';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@action
|
||||||
|
void _updateCryptoAmount() {
|
||||||
|
try {
|
||||||
|
final crypto = double.parse(fiatAmount.replaceAll(',', '.')) /
|
||||||
|
_fiatConversationStore.prices[_wallet.currency];
|
||||||
|
final cryptoAmountTmp = _cryptoNumberFormat.format(crypto);
|
||||||
|
|
||||||
|
if (cryptoAmount != cryptoAmountTmp) {
|
||||||
|
cryptoAmount = cryptoAmountTmp;
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
cryptoAmount = '';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void _setCryptoNumMaximumFractionDigits() {
|
||||||
|
var maximumFractionDigits = 0;
|
||||||
|
|
||||||
|
switch (_wallet.type) {
|
||||||
|
case WalletType.monero:
|
||||||
|
maximumFractionDigits = 12;
|
||||||
|
break;
|
||||||
|
case WalletType.bitcoin:
|
||||||
|
maximumFractionDigits = 8;
|
||||||
|
break;
|
||||||
|
case WalletType.litecoin:
|
||||||
|
maximumFractionDigits = 8;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
_cryptoNumberFormat.maximumFractionDigits = maximumFractionDigits;
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<OpenaliasRecord> getOpenaliasRecord() async {
|
||||||
|
final formattedName = OpenaliasRecord.formatDomainName(address);
|
||||||
|
final record = await OpenaliasRecord.fetchAddressAndName(formattedName);
|
||||||
|
|
||||||
|
if (record == null || record.address.contains(formattedName)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
address = record.address;
|
||||||
|
return record;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,66 @@
|
|||||||
|
import 'package:cake_wallet/view_model/send/send_item.dart';
|
||||||
|
import 'package:mobx/mobx.dart';
|
||||||
|
import 'package:cake_wallet/entities/template.dart';
|
||||||
|
import 'package:cake_wallet/store/templates/send_template_store.dart';
|
||||||
|
import 'package:cake_wallet/core/template_validator.dart';
|
||||||
|
import 'package:cake_wallet/core/address_validator.dart';
|
||||||
|
import 'package:cake_wallet/core/amount_validator.dart';
|
||||||
|
import 'package:cake_wallet/core/validator.dart';
|
||||||
|
import 'package:cake_wallet/core/wallet_base.dart';
|
||||||
|
import 'package:cake_wallet/entities/crypto_currency.dart';
|
||||||
|
import 'package:cake_wallet/entities/fiat_currency.dart';
|
||||||
|
import 'package:cake_wallet/store/dashboard/fiat_conversion_store.dart';
|
||||||
|
import 'package:cake_wallet/store/settings_store.dart';
|
||||||
|
|
||||||
|
part 'send_template_view_model.g.dart';
|
||||||
|
|
||||||
|
class SendTemplateViewModel = SendTemplateViewModelBase
|
||||||
|
with _$SendTemplateViewModel;
|
||||||
|
|
||||||
|
abstract class SendTemplateViewModelBase with Store {
|
||||||
|
SendTemplateViewModelBase(this._wallet, this._settingsStore,
|
||||||
|
this._sendTemplateStore, this._fiatConversationStore) {
|
||||||
|
|
||||||
|
sendItem = SendItem(_wallet, _settingsStore, _fiatConversationStore);
|
||||||
|
}
|
||||||
|
|
||||||
|
SendItem sendItem;
|
||||||
|
|
||||||
|
Validator get amountValidator => AmountValidator(type: _wallet.type);
|
||||||
|
|
||||||
|
Validator get addressValidator => AddressValidator(type: _wallet.currency);
|
||||||
|
|
||||||
|
Validator get templateValidator => TemplateValidator();
|
||||||
|
|
||||||
|
CryptoCurrency get currency => _wallet.currency;
|
||||||
|
|
||||||
|
FiatCurrency get fiat => _settingsStore.fiatCurrency;
|
||||||
|
|
||||||
|
@computed
|
||||||
|
ObservableList<Template> get templates => _sendTemplateStore.templates;
|
||||||
|
|
||||||
|
final WalletBase _wallet;
|
||||||
|
final SettingsStore _settingsStore;
|
||||||
|
final SendTemplateStore _sendTemplateStore;
|
||||||
|
final FiatConversionStore _fiatConversationStore;
|
||||||
|
|
||||||
|
void updateTemplate() => _sendTemplateStore.update();
|
||||||
|
|
||||||
|
void addTemplate(
|
||||||
|
{String name,
|
||||||
|
String address,
|
||||||
|
String cryptoCurrency,
|
||||||
|
String amount}) {
|
||||||
|
_sendTemplateStore.addTemplate(
|
||||||
|
name: name,
|
||||||
|
address: address,
|
||||||
|
cryptoCurrency: cryptoCurrency,
|
||||||
|
amount: amount);
|
||||||
|
updateTemplate();
|
||||||
|
}
|
||||||
|
|
||||||
|
void removeTemplate({Template template}) {
|
||||||
|
_sendTemplateStore.remove(template: template);
|
||||||
|
updateTemplate();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue