seperate settings view model
Godwin Asuquo committed
Nov 23, 2022 at 19:06 UTC
5462836b8d62227c3d1ec00fdaa4a66f42b6a802
13 files changed
+248
-113
lib/di.dart
+28
-4
@@ -31,6 +31,10 @@ import 'package:cake_wallet/src/screens/dashboard/widgets/balance_page.dart';
31
import 'package:cake_wallet/view_model/ionia/ionia_account_view_model.dart';
32
import 'package:cake_wallet/view_model/ionia/ionia_gift_cards_list_view_model.dart';
33
import 'package:cake_wallet/view_model/ionia/ionia_purchase_merch_view_model.dart';
34
+import 'package:cake_wallet/view_model/settings/display_settings_view_model.dart';
35
+import 'package:cake_wallet/view_model/settings/other_settings_view_model.dart';
36
+import 'package:cake_wallet/view_model/settings/privacy_settings_view_model.dart';
37
+import 'package:cake_wallet/view_model/settings/security_settings_view_model.dart';
38
import 'package:cw_core/unspent_coins_info.dart';
39
import 'package:cake_wallet/core/backup_service.dart';
40
import 'package:cw_core/wallet_service.dart';
@@ -443,6 +447,26 @@ Future setup(
447
return SettingsViewModel(appStore.settingsStore, yatStore, appStore.wallet!);
448
});
449
450
+ getIt.registerFactory(() {
451
+ final appStore = getIt.get<AppStore>();
452
+ return DisplaySettingsViewModel(appStore.settingsStore);
453
+ });
454
+
455
+ getIt.registerFactory(() {
456
+ final appStore = getIt.get<AppStore>();
457
+ return PrivacySettingsViewModel(appStore.settingsStore);
458
+ });
459
+
460
+ getIt.registerFactory(() {
461
+ final appStore = getIt.get<AppStore>();
462
+ return OtherSettingsViewModel(appStore.settingsStore, appStore.wallet!);
463
+ });
464
+
465
+ getIt.registerFactory(() {
466
+ final appStore = getIt.get<AppStore>();
467
+ return SecuritySettingsViewModel(appStore.settingsStore);
468
+ });
469
+
470
getIt
471
.registerFactory(() => WalletSeedViewModel(getIt.get<AppStore>().wallet!));
472
@@ -481,13 +505,13 @@ Future setup(
505
506
getIt.registerFactory(() => ConnectionSyncPage(getIt.get<NodeListViewModel>(), getIt.get<DashboardViewModel>()));
507
484
- getIt.registerFactory(() => SecurityBackupPage(getIt.get<SettingsViewModel>()));
508
+ getIt.registerFactory(() => SecurityBackupPage(getIt.get<SecuritySettingsViewModel>()));
509
486
- getIt.registerFactory(() => PrivacyPage(getIt.get<SettingsViewModel>()));
510
+ getIt.registerFactory(() => PrivacyPage(getIt.get<PrivacySettingsViewModel>()));
511
488
- getIt.registerFactory(() => DisplaySettingsPage(getIt.get<SettingsViewModel>()));
512
+ getIt.registerFactory(() => DisplaySettingsPage(getIt.get<DisplaySettingsViewModel>()));
513
490
- getIt.registerFactory(() => OtherSettingsPage(getIt.get<SettingsViewModel>()));
514
+ getIt.registerFactory(() => OtherSettingsPage(getIt.get<OtherSettingsViewModel>()));
515
516
getIt.registerFactory(() =>
517
NodeCreateOrEditViewModel(_nodeSource, getIt.get<AppStore>().wallet!));
lib/entities/priority_for_wallet_type.dart
new
+21
@@ -0,0 +1,21 @@
1
+import 'package:cake_wallet/bitcoin/bitcoin.dart';
2
+import 'package:cake_wallet/haven/haven.dart';
3
+import 'package:cake_wallet/monero/monero.dart';
4
+import 'package:cw_core/transaction_priority.dart';
5
+import 'package:cw_core/wallet_type.dart';
6
+
7
+List<TransactionPriority> priorityForWalletType(WalletType type) {
8
+ switch (type) {
9
+ case WalletType.monero:
10
+ return monero!.getTransactionPriorities();
11
+ case WalletType.bitcoin:
12
+ return bitcoin!.getTransactionPriorities();
13
+ case WalletType.litecoin:
14
+ return bitcoin!.getLitecoinTransactionPriorities();
15
+ case WalletType.haven:
16
+ return haven!.getTransactionPriorities();
17
+ default:
18
+ return [];
19
+ }
20
+}
21
+
lib/src/screens/send/widgets/send_card.dart
+1
-2
@@ -1,11 +1,10 @@
1
-import 'dart:ui';
1
+import 'package:cake_wallet/entities/priority_for_wallet_type.dart';
2
import 'package:cake_wallet/utils/payment_request.dart';
3
import 'package:cw_core/transaction_priority.dart';
4
import 'package:cake_wallet/routes.dart';
5
import 'package:cake_wallet/src/widgets/keyboard_done_button.dart';
6
import 'package:cake_wallet/src/widgets/picker.dart';
7
import 'package:cake_wallet/view_model/send/output.dart';
8
-import 'package:cake_wallet/view_model/settings/settings_view_model.dart';
8
import 'package:flutter/material.dart';
9
import 'package:flutter/services.dart';
10
import 'package:flutter_mobx/flutter_mobx.dart';
lib/src/screens/settings/display_settings_page.dart
+2
-2
@@ -8,7 +8,7 @@ import 'package:cake_wallet/src/screens/settings/widgets/settings_switcher_cell.
8
import 'package:cake_wallet/themes/theme_base.dart';
9
import 'package:cake_wallet/themes/theme_list.dart';
10
import 'package:cake_wallet/view_model/settings/choices_list_item.dart';
11
-import 'package:cake_wallet/view_model/settings/settings_view_model.dart';
11
+import 'package:cake_wallet/view_model/settings/display_settings_view_model.dart';
12
import 'package:cake_wallet/wallet_type_utils.dart';
13
import 'package:flutter/material.dart';
14
import 'package:flutter_mobx/flutter_mobx.dart';
@@ -19,7 +19,7 @@ class DisplaySettingsPage extends BasePage {
19
@override
20
String get title => S.current.display_settings;
21
22
- final SettingsViewModel settingsViewModel;
22
+ final DisplaySettingsViewModel settingsViewModel;
23
24
@override
25
Widget body(BuildContext context) {
lib/src/screens/settings/other_settings_page.dart
+3
-3
@@ -1,3 +1,4 @@
1
+import 'package:cake_wallet/entities/priority_for_wallet_type.dart';
2
import 'package:cake_wallet/generated/i18n.dart';
3
import 'package:cake_wallet/routes.dart';
4
import 'package:cake_wallet/src/screens/base_page.dart';
@@ -5,8 +6,7 @@ import 'package:cake_wallet/src/screens/settings/widgets/settings_cell_with_arro
6
import 'package:cake_wallet/src/screens/settings/widgets/settings_picker_cell.dart';
7
import 'package:cake_wallet/src/screens/settings/widgets/settings_version_cell.dart';
8
import 'package:cake_wallet/src/widgets/standard_list.dart';
8
-import 'package:cake_wallet/view_model/settings/settings_view_model.dart';
9
-import 'package:cw_core/transaction_priority.dart';
9
+import 'package:cake_wallet/view_model/settings/other_settings_view_model.dart';
10
import 'package:flutter/material.dart';
11
import 'package:flutter_mobx/flutter_mobx.dart';
12
@@ -16,7 +16,7 @@ class OtherSettingsPage extends BasePage {
16
@override
17
String get title => S.current.other_settings;
18
19
- final SettingsViewModel _settingsViewModel;
19
+ final OtherSettingsViewModel _settingsViewModel;
20
21
@override
22
Widget body(BuildContext context) {
lib/src/screens/settings/privacy_page.dart
+2
-2
@@ -1,7 +1,7 @@
1
import 'package:cake_wallet/generated/i18n.dart';
2
import 'package:cake_wallet/src/screens/base_page.dart';
3
import 'package:cake_wallet/src/screens/settings/widgets/settings_switcher_cell.dart';
4
-import 'package:cake_wallet/view_model/settings/settings_view_model.dart';
4
+import 'package:cake_wallet/view_model/settings/privacy_settings_view_model.dart';
5
import 'package:flutter/material.dart';
6
import 'package:flutter_mobx/flutter_mobx.dart';
7
@@ -11,7 +11,7 @@ class PrivacyPage extends BasePage {
11
@override
12
String get title => S.current.privacy_settings;
13
14
- final SettingsViewModel settingsViewModel;
14
+ final PrivacySettingsViewModel settingsViewModel;
15
16
@override
17
Widget body(BuildContext context) {
lib/src/screens/settings/security_backup_page.dart
+2
-1
@@ -6,6 +6,7 @@ import 'package:cake_wallet/src/screens/pin_code/pin_code_widget.dart';
6
import 'package:cake_wallet/src/screens/settings/widgets/settings_cell_with_arrow.dart';
7
import 'package:cake_wallet/src/screens/settings/widgets/settings_switcher_cell.dart';
8
import 'package:cake_wallet/src/widgets/standard_list.dart';
9
+import 'package:cake_wallet/view_model/settings/security_settings_view_model.dart';
10
import 'package:cake_wallet/view_model/settings/settings_view_model.dart';
11
import 'package:flutter/material.dart';
12
import 'package:flutter_mobx/flutter_mobx.dart';
@@ -16,7 +17,7 @@ class SecurityBackupPage extends BasePage {
17
@override
18
String get title => S.current.security_and_backup;
19
19
- final SettingsViewModel settingsViewModel;
20
+ final SecuritySettingsViewModel settingsViewModel;
21
22
@override
23
Widget body(BuildContext context) {
lib/view_model/send/send_view_model.dart
+1
-1
@@ -1,10 +1,10 @@
1
import 'package:cake_wallet/entities/balance_display_mode.dart';
2
+import 'package:cake_wallet/entities/priority_for_wallet_type.dart';
3
import 'package:cake_wallet/entities/transaction_description.dart';
4
import 'package:cake_wallet/view_model/dashboard/balance_view_model.dart';
5
import 'package:cw_core/transaction_priority.dart';
6
import 'package:cake_wallet/view_model/send/output.dart';
7
import 'package:cake_wallet/view_model/send/send_template_view_model.dart';
7
-import 'package:cake_wallet/view_model/settings/settings_view_model.dart';
8
import 'package:hive/hive.dart';
9
import 'package:mobx/mobx.dart';
10
import 'package:cake_wallet/entities/template.dart';
lib/view_model/settings/display_settings_view_model.dart
new
+63
@@ -0,0 +1,63 @@
1
+import 'package:cake_wallet/entities/balance_display_mode.dart';
2
+import 'package:cake_wallet/entities/fiat_currency.dart';
3
+import 'package:cake_wallet/store/settings_store.dart';
4
+import 'package:cake_wallet/themes/theme_base.dart';
5
+import 'package:mobx/mobx.dart';
6
+
7
+part 'display_settings_view_model.g.dart';
8
+
9
+class DisplaySettingsViewModel = DisplaySettingsViewModelBase with _$DisplaySettingsViewModel;
10
+
11
+
12
+abstract class DisplaySettingsViewModelBase with Store {
13
+ DisplaySettingsViewModelBase(
14
+ this._settingsStore,
15
+ );
16
+
17
+ final SettingsStore _settingsStore;
18
+
19
+ @computed
20
+ FiatCurrency get fiatCurrency => _settingsStore.fiatCurrency;
21
+
22
+ @computed
23
+ String get languageCode => _settingsStore.languageCode;
24
+
25
+
26
+ @computed
27
+ BalanceDisplayMode get balanceDisplayMode =>
28
+ _settingsStore.balanceDisplayMode;
29
+
30
+ @computed
31
+ bool get shouldDisplayBalance => balanceDisplayMode == BalanceDisplayMode.displayableBalance;
32
+
33
+ @computed
34
+ ThemeBase get theme => _settingsStore.currentTheme;
35
+
36
+ @action
37
+ void setBalanceDisplayMode(BalanceDisplayMode value) =>
38
+ _settingsStore.balanceDisplayMode = value;
39
+
40
+ @action
41
+ void setShouldDisplayBalance(bool value){
42
+ if (value) {
43
+ _settingsStore.balanceDisplayMode = BalanceDisplayMode.displayableBalance;
44
+ } else {
45
+ _settingsStore.balanceDisplayMode = BalanceDisplayMode.hiddenBalance;
46
+ }
47
+ }
48
+
49
+ @action
50
+ void onLanguageSelected (String code) {
51
+ _settingsStore.languageCode = code;
52
+ }
53
+
54
+ @action
55
+ void setTheme(ThemeBase newTheme){
56
+ _settingsStore.currentTheme = newTheme;
57
+ }
58
+
59
+ @action
60
+ void setFiatCurrency(FiatCurrency value) =>
61
+ _settingsStore.fiatCurrency = value;
62
+
63
+}
\ No newline at end of file
lib/view_model/settings/other_settings_view_model.dart
new
+72
@@ -0,0 +1,72 @@
1
+import 'package:cake_wallet/bitcoin/bitcoin.dart';
2
+import 'package:cake_wallet/entities/priority_for_wallet_type.dart';
3
+import 'package:cake_wallet/store/settings_store.dart';
4
+import 'package:cw_core/balance.dart';
5
+import 'package:cw_core/transaction_history.dart';
6
+import 'package:cw_core/transaction_info.dart';
7
+import 'package:cw_core/transaction_priority.dart';
8
+import 'package:cw_core/wallet_base.dart';
9
+import 'package:cw_core/wallet_type.dart';
10
+import 'package:mobx/mobx.dart';
11
+import 'package:package_info/package_info.dart';
12
+
13
+part 'other_settings_view_model.g.dart';
14
+
15
+class OtherSettingsViewModel = OtherSettingsViewModelBase
16
+ with _$OtherSettingsViewModel;
17
+
18
+
19
+abstract class OtherSettingsViewModelBase with Store {
20
+ OtherSettingsViewModelBase(this._settingsStore, WalletBase<Balance, TransactionHistoryBase<TransactionInfo>,
21
+ TransactionInfo>
22
+ wallet):
23
+ walletType = wallet.type,
24
+ _wallet = wallet,
25
+ currentVersion = ''{
26
+ PackageInfo.fromPlatform().then(
27
+ (PackageInfo packageInfo) => currentVersion = packageInfo.version);
28
+
29
+ final priority = _settingsStore.priority[wallet.type];
30
+ final priorities = priorityForWalletType(wallet.type);
31
+
32
+ if (!priorities.contains(priority)) {
33
+ _settingsStore.priority[wallet.type] = priorities.first;
34
+ }
35
+
36
+ }
37
+
38
+ final WalletType walletType;
39
+ final WalletBase<Balance, TransactionHistoryBase<TransactionInfo>,
40
+ TransactionInfo> _wallet;
41
+
42
+ @observable
43
+ String currentVersion;
44
+
45
+ final SettingsStore _settingsStore;
46
+
47
+ @computed
48
+ TransactionPriority get transactionPriority {
49
+ final priority = _settingsStore.priority[walletType];
50
+
51
+ if (priority == null) {
52
+ throw Exception('Unexpected type ${walletType.toString()}');
53
+ }
54
+
55
+ return priority;
56
+ }
57
+
58
+ String getDisplayPriority(dynamic priority) {
59
+ final _priority = priority as TransactionPriority;
60
+
61
+ if (_wallet.type == WalletType.bitcoin
62
+ || _wallet.type == WalletType.litecoin) {
63
+ final rate = bitcoin!.getFeeRate(_wallet, _priority);
64
+ return bitcoin!.bitcoinTransactionPriorityWithLabel(_priority, rate);
65
+ }
66
+
67
+ return priority.toString();
68
+ }
69
+
70
+ void onDisplayPrioritySelected(TransactionPriority priority) =>
71
+ _settingsStore.priority[_wallet.type] = priority;
72
+}
\ No newline at end of file
lib/view_model/settings/privacy_settings_view_model.dart
new
+21
@@ -0,0 +1,21 @@
1
+import 'package:cake_wallet/store/settings_store.dart';
2
+import 'package:mobx/mobx.dart';
3
+
4
+part 'privacy_settings_view_model.g.dart';
5
+
6
+class PrivacySettingsViewModel = PrivacySettingsViewModelBase
7
+ with _$PrivacySettingsViewModel;
8
+
9
+abstract class PrivacySettingsViewModelBase with Store {
10
+ PrivacySettingsViewModelBase(this._settingsStore);
11
+
12
+ final SettingsStore _settingsStore;
13
+
14
+ @computed
15
+ bool get shouldSaveRecipientAddress =>
16
+ _settingsStore.shouldSaveRecipientAddress;
17
+
18
+ @action
19
+ void setShouldSaveRecipientAddress(bool value) =>
20
+ _settingsStore.shouldSaveRecipientAddress = value;
21
+}
\ No newline at end of file
lib/view_model/settings/security_settings_view_model.dart
new
+29
@@ -0,0 +1,29 @@
1
+import 'package:cake_wallet/entities/biometric_auth.dart';
2
+import 'package:cake_wallet/store/settings_store.dart';
3
+import 'package:mobx/mobx.dart';
4
+
5
+part 'security_settings_view_model.g.dart';
6
+
7
+class SecuritySettingsViewModel = SecuritySettingsViewModelBase
8
+ with _$SecuritySettingsViewModel;
9
+
10
+abstract class SecuritySettingsViewModelBase with Store {
11
+ SecuritySettingsViewModelBase(this._settingsStore): _biometricAuth = BiometricAuth();
12
+
13
+ final BiometricAuth _biometricAuth;
14
+ final SettingsStore _settingsStore;
15
+
16
+ @computed
17
+ bool get allowBiometricalAuthentication =>
18
+ _settingsStore.allowBiometricalAuthentication;
19
+
20
+ @action
21
+ Future<bool> biometricAuthenticated()async{
22
+ return await _biometricAuth.canCheckBiometrics() && await _biometricAuth.isAuthenticated();
23
+ }
24
+
25
+ @action
26
+ void setAllowBiometricalAuthentication(bool value) =>
27
+ _settingsStore.allowBiometricalAuthentication = value;
28
+
29
+}
\ No newline at end of file
lib/view_model/settings/settings_view_model.dart
+3
-98
@@ -1,6 +1,6 @@
1
+import 'package:cake_wallet/entities/priority_for_wallet_type.dart';
2
import 'package:cake_wallet/store/yat/yat_store.dart';
3
import 'package:mobx/mobx.dart';
3
-import 'package:package_info/package_info.dart';
4
import 'package:cw_core/wallet_base.dart';
5
import 'package:cake_wallet/store/settings_store.dart';
6
import 'package:cake_wallet/entities/biometric_auth.dart';
@@ -22,21 +22,6 @@ part 'settings_view_model.g.dart';
22
23
class SettingsViewModel = SettingsViewModelBase with _$SettingsViewModel;
24
25
-List<TransactionPriority> priorityForWalletType(WalletType type) {
26
- switch (type) {
27
- case WalletType.monero:
28
- return monero!.getTransactionPriorities();
29
- case WalletType.bitcoin:
30
- return bitcoin!.getTransactionPriorities();
31
- case WalletType.litecoin:
32
- return bitcoin!.getLitecoinTransactionPriorities();
33
- case WalletType.haven:
34
- return haven!.getTransactionPriorities();
35
- default:
36
- return [];
37
- }
38
-}
39
-
25
abstract class SettingsViewModelBase with Store {
26
SettingsViewModelBase(
27
this._settingsStore,
@@ -46,12 +31,8 @@ abstract class SettingsViewModelBase with Store {
31
wallet)
32
: itemHeaders = {},
33
walletType = wallet.type,
49
- _wallet = wallet,
50
- _biometricAuth = BiometricAuth(),
51
- currentVersion = '' {
52
- PackageInfo.fromPlatform().then(
53
- (PackageInfo packageInfo) => currentVersion = packageInfo.version);
54
-
34
+ _wallet = wallet{
35
+
36
final priority = _settingsStore.priority[wallet.type];
37
final priorities = priorityForWalletType(wallet.type);
38
@@ -85,18 +66,9 @@ abstract class SettingsViewModelBase with Store {
66
67
}
68
88
- @observable
89
- String currentVersion;
90
-
91
- @computed
92
- Node get node => _settingsStore.getCurrentNode(walletType);
93
-
69
@computed
70
FiatCurrency get fiatCurrency => _settingsStore.fiatCurrency;
71
97
- @computed
98
- String get languageCode => _settingsStore.languageCode;
99
-
72
@computed
73
ObservableList<ActionListDisplayMode> get actionlistDisplayMode =>
74
_settingsStore.actionlistDisplayMode;
@@ -112,21 +84,6 @@ abstract class SettingsViewModelBase with Store {
84
return priority;
85
}
86
115
- @computed
116
- BalanceDisplayMode get balanceDisplayMode =>
117
- _settingsStore.balanceDisplayMode;
118
-
119
- @computed
120
- bool get shouldDisplayBalance => balanceDisplayMode == BalanceDisplayMode.displayableBalance;
121
-
122
- @computed
123
- bool get shouldSaveRecipientAddress =>
124
- _settingsStore.shouldSaveRecipientAddress;
125
-
126
- @computed
127
- bool get allowBiometricalAuthentication =>
128
- _settingsStore.allowBiometricalAuthentication;
129
-
87
@computed
88
ThemeBase get theme => _settingsStore.currentTheme;
89
@@ -136,26 +93,13 @@ abstract class SettingsViewModelBase with Store {
93
final SettingsStore _settingsStore;
94
final YatStore _yatStore;
95
final WalletType walletType;
139
- final BiometricAuth _biometricAuth;
96
final WalletBase<Balance, TransactionHistoryBase<TransactionInfo>,
97
TransactionInfo> _wallet;
98
143
- @action
144
- void setBalanceDisplayMode(BalanceDisplayMode value) =>
145
- _settingsStore.balanceDisplayMode = value;
146
-
99
@action
100
void setFiatCurrency(FiatCurrency value) =>
101
_settingsStore.fiatCurrency = value;
102
151
- @action
152
- void setShouldSaveRecipientAddress(bool value) =>
153
- _settingsStore.shouldSaveRecipientAddress = value;
154
-
155
- @action
156
- void setAllowBiometricalAuthentication(bool value) =>
157
- _settingsStore.allowBiometricalAuthentication = value;
158
-
103
@action
104
void toggleTransactionsDisplay() =>
105
actionlistDisplayMode.contains(ActionListDisplayMode.transactions)
@@ -183,43 +127,4 @@ abstract class SettingsViewModelBase with Store {
127
@action
128
void _showTrades() => actionlistDisplayMode.add(ActionListDisplayMode.trades);
129
186
- @action
187
- Future<bool> biometricAuthenticated()async{
188
- return await _biometricAuth.canCheckBiometrics() && await _biometricAuth.isAuthenticated();
189
- }
190
-
191
- @action
192
- void onLanguageSelected (String code) {
193
- _settingsStore.languageCode = code;
194
- }
195
-
196
- @action
197
- void setTheme(ThemeBase newTheme){
198
- _settingsStore.currentTheme = newTheme;
199
- }
200
-
201
- @action
202
- void setShouldDisplayBalance(bool value){
203
- if (value) {
204
- _settingsStore.balanceDisplayMode = BalanceDisplayMode.displayableBalance;
205
- } else {
206
- _settingsStore.balanceDisplayMode = BalanceDisplayMode.hiddenBalance;
207
- }
208
- }
209
-
210
- String getDisplayPriority(dynamic priority) {
211
- final _priority = priority as TransactionPriority;
212
-
213
- if (_wallet.type == WalletType.bitcoin
214
- || _wallet.type == WalletType.litecoin) {
215
- final rate = bitcoin!.getFeeRate(_wallet, _priority);
216
- return bitcoin!.bitcoinTransactionPriorityWithLabel(_priority, rate);
217
- }
218
-
219
- return priority.toString();
220
- }
221
-
222
- void onDisplayPrioritySelected(TransactionPriority priority) =>
223
- _settingsStore.priority[_wallet.type] = priority;
224
-
130
}