cake_wallet/lib/core/transaction_history.dart

46 lines
1.2 KiB
Dart
Raw Normal View History

2020-08-25 16:32:40 +00:00
import 'package:flutter/foundation.dart';
2020-06-01 18:13:56 +00:00
import 'package:mobx/mobx.dart';
2020-09-21 11:50:26 +00:00
import 'package:cake_wallet/entities/transaction_info.dart';
2020-06-01 18:13:56 +00:00
2020-06-20 07:10:00 +00:00
abstract class TransactionHistoryBase<TransactionType extends TransactionInfo> {
TransactionHistoryBase() : _isUpdating = false;
2020-06-01 18:13:56 +00:00
@observable
2020-08-25 16:32:40 +00:00
ObservableMap<String, TransactionType> transactions;
2020-06-01 18:13:56 +00:00
bool _isUpdating;
@action
2020-06-01 18:13:56 +00:00
Future<void> update() async {
if (_isUpdating) {
return;
}
try {
_isUpdating = true;
final _transactions = await fetchTransactions();
2020-09-28 19:02:30 +00:00
transactions.keys
.toSet()
.difference(_transactions.keys.toSet())
.forEach((k) => transactions.remove(k));
_transactions.forEach((key, value) => transactions[key] = value);
_isUpdating = false;
2020-06-01 18:13:56 +00:00
} catch (e) {
_isUpdating = false;
rethrow;
}
}
2020-08-25 16:32:40 +00:00
void updateAsync({void Function() onFinished}) {
fetchTransactionsAsync(
(transaction) => transactions[transaction.id] = transaction,
onFinished: onFinished);
}
void fetchTransactionsAsync(
void Function(TransactionType transaction) onTransactionLoaded,
{void Function() onFinished});
Future<Map<String, TransactionType>> fetchTransactions();
}