Skip to content

Commit

Permalink
fix(EWM-369): use tokenWalletService to search wallets on the first…
Browse files Browse the repository at this point in the history
… entering
  • Loading branch information
Egor Komarov committed Oct 31, 2024
1 parent b3a3ce3 commit fab32aa
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ class AccountAssetsTab extends StatelessWidget {
create: (_) => AccountAssetTabCubit(
account,
isFirstEntering,
inject<NekotonRepository>(),
inject<TokenWalletsService>(),
inject<AssetsService>(),
inject<BalanceStorageService>(),
),
child: BlocBuilder<AccountAssetTabCubit, AccountAssetTabState>(
builder: (context, state) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ class AccountAssetTabCubit extends Cubit<AccountAssetTabState> {
KeyAccount account,
// ignore: avoid_positional_boolean_parameters
this.isFirstEntering,
this.nekotonRepository,
this.tokenWalletsService,
this.assetsService,
this.balanceStorage,
) : tonWallet = account.account.tonWallet,
super(
AccountAssetTabState.accounts(account.account.tonWallet, null, 0),
Expand All @@ -33,53 +32,41 @@ class AccountAssetTabCubit extends Cubit<AccountAssetTabState> {
AccountAssetTabState.accounts(tonWallet, _contracts, _contractCount),
);
});
if (isFirstEntering) {
_allContractsSubscription = assetsService
.allAvailableContractsForAccount(account.address)
.listen((value) async {
final assets = <TokenContractAsset>[];
for (final asset in value.$1) {
try {
final wallet = await nekotonRepository.subscribeToken(
owner: tonWallet.address,
rootTokenContract: asset.address,
);

if (wallet.wallet?.moneyBalance != null &&
wallet.wallet?.moneyBalance.amount != Fixed.zero) {
assets.add(asset);
}
} finally {
nekotonRepository.unsubscribeToken(
tonWallet.address,
asset.address,
);
}
}
_contractCount = assets.length;
emit(
AccountAssetTabState.accounts(tonWallet, _contracts, _contractCount),
);
});
if (isFirstEntering || true) {
_searchSubscription = tokenWalletsService
.searchTokenWalletsForAddress(tonWallet.address)
.reduce((previous, element) => [...previous, ...element])
.asStream()
.listen(
(value) {
_contractCount = value.length;
emit(
AccountAssetTabState.accounts(
tonWallet,
_contracts,
_contractCount,
),
);
},
);
}
}

final AssetsService assetsService;
final BalanceStorageService balanceStorage;
final NekotonRepository nekotonRepository;
final TokenWalletsService tokenWalletsService;
final TonWalletAsset tonWallet;
final bool isFirstEntering;

late List<TokenContractAsset>? _contracts;
int? _contractCount;
late StreamSubscription<List<TokenContractAsset>> _contractsSubscription;
StreamSubscription<(List<TokenContractAsset>, List<TokenContractAsset>)>?
_allContractsSubscription;
StreamSubscription<dynamic>? _searchSubscription;

@override
Future<void> close() {
_contractsSubscription.cancel();
_allContractsSubscription?.cancel();
Future<void> close() async {
await _contractsSubscription.cancel();
await _searchSubscription?.cancel();

return super.close();
}
Expand Down

0 comments on commit fab32aa

Please sign in to comment.