Fix Accounts Popup UI

This commit is contained in:
OmarHatem 2023-07-03 20:18:24 +03:00
parent 474abe15a7
commit 5905ac35d8
6 changed files with 37 additions and 34 deletions

View file

@ -13,7 +13,7 @@ class Erc20Token extends CryptoCurrency with HiveObjectMixin {
final String contractAddress; final String contractAddress;
@HiveField(3) @HiveField(3)
final int decimal; final int decimal;
@HiveField(4, defaultValue: false) @HiveField(4, defaultValue: true)
bool _enabled; bool _enabled;
@HiveField(5) @HiveField(5)
final String? iconPath; final String? iconPath;
@ -30,7 +30,7 @@ class Erc20Token extends CryptoCurrency with HiveObjectMixin {
required this.symbol, required this.symbol,
required this.contractAddress, required this.contractAddress,
required this.decimal, required this.decimal,
bool enabled = false, bool enabled = true,
this.iconPath, this.iconPath,
}) : _enabled = enabled, }) : _enabled = enabled,
super( super(

View file

@ -310,7 +310,7 @@ abstract class EthereumWalletBase
symbol: token.symbol, symbol: token.symbol,
contractAddress: token.contractAddress, contractAddress: token.contractAddress,
decimal: token.decimal, decimal: token.decimal,
enabled: true, enabled: token.enabled,
iconPath: iconPath, iconPath: iconPath,
)); ));

View file

@ -31,6 +31,7 @@ class EditTokenPage extends BasePage {
return EditTokenPageBody( return EditTokenPageBody(
homeSettingsViewModel: homeSettingsViewModel, homeSettingsViewModel: homeSettingsViewModel,
erc20token: erc20token, erc20token: erc20token,
initialContractAddress: initialContractAddress,
); );
} }
} }

View file

@ -70,6 +70,7 @@ class HomeSettingsPage extends BasePage {
borderSide: const BorderSide(color: Colors.transparent), borderSide: const BorderSide(color: Colors.transparent),
), ),
), ),
onChanged: (String text) => _homeSettingsViewModel.changeSearchText(text),
), ),
), ),
), ),
@ -104,7 +105,7 @@ class HomeSettingsPage extends BasePage {
margin: EdgeInsets.only(top: 16, left: 16, right: 16), margin: EdgeInsets.only(top: 16, left: 16, right: 16),
child: Observer( child: Observer(
builder: (_) { builder: (_) {
final token = _homeSettingsViewModel.tokens[index]; final token = _homeSettingsViewModel.tokens.elementAt(index);
return SettingsSwitcherCell( return SettingsSwitcherCell(
title: "${token.name} " title: "${token.name} "

View file

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

View file

@ -13,24 +13,19 @@ part 'home_settings_view_model.g.dart';
class HomeSettingsViewModel = HomeSettingsViewModelBase with _$HomeSettingsViewModel; class HomeSettingsViewModel = HomeSettingsViewModelBase with _$HomeSettingsViewModel;
abstract class HomeSettingsViewModelBase with Store { abstract class HomeSettingsViewModelBase with Store {
HomeSettingsViewModelBase(this._settingsStore, this._balanceViewModel) HomeSettingsViewModelBase(this._settingsStore, this._balanceViewModel);
: tokens = ObservableList<Erc20Token>() {
_updateTokensList();
}
final SettingsStore _settingsStore; final SettingsStore _settingsStore;
final BalanceViewModel _balanceViewModel; final BalanceViewModel _balanceViewModel;
final ObservableList<Erc20Token> tokens; @observable
String searchText = '';
@computed @computed
SortBalanceBy get sortBalanceBy => _settingsStore.sortBalanceBy; SortBalanceBy get sortBalanceBy => _settingsStore.sortBalanceBy;
@action @action
void setSortBalanceBy(SortBalanceBy value) { void setSortBalanceBy(SortBalanceBy value) => _settingsStore.sortBalanceBy = value;
_settingsStore.sortBalanceBy = value;
_sortTokens();
}
@computed @computed
bool get pinNativeToken => _settingsStore.pinNativeTokenAtTop; bool get pinNativeToken => _settingsStore.pinNativeTokenAtTop;
@ -38,20 +33,13 @@ abstract class HomeSettingsViewModelBase with Store {
@action @action
void setPinNativeToken(bool value) => _settingsStore.pinNativeTokenAtTop = value; void setPinNativeToken(bool value) => _settingsStore.pinNativeTokenAtTop = value;
@action
void _updateTokensList() {
_sortTokens();
}
Future<void> addErc20Token(Erc20Token token) async { Future<void> addErc20Token(Erc20Token token) async {
await ethereum!.addErc20Token(_balanceViewModel.wallet, token); await ethereum!.addErc20Token(_balanceViewModel.wallet, token);
_updateTokensList();
_updateFiatPrices(token); _updateFiatPrices(token);
} }
Future<void> deleteErc20Token(Erc20Token token) async { Future<void> deleteErc20Token(Erc20Token token) async {
await ethereum!.deleteErc20Token(_balanceViewModel.wallet, token); await ethereum!.deleteErc20Token(_balanceViewModel.wallet, token);
_updateTokensList();
} }
Future<Erc20Token?> getErc20Token(String contractAddress) async => Future<Erc20Token?> getErc20Token(String contractAddress) async =>
@ -70,25 +58,34 @@ abstract class HomeSettingsViewModelBase with Store {
} }
void changeTokenAvailability(int index, bool value) async { void changeTokenAvailability(int index, bool value) async {
tokens[index].enabled = value; tokens.elementAt(index).enabled = value;
_balanceViewModel.wallet.updateBalance(); _balanceViewModel.wallet.updateBalance();
_updateTokensList();
} }
void _sortTokens() { @computed
tokens.clear(); Set<Erc20Token> get tokens {
final Set<Erc20Token> tokens = {};
// Add Sorted Enabled tokens _balanceViewModel.formattedBalances.forEach((e) {
for (int i = 0; i < _balanceViewModel.balances.keys.length; i++) { if (e.asset is Erc20Token && _matchesSearchText(e.asset as Erc20Token)) {
final CryptoCurrency currency = _balanceViewModel.balances.keys.elementAt(i); tokens.add(e.asset as Erc20Token);
if (currency is Erc20Token) {
tokens.add(currency);
} }
} });
// Add disabled tokens
tokens.addAll(ethereum! tokens.addAll(ethereum!
.getERC20Currencies(_balanceViewModel.wallet) .getERC20Currencies(_balanceViewModel.wallet)
.where((element) => !element.enabled)); .where((element) => _matchesSearchText(element)));
return tokens;
}
@action
void changeSearchText(String text) => searchText = text;
bool _matchesSearchText(Erc20Token asset) {
return searchText.isEmpty ||
asset.fullName!.toLowerCase().contains(searchText.toLowerCase()) ||
asset.title.toLowerCase().contains(searchText.toLowerCase()) ||
asset.contractAddress == searchText;
} }
} }