Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions lib/bloc/auth_bloc/auth_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,12 @@ class AuthBloc extends Bloc<AuthBlocEvent, AuthBlocState> with TrezorAuthMixin {
} finally {
// Explicitly disconnect SSE on sign-out
_log.info('User signed out, disconnecting SSE...');
_kdfSdk.streaming.disconnect();
try {
_kdfSdk.streaming.disconnect();
} on StateError {
// SDK has been disposed, skip disconnect
_log.info('SDK already disposed, skipping SSE disconnect');
}

await _authChangesSubscription?.cancel();
emit(AuthBlocState.initial());
Expand Down Expand Up @@ -467,11 +472,21 @@ class AuthBloc extends Bloc<AuthBlocEvent, AuthBlocState> with TrezorAuthMixin {
if (user != null) {
// User authenticated - connect SSE for balance/tx history streaming
_log.info('User authenticated, connecting SSE for streaming...');
_kdfSdk.streaming.connectIfNeeded();
try {
_kdfSdk.streaming.connectIfNeeded();
} on StateError {
// SDK has been disposed, skip connect
_log.info('SDK already disposed, skipping SSE connect');
}
} else {
// User signed out - disconnect SSE to clean up resources
_log.info('User signed out, disconnecting SSE...');
_kdfSdk.streaming.disconnect();
try {
_kdfSdk.streaming.disconnect();
} on StateError {
// SDK has been disposed, skip disconnect
_log.info('SDK already disposed, skipping SSE disconnect');
}
}
});
}
Expand Down
38 changes: 27 additions & 11 deletions lib/shared/utils/extensions/legacy_coin_migration_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,14 @@ extension LegacyCoinMigrationExtensions on Coin {
///
/// NB: This is not a real-time balance. Prefer using [getBalance] or
/// [watchBalance] for up-to-date data.
double? balance(KomodoDefiSdk sdk) =>
sdk.balances.lastKnown(id)?.spendable.toDouble();
double? balance(KomodoDefiSdk sdk) {
try {
return sdk.balances.lastKnown(id)?.spendable.toDouble();
} on StateError {
// SDK has been disposed, return null
return null;
}
}

/// Gets the current USD balance of this coin
///
Expand All @@ -45,20 +51,30 @@ extension LegacyCoinMigrationExtensions on Coin {
}

double? lastKnownUsdBalance(KomodoDefiSdk sdk) {
final balance = sdk.balances.lastKnown(id);
if (balance == null) return null;
if (balance.spendable == Decimal.zero) return 0;
try {
final balance = sdk.balances.lastKnown(id);
if (balance == null) return null;
if (balance.spendable == Decimal.zero) return 0;

final price = sdk.marketData.priceIfKnown(id);
if (price == null) return null;
final price = sdk.marketData.priceIfKnown(id);
if (price == null) return null;

return (price * balance.spendable).toDouble();
return (price * balance.spendable).toDouble();
} on StateError {
// SDK has been disposed, return null
return null;
}
}

double? lastKnownUsdPrice(KomodoDefiSdk sdk) {
final price = sdk.marketData.priceIfKnown(id);
if (price == null) return null;
return price.toDouble();
try {
final price = sdk.marketData.priceIfKnown(id);
if (price == null) return null;
return price.toDouble();
} on StateError {
// SDK has been disposed, return null
return null;
}
}

/// Get cached 24hr change from CoinsBloc state
Expand Down
3 changes: 1 addition & 2 deletions lib/shared/widgets/coin_balance.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ class CoinBalance extends StatelessWidget {
final baseFont = Theme.of(context).textTheme.bodySmall;
final balanceStyle = baseFont?.copyWith(fontWeight: FontWeight.w500);

final balance =
context.sdk.balances.lastKnown(coin.id)?.spendable.toDouble() ?? 0.0;
final balance = coin.balance(context.sdk) ?? 0.0;

final children = [
Row(
Expand Down
8 changes: 7 additions & 1 deletion lib/shared/widgets/coin_fiat_balance.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ class CoinFiatBalance extends StatelessWidget {

@override
Widget build(BuildContext context) {
final balanceStream = context.sdk.balances.watchBalance(coin.id);
Stream<BalanceInfo>? balanceStream;
try {
balanceStream = context.sdk.balances.watchBalance(coin.id);
} on StateError {
// SDK has been disposed, return empty widget
return const SizedBox();
}

final TextStyle mergedStyle = const TextStyle(
fontSize: 12,
Expand Down
Loading