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;
|
|
|
|
|
2020-09-22 13:35:23 +00:00
|
|
|
@action
|
2020-06-01 18:13:56 +00:00
|
|
|
Future<void> update() async {
|
|
|
|
if (_isUpdating) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
_isUpdating = true;
|
2020-09-22 13:35:23 +00:00
|
|
|
final _transactions = await fetchTransactions();
|
2020-09-28 19:02:30 +00:00
|
|
|
transactions.keys
|
|
|
|
.toSet()
|
|
|
|
.difference(_transactions.keys.toSet())
|
|
|
|
.forEach((k) => transactions.remove(k));
|
2020-09-22 13:35:23 +00:00
|
|
|
_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();
|
|
|
|
}
|