mirror of
https://github.com/Such-Software/hash-wallet.git
synced 2026-12-01 12:04:21 +01:00
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
This commit is contained in:
@@ -79,8 +79,11 @@ abstract class ExchangeTradeViewModelBase with Store {
|
||||
return;
|
||||
}
|
||||
|
||||
sendViewModel.address = trade.inputAddress;
|
||||
sendViewModel.setCryptoAmount(trade.amount);
|
||||
sendViewModel.clearSendItemList();
|
||||
final item = sendViewModel.sendItemList.first;
|
||||
|
||||
item.address = trade.inputAddress;
|
||||
item.setCryptoAmount(trade.amount);
|
||||
await sendViewModel.createTransaction();
|
||||
}
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,8 @@ import 'package:cake_wallet/entities/calculate_fiat_amount_raw.dart';
|
||||
import 'package:cake_wallet/entities/transaction_description.dart';
|
||||
import 'package:cake_wallet/entities/transaction_priority.dart';
|
||||
import 'package:cake_wallet/monero/monero_amount_format.dart';
|
||||
import 'package:cake_wallet/view_model/send/send_item.dart';
|
||||
import 'package:cake_wallet/view_model/send/send_template_view_model.dart';
|
||||
import 'package:cake_wallet/view_model/settings/settings_view_model.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
@@ -37,17 +39,13 @@ import 'package:cake_wallet/generated/i18n.dart';
|
||||
|
||||
part 'send_view_model.g.dart';
|
||||
|
||||
const String cryptoNumberPattern = '0.0';
|
||||
|
||||
class SendViewModel = SendViewModelBase with _$SendViewModel;
|
||||
|
||||
abstract class SendViewModelBase with Store {
|
||||
SendViewModelBase(this._wallet, this._settingsStore, this._sendTemplateStore,
|
||||
this._fiatConversationStore, this.transactionDescriptionBox)
|
||||
: state = InitialExecutionState(),
|
||||
_cryptoNumberFormat = NumberFormat(cryptoNumberPattern),
|
||||
note = '',
|
||||
sendAll = false {
|
||||
SendViewModelBase(this._wallet, this._settingsStore,
|
||||
this.sendTemplateViewModel, this._fiatConversationStore,
|
||||
this.transactionDescriptionBox)
|
||||
: state = InitialExecutionState() {
|
||||
final priority = _settingsStore.priority[_wallet.type];
|
||||
final priorities = priorityForWalletType(_wallet.type);
|
||||
|
||||
@@ -55,82 +53,33 @@ abstract class SendViewModelBase with Store {
|
||||
_settingsStore.priority[_wallet.type] = priorities.first;
|
||||
}
|
||||
|
||||
_setCryptoNumMaximumFractionDigits();
|
||||
sendItemList = ObservableList<SendItem>()
|
||||
..add(SendItem(_wallet, _settingsStore, _fiatConversationStore));
|
||||
}
|
||||
|
||||
@observable
|
||||
ExecutionState state;
|
||||
|
||||
@observable
|
||||
String fiatAmount;
|
||||
ObservableList<SendItem> sendItemList;
|
||||
|
||||
@observable
|
||||
String cryptoAmount;
|
||||
@action
|
||||
void addSendItem() {
|
||||
sendItemList.add(SendItem(_wallet, _settingsStore, _fiatConversationStore));
|
||||
}
|
||||
|
||||
@observable
|
||||
String address;
|
||||
@action
|
||||
void removeSendItem(SendItem item) {
|
||||
sendItemList.remove(item);
|
||||
}
|
||||
|
||||
@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;
|
||||
@action
|
||||
void clearSendItemList() {
|
||||
sendItemList.clear();
|
||||
addSendItem();
|
||||
}
|
||||
|
||||
@computed
|
||||
String get estimatedFeeFiatAmount {
|
||||
try {
|
||||
final fiat = calculateFiatAmountRaw(
|
||||
price: _fiatConversationStore.prices[_wallet.currency],
|
||||
cryptoAmount: estimatedFee);
|
||||
return fiat;
|
||||
} catch (_) {
|
||||
return '0.00';
|
||||
}
|
||||
}
|
||||
bool get isRemoveButtonShow => sendItemList.length > 1;
|
||||
|
||||
@computed
|
||||
String get pendingTransactionFiatAmount {
|
||||
@@ -177,8 +126,6 @@ abstract class SendViewModelBase with Store {
|
||||
|
||||
Validator get addressValidator => AddressValidator(type: _wallet.currency);
|
||||
|
||||
Validator get templateValidator => TemplateValidator();
|
||||
|
||||
@observable
|
||||
PendingTransaction pendingTransaction;
|
||||
|
||||
@@ -189,28 +136,15 @@ abstract class SendViewModelBase with Store {
|
||||
bool get isReadyForSend => _wallet.syncStatus is SyncedSyncStatus;
|
||||
|
||||
@computed
|
||||
ObservableList<Template> get templates => _sendTemplateStore.templates;
|
||||
ObservableList<Template> get templates => sendTemplateViewModel.templates;
|
||||
|
||||
WalletType get walletType => _wallet.type;
|
||||
final WalletBase _wallet;
|
||||
final SettingsStore _settingsStore;
|
||||
final SendTemplateStore _sendTemplateStore;
|
||||
final SendTemplateViewModel sendTemplateViewModel;
|
||||
final FiatConversionStore _fiatConversationStore;
|
||||
final NumberFormat _cryptoNumberFormat;
|
||||
final Box<TransactionDescription> transactionDescriptionBox;
|
||||
|
||||
@action
|
||||
void setSendAll() => sendAll = true;
|
||||
|
||||
@action
|
||||
void reset() {
|
||||
sendAll = false;
|
||||
cryptoAmount = '';
|
||||
fiatAmount = '';
|
||||
address = '';
|
||||
note = '';
|
||||
}
|
||||
|
||||
@action
|
||||
Future<void> createTransaction() async {
|
||||
try {
|
||||
@@ -224,6 +158,8 @@ abstract class SendViewModelBase with Store {
|
||||
|
||||
@action
|
||||
Future<void> commitTransaction() async {
|
||||
final address = ''; // FIXME: get it from item
|
||||
final note = ''; // FIXME: get it from item
|
||||
try {
|
||||
state = TransactionCommitting();
|
||||
await pendingTransaction.commit();
|
||||
@@ -244,64 +180,14 @@ abstract class SendViewModelBase with Store {
|
||||
}
|
||||
}
|
||||
|
||||
@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 setTransactionPriority(TransactionPriority priority) =>
|
||||
_settingsStore.priority[_wallet.type] = priority;
|
||||
|
||||
Future<OpenaliasRecord> decodeOpenaliasRecord(String name) async {
|
||||
final record = await OpenaliasRecord.fetchAddressAndName(
|
||||
OpenaliasRecord.formatDomainName(name));
|
||||
|
||||
return record.name != name ? record : null;
|
||||
}
|
||||
|
||||
@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 = '';
|
||||
}
|
||||
}
|
||||
|
||||
Object _credentials() {
|
||||
final _amount = cryptoAmount.replaceAll(',', '.');
|
||||
// FIXME: get it from item
|
||||
return null;
|
||||
/*final _amount = cryptoAmount.replaceAll(',', '.');
|
||||
|
||||
switch (_wallet.type) {
|
||||
case WalletType.bitcoin:
|
||||
@@ -327,45 +213,9 @@ abstract class SendViewModelBase with Store {
|
||||
amount: amount);
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
void updateTemplate() => _sendTemplateStore.update();
|
||||
|
||||
void addTemplate(
|
||||
{String name,
|
||||
String address,
|
||||
String cryptoCurrency,
|
||||
String amount}) =>
|
||||
_sendTemplateStore.addTemplate(
|
||||
name: name,
|
||||
address: address,
|
||||
cryptoCurrency: cryptoCurrency,
|
||||
amount: amount);
|
||||
|
||||
void removeTemplate({Template template}) =>
|
||||
_sendTemplateStore.remove(template: template);
|
||||
|
||||
String displayFeeRate(dynamic priority) {
|
||||
final _priority = priority as TransactionPriority;
|
||||
final wallet = _wallet;
|
||||
|
||||
Reference in New Issue
Block a user