Skip to content

Commit

Permalink
Refactor Balance
Browse files Browse the repository at this point in the history
  • Loading branch information
ppupha committed Jan 7, 2025
1 parent 138ab1a commit d492e05
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 450 deletions.
10 changes: 10 additions & 0 deletions lib/model/currency_exchange.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import 'dart:math';

import 'package:autonomy_flutter/util/constants.dart';

class CurrencyExchange {
CurrencyExchange({required this.currency, required this.rates});

Expand Down Expand Up @@ -51,4 +53,12 @@ class CurrencyExchangeRate {
String xtzToUsd(int amount) {
return (amount / pow(10, 6) / double.parse(xtz)).toStringAsFixed(2);
}

String toUsd({required BigInt amount, required CryptoType cryptoType}) {
if (cryptoType == CryptoType.ETH) {
return '${ethToUsd(amount)} USD';
} else {
return '${xtzToUsd(amount.toInt())} USD';
}
}
}
47 changes: 18 additions & 29 deletions lib/screen/app_router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// that can be found in the LICENSE file.
//

import 'package:autonomy_flutter/common/database.dart';
import 'package:autonomy_flutter/common/injector.dart';
import 'package:autonomy_flutter/model/ff_exhibition.dart';
import 'package:autonomy_flutter/model/play_list_model.dart';
Expand Down Expand Up @@ -149,9 +148,10 @@ class AppRouter {
static const playlistActivationPage = 'playlist_activation_page';

static Route<dynamic> onGenerateRoute(RouteSettings settings) {
final accountsBloc = AccountsBloc(injector(), injector());
final accountsBloc = injector<AccountsBloc>();
final walletDetailBloc = injector<WalletDetailBloc>();

final identityBloc = IdentityBloc(ObjectBox.identityBox, injector());
final identityBloc = injector<IdentityBloc>();
final canvasDeviceBloc = injector<CanvasDeviceBloc>();

final subscriptionBloc = injector<SubscriptionBloc>();
Expand Down Expand Up @@ -212,7 +212,7 @@ class AppRouter {
settings: settings,
child: MultiBlocProvider(
providers: [
BlocProvider(create: (_) => identityBloc),
BlocProvider.value(value: identityBloc),
],
child: PreviewPrimerPage(
token: settings.arguments! as AssetToken,
Expand All @@ -229,12 +229,12 @@ class AppRouter {
BlocProvider(
create: (_) => HomeBloc(),
),
BlocProvider(create: (_) => identityBloc),
BlocProvider.value(value: identityBloc),
BlocProvider.value(value: royaltyBloc),
BlocProvider.value(
value: subscriptionBloc,
),
BlocProvider(create: (_) => canvasDeviceBloc),
BlocProvider.value(value: canvasDeviceBloc),
BlocProvider.value(value: listPlaylistBloc),
],
child: HomeNavigationPage(
Expand Down Expand Up @@ -323,7 +323,7 @@ class AppRouter {
providers: [
BlocProvider.value(value: accountsBloc),
BlocProvider.value(value: subscriptionBloc),
BlocProvider(create: (_) => identityBloc),
BlocProvider.value(value: identityBloc),
],
child: const SettingsPage(),
),
Expand All @@ -334,13 +334,10 @@ class AppRouter {
settings: settings,
builder: (context) => MultiBlocProvider(
providers: [
BlocProvider(
create: (_) => WalletDetailBloc(
injector(),
injector(),
injector(),
),
BlocProvider.value(
value: walletDetailBloc,
),
BlocProvider.value(value: accountsBloc),
],
child: LinkedWalletDetailPage(
payload: settings.arguments! as LinkedWalletDetailsPayload,
Expand Down Expand Up @@ -368,7 +365,7 @@ class AppRouter {
child: MultiBlocProvider(
providers: [
BlocProvider.value(value: accountsBloc),
BlocProvider(create: (_) => identityBloc),
BlocProvider.value(value: identityBloc),
BlocProvider(create: (_) => royaltyBloc),
BlocProvider(
create: (_) => ArtworkDetailBloc(
Expand All @@ -380,8 +377,8 @@ class AppRouter {
injector(),
),
),
BlocProvider(
create: (_) => canvasDeviceBloc,
BlocProvider.value(
value: canvasDeviceBloc,
),
BlocProvider.value(
value: subscriptionBloc,
Expand All @@ -393,14 +390,6 @@ class AppRouter {
),
);

// TODO: Implement the recovery phrase page
// case recoveryPhrasePage:
// return CupertinoPageRoute(
// settings: settings,
// builder: (context) => RecoveryPhrasePage(
// payload: settings.arguments! as RecoveryPhrasePayload,
// ));

case autonomySecurityPage:
return CupertinoPageRoute(
settings: settings,
Expand Down Expand Up @@ -467,8 +456,8 @@ class AppRouter {
BlocProvider(
create: (_) => ExhibitionDetailBloc(injector()),
),
BlocProvider(
create: (_) => canvasDeviceBloc,
BlocProvider.value(
value: canvasDeviceBloc,
),
BlocProvider.value(
value: subscriptionBloc,
Expand All @@ -485,8 +474,8 @@ class AppRouter {
settings: settings,
builder: (context) => MultiBlocProvider(
providers: [
BlocProvider(
create: (_) => royaltyBloc,
BlocProvider.value(
value: royaltyBloc,
),
BlocProvider.value(
value: subscriptionBloc,
Expand Down Expand Up @@ -585,7 +574,7 @@ class AppRouter {
settings: settings,
builder: (context) => MultiBlocProvider(
providers: [
BlocProvider(create: (_) => identityBloc),
BlocProvider.value(value: identityBloc),
],
child: const DataManagementPage(),
),
Expand Down
37 changes: 36 additions & 1 deletion lib/screen/bloc/accounts/accounts_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@

// ignore_for_file: sort_constructors_first

import 'dart:async';

import 'package:autonomy_flutter/au_bloc.dart';
import 'package:autonomy_flutter/graphql/account_settings/cloud_manager.dart';
import 'package:autonomy_flutter/model/pair.dart';
import 'package:autonomy_flutter/model/wallet_address.dart';
import 'package:autonomy_flutter/screen/bloc/accounts/accounts_state.dart';
import 'package:autonomy_flutter/service/address_service.dart';
import 'package:autonomy_flutter/view/account_view.dart';
import 'package:sentry/sentry.dart';

class AccountsBloc extends AuBloc<AccountsEvent, AccountsState> {
final AddressService _addressService;
Expand All @@ -22,10 +27,11 @@ class AccountsBloc extends AuBloc<AccountsEvent, AccountsState> {
on<GetAccountsEvent>((event, emit) async {
final addresses = _cloudObject.addressObject.getAllAddresses();
emit(
AccountsState(
state.copyWith(
addresses: addresses,
),
);
add(GetAccountBalanceEvent(addresses.map((e) => e.address).toList()));
});

on<ChangeAccountOrderEvent>((event, emit) {
Expand All @@ -48,6 +54,35 @@ class AccountsBloc extends AuBloc<AccountsEvent, AccountsState> {
_addressService.insertAddresses(newAddresses);
});

on<GetAccountBalanceEvent>((event, emit) async {
final addressBalances = <String, Pair<BigInt?, String>>{};
for (final address in event.addresses) {
try {
final balance = await getAddressBalance(address);
if (balance != null) {
addressBalances[address] = balance;
}
emit(
state.copyWith(
addressBalances: Map.from(
addressBalances
..addAll(
state.addressBalances,
),
),
),
);
} catch (e, s) {
unawaited(
Sentry.captureException(
'Failed to get balance for $address: $e',
stackTrace: s,
),
);
}
}
});

on<FetchAllAddressesEvent>((event, emit) async {
final addresses = _addressService.getAllWalletAddresses()
..removeWhere((e) => e.address.isEmpty);
Expand Down
16 changes: 15 additions & 1 deletion lib/screen/bloc/accounts/accounts_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
// that can be found in the LICENSE file.
//

import 'package:autonomy_flutter/model/pair.dart';
import 'package:autonomy_flutter/model/wallet_address.dart';
import 'package:autonomy_flutter/util/log.dart';

abstract class AccountsEvent {}

Expand All @@ -20,18 +22,30 @@ class ChangeAccountOrderEvent extends AccountsEvent {

class FetchAllAddressesEvent extends AccountsEvent {}

class GetAccountBalanceEvent extends AccountsEvent {
GetAccountBalanceEvent(this.addresses);

final List<String> addresses;
}

class AccountsState {
AccountsState({
this.addresses,
});
this.addressBalances = const {},
}) {
log.info('Create AccountsState');
}

List<WalletAddress>? addresses;
final Map<String, Pair<BigInt?, String>> addressBalances;

AccountsState copyWith({
List<WalletAddress>? addresses,
Map<String, Pair<BigInt?, String>>? addressBalances,
}) =>
AccountsState(
addresses: addresses ?? this.addresses,
addressBalances: addressBalances ?? this.addressBalances,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import 'dart:async';
import 'package:autonomy_flutter/common/injector.dart';
import 'package:autonomy_flutter/main.dart';
import 'package:autonomy_flutter/model/wallet_address.dart';
import 'package:autonomy_flutter/screen/bloc/accounts/accounts_bloc.dart';
import 'package:autonomy_flutter/screen/bloc/accounts/accounts_state.dart';
import 'package:autonomy_flutter/screen/settings/crypto/wallet_detail/wallet_detail_bloc.dart';
import 'package:autonomy_flutter/screen/settings/crypto/wallet_detail/wallet_detail_state.dart';
import 'package:autonomy_flutter/service/address_service.dart';
Expand All @@ -18,6 +20,7 @@ import 'package:autonomy_flutter/util/au_icons.dart';
import 'package:autonomy_flutter/util/constants.dart';
import 'package:autonomy_flutter/util/feral_file_custom_tab.dart';
import 'package:autonomy_flutter/util/inapp_notifications.dart';
import 'package:autonomy_flutter/util/int_ext.dart';
import 'package:autonomy_flutter/util/string_ext.dart';
import 'package:autonomy_flutter/util/style.dart';
import 'package:autonomy_flutter/util/ui_helper.dart';
Expand Down Expand Up @@ -91,20 +94,10 @@ class _LinkedWalletDetailPageState extends State<LinkedWalletDetailPage>
}

void _callBloc() {
final cryptoType = widget.payload.address.cryptoType;

switch (cryptoType) {
case CryptoType.ETH:
context
.read<WalletDetailBloc>()
.add(WalletDetailBalanceEvent(cryptoType, _address));
case CryptoType.XTZ:
context
.read<WalletDetailBloc>()
.add(WalletDetailBalanceEvent(cryptoType, _address));
default:
// do nothing
}
// update exchange rate
context.read<WalletDetailBloc>().add(WalletDetailBalanceEvent(_address));
// update balance
context.read<AccountsBloc>().add(GetAccountBalanceEvent([_address]));
}

void _listener() {
Expand Down Expand Up @@ -189,10 +182,26 @@ class _LinkedWalletDetailPageState extends State<LinkedWalletDetailPage>
AnimatedContainer(
duration: const Duration(milliseconds: 3000),
height: hideConnection ? 60 : null,
child: _balanceSection(
context,
state.balance,
state.balanceInUSD,
child: BlocBuilder<AccountsBloc, AccountsState>(
builder: (context, accountsState) {
final balance = accountsState.addressBalances[_address];
final cryptoBalance = balance?.first;
final exchangeRate = state.exchangeRate;
final balanceString = cryptoBalance == null
? '--'
: cryptoBalance
.toBalanceStringValue(_walletAddress.cryptoType);
final balanceInUSD =
exchangeRate != null && cryptoBalance != null
? exchangeRate.toUsd(
amount: cryptoBalance, cryptoType: cryptoType)
: '';
return _balanceSection(
context,
balanceString,
balanceInUSD,
);
},
),
),
Visibility(
Expand Down
44 changes: 9 additions & 35 deletions lib/screen/settings/crypto/wallet_detail/wallet_detail_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,20 @@
import 'package:autonomy_flutter/au_bloc.dart';
import 'package:autonomy_flutter/screen/settings/crypto/wallet_detail/wallet_detail_state.dart';
import 'package:autonomy_flutter/service/currency_service.dart';
import 'package:autonomy_flutter/service/ethereum_service.dart';
import 'package:autonomy_flutter/service/tezos_service.dart';
import 'package:autonomy_flutter/util/constants.dart';
import 'package:autonomy_flutter/util/eth_amount_formatter.dart';
import 'package:autonomy_flutter/util/xtz_utils.dart';

class WalletDetailBloc extends AuBloc<WalletDetailEvent, WalletDetailState> {
final EthereumService _ethereumService;
final TezosService _tezosService;
final CurrencyService _currencyService;
final ethFormatter = EthAmountFormatter();
final xtzFormatter = XtzAmountFormatter();

WalletDetailBloc(
this._ethereumService, this._tezosService, this._currencyService)
: super(WalletDetailState()) {
this._currencyService,
) : super(WalletDetailState()) {
on<WalletDetailBalanceEvent>((event, emit) async {
final exchangeRate = await _currencyService.getExchangeRates();
String balanceS = '';
String balanceInUSD = '';

switch (event.type) {
case CryptoType.ETH:
final balance = await _ethereumService.getBalance(event.address);
balanceS = '${ethFormatter.format(balance.getInWei)} ETH';
final usdBalance = exchangeRate.ethToUsd(balance.getInWei);
balanceInUSD = '$usdBalance USD';
case CryptoType.XTZ:
final balance = await _tezosService.getBalance(event.address);
balanceS = '${xtzFormatter.format(balance)} XTZ';
final usdBalance = exchangeRate.xtzToUsd(balance);
balanceInUSD = '$usdBalance USD';

default:
}

emit(state.copyWith(
balance: balanceS,
balanceInUSD: balanceInUSD,
));
emit(
state.copyWith(
exchangeRate: exchangeRate,
),
);
});
}

final CurrencyService _currencyService;
}
Loading

0 comments on commit d492e05

Please sign in to comment.