Fixes for monero transaction history updates.

This commit is contained in:
M 2020-09-22 16:35:23 +03:00
parent 2b05eedfc6
commit c53b6676b2
9 changed files with 66 additions and 17 deletions

View file

@ -1 +0,0 @@
09c81fe0a3d701eb6da3bd2c6fc5ec65

View file

@ -10,15 +10,17 @@ abstract class TransactionHistoryBase<TransactionType extends TransactionInfo> {
bool _isUpdating; bool _isUpdating;
@action
Future<void> update() async { Future<void> update() async {
if (_isUpdating) { if (_isUpdating) {
return; return;
} }
try { try {
_isUpdating = false;
transactions.addAll(await fetchTransactions());
_isUpdating = true; _isUpdating = true;
final _transactions = await fetchTransactions();
_transactions.forEach((key, value) => transactions[key] = value);
_isUpdating = false;
} catch (e) { } catch (e) {
_isUpdating = false; _isUpdating = false;
rethrow; rethrow;

View file

@ -1,6 +1,7 @@
import 'package:cake_wallet/entities/transaction_direction.dart'; import 'package:cake_wallet/entities/transaction_direction.dart';
import 'package:cake_wallet/utils/mobx.dart';
abstract class TransactionInfo extends Object { abstract class TransactionInfo extends Object with Keyable {
String id; String id;
int amount; int amount;
TransactionDirection direction; TransactionDirection direction;
@ -11,4 +12,7 @@ abstract class TransactionInfo extends Object {
String amountFormatted(); String amountFormatted();
String fiatAmount(); String fiatAmount();
void changeFiatAmount(String amount); void changeFiatAmount(String amount);
@override
dynamic get keyIndex => id;
} }

View file

@ -34,6 +34,7 @@ abstract class MoneroTransactionHistoryBase
} }
@override @override
@action
void updateAsync({void Function() onFinished}) { void updateAsync({void Function() onFinished}) {
fetchTransactionsAsync( fetchTransactionsAsync(
(transaction) => transactions[transaction.id] = transaction, (transaction) => transactions[transaction.id] = transaction,

View file

@ -92,6 +92,7 @@ abstract class MoneroWalletBase extends WalletBase<MoneroBalance> with Store {
monero_wallet.getFullBalance(accountIndex: account.id)); monero_wallet.getFullBalance(accountIndex: account.id));
address = subaddress.address; address = subaddress.address;
_setListeners(); _setListeners();
await transactionHistory.update();
} }
void close() { void close() {
@ -245,7 +246,11 @@ abstract class MoneroWalletBase extends WalletBase<MoneroBalance> with Store {
} }
} }
void _askForUpdateTransactionHistory() => transactionHistory.updateAsync(); Future<void> _askForUpdateTransactionHistory() async {
print('start');
await transactionHistory.update();
print('end');
}
int _getFullBalance() => int _getFullBalance() =>
monero_wallet.getFullBalance(accountIndex: account.id); monero_wallet.getFullBalance(accountIndex: account.id);

View file

@ -6,7 +6,7 @@ mixin Keyable {
dynamic keyIndex; dynamic keyIndex;
} }
void connectDifferent<T extends Keyable, Y extends Keyable>( void connectWithTransform<T extends Keyable, Y extends Keyable>(
ObservableList<T> source, ObservableList<Y> dest, Y Function(T) transform, ObservableList<T> source, ObservableList<Y> dest, Y Function(T) transform,
{bool Function(T) filter}) { {bool Function(T) filter}) {
source.observe((ListChange<T> change) { source.observe((ListChange<T> change) {
@ -36,6 +36,36 @@ void connectDifferent<T extends Keyable, Y extends Keyable>(
}); });
} }
void connectMapToListWithTransform<T extends Keyable, Y extends Keyable>(
ObservableMap<dynamic, T> source,
ObservableList<Y> dest,
Y Function(T) transform,
{bool Function(T) filter}) {
source.observe((MapChange<dynamic, T> change) {
switch (change.type) {
case OperationType.add:
if (filter?.call(change.newValue) ?? true) {
dest.add(transform(change.newValue));
}
break;
case OperationType.remove:
// Hive could has equal index and key
dest.removeWhere(
(elem) => elem.keyIndex == (change.key ?? change.newValue.keyIndex));
break;
case OperationType.update:
for (var i = 0; i < dest.length; i++) {
final item = dest[i];
if (item.keyIndex == change.key) {
dest[i] = transform(change.newValue);
}
}
break;
}
});
}
void connect<T extends Keyable>( void connect<T extends Keyable>(
ObservableList<T> source, ObservableList<T> dest) { ObservableList<T> source, ObservableList<T> dest) {
source.observe((ListChange<T> change) { source.observe((ListChange<T> change) {

View file

@ -7,6 +7,7 @@ import 'package:cake_wallet/entities/transaction_direction.dart';
import 'package:cake_wallet/entities/transaction_info.dart'; import 'package:cake_wallet/entities/transaction_info.dart';
import 'package:cake_wallet/exchange/exchange_provider_description.dart'; import 'package:cake_wallet/exchange/exchange_provider_description.dart';
import 'package:cake_wallet/exchange/trade.dart'; import 'package:cake_wallet/exchange/trade.dart';
import 'package:cake_wallet/utils/mobx.dart';
import 'package:cake_wallet/view_model/dashboard/balance_view_model.dart'; import 'package:cake_wallet/view_model/dashboard/balance_view_model.dart';
import 'package:cake_wallet/view_model/dashboard/filter_item.dart'; import 'package:cake_wallet/view_model/dashboard/filter_item.dart';
import 'package:cake_wallet/view_model/dashboard/trade_list_item.dart'; import 'package:cake_wallet/view_model/dashboard/trade_list_item.dart';
@ -82,6 +83,15 @@ abstract class DashboardViewModelBase with Store {
displayMode: balanceDisplayMode))); displayMode: balanceDisplayMode)));
_reaction = reaction((_) => appStore.wallet, _onWalletChange); _reaction = reaction((_) => appStore.wallet, _onWalletChange);
// FIXME: fixme
connectMapToListWithTransform(
appStore.wallet.transactionHistory.transactions,
transactions,
(TransactionInfo val) => TransactionListItem(
transaction: val,
price: price,
fiatCurrency: appStore.settingsStore.fiatCurrency,
displayMode: balanceDisplayMode));
final _wallet = wallet; final _wallet = wallet;

View file

@ -1,6 +1,7 @@
import 'package:cake_wallet/entities/balance_display_mode.dart'; import 'package:cake_wallet/entities/balance_display_mode.dart';
import 'package:cake_wallet/entities/fiat_currency.dart'; import 'package:cake_wallet/entities/fiat_currency.dart';
import 'package:cake_wallet/entities/transaction_info.dart'; import 'package:cake_wallet/entities/transaction_info.dart';
import 'package:cake_wallet/utils/mobx.dart';
import 'package:cake_wallet/view_model/dashboard/action_list_item.dart'; import 'package:cake_wallet/view_model/dashboard/action_list_item.dart';
import 'package:cake_wallet/bitcoin/bitcoin_transaction_info.dart'; import 'package:cake_wallet/bitcoin/bitcoin_transaction_info.dart';
import 'package:cake_wallet/monero/monero_transaction_info.dart'; import 'package:cake_wallet/monero/monero_transaction_info.dart';
@ -8,28 +9,25 @@ import 'package:cake_wallet/monero/monero_amount_format.dart';
import 'package:cake_wallet/bitcoin/bitcoin_amount_format.dart'; import 'package:cake_wallet/bitcoin/bitcoin_amount_format.dart';
import 'package:cake_wallet/entities/calculate_fiat_amount_raw.dart'; import 'package:cake_wallet/entities/calculate_fiat_amount_raw.dart';
class TransactionListItem extends ActionListItem { class TransactionListItem extends ActionListItem with Keyable {
TransactionListItem({ TransactionListItem(
this.transaction, {this.transaction, this.price, this.fiatCurrency, this.displayMode});
this.price,
this.fiatCurrency,
this.displayMode
});
final TransactionInfo transaction; final TransactionInfo transaction;
final double price; final double price;
final FiatCurrency fiatCurrency; final FiatCurrency fiatCurrency;
final BalanceDisplayMode displayMode; final BalanceDisplayMode displayMode;
String get formattedCryptoAmount { @override
dynamic get keyIndex => transaction.id;
String get formattedCryptoAmount {
return displayMode == BalanceDisplayMode.hiddenBalance return displayMode == BalanceDisplayMode.hiddenBalance
? '---' ? '---'
: transaction.amountFormatted(); : transaction.amountFormatted();
} }
String get formattedFiatAmount { String get formattedFiatAmount {
if (transaction is MoneroTransactionInfo) { if (transaction is MoneroTransactionInfo) {
final amount = calculateFiatAmountRaw( final amount = calculateFiatAmountRaw(
cryptoAmount: moneroAmountToDouble(amount: transaction.amount), cryptoAmount: moneroAmountToDouble(amount: transaction.amount),
@ -51,4 +49,4 @@ class TransactionListItem extends ActionListItem {
@override @override
DateTime get date => transaction.date; DateTime get date => transaction.date;
} }

View file

@ -24,7 +24,7 @@ abstract class NodeListViewModelBase with Store {
nodes.addAll(values.where((Node node) => node.type == _wallet.type).map( nodes.addAll(values.where((Node node) => node.type == _wallet.type).map(
(Node val) => ItemCell<Node>(val, (Node val) => ItemCell<Node>(val,
isSelected: val.key == currentNode.key, key: val.key))); isSelected: val.key == currentNode.key, key: val.key)));
connectDifferent( connectWithTransform(
_nodeListStore.nodes, _nodeListStore.nodes,
nodes, nodes,
(Node val) => ItemCell<Node>(val, (Node val) => ItemCell<Node>(val,