CW-240 Receive fiat currency amount and receive animations (#877)

* Redesign receive amount field

* Fix issues with animations

* Fix issues with animations

* Fix max fraction digit to 8

* add another 0

* Update amount when currency is changed

---------

Co-authored-by: Justin Ehrenhofer <justin.ehrenhofer@gmail.com>
Co-authored-by: OmarHatem <omarh.ismail1@gmail.com>
This commit is contained in:
Godwin Asuquo
2023-04-21 20:03:42 +02:00
committed by GitHub
co-authored by Justin Ehrenhofer OmarHatem
parent 8ffac75e8c
commit f2b8dd21a1
13 changed files with 504 additions and 340 deletions
@@ -1,5 +1,8 @@
import 'package:cake_wallet/entities/fiat_currency.dart';
import 'package:cake_wallet/store/dashboard/fiat_conversion_store.dart';
import 'package:cake_wallet/store/yat/yat_store.dart';
import 'package:flutter/foundation.dart';
import 'package:cw_core/currency.dart';
import 'package:intl/intl.dart';
import 'package:mobx/mobx.dart';
import 'package:cw_core/wallet_base.dart';
import 'package:cake_wallet/utils/list_item.dart';
@@ -11,37 +14,30 @@ import 'package:cake_wallet/bitcoin/bitcoin.dart';
import 'package:cw_core/transaction_history.dart';
import 'package:cw_core/balance.dart';
import 'package:cw_core/transaction_info.dart';
import 'package:cw_core/wallet_type.dart';
import 'package:cake_wallet/store/app_store.dart';
import 'dart:async';
import 'package:cake_wallet/monero/monero.dart';
import 'package:cake_wallet/haven/haven.dart';
part 'wallet_address_list_view_model.g.dart';
class WalletAddressListViewModel = WalletAddressListViewModelBase
with _$WalletAddressListViewModel;
class WalletAddressListViewModel = WalletAddressListViewModelBase with _$WalletAddressListViewModel;
abstract class PaymentURI {
PaymentURI({
required this.amount,
required this.address});
PaymentURI({required this.amount, required this.address});
final String amount;
final String address;
}
class MoneroURI extends PaymentURI {
MoneroURI({
required String amount,
required String address})
MoneroURI({required String amount, required String address})
: super(amount: amount, address: address);
@override
String toString() {
var base = 'monero:' + address;
if (amount?.isNotEmpty ?? false) {
if (amount.isNotEmpty) {
base += '?tx_amount=${amount.replaceAll(',', '.')}';
}
@@ -50,16 +46,14 @@ class MoneroURI extends PaymentURI {
}
class HavenURI extends PaymentURI {
HavenURI({
required String amount,
required String address})
HavenURI({required String amount, required String address})
: super(amount: amount, address: address);
@override
String toString() {
var base = 'haven:' + address;
if (amount?.isNotEmpty ?? false) {
if (amount.isNotEmpty) {
base += '?tx_amount=${amount.replaceAll(',', '.')}';
}
@@ -68,16 +62,14 @@ class HavenURI extends PaymentURI {
}
class BitcoinURI extends PaymentURI {
BitcoinURI({
required String amount,
required String address})
BitcoinURI({required String amount, required String address})
: super(amount: amount, address: address);
@override
String toString() {
var base = 'bitcoin:' + address;
if (amount?.isNotEmpty ?? false) {
if (amount.isNotEmpty) {
base += '?amount=${amount.replaceAll(',', '.')}';
}
@@ -86,16 +78,14 @@ class BitcoinURI extends PaymentURI {
}
class LitecoinURI extends PaymentURI {
LitecoinURI({
required String amount,
required String address})
LitecoinURI({required String amount, required String address})
: super(amount: amount, address: address);
@override
String toString() {
var base = 'litecoin:' + address;
if (amount?.isNotEmpty ?? false) {
if (amount.isNotEmpty) {
base += '?amount=${amount.replaceAll(',', '.')}';
}
@@ -106,24 +96,33 @@ class LitecoinURI extends PaymentURI {
abstract class WalletAddressListViewModelBase with Store {
WalletAddressListViewModelBase({
required AppStore appStore,
required this.yatStore
}) : _appStore = appStore,
_baseItems = <ListItem>[],
_wallet = appStore.wallet!,
hasAccounts = appStore.wallet!.type == WalletType.monero || appStore.wallet!.type == WalletType.haven,
amount = '' {
_onWalletChangeReaction = reaction((_) => _appStore.wallet, (WalletBase<
Balance, TransactionHistoryBase<TransactionInfo>, TransactionInfo>?
wallet) {
if (wallet == null) {
return;
}
_wallet = wallet;
hasAccounts = _wallet.type == WalletType.monero;
});
required this.yatStore,
required this.fiatConversionStore,
}) : _appStore = appStore,
_baseItems = <ListItem>[],
_wallet = appStore.wallet!,
selectedCurrency = walletTypeToCryptoCurrency(appStore.wallet!.type),
_cryptoNumberFormat = NumberFormat(_cryptoNumberPattern),
hasAccounts =
appStore.wallet!.type == WalletType.monero || appStore.wallet!.type == WalletType.haven,
amount = '' {
_init();
}
static const String _cryptoNumberPattern = '0.00000000';
final NumberFormat _cryptoNumberFormat;
final FiatConversionStore fiatConversionStore;
List<Currency> get currencies => [walletTypeToCryptoCurrency(_wallet.type), ...FiatCurrency.all];
@observable
Currency selectedCurrency;
@computed
int get selectedCurrencyIndex => currencies.indexOf(selectedCurrency);
@observable
String amount;
@@ -156,8 +155,9 @@ abstract class WalletAddressListViewModelBase with Store {
}
@computed
ObservableList<ListItem> get items =>
ObservableList<ListItem>()..addAll(_baseItems)..addAll(addressList);
ObservableList<ListItem> get items => ObservableList<ListItem>()
..addAll(_baseItems)
..addAll(addressList);
@computed
ObservableList<ListItem> get addressList {
@@ -166,10 +166,7 @@ abstract class WalletAddressListViewModelBase with Store {
if (wallet.type == WalletType.monero) {
final primaryAddress = monero!.getSubaddressList(wallet).subaddresses.first;
final addressItems = monero
!.getSubaddressList(wallet)
.subaddresses
.map((subaddress) {
final addressItems = monero!.getSubaddressList(wallet).subaddresses.map((subaddress) {
final isPrimary = subaddress == primaryAddress;
return WalletAddressListItem(
@@ -183,10 +180,7 @@ abstract class WalletAddressListViewModelBase with Store {
if (wallet.type == WalletType.haven) {
final primaryAddress = haven!.getSubaddressList(wallet).subaddresses.first;
final addressItems = haven
!.getSubaddressList(wallet)
.subaddresses
.map((subaddress) {
final addressItems = haven!.getSubaddressList(wallet).subaddresses.map((subaddress) {
final isPrimary = subaddress == primaryAddress;
return WalletAddressListItem(
@@ -203,8 +197,7 @@ abstract class WalletAddressListViewModelBase with Store {
final bitcoinAddresses = bitcoin!.getAddresses(wallet).map((addr) {
final isPrimary = addr == primaryAddress;
return WalletAddressListItem(
isPrimary: isPrimary, name: null, address: addr);
return WalletAddressListItem(isPrimary: isPrimary, name: null, address: addr);
});
addressList.addAll(bitcoinAddresses);
}
@@ -234,8 +227,7 @@ abstract class WalletAddressListViewModelBase with Store {
bool get hasAddressList => _wallet.type == WalletType.monero || _wallet.type == WalletType.haven;
@observable
WalletBase<Balance, TransactionHistoryBase<TransactionInfo>, TransactionInfo>
_wallet;
WalletBase<Balance, TransactionHistoryBase<TransactionInfo>, TransactionInfo> _wallet;
List<ListItem> _baseItems;
@@ -243,8 +235,6 @@ abstract class WalletAddressListViewModelBase with Store {
final YatStore yatStore;
ReactionDisposer? _onWalletChangeReaction;
@action
void setAddress(WalletAddressListItem address) =>
_wallet.walletAddresses.address = address.address;
@@ -258,4 +248,31 @@ abstract class WalletAddressListViewModelBase with Store {
_baseItems.add(WalletAddressListHeader());
}
@action
void selectCurrency(Currency currency) {
selectedCurrency = currency;
}
@action
void changeAmount(String amount) {
this.amount = amount;
if (selectedCurrency is FiatCurrency) {
_convertAmountToCrypto();
}
}
void _convertAmountToCrypto() {
final cryptoCurrency = walletTypeToCryptoCurrency(_wallet.type);
try {
final crypto =
double.parse(amount.replaceAll(',', '.')) / fiatConversionStore.prices[cryptoCurrency]!;
final cryptoAmountTmp = _cryptoNumberFormat.format(crypto);
if (amount != cryptoAmountTmp) {
amount = cryptoAmountTmp;
}
} catch (e) {
amount = '';
}
}
}