From fcabc7aec1e54da7fb9f6674b8adeedadabc4594 Mon Sep 17 00:00:00 2001 From: OmarHatem Date: Tue, 4 Jul 2023 04:41:37 +0300 Subject: [PATCH] Fix deleting token Fix renaming tokens --- cw_ethereum/lib/ethereum_wallet.dart | 28 +++++++++---------- .../dashboard/home_settings_view_model.dart | 24 ++++++++++------ 2 files changed, 30 insertions(+), 22 deletions(-) diff --git a/cw_ethereum/lib/ethereum_wallet.dart b/cw_ethereum/lib/ethereum_wallet.dart index febdc57b1..b16001a3a 100644 --- a/cw_ethereum/lib/ethereum_wallet.dart +++ b/cw_ethereum/lib/ethereum_wallet.dart @@ -305,24 +305,24 @@ abstract class EthereumWalletBase .iconPath; } catch (_) {} - await erc20TokensBox.put( - token.contractAddress, - Erc20Token( - name: token.name, - symbol: token.symbol, - contractAddress: token.contractAddress, - decimal: token.decimal, - enabled: token.enabled, - iconPath: iconPath, - )); + final _token = Erc20Token( + name: token.name, + symbol: token.symbol, + contractAddress: token.contractAddress, + decimal: token.decimal, + enabled: token.enabled, + iconPath: iconPath, + ); - if (token.enabled) { - balance[token] = await _client.fetchERC20Balances( + await erc20TokensBox.put(_token.contractAddress, _token); + + if (_token.enabled) { + balance[_token] = await _client.fetchERC20Balances( _privateKey.address, - token.contractAddress, + _token.contractAddress, ); } else { - balance.remove(token); + balance.remove(_token); } } diff --git a/lib/view_model/dashboard/home_settings_view_model.dart b/lib/view_model/dashboard/home_settings_view_model.dart index 165ae6f4f..aaf3e5e55 100644 --- a/lib/view_model/dashboard/home_settings_view_model.dart +++ b/lib/view_model/dashboard/home_settings_view_model.dart @@ -30,7 +30,10 @@ abstract class HomeSettingsViewModelBase with Store { SortBalanceBy get sortBalanceBy => _settingsStore.sortBalanceBy; @action - void setSortBalanceBy(SortBalanceBy value) => _settingsStore.sortBalanceBy = value; + void setSortBalanceBy(SortBalanceBy value) { + _settingsStore.sortBalanceBy = value; + _updateTokensList(); + } @computed bool get pinNativeToken => _settingsStore.pinNativeTokenAtTop; @@ -40,11 +43,13 @@ abstract class HomeSettingsViewModelBase with Store { Future addErc20Token(Erc20Token token) async { await ethereum!.addErc20Token(_balanceViewModel.wallet, token); + _updateTokensList(); _updateFiatPrices(token); } Future deleteErc20Token(Erc20Token token) async { await ethereum!.deleteErc20Token(_balanceViewModel.wallet, token); + _updateTokensList(); } Future getErc20Token(String contractAddress) async => @@ -70,17 +75,20 @@ abstract class HomeSettingsViewModelBase with Store { @action void _updateTokensList() { - tokens.clear(); + int _sortFunc(e1, e2) { + int index1 = _balanceViewModel.formattedBalances.indexWhere((element) => element.asset == e1); + int index2 = _balanceViewModel.formattedBalances.indexWhere((element) => element.asset == e2); - _balanceViewModel.formattedBalances.forEach((e) { - if (e.asset is Erc20Token && _matchesSearchText(e.asset as Erc20Token)) { - tokens.add(e.asset as Erc20Token); - } - }); + return index1.compareTo(index2); + } + + tokens.clear(); tokens.addAll(ethereum! .getERC20Currencies(_balanceViewModel.wallet) - .where((element) => _matchesSearchText(element))); + .where((element) => _matchesSearchText(element)) + .toList() + ..sort(_sortFunc)); } @action