Pass selected currency to the view model instead of the UI

This commit is contained in:
OmarHatem
2022-12-05 18:37:35 +02:00
parent 4c7a0a8cb9
commit 3aa0e626ff
3 changed files with 28 additions and 42 deletions
@@ -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<ContactRecord>(),
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<WalletInfo> walletInfoSource;
final ObservableList<ContactRecord> contacts;
final List<WalletContact> walletContacts;
final CryptoCurrency? _currency;
StreamSubscription<BoxEvent>? _subscription;
bool get isEditable => _currency == null;
Future<void> delete(ContactRecord contact) async => contact.original.delete();
List<ContactRecord> getContacts(CryptoCurrency? cur) {
if (cur != null) {
return contacts.where((element) => element.type == cur).toList();
}
@computed
List<ContactRecord> get contactsToShow =>
contacts.where((element) => _currency == null || element.type == _currency).toList();
return contacts;
}
List<WalletContact> getWallets(CryptoCurrency? cur) {
if (cur != null) {
return walletContacts.where((element) => element.type == cur).toList();
}
return walletContacts;
}
@computed
List<WalletContact> get walletContactsToShow =>
walletContacts.where((element) => _currency == null || element.type == _currency).toList();
}