Fix Enabling/Disabling tokens

Fix sorting by fiat once app is opened
Improve token availability mechanism
This commit is contained in:
OmarHatem 2023-07-04 04:09:17 +03:00
parent 4c37ed3a55
commit 72a99ab7dd
6 changed files with 63 additions and 33 deletions

View file

@ -20,10 +20,7 @@ class Erc20Token extends CryptoCurrency with HiveObjectMixin {
bool get enabled => _enabled;
set enabled(bool value) {
_enabled = value;
this.save();
}
set enabled(bool value) => _enabled = value;
Erc20Token({
required this.name,

View file

@ -305,16 +305,25 @@ 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,
));
await erc20TokensBox.put(
token.contractAddress,
Erc20Token(
name: token.name,
symbol: token.symbol,
contractAddress: token.contractAddress,
decimal: token.decimal,
enabled: token.enabled,
iconPath: iconPath,
));
_updateBalance();
if (token.enabled) {
balance[token] = await _client.fetchERC20Balances(
_privateKey.address,
token.contractAddress,
);
} else {
balance.remove(token);
}
}
Future<void> deleteErc20Token(Erc20Token token) async {
@ -349,14 +358,16 @@ abstract class EthereumWalletBase
};
for (var currency in _initialErc20Currencies.keys) {
erc20TokensBox.put(_initialErc20Currencies[currency]!['contractAddress'], Erc20Token(
name: currency.fullName ?? currency.title,
symbol: currency.title,
contractAddress: _initialErc20Currencies[currency]!['contractAddress'],
decimal: _initialErc20Currencies[currency]!['decimal'],
enabled: true,
iconPath: currency.iconPath,
));
erc20TokensBox.put(
_initialErc20Currencies[currency]!['contractAddress'],
Erc20Token(
name: currency.fullName ?? currency.title,
symbol: currency.title,
contractAddress: _initialErc20Currencies[currency]!['contractAddress'],
decimal: _initialErc20Currencies[currency]!['decimal'],
enabled: true,
iconPath: currency.iconPath,
));
}
}
}

View file

@ -1,6 +1,6 @@
import 'package:cake_wallet/entities/fiat_api_mode.dart';
import 'package:cake_wallet/entities/fiat_currency.dart';
import 'package:cake_wallet/entities/update_haven_rate.dart';
import 'package:cake_wallet/ethereum/ethereum.dart';
import 'package:cw_core/transaction_history.dart';
import 'package:cw_core/balance.dart';
import 'package:cw_core/transaction_info.dart';
@ -97,6 +97,17 @@ void startCurrentWalletChangeReaction(AppStore appStore,
crypto: wallet.currency,
fiat: settingsStore.fiatCurrency,
torOnly: settingsStore.fiatApiMode == FiatApiMode.torOnly);
if (wallet.type == WalletType.ethereum) {
final currencies = ethereum!.getERC20Currencies(appStore.wallet!);
for (final currency in currencies) {
fiatConversionStore.prices[currency] = await FiatConversionService.fetchPrice(
crypto: currency,
fiat: settingsStore.fiatCurrency,
torOnly: settingsStore.fiatApiMode == FiatApiMode.torOnly);
}
}
} catch (e) {
print(e.toString());
}

View file

@ -112,7 +112,7 @@ class HomeSettingsPage extends BasePage {
"(${token.symbol})",
value: token.enabled,
onValueChange: (_, bool value) {
_homeSettingsViewModel.changeTokenAvailability(index, value);
_homeSettingsViewModel.changeTokenAvailability(token, value);
},
onTap: (_) {
Navigator.pushNamed(context, Routes.editToken, arguments: {

View file

@ -310,10 +310,10 @@ abstract class BalanceViewModelBase with Store {
switch (sortBalanceBy) {
case SortBalanceBy.FiatBalance:
return double.parse(_getFiatBalance(
price: fiatConvertationStore.prices[b.asset]!,
price: fiatConvertationStore.prices[b.asset] ?? 0,
cryptoAmount: b.availableBalance))
.compareTo(double.parse(_getFiatBalance(
price: fiatConvertationStore.prices[a.asset]!,
price: fiatConvertationStore.prices[a.asset] ?? 0,
cryptoAmount: a.availableBalance)));
case SortBalanceBy.GrossBalance:
return double.parse(b.availableBalance)

View file

@ -13,11 +13,16 @@ part 'home_settings_view_model.g.dart';
class HomeSettingsViewModel = HomeSettingsViewModelBase with _$HomeSettingsViewModel;
abstract class HomeSettingsViewModelBase with Store {
HomeSettingsViewModelBase(this._settingsStore, this._balanceViewModel);
HomeSettingsViewModelBase(this._settingsStore, this._balanceViewModel)
: tokens = ObservableSet<Erc20Token>() {
_updateTokensList();
}
final SettingsStore _settingsStore;
final BalanceViewModel _balanceViewModel;
final ObservableSet<Erc20Token> tokens;
@observable
String searchText = '';
@ -57,14 +62,15 @@ abstract class HomeSettingsViewModelBase with Store {
} catch (_) {}
}
void changeTokenAvailability(int index, bool value) async {
tokens.elementAt(index).enabled = value;
_balanceViewModel.wallet.updateBalance();
void changeTokenAvailability(Erc20Token token, bool value) async {
token.enabled = value;
ethereum!.addErc20Token(_balanceViewModel.wallet, token);
_refreshTokensList();
}
@computed
Set<Erc20Token> get tokens {
final Set<Erc20Token> tokens = {};
@action
void _updateTokensList() {
tokens.clear();
_balanceViewModel.formattedBalances.forEach((e) {
if (e.asset is Erc20Token && _matchesSearchText(e.asset as Erc20Token)) {
@ -75,8 +81,13 @@ abstract class HomeSettingsViewModelBase with Store {
tokens.addAll(ethereum!
.getERC20Currencies(_balanceViewModel.wallet)
.where((element) => _matchesSearchText(element)));
}
return tokens;
@action
void _refreshTokensList() {
final _tokens = Set.of(tokens);
tokens.clear();
tokens.addAll(_tokens);
}
@action