[skip ci] rework UI for disable fiat mode

Serhii committed Dec 7, 2022 at 01:38 UTC f4148e0989a00081e20ed3f08154a98e5460a5ec
9 files changed +98 -18
lib/core/backup_service.dart
+7
@@ -214,6 +214,7 @@ class BackupService {
214 final currentBitcoinElectrumSererId = data[PreferencesKey.currentBitcoinElectrumSererIdKey] as int?;
215 final currentLanguageCode = data[PreferencesKey.currentLanguageCode] as String?;
216 final displayActionListMode = data[PreferencesKey.displayActionListModeKey] as int?;
217 + final fiatApiMode = data[PreferencesKey.currentFiatApiModeKey] as int?;
218 final currentPinLength = data[PreferencesKey.currentPinLength] as int?;
219 final currentTheme = data[PreferencesKey.currentTheme] as int?;
220 final currentDefaultSettingsMigrationVersion = data[PreferencesKey.currentDefaultSettingsMigrationVersion] as int?;
@@ -266,6 +267,10 @@ class BackupService {
267 await _sharedPreferences.setInt(PreferencesKey.displayActionListModeKey,
268 displayActionListMode);
269
270 + if (fiatApiMode != null)
271 + await _sharedPreferences.setInt(PreferencesKey.currentFiatApiModeKey,
272 + fiatApiMode);
273 +
274 if (currentPinLength != null)
275 await _sharedPreferences.setInt(PreferencesKey.currentPinLength,
276 currentPinLength);
@@ -427,6 +432,8 @@ class BackupService {
432 _sharedPreferences.getInt(PreferencesKey.bitcoinTransactionPriority),
433 PreferencesKey.moneroTransactionPriority:
434 _sharedPreferences.getInt(PreferencesKey.moneroTransactionPriority),
435 + PreferencesKey.currentFiatApiModeKey:
436 + _sharedPreferences.getInt(PreferencesKey.currentFiatApiModeKey),
437 };
438
439 return json.encode(preferences);
lib/entities/default_settings_migration.dart
+17 -1
@@ -20,6 +20,8 @@ import 'package:cake_wallet/exchange/trade.dart';
20 import 'package:encrypt/encrypt.dart' as encrypt;
21 import 'package:collection/collection.dart';
22
23 +import 'fiat_api_mode.dart';
24 +
25 const newCakeWalletMoneroUri = 'xmr-node.cakewallet.com:18081';
26 const cakeWalletBitcoinElectrumUri = 'electrum.cakewallet.com:50002';
27 const cakeWalletLitecoinElectrumUri = 'ltc-electrum.cakewallet.com:50002';
@@ -61,7 +63,9 @@ Future defaultSettingsMigration(
63 await sharedPreferences.setInt(
64 PreferencesKey.currentBalanceDisplayModeKey,
65 BalanceDisplayMode.availableBalance.raw);
64 - await sharedPreferences.setBool('disable_fiat', false);
66 + await sharedPreferences.setInt(
67 + PreferencesKey.currentFiatApiModeKey,
68 + FiatApiMode.enabled.raw);
69 await sharedPreferences.setBool('save_recipient_address', true);
70 await resetToDefault(nodes);
71 await changeMoneroCurrentNodeToDefault(
@@ -140,6 +144,10 @@ Future defaultSettingsMigration(
144 await addOnionNode(nodes);
145 break;
146
147 + case 19:
148 + await updateFiatApiModes(sharedPreferences);
149 + break;
150 +
151 default:
152 break;
153 }
@@ -350,6 +358,14 @@ Future<void> updateDisplayModes(SharedPreferences sharedPreferences) async {
358 PreferencesKey.currentBalanceDisplayModeKey, balanceDisplayMode);
359 }
360
361 +Future<void> updateFiatApiModes(SharedPreferences sharedPreferences) async {
362 + final currentFiatApiMode =
363 + sharedPreferences.getInt(PreferencesKey.currentFiatApiModeKey) ?? -1;
364 + final fiatApiMode = currentFiatApiMode < 1 ? 2 : 1;
365 + await sharedPreferences.setInt(
366 + PreferencesKey.currentFiatApiModeKey, fiatApiMode);
367 +}
368 +
369 Future<void> generateBackupPassword(FlutterSecureStorage secureStorage) async {
370 final key = generateStoreKeyFor(key: SecretStoreKey.backupPassword);
371
lib/entities/fiat_api_mode.dart new
+39
@@ -0,0 +1,39 @@
1 +import 'package:cake_wallet/generated/i18n.dart';
2 +import 'package:cw_core/enumerable_item.dart';
3 +
4 +class FiatApiMode extends EnumerableItem<int> with Serializable<int> {
5 + const FiatApiMode({required String title, required int raw}) : super(title: title, raw: raw);
6 +
7 + static const all = [FiatApiMode.enabled, FiatApiMode.torOnly, FiatApiMode.disabled];
8 +
9 + static const enabled = FiatApiMode(raw: 0, title: 'Enabled');
10 + static const torOnly = FiatApiMode(raw: 1, title: 'Tor only');
11 + static const disabled = FiatApiMode(raw: 2, title: 'Disabled');
12 +
13 + static FiatApiMode deserialize({required int raw}) {
14 + switch (raw) {
15 + case 0:
16 + return enabled;
17 + case 1:
18 + return torOnly;
19 + case 2:
20 + return disabled;
21 + default:
22 + throw Exception('Unexpected token: $raw for FiatApiMode deserialize');
23 + }
24 + }
25 +
26 + @override
27 + String toString() {
28 + switch (this) {
29 + case FiatApiMode.enabled:
30 + return 'enable';
31 + case FiatApiMode.torOnly:
32 + return 'torOnly';
33 + case FiatApiMode.disabled:
34 + return 'disable';
35 + default:
36 + return '';
37 + }
38 + }
39 +}
lib/entities/preferences_key.dart
+1 -1
@@ -9,7 +9,7 @@ class PreferencesKey {
9 static const currentTransactionPriorityKeyLegacy = 'current_fee_priority';
10 static const currentBalanceDisplayModeKey = 'current_balance_display_mode';
11 static const shouldSaveRecipientAddressKey = 'save_recipient_address';
12 - static const shouldDisableFiatKey = 'disable_fiat';
12 + static const currentFiatApiModeKey = 'current_fiat_api_mode';
13 static const allowBiometricalAuthenticationKey =
14 'allow_biometrical_authentication';
15 static const disableExchangeKey = 'disable_exchange';
lib/src/screens/send/widgets/send_card.dart
+1 -1
@@ -331,7 +331,7 @@ class SendCardState extends State<SendCard>
331 ],
332 ),
333 )),
334 - if (sendViewModel.balanceViewModel.disableFiat)
334 + if (!sendViewModel.balanceViewModel.disableFiat)
335 Padding(
336 padding: const EdgeInsets.only(top: 20),
337 child: BaseTextFormField(
lib/src/screens/settings/privacy_page.dart
+11 -2
@@ -1,9 +1,12 @@
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_choices_cell.dart';
4 import 'package:cake_wallet/src/screens/settings/widgets/settings_switcher_cell.dart';
5 import 'package:cake_wallet/view_model/settings/privacy_settings_view_model.dart';
6 import 'package:flutter/material.dart';
7 import 'package:flutter_mobx/flutter_mobx.dart';
8 +import 'package:cake_wallet/view_model/settings/choices_list_item.dart';
9 +import 'package:cake_wallet/entities/fiat_api_mode.dart';
10
11 class PrivacyPage extends BasePage {
12 PrivacyPage(this._privacySettingsViewModel);
@@ -18,10 +21,17 @@ class PrivacyPage extends BasePage {
21 return Container(
22 padding: EdgeInsets.only(top: 10),
23 child: Observer(builder: (_) {
21 - return Observer(builder: (_) {
24 return Column(
25 mainAxisSize: MainAxisSize.min,
26 children: [
27 + SettingsChoicesCell(
28 + ChoicesListItem<FiatApiMode>(
29 + title: 'Fiat api',
30 + items: FiatApiMode.all,
31 + selectedItem: _privacySettingsViewModel.fiatApiMode,
32 + onItemSelected: (FiatApiMode mode) => _privacySettingsViewModel.setFiatMode(mode),
33 + ),
34 + ),
35 SettingsSwitcherCell(
36 title: S.current.disable_exchange,
37 value: _privacySettingsViewModel.disableExchange,
@@ -36,7 +46,6 @@ class PrivacyPage extends BasePage {
46 })
47 ],
48 );
39 - });
49 }),
50 );
51 }
lib/store/settings_store.dart
+13 -11
@@ -17,8 +17,10 @@ import 'package:cake_wallet/entities/fiat_currency.dart';
17 import 'package:cw_core/node.dart';
18 import 'package:cake_wallet/monero/monero.dart';
19 import 'package:cake_wallet/entities/action_list_display_mode.dart';
20 +import 'package:cake_wallet/entities/fiat_api_mode.dart';
21 import 'package:cake_wallet/.secrets.g.dart' as secrets;
22
23 +
24 part 'settings_store.g.dart';
25
26 class SettingsStore = SettingsStoreBase with _$SettingsStore;
@@ -29,7 +31,7 @@ abstract class SettingsStoreBase with Store {
31 required FiatCurrency initialFiatCurrency,
32 required BalanceDisplayMode initialBalanceDisplayMode,
33 required bool initialSaveRecipientAddress,
32 - required bool initialDisableFiat,
34 + required FiatApiMode initialFiatMode,
35 required bool initialAllowBiometricalAuthentication,
36 required bool initialExchangeEnabled,
37 required ThemeBase initialTheme,
@@ -48,7 +50,7 @@ abstract class SettingsStoreBase with Store {
50 fiatCurrency = initialFiatCurrency,
51 balanceDisplayMode = initialBalanceDisplayMode,
52 shouldSaveRecipientAddress = initialSaveRecipientAddress,
51 - shouldDisableFiat = initialDisableFiat,
53 + fiatApiMode = initialFiatMode,
54 allowBiometricalAuthentication = initialAllowBiometricalAuthentication,
55 disableExchange = initialExchangeEnabled,
56 currentTheme = initialTheme,
@@ -92,10 +94,9 @@ abstract class SettingsStoreBase with Store {
94 shouldSaveRecipientAddress));
95
96 reaction(
95 - (_) => shouldDisableFiat,
96 - (bool shouldDisableFiat) => sharedPreferences.setBool(
97 - PreferencesKey.shouldDisableFiatKey,
98 - shouldDisableFiat));
97 + (_) => fiatApiMode,
98 + (FiatApiMode mode) => sharedPreferences.setInt(
99 + PreferencesKey.currentFiatApiModeKey, mode.serialize()));
100
101 reaction(
102 (_) => currentTheme,
@@ -148,10 +149,10 @@ abstract class SettingsStoreBase with Store {
149 BalanceDisplayMode balanceDisplayMode;
150
151 @observable
151 - bool shouldSaveRecipientAddress;
152 + FiatApiMode fiatApiMode;
153
154 @observable
154 - bool shouldDisableFiat;
155 + bool shouldSaveRecipientAddress;
156
157 @observable
158 bool allowBiometricalAuthentication;
@@ -234,8 +235,9 @@ abstract class SettingsStoreBase with Store {
235 // FIX-ME: Check for which default value we should have here
236 final shouldSaveRecipientAddress =
237 sharedPreferences.getBool(PreferencesKey.shouldSaveRecipientAddressKey) ?? false;
237 - final shouldDisableFiat =
238 - sharedPreferences.getBool(PreferencesKey.shouldDisableFiatKey) ?? false;
238 + final currentFiatApiMode = FiatApiMode.deserialize(
239 + raw: sharedPreferences
240 + .getInt(PreferencesKey.currentFiatApiModeKey)!);
241 final allowBiometricalAuthentication = sharedPreferences
242 .getBool(PreferencesKey.allowBiometricalAuthenticationKey) ??
243 false;
@@ -303,7 +305,7 @@ abstract class SettingsStoreBase with Store {
305 initialFiatCurrency: currentFiatCurrency,
306 initialBalanceDisplayMode: currentBalanceDisplayMode,
307 initialSaveRecipientAddress: shouldSaveRecipientAddress,
306 - initialDisableFiat: shouldDisableFiat,
308 + initialFiatMode: currentFiatApiMode,
309 initialAllowBiometricalAuthentication: allowBiometricalAuthentication,
310 initialExchangeEnabled: disableExchange,
311 initialTheme: savedTheme,
lib/view_model/dashboard/balance_view_model.dart
+2 -2
@@ -1,3 +1,4 @@
1 +import 'package:cake_wallet/entities/fiat_api_mode.dart';
2 import 'package:cw_core/transaction_history.dart';
3 import 'package:cw_core/wallet_base.dart';
4 import 'package:cw_core/balance.dart';
@@ -10,7 +11,6 @@ import 'package:cake_wallet/entities/calculate_fiat_amount.dart';
11 import 'package:cake_wallet/store/app_store.dart';
12 import 'package:cake_wallet/store/settings_store.dart';
13 import 'package:cake_wallet/store/dashboard/fiat_conversion_store.dart';
13 -import 'package:flutter/cupertino.dart';
14 import 'package:mobx/mobx.dart';
15
16 part 'balance_view_model.g.dart';
@@ -72,7 +72,7 @@ abstract class BalanceViewModelBase with Store {
72 BalanceDisplayMode get savedDisplayMode => settingsStore.balanceDisplayMode;
73
74 @computed
75 - bool get disableFiat => settingsStore.shouldDisableFiat;
75 + bool get disableFiat => settingsStore.fiatApiMode == FiatApiMode.disabled;
76
77 @computed
78 String get asset {
lib/view_model/settings/privacy_settings_view_model.dart
+7
@@ -1,5 +1,6 @@
1 import 'package:cake_wallet/store/settings_store.dart';
2 import 'package:mobx/mobx.dart';
3 +import 'package:cake_wallet/entities/fiat_api_mode.dart';
4
5 part 'privacy_settings_view_model.g.dart';
6
@@ -16,9 +17,15 @@ abstract class PrivacySettingsViewModelBase with Store {
17 @computed
18 bool get shouldSaveRecipientAddress => _settingsStore.shouldSaveRecipientAddress;
19
20 + @computed
21 + FiatApiMode get fiatApiMode => _settingsStore.fiatApiMode;
22 +
23 @action
24 void setShouldSaveRecipientAddress(bool value) => _settingsStore.shouldSaveRecipientAddress = value;
25
26 @action
27 void setEnableExchange(bool value) => _settingsStore.disableExchange = value;
28 +
29 + @action
30 + void setFiatMode(FiatApiMode mode) => _settingsStore.fiatApiMode = mode;
31 }