Pass selected currency to the view model instead of the UI
OmarHatem committed
Dec 5, 2022 at 18:37 UTC
3aa0e626ff2e1936bed9d9bbdb248d867d68e035
3 files changed
+28
-42
lib/di.dart
+3
-5
@@ -459,13 +459,11 @@ Future setup(
459
(ContactRecord? contact, _) =>
460
ContactViewModel(_contactSource, contact: contact));
461
462
- getIt.registerFactory(
463
- () => ContactListViewModel(_contactSource, _walletInfoSource));
462
+ getIt.registerFactoryParam<ContactListViewModel, CryptoCurrency?, void>(
463
+ (CryptoCurrency? cur, _) => ContactListViewModel(_contactSource, _walletInfoSource, cur));
464
465
getIt.registerFactoryParam<ContactListPage, CryptoCurrency?, void>((CryptoCurrency? cur, _)
466
- => ContactListPage(
467
- getIt.get<ContactListViewModel>(),
468
- selectedCurrency: cur));
466
+ => ContactListPage(getIt.get<ContactListViewModel>(param1: cur)));
467
468
getIt.registerFactoryParam<ContactPage, ContactRecord?, void>(
469
(ContactRecord? contact, _) =>
lib/src/screens/contact/contact_list_page.dart
+9
-14
@@ -9,25 +9,22 @@ import 'package:flutter_mobx/flutter_mobx.dart';
9
import 'package:flutter_slidable/flutter_slidable.dart';
10
import 'package:cake_wallet/routes.dart';
11
import 'package:cake_wallet/generated/i18n.dart';
12
-import 'package:cw_core/crypto_currency.dart';
12
import 'package:cake_wallet/src/screens/base_page.dart';
13
import 'package:cake_wallet/src/widgets/alert_with_two_actions.dart';
14
import 'package:cake_wallet/view_model/contact_list/contact_list_view_model.dart';
15
import 'package:cake_wallet/src/widgets/collapsible_standart_list.dart';
16
17
class ContactListPage extends BasePage {
19
- ContactListPage(this.contactListViewModel,
20
- {this.selectedCurrency});
18
+ ContactListPage(this.contactListViewModel);
19
20
final ContactListViewModel contactListViewModel;
23
- final CryptoCurrency? selectedCurrency;
21
22
@override
23
String get title => S.current.address_book;
24
25
@override
26
Widget? trailing(BuildContext context) {
30
- if (selectedCurrency != null) {
27
+ if (!contactListViewModel.isEditable) {
28
return null;
29
}
30
@@ -66,10 +63,8 @@ class ContactListPage extends BasePage {
63
padding: EdgeInsets.only(top: 20.0, bottom: 20.0),
64
child: Observer(
65
builder: (_) {
69
- final contacts =
70
- contactListViewModel.getContacts(selectedCurrency);
71
- final walletContacts =
72
- contactListViewModel.getWallets(selectedCurrency);
66
+ final contacts = contactListViewModel.contactsToShow;
67
+ final walletContacts = contactListViewModel.walletContactsToShow;
68
return CollapsibleSectionList(
69
context: context,
70
sectionCount: 2,
@@ -98,13 +93,13 @@ class ContactListPage extends BasePage {
93
94
final contact = contacts[index];
95
final content = generateRaw(context, contact);
101
- return selectedCurrency != null
102
- ? content
103
- : Slidable(
96
+ return contactListViewModel.isEditable
97
+ ? Slidable(
98
key: Key('${contact.key}'),
99
endActionPane: _actionPane(context, contact),
100
child: content,
107
- );
101
+ )
102
+ : content;
103
},
104
);})
105
);
@@ -118,7 +113,7 @@ class ContactListPage extends BasePage {
113
114
return GestureDetector(
115
onTap: () async {
121
- if (selectedCurrency != null) {
116
+ if (!contactListViewModel.isEditable) {
117
Navigator.of(context).pop(contact);
118
return;
119
}
lib/view_model/contact_list/contact_list_view_model.dart
+16
-23
@@ -11,24 +11,22 @@ import 'package:cw_core/crypto_currency.dart';
11
12
part 'contact_list_view_model.g.dart';
13
14
-class ContactListViewModel = ContactListViewModelBase
15
- with _$ContactListViewModel;
14
+class ContactListViewModel = ContactListViewModelBase with _$ContactListViewModel;
15
16
abstract class ContactListViewModelBase with Store {
18
- ContactListViewModelBase(this.contactSource, this.walletInfoSource)
17
+ ContactListViewModelBase(this.contactSource, this.walletInfoSource, this._currency)
18
: contacts = ObservableList<ContactRecord>(),
19
walletContacts = [] {
20
walletInfoSource.values.forEach((info) {
21
if (info.addresses?.isNotEmpty ?? false) {
22
info.addresses?.forEach((address, label) {
24
- final name = label.isNotEmpty
25
- ? info.name + ' ($label)'
26
- : info.name;
23
+ final name = label.isNotEmpty ? info.name + ' ($label)' : info.name;
24
25
walletContacts.add(WalletContact(
29
- address,
30
- name,
31
- walletTypeToCryptoCurrency(info.type)));
26
+ address,
27
+ name,
28
+ walletTypeToCryptoCurrency(info.type),
29
+ ));
30
});
31
}
32
});
@@ -42,23 +40,18 @@ abstract class ContactListViewModelBase with Store {
40
final Box<WalletInfo> walletInfoSource;
41
final ObservableList<ContactRecord> contacts;
42
final List<WalletContact> walletContacts;
43
+ final CryptoCurrency? _currency;
44
StreamSubscription<BoxEvent>? _subscription;
45
47
- Future<void> delete(ContactRecord contact) async => contact.original.delete();
48
-
49
- List<ContactRecord> getContacts(CryptoCurrency? cur) {
50
- if (cur != null) {
51
- return contacts.where((element) => element.type == cur).toList();
52
- }
46
+ bool get isEditable => _currency == null;
47
54
- return contacts;
55
- }
48
+ Future<void> delete(ContactRecord contact) async => contact.original.delete();
49
57
- List<WalletContact> getWallets(CryptoCurrency? cur) {
58
- if (cur != null) {
59
- return walletContacts.where((element) => element.type == cur).toList();
60
- }
50
+ @computed
51
+ List<ContactRecord> get contactsToShow =>
52
+ contacts.where((element) => _currency == null || element.type == _currency).toList();
53
62
- return walletContacts;
63
- }
54
+ @computed
55
+ List<WalletContact> get walletContactsToShow =>
56
+ walletContacts.where((element) => _currency == null || element.type == _currency).toList();
57
}