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

View file

@ -459,13 +459,11 @@ Future setup(
(ContactRecord? contact, _) =>
ContactViewModel(_contactSource, contact: contact));
getIt.registerFactory(
() => ContactListViewModel(_contactSource, _walletInfoSource));
getIt.registerFactoryParam<ContactListViewModel, CryptoCurrency?, void>(
(CryptoCurrency? cur, _) => ContactListViewModel(_contactSource, _walletInfoSource, cur));
getIt.registerFactoryParam<ContactListPage, CryptoCurrency?, void>((CryptoCurrency? cur, _)
=> ContactListPage(
getIt.get<ContactListViewModel>(),
selectedCurrency: cur));
=> ContactListPage(getIt.get<ContactListViewModel>(param1: cur)));
getIt.registerFactoryParam<ContactPage, ContactRecord?, void>(
(ContactRecord? contact, _) =>

View file

@ -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;
}

View file

@ -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();
}