update balance after viewing coin control

This commit is contained in:
julian 2023-03-08 12:22:33 -06:00
parent 93003d2880
commit ea49ed32ab
3 changed files with 99 additions and 31 deletions

View file

@ -1,3 +1,5 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:isar/isar.dart';
@ -6,6 +8,7 @@ import 'package:stackwallet/models/isar/models/isar_models.dart';
import 'package:stackwallet/pages/coin_control/utxo_card.dart';
import 'package:stackwallet/pages/coin_control/utxo_details_view.dart';
import 'package:stackwallet/providers/global/wallets_provider.dart';
import 'package:stackwallet/services/mixins/coin_control_interface.dart';
import 'package:stackwallet/utilities/constants.dart';
import 'package:stackwallet/utilities/enums/coin_enum.dart';
import 'package:stackwallet/utilities/format.dart';
@ -51,6 +54,14 @@ class _CoinControlViewState extends ConsumerState<CoinControlView> {
final Set<UTXO> _selectedAvailable = {};
final Set<UTXO> _selectedBlocked = {};
Future<void> _refreshBalance() async {
final coinControlInterface = ref
.read(walletsChangeNotifierProvider)
.getManager(widget.walletId)
.wallet as CoinControlInterface;
await coinControlInterface.refreshBalance(notify: true);
}
@override
void initState() {
if (widget.selectedUTXOs != null) {
@ -96,6 +107,7 @@ class _CoinControlViewState extends ConsumerState<CoinControlView> {
return WillPopScope(
onWillPop: () async {
unawaited(_refreshBalance());
Navigator.of(context).pop(
widget.type == CoinControlViewType.use ? _selectedAvailable : null);
return false;
@ -123,6 +135,7 @@ class _CoinControlViewState extends ConsumerState<CoinControlView> {
)
: AppBarBackButton(
onPressed: () {
unawaited(_refreshBalance());
Navigator.of(context).pop(
widget.type == CoinControlViewType.use
? _selectedAvailable

View file

@ -120,6 +120,14 @@ class BitcoinWallet extends CoinServiceAPI
_secureStore = secureStore;
initCache(walletId, coin);
initWalletDB(mockableOverride: mockableOverride);
initCoinControlInterface(
walletId: walletId,
walletName: walletName,
coin: coin,
db: db,
getChainHeight: () => chainHeight,
refreshedBalanceCallback: updateCachedBalance,
);
initPaynymWalletInterface(
walletId: walletId,
walletName: walletName,
@ -1887,36 +1895,7 @@ class BitcoinWallet extends CoinServiceAPI
}
Future<void> _updateBalance() async {
final utxos = await db.getUTXOs(walletId).findAll();
final currentChainHeight = await chainHeight;
int satoshiBalanceTotal = 0;
int satoshiBalancePending = 0;
int satoshiBalanceSpendable = 0;
int satoshiBalanceBlocked = 0;
for (final utxo in utxos) {
satoshiBalanceTotal += utxo.value;
if (utxo.isBlocked) {
satoshiBalanceBlocked += utxo.value;
} else {
if (utxo.isConfirmed(currentChainHeight, MINIMUM_CONFIRMATIONS)) {
satoshiBalanceSpendable += utxo.value;
} else {
satoshiBalancePending += utxo.value;
}
}
}
_balance = Balance(
coin: coin,
total: satoshiBalanceTotal,
spendable: satoshiBalanceSpendable,
blockedTotal: satoshiBalanceBlocked,
pendingSpendable: satoshiBalancePending,
);
await updateCachedBalance(_balance!);
await refreshBalance();
}
@override

View file

@ -1,3 +1,79 @@
import 'dart:async';
import 'package:isar/isar.dart';
import 'package:stackwallet/db/main_db.dart';
import 'package:stackwallet/models/balance.dart';
import 'package:stackwallet/services/event_bus/events/global/updated_in_background_event.dart';
import 'package:stackwallet/services/event_bus/global_event_bus.dart';
import 'package:stackwallet/utilities/enums/coin_enum.dart';
mixin CoinControlInterface {
//
late final String _walletId;
late final String _walletName;
late final Coin _coin;
late final MainDB _db;
late final Future<int> Function() _getChainHeight;
late final Future<void> Function(Balance) _refreshedBalanceCallback;
void initCoinControlInterface({
required String walletId,
required String walletName,
required Coin coin,
required MainDB db,
required Future<int> Function() getChainHeight,
required Future<void> Function(Balance) refreshedBalanceCallback,
}) {
_walletId = walletId;
_walletName = walletName;
_coin = coin;
_db = db;
_getChainHeight = getChainHeight;
_refreshedBalanceCallback = refreshedBalanceCallback;
}
Future<void> refreshBalance({bool notify = false}) async {
final utxos = await _db.getUTXOs(_walletId).findAll();
final currentChainHeight = await _getChainHeight();
int satoshiBalanceTotal = 0;
int satoshiBalancePending = 0;
int satoshiBalanceSpendable = 0;
int satoshiBalanceBlocked = 0;
for (final utxo in utxos) {
satoshiBalanceTotal += utxo.value;
if (utxo.isBlocked) {
satoshiBalanceBlocked += utxo.value;
} else {
if (utxo.isConfirmed(
currentChainHeight,
_coin.requiredConfirmations,
)) {
satoshiBalanceSpendable += utxo.value;
} else {
satoshiBalancePending += utxo.value;
}
}
}
final balance = Balance(
coin: _coin,
total: satoshiBalanceTotal,
spendable: satoshiBalanceSpendable,
blockedTotal: satoshiBalanceBlocked,
pendingSpendable: satoshiBalancePending,
);
await _refreshedBalanceCallback(balance);
if (notify) {
GlobalEventBus.instance.fire(
UpdatedInBackgroundEvent(
"coin control refresh balance in $_walletId $_walletName!",
_walletId,
),
);
}
}
}