From 2cd748c151488a8378d1e28c25a98588ad163c6e Mon Sep 17 00:00:00 2001 From: Serhii Date: Sat, 5 Nov 2022 20:05:55 +0200 Subject: [PATCH 1/6] filter contacts and wallets by type --- lib/di.dart | 9 ++++--- lib/router.dart | 8 ++++-- .../screens/contact/contact_list_page.dart | 25 +++++++++++-------- .../exchange/widgets/exchange_card.dart | 3 ++- lib/src/screens/send/widgets/send_card.dart | 1 + lib/src/widgets/address_text_field.dart | 7 ++++-- .../contact_list/contact_list_view_model.dart | 17 +++++++++++++ 7 files changed, 51 insertions(+), 19 deletions(-) diff --git a/lib/di.dart b/lib/di.dart index 7ce2ff6d9..01d77b881 100644 --- a/lib/di.dart +++ b/lib/di.dart @@ -153,6 +153,7 @@ import 'package:cake_wallet/anypay/any_pay_payment_committed_info.dart'; import 'package:cake_wallet/ionia/ionia_any_pay_payment_info.dart'; import 'package:cake_wallet/src/screens/receive/fullscreen_qr_page.dart'; import 'package:cake_wallet/core/wallet_loading_service.dart'; +import 'package:cw_core/crypto_currency.dart'; final getIt = GetIt.instance; @@ -461,9 +462,11 @@ Future setup( getIt.registerFactory( () => ContactListViewModel(_contactSource, _walletInfoSource)); - getIt.registerFactoryParam( - (bool isEditable, _) => ContactListPage(getIt.get(), - isEditable: isEditable)); + getIt.registerFactoryParam((bool isEditable, CryptoCurrency? cur) + => ContactListPage( + getIt.get(), + isEditable: isEditable, + selectedCurrency: cur)); getIt.registerFactoryParam( (ContactRecord? contact, _) => diff --git a/lib/router.dart b/lib/router.dart index b255ce8b9..8b43fbaf5 100644 --- a/lib/router.dart +++ b/lib/router.dart @@ -74,6 +74,7 @@ import 'package:cake_wallet/src/screens/ionia/ionia.dart'; import 'package:cake_wallet/src/screens/ionia/cards/ionia_payment_status_page.dart'; import 'package:cake_wallet/anypay/any_pay_payment_committed_info.dart'; import 'package:cake_wallet/ionia/ionia_any_pay_payment_info.dart'; +import 'package:cw_core/crypto_currency.dart'; late RouteSettings currentRouteSettings; @@ -298,11 +299,14 @@ Route createRoute(RouteSettings settings) { case Routes.addressBook: return MaterialPageRoute( - builder: (_) => getIt.get(param1: true)); + builder: (_) => + getIt.get(param1: true, param2: null)); case Routes.pickerAddressBook: + final selectedCurrency = settings.arguments as CryptoCurrency; return MaterialPageRoute( - builder: (_) => getIt.get(param1: false)); + builder: (_) => getIt.get(param1: false, + param2: selectedCurrency)); case Routes.addressBookAddContact: return CupertinoPageRoute( diff --git a/lib/src/screens/contact/contact_list_page.dart b/lib/src/screens/contact/contact_list_page.dart index 7912472d8..6d23518b8 100644 --- a/lib/src/screens/contact/contact_list_page.dart +++ b/lib/src/screens/contact/contact_list_page.dart @@ -16,10 +16,12 @@ import 'package:cake_wallet/view_model/contact_list/contact_list_view_model.dart import 'package:cake_wallet/src/widgets/collapsible_standart_list.dart'; class ContactListPage extends BasePage { - ContactListPage(this.contactListViewModel, {this.isEditable = true}); + ContactListPage(this.contactListViewModel, + {this.isEditable = true, required this.selectedCurrency}); final ContactListViewModel contactListViewModel; final bool isEditable; + final CryptoCurrency? selectedCurrency; @override String get title => S.current.address_book; @@ -60,11 +62,13 @@ class ContactListPage extends BasePage { @override Widget body(BuildContext context) { + final contacts = + contactListViewModel.getContacts(isEditable, selectedCurrency); + final walletContacts = + contactListViewModel.getWallets(isEditable, selectedCurrency); return Container( padding: EdgeInsets.only(top: 20.0, bottom: 20.0), - child: Observer( - builder: (_) { - return CollapsibleSectionList( + child: CollapsibleSectionList( context: context, sectionCount: 2, themeColor: Theme.of(context).primaryTextTheme.headline6!.color!, @@ -82,15 +86,15 @@ class ContactListPage extends BasePage { child: Text(title, style: TextStyle(fontSize: 36))); }, itemCounter: (int sectionIndex) => sectionIndex == 0 - ? contactListViewModel.walletContacts.length - : contactListViewModel.contacts.length, + ? walletContacts.length + : contacts.length, itemBuilder: (_, sectionIndex, index) { if (sectionIndex == 0) { - final walletInfo = contactListViewModel.walletContacts[index]; + final walletInfo = walletContacts[index]; return generateRaw(context, walletInfo); } - final contact = contactListViewModel.contacts[index]; + final contact = contacts[index]; final content = generateRaw(context, contact); return !isEditable ? content @@ -100,9 +104,8 @@ class ContactListPage extends BasePage { child: content, ); }, - ); - }, - )); + ) + ); } Widget generateRaw(BuildContext context, ContactBase contact) { diff --git a/lib/src/screens/exchange/widgets/exchange_card.dart b/lib/src/screens/exchange/widgets/exchange_card.dart index 98843b1fb..40aa679a4 100644 --- a/lib/src/screens/exchange/widgets/exchange_card.dart +++ b/lib/src/screens/exchange/widgets/exchange_card.dart @@ -395,7 +395,8 @@ class ExchangeCardState extends State { buttonColor: widget.addressButtonsColor, validator: widget.addressTextFieldValidator, onPushPasteButton: widget.onPushPasteButton, - onPushAddressBookButton: widget.onPushAddressBookButton + onPushAddressBookButton: widget.onPushAddressBookButton, + selectedCurrency: _selectedCurrency ), ) diff --git a/lib/src/screens/send/widgets/send_card.dart b/lib/src/screens/send/widgets/send_card.dart index dc589923f..03e4471fe 100644 --- a/lib/src/screens/send/widgets/send_card.dart +++ b/lib/src/screens/send/widgets/send_card.dart @@ -155,6 +155,7 @@ class SendCardState extends State await output.fetchParsedAddress(context); }, validator: validator, + selectedCurrency: sendViewModel.currency, ); }), if (output.isParsedAddress) Padding( diff --git a/lib/src/widgets/address_text_field.dart b/lib/src/widgets/address_text_field.dart index 35bf3ed85..7e0727312 100644 --- a/lib/src/widgets/address_text_field.dart +++ b/lib/src/widgets/address_text_field.dart @@ -4,6 +4,7 @@ import 'package:cake_wallet/routes.dart'; import 'package:cake_wallet/generated/i18n.dart'; import 'package:cake_wallet/entities/qr_scanner.dart'; import 'package:cake_wallet/entities/contact_base.dart'; +import 'package:cw_core/crypto_currency.dart'; enum AddressTextFieldOption { paste, qrCode, addressBook } @@ -26,7 +27,8 @@ class AddressTextField extends StatelessWidget { this.hintStyle, this.validator, this.onPushPasteButton, - this.onPushAddressBookButton}); + this.onPushAddressBookButton, + this.selectedCurrency}); static const prefixIconWidth = 34.0; static const prefixIconHeight = 34.0; @@ -47,6 +49,7 @@ class AddressTextField extends StatelessWidget { final FocusNode? focusNode; final Function(BuildContext context)? onPushPasteButton; final Function(BuildContext context)? onPushAddressBookButton; + final CryptoCurrency? selectedCurrency; @override Widget build(BuildContext context) { @@ -207,7 +210,7 @@ class AddressTextField extends StatelessWidget { Future _presetAddressBookPicker(BuildContext context) async { final contact = await Navigator.of(context, rootNavigator: true) - .pushNamed(Routes.pickerAddressBook); + .pushNamed(Routes.pickerAddressBook,arguments: selectedCurrency); if (contact is ContactBase && contact.address != null) { controller?.text = contact.address; diff --git a/lib/view_model/contact_list/contact_list_view_model.dart b/lib/view_model/contact_list/contact_list_view_model.dart index ec145ad44..0f67245af 100644 --- a/lib/view_model/contact_list/contact_list_view_model.dart +++ b/lib/view_model/contact_list/contact_list_view_model.dart @@ -7,6 +7,7 @@ import 'package:mobx/mobx.dart'; import 'package:cake_wallet/entities/contact_record.dart'; import 'package:cake_wallet/entities/contact.dart'; import 'package:cake_wallet/utils/mobx.dart'; +import 'package:cw_core/crypto_currency.dart'; part 'contact_list_view_model.g.dart'; @@ -44,4 +45,20 @@ abstract class ContactListViewModelBase with Store { StreamSubscription? _subscription; Future delete(ContactRecord contact) async => contact.original.delete(); + + List getContacts(bool isEditable, CryptoCurrency? cur) { + if (!isEditable) { + return contacts.where((element) => element.type == cur).toList(); + } + + return contacts; + } + + List getWallets(bool isEditable, CryptoCurrency? cur) { + if (!isEditable) { + return walletContacts.where((element) => element.type == cur).toList(); + } + + return walletContacts; + } } From 05f1b17a732932ed12b2b93b751acf218160214b Mon Sep 17 00:00:00 2001 From: Serhii Date: Mon, 21 Nov 2022 22:41:09 +0200 Subject: [PATCH 2/6] fix contact list bug --- lib/router.dart | 2 +- lib/src/screens/contact/contact_list_page.dart | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/router.dart b/lib/router.dart index 8b43fbaf5..9fe22e22d 100644 --- a/lib/router.dart +++ b/lib/router.dart @@ -300,7 +300,7 @@ Route createRoute(RouteSettings settings) { case Routes.addressBook: return MaterialPageRoute( builder: (_) => - getIt.get(param1: true, param2: null)); + getIt.get(param1: true)); case Routes.pickerAddressBook: final selectedCurrency = settings.arguments as CryptoCurrency; diff --git a/lib/src/screens/contact/contact_list_page.dart b/lib/src/screens/contact/contact_list_page.dart index 6d23518b8..8e9de23fd 100644 --- a/lib/src/screens/contact/contact_list_page.dart +++ b/lib/src/screens/contact/contact_list_page.dart @@ -68,7 +68,9 @@ class ContactListPage extends BasePage { contactListViewModel.getWallets(isEditable, selectedCurrency); return Container( padding: EdgeInsets.only(top: 20.0, bottom: 20.0), - child: CollapsibleSectionList( + child: Observer( + builder: (_) { + return CollapsibleSectionList( context: context, sectionCount: 2, themeColor: Theme.of(context).primaryTextTheme.headline6!.color!, @@ -104,7 +106,7 @@ class ContactListPage extends BasePage { child: content, ); }, - ) + );}) ); } From 2db75344802296ad6fe0d68c4bcedc997e9263af Mon Sep 17 00:00:00 2001 From: Serhii Date: Tue, 22 Nov 2022 00:07:27 +0200 Subject: [PATCH 3/6] small fix --- lib/src/screens/contact/contact_list_page.dart | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/src/screens/contact/contact_list_page.dart b/lib/src/screens/contact/contact_list_page.dart index 8e9de23fd..7895a1df3 100644 --- a/lib/src/screens/contact/contact_list_page.dart +++ b/lib/src/screens/contact/contact_list_page.dart @@ -62,14 +62,15 @@ class ContactListPage extends BasePage { @override Widget body(BuildContext context) { - final contacts = - contactListViewModel.getContacts(isEditable, selectedCurrency); - final walletContacts = - contactListViewModel.getWallets(isEditable, selectedCurrency); + return Container( padding: EdgeInsets.only(top: 20.0, bottom: 20.0), child: Observer( builder: (_) { + final contacts = + contactListViewModel.getContacts(isEditable, selectedCurrency); + final walletContacts = + contactListViewModel.getWallets(isEditable, selectedCurrency); return CollapsibleSectionList( context: context, sectionCount: 2, From 5f1e8e9d33fa04bac655e870820620f059ffd4e7 Mon Sep 17 00:00:00 2001 From: Serhii Date: Fri, 2 Dec 2022 23:36:22 +0200 Subject: [PATCH 4/6] remove isEditable parameter --- lib/di.dart | 3 +-- lib/router.dart | 5 ++--- lib/src/screens/contact/contact_list_page.dart | 13 ++++++------- .../contact_list/contact_list_view_model.dart | 8 ++++---- 4 files changed, 13 insertions(+), 16 deletions(-) diff --git a/lib/di.dart b/lib/di.dart index b84f98ad9..539681036 100644 --- a/lib/di.dart +++ b/lib/di.dart @@ -462,10 +462,9 @@ Future setup( getIt.registerFactory( () => ContactListViewModel(_contactSource, _walletInfoSource)); - getIt.registerFactoryParam((bool isEditable, CryptoCurrency? cur) + getIt.registerFactoryParam((CryptoCurrency? cur, _) => ContactListPage( getIt.get(), - isEditable: isEditable, selectedCurrency: cur)); getIt.registerFactoryParam( diff --git a/lib/router.dart b/lib/router.dart index 9fe22e22d..07e500544 100644 --- a/lib/router.dart +++ b/lib/router.dart @@ -300,13 +300,12 @@ Route createRoute(RouteSettings settings) { case Routes.addressBook: return MaterialPageRoute( builder: (_) => - getIt.get(param1: true)); + getIt.get()); case Routes.pickerAddressBook: final selectedCurrency = settings.arguments as CryptoCurrency; return MaterialPageRoute( - builder: (_) => getIt.get(param1: false, - param2: selectedCurrency)); + builder: (_) => getIt.get(param1: selectedCurrency)); case Routes.addressBookAddContact: return CupertinoPageRoute( diff --git a/lib/src/screens/contact/contact_list_page.dart b/lib/src/screens/contact/contact_list_page.dart index 7895a1df3..8fcaf31a2 100644 --- a/lib/src/screens/contact/contact_list_page.dart +++ b/lib/src/screens/contact/contact_list_page.dart @@ -17,10 +17,9 @@ import 'package:cake_wallet/src/widgets/collapsible_standart_list.dart'; class ContactListPage extends BasePage { ContactListPage(this.contactListViewModel, - {this.isEditable = true, required this.selectedCurrency}); + {this.selectedCurrency}); final ContactListViewModel contactListViewModel; - final bool isEditable; final CryptoCurrency? selectedCurrency; @override @@ -28,7 +27,7 @@ class ContactListPage extends BasePage { @override Widget? trailing(BuildContext context) { - if (!isEditable) { + if (selectedCurrency != null) { return null; } @@ -68,9 +67,9 @@ class ContactListPage extends BasePage { child: Observer( builder: (_) { final contacts = - contactListViewModel.getContacts(isEditable, selectedCurrency); + contactListViewModel.getContacts(selectedCurrency); final walletContacts = - contactListViewModel.getWallets(isEditable, selectedCurrency); + contactListViewModel.getWallets(selectedCurrency); return CollapsibleSectionList( context: context, sectionCount: 2, @@ -99,7 +98,7 @@ class ContactListPage extends BasePage { final contact = contacts[index]; final content = generateRaw(context, contact); - return !isEditable + return selectedCurrency != null ? content : Slidable( key: Key('${contact.key}'), @@ -116,7 +115,7 @@ class ContactListPage extends BasePage { return GestureDetector( onTap: () async { - if (!isEditable) { + if (selectedCurrency != null) { Navigator.of(context).pop(contact); return; } diff --git a/lib/view_model/contact_list/contact_list_view_model.dart b/lib/view_model/contact_list/contact_list_view_model.dart index 0f67245af..3fa9a73a4 100644 --- a/lib/view_model/contact_list/contact_list_view_model.dart +++ b/lib/view_model/contact_list/contact_list_view_model.dart @@ -46,16 +46,16 @@ abstract class ContactListViewModelBase with Store { Future delete(ContactRecord contact) async => contact.original.delete(); - List getContacts(bool isEditable, CryptoCurrency? cur) { - if (!isEditable) { + List getContacts(CryptoCurrency? cur) { + if (cur != null) { return contacts.where((element) => element.type == cur).toList(); } return contacts; } - List getWallets(bool isEditable, CryptoCurrency? cur) { - if (!isEditable) { + List getWallets(CryptoCurrency? cur) { + if (cur != null) { return walletContacts.where((element) => element.type == cur).toList(); } From 4c7a0a8cb97a4e1af58127bd52e185a3ae60a4e5 Mon Sep 17 00:00:00 2001 From: Serhii Date: Sat, 3 Dec 2022 00:34:34 +0200 Subject: [PATCH 5/6] rework _getCurrencyImage func --- .../screens/contact/contact_list_page.dart | 74 ++----------------- 1 file changed, 6 insertions(+), 68 deletions(-) diff --git a/lib/src/screens/contact/contact_list_page.dart b/lib/src/screens/contact/contact_list_page.dart index 8fcaf31a2..9d0d1ab5a 100644 --- a/lib/src/screens/contact/contact_list_page.dart +++ b/lib/src/screens/contact/contact_list_page.dart @@ -111,7 +111,10 @@ class ContactListPage extends BasePage { } Widget generateRaw(BuildContext context, ContactBase contact) { - final image = _getCurrencyImage(contact.type); + final image = contact.type.iconPath; + final currencyIcon = image != null ? Image.asset(image, height: 24, width: 24) + : const SizedBox(height: 24, width: 24); + return GestureDetector( onTap: () async { @@ -136,12 +139,10 @@ class ContactListPage extends BasePage { mainAxisSize: MainAxisSize.min, mainAxisAlignment: MainAxisAlignment.start, children: [ - image ?? Offstage(), + currencyIcon, Expanded( child: Padding( - padding: image != null - ? EdgeInsets.only(left: 12) - : EdgeInsets.only(left: 0), + padding: EdgeInsets.only(left: 12), child: Text( contact.name, style: TextStyle( @@ -157,69 +158,6 @@ class ContactListPage extends BasePage { ); } - Image? _getCurrencyImage(CryptoCurrency currency) { - Image? image; - - switch (currency) { - case CryptoCurrency.xmr: - image = - Image.asset('assets/images/monero_logo.png', height: 24, width: 24); - break; - case CryptoCurrency.ada: - image = Image.asset('assets/images/ada.png', height: 24, width: 24); - break; - case CryptoCurrency.bch: - image = Image.asset('assets/images/bch.png', height: 24, width: 24); - break; - case CryptoCurrency.bnb: - image = Image.asset('assets/images/bnb.png', height: 24, width: 24); - break; - case CryptoCurrency.btc: - image = Image.asset('assets/images/bitcoin.png', height: 24, width: 24); - break; - case CryptoCurrency.dai: - image = Image.asset('assets/images/dai.png', height: 24, width: 24); - break; - case CryptoCurrency.dash: - image = Image.asset('assets/images/dash.png', height: 24, width: 24); - break; - case CryptoCurrency.eos: - image = Image.asset('assets/images/eos.png', height: 24, width: 24); - break; - case CryptoCurrency.eth: - image = Image.asset('assets/images/eth.png', height: 24, width: 24); - break; - case CryptoCurrency.ltc: - image = - Image.asset('assets/images/litecoin.png', height: 24, width: 24); - break; - case CryptoCurrency.nano: - image = Image.asset('assets/images/nano.png', height: 24, width: 24); - break; - case CryptoCurrency.trx: - image = Image.asset('assets/images/trx.png', height: 24, width: 24); - break; - case CryptoCurrency.usdt: - image = Image.asset('assets/images/usdt.png', height: 24, width: 24); - break; - case CryptoCurrency.usdterc20: - image = Image.asset('assets/images/usdterc.png', height: 24, width: 24); - break; - case CryptoCurrency.xlm: - image = Image.asset('assets/images/xlm.png', height: 24, width: 24); - break; - case CryptoCurrency.xrp: - image = Image.asset('assets/images/xrp.png', height: 24, width: 24); - break; - case CryptoCurrency.xhv: - image = Image.asset('assets/images/haven_logo.png', height: 24, width: 24); - break; - default: - image = null; - } - return image; - } - Future showAlertDialog(BuildContext context) async { return await showPopUp( context: context, From 3aa0e626ff2e1936bed9d9bbdb248d867d68e035 Mon Sep 17 00:00:00 2001 From: OmarHatem Date: Mon, 5 Dec 2022 18:37:35 +0200 Subject: [PATCH 6/6] Pass selected currency to the view model instead of the UI --- lib/di.dart | 8 ++-- .../screens/contact/contact_list_page.dart | 23 +++++------ .../contact_list/contact_list_view_model.dart | 39 ++++++++----------- 3 files changed, 28 insertions(+), 42 deletions(-) diff --git a/lib/di.dart b/lib/di.dart index 539681036..c37f4eeac 100644 --- a/lib/di.dart +++ b/lib/di.dart @@ -459,13 +459,11 @@ Future setup( (ContactRecord? contact, _) => ContactViewModel(_contactSource, contact: contact)); - getIt.registerFactory( - () => ContactListViewModel(_contactSource, _walletInfoSource)); + getIt.registerFactoryParam( + (CryptoCurrency? cur, _) => ContactListViewModel(_contactSource, _walletInfoSource, cur)); getIt.registerFactoryParam((CryptoCurrency? cur, _) - => ContactListPage( - getIt.get(), - selectedCurrency: cur)); + => ContactListPage(getIt.get(param1: cur))); getIt.registerFactoryParam( (ContactRecord? contact, _) => diff --git a/lib/src/screens/contact/contact_list_page.dart b/lib/src/screens/contact/contact_list_page.dart index 9d0d1ab5a..7cd7a0157 100644 --- a/lib/src/screens/contact/contact_list_page.dart +++ b/lib/src/screens/contact/contact_list_page.dart @@ -9,25 +9,22 @@ import 'package:flutter_mobx/flutter_mobx.dart'; import 'package:flutter_slidable/flutter_slidable.dart'; import 'package:cake_wallet/routes.dart'; import 'package:cake_wallet/generated/i18n.dart'; -import 'package:cw_core/crypto_currency.dart'; import 'package:cake_wallet/src/screens/base_page.dart'; import 'package:cake_wallet/src/widgets/alert_with_two_actions.dart'; import 'package:cake_wallet/view_model/contact_list/contact_list_view_model.dart'; import 'package:cake_wallet/src/widgets/collapsible_standart_list.dart'; class ContactListPage extends BasePage { - ContactListPage(this.contactListViewModel, - {this.selectedCurrency}); + ContactListPage(this.contactListViewModel); final ContactListViewModel contactListViewModel; - final CryptoCurrency? selectedCurrency; @override String get title => S.current.address_book; @override Widget? trailing(BuildContext context) { - if (selectedCurrency != null) { + if (!contactListViewModel.isEditable) { return null; } @@ -66,10 +63,8 @@ class ContactListPage extends BasePage { padding: EdgeInsets.only(top: 20.0, bottom: 20.0), child: Observer( builder: (_) { - final contacts = - contactListViewModel.getContacts(selectedCurrency); - final walletContacts = - contactListViewModel.getWallets(selectedCurrency); + final contacts = contactListViewModel.contactsToShow; + final walletContacts = contactListViewModel.walletContactsToShow; return CollapsibleSectionList( context: context, sectionCount: 2, @@ -98,13 +93,13 @@ class ContactListPage extends BasePage { final contact = contacts[index]; final content = generateRaw(context, contact); - return selectedCurrency != null - ? content - : Slidable( + return contactListViewModel.isEditable + ? Slidable( key: Key('${contact.key}'), endActionPane: _actionPane(context, contact), child: content, - ); + ) + : content; }, );}) ); @@ -118,7 +113,7 @@ class ContactListPage extends BasePage { return GestureDetector( onTap: () async { - if (selectedCurrency != null) { + if (!contactListViewModel.isEditable) { Navigator.of(context).pop(contact); return; } diff --git a/lib/view_model/contact_list/contact_list_view_model.dart b/lib/view_model/contact_list/contact_list_view_model.dart index 3fa9a73a4..5b0187fc8 100644 --- a/lib/view_model/contact_list/contact_list_view_model.dart +++ b/lib/view_model/contact_list/contact_list_view_model.dart @@ -11,24 +11,22 @@ import 'package:cw_core/crypto_currency.dart'; part 'contact_list_view_model.g.dart'; -class ContactListViewModel = ContactListViewModelBase - with _$ContactListViewModel; +class ContactListViewModel = ContactListViewModelBase with _$ContactListViewModel; abstract class ContactListViewModelBase with Store { - ContactListViewModelBase(this.contactSource, this.walletInfoSource) + ContactListViewModelBase(this.contactSource, this.walletInfoSource, this._currency) : contacts = ObservableList(), walletContacts = [] { walletInfoSource.values.forEach((info) { if (info.addresses?.isNotEmpty ?? false) { info.addresses?.forEach((address, label) { - final name = label.isNotEmpty - ? info.name + ' ($label)' - : info.name; + final name = label.isNotEmpty ? info.name + ' ($label)' : info.name; walletContacts.add(WalletContact( - address, - name, - walletTypeToCryptoCurrency(info.type))); + address, + name, + walletTypeToCryptoCurrency(info.type), + )); }); } }); @@ -42,23 +40,18 @@ abstract class ContactListViewModelBase with Store { final Box walletInfoSource; final ObservableList contacts; final List walletContacts; + final CryptoCurrency? _currency; StreamSubscription? _subscription; + bool get isEditable => _currency == null; + Future delete(ContactRecord contact) async => contact.original.delete(); - List getContacts(CryptoCurrency? cur) { - if (cur != null) { - return contacts.where((element) => element.type == cur).toList(); - } + @computed + List get contactsToShow => + contacts.where((element) => _currency == null || element.type == _currency).toList(); - return contacts; - } - - List getWallets(CryptoCurrency? cur) { - if (cur != null) { - return walletContacts.where((element) => element.type == cur).toList(); - } - - return walletContacts; - } + @computed + List get walletContactsToShow => + walletContacts.where((element) => _currency == null || element.type == _currency).toList(); }