cake_wallet/lib/store/settings_store.dart

214 lines
7.8 KiB
Dart
Raw Normal View History

2020-09-21 11:50:26 +00:00
import 'package:cake_wallet/entities/preferences_key.dart';
2020-09-28 15:47:43 +00:00
import 'package:cake_wallet/themes.dart';
2020-07-06 20:09:03 +00:00
import 'package:flutter/foundation.dart';
2020-09-28 15:47:43 +00:00
import 'package:flutter/material.dart';
2020-07-06 20:09:03 +00:00
import 'package:hive/hive.dart';
import 'package:mobx/mobx.dart';
import 'package:package_info/package_info.dart';
2020-09-21 11:50:26 +00:00
import 'package:devicelocale/devicelocale.dart';
import 'package:cake_wallet/di.dart';
import 'package:cake_wallet/entities/wallet_type.dart';
2020-07-06 20:09:03 +00:00
import 'package:shared_preferences/shared_preferences.dart';
2020-09-28 15:47:43 +00:00
import 'package:cake_wallet/entities/language_service.dart';
2020-09-21 11:50:26 +00:00
import 'package:cake_wallet/entities/balance_display_mode.dart';
import 'package:cake_wallet/entities/fiat_currency.dart';
import 'package:cake_wallet/entities/node.dart';
import 'package:cake_wallet/entities/transaction_priority.dart';
import 'package:cake_wallet/entities/action_list_display_mode.dart';
2020-07-06 20:09:03 +00:00
part 'settings_store.g.dart';
class SettingsStore = SettingsStoreBase with _$SettingsStore;
abstract class SettingsStoreBase with Store {
SettingsStoreBase(
{@required SharedPreferences sharedPreferences,
@required FiatCurrency initialFiatCurrency,
@required TransactionPriority initialTransactionPriority,
@required BalanceDisplayMode initialBalanceDisplayMode,
@required bool initialSaveRecipientAddress,
@required bool initialAllowBiometricalAuthentication,
@required bool initialDarkTheme,
@required int initialPinLength,
@required String initialLanguageCode,
2020-09-28 15:47:43 +00:00
// @required String initialCurrentLocale,
2020-07-06 20:09:03 +00:00
@required this.appVersion,
2020-08-27 16:54:34 +00:00
@required Map<WalletType, Node> nodes,
2020-07-06 20:09:03 +00:00
this.actionlistDisplayMode}) {
fiatCurrency = initialFiatCurrency;
transactionPriority = initialTransactionPriority;
balanceDisplayMode = initialBalanceDisplayMode;
shouldSaveRecipientAddress = initialSaveRecipientAddress;
allowBiometricalAuthentication = initialAllowBiometricalAuthentication;
isDarkTheme = initialDarkTheme;
2020-09-21 11:50:26 +00:00
pinCodeLength = initialPinLength;
2020-07-06 20:09:03 +00:00
languageCode = initialLanguageCode;
2020-09-26 19:17:31 +00:00
currentNode = nodes[WalletType.monero];
2020-09-07 15:13:39 +00:00
this.nodes = ObservableMap<WalletType, Node>.of(nodes);
2020-07-06 20:09:03 +00:00
_sharedPreferences = sharedPreferences;
2020-09-10 14:51:59 +00:00
2020-09-28 19:02:30 +00:00
reaction(
(_) => fiatCurrency,
(FiatCurrency fiatCurrency) => sharedPreferences.setString(
PreferencesKey.currentFiatCurrencyKey, fiatCurrency.serialize()));
reaction(
(_) => transactionPriority,
(TransactionPriority priority) => sharedPreferences.setInt(
PreferencesKey.currentTransactionPriorityKey,
priority.serialize()));
reaction(
(_) => shouldSaveRecipientAddress,
(bool shouldSaveRecipientAddress) => sharedPreferences.setBool(
PreferencesKey.shouldSaveRecipientAddressKey,
shouldSaveRecipientAddress));
reaction(
(_) => isDarkTheme,
(bool isDarkTheme) => sharedPreferences.setBool(
PreferencesKey.currentDarkTheme, isDarkTheme));
2020-09-10 14:51:59 +00:00
reaction(
(_) => allowBiometricalAuthentication,
(bool biometricalAuthentication) => sharedPreferences.setBool(
2020-09-21 11:50:26 +00:00
PreferencesKey.allowBiometricalAuthenticationKey,
biometricalAuthentication));
reaction(
(_) => pinCodeLength,
(int pinLength) => sharedPreferences.setInt(
PreferencesKey.currentPinLength, pinLength));
2020-09-26 19:17:31 +00:00
reaction((_) => currentNode,
(Node node) => _saveCurrentNode(node, WalletType.monero));
2020-09-28 15:47:43 +00:00
2020-09-28 19:02:30 +00:00
reaction(
(_) => languageCode,
(String languageCode) => sharedPreferences.setString(
PreferencesKey.currentLanguageCode, languageCode));
2020-07-06 20:09:03 +00:00
}
2020-09-21 11:50:26 +00:00
static const defaultPinLength = 4;
static const defaultActionsMode = 11;
2020-07-06 20:09:03 +00:00
@observable
FiatCurrency fiatCurrency;
@observable
ObservableList<ActionListDisplayMode> actionlistDisplayMode;
@observable
TransactionPriority transactionPriority;
@observable
BalanceDisplayMode balanceDisplayMode;
@observable
bool shouldSaveRecipientAddress;
@observable
bool allowBiometricalAuthentication;
@observable
bool isDarkTheme;
@observable
2020-09-21 11:50:26 +00:00
int pinCodeLength;
2020-07-06 20:09:03 +00:00
@observable
2020-09-26 19:17:31 +00:00
Node currentNode;
2020-07-06 20:09:03 +00:00
2020-09-28 15:47:43 +00:00
@computed
ThemeData get theme => isDarkTheme ? Themes.darkTheme : Themes.lightTheme;
2020-07-06 20:09:03 +00:00
2020-09-28 15:47:43 +00:00
@observable
String languageCode;
2020-07-06 20:09:03 +00:00
String appVersion;
SharedPreferences _sharedPreferences;
2020-09-07 15:13:39 +00:00
ObservableMap<WalletType, Node> nodes;
2020-08-27 16:54:34 +00:00
2020-09-07 15:13:39 +00:00
Node getCurrentNode(WalletType walletType) => nodes[walletType];
2020-08-27 16:54:34 +00:00
2020-07-06 20:09:03 +00:00
static Future<SettingsStore> load(
{@required Box<Node> nodeSource,
FiatCurrency initialFiatCurrency = FiatCurrency.usd,
TransactionPriority initialTransactionPriority = TransactionPriority.slow,
BalanceDisplayMode initialBalanceDisplayMode =
BalanceDisplayMode.availableBalance}) async {
final sharedPreferences = await getIt.getAsync<SharedPreferences>();
final currentFiatCurrency = FiatCurrency(
2020-09-21 11:50:26 +00:00
symbol:
sharedPreferences.getString(PreferencesKey.currentFiatCurrencyKey));
2020-07-06 20:09:03 +00:00
final currentTransactionPriority = TransactionPriority.deserialize(
2020-09-21 11:50:26 +00:00
raw: sharedPreferences
.getInt(PreferencesKey.currentTransactionPriorityKey));
2020-07-06 20:09:03 +00:00
final currentBalanceDisplayMode = BalanceDisplayMode.deserialize(
2020-09-21 11:50:26 +00:00
raw: sharedPreferences
.getInt(PreferencesKey.currentBalanceDisplayModeKey));
2020-07-06 20:09:03 +00:00
final shouldSaveRecipientAddress =
2020-09-21 11:50:26 +00:00
sharedPreferences.getBool(PreferencesKey.shouldSaveRecipientAddressKey);
final allowBiometricalAuthentication = sharedPreferences
.getBool(PreferencesKey.allowBiometricalAuthenticationKey) ??
false;
final savedDarkTheme =
sharedPreferences.getBool(PreferencesKey.currentDarkTheme) ?? false;
2020-07-06 20:09:03 +00:00
final actionListDisplayMode = ObservableList<ActionListDisplayMode>();
actionListDisplayMode.addAll(deserializeActionlistDisplayModes(
2020-09-21 11:50:26 +00:00
sharedPreferences.getInt(PreferencesKey.displayActionListModeKey) ??
defaultActionsMode));
2020-11-09 23:14:47 +00:00
var pinLength = sharedPreferences.getInt(PreferencesKey.currentPinLength);
// If no value
if (pinLength == null || pinLength == 0) {
pinLength = defaultPinLength;
}
2020-07-06 20:09:03 +00:00
final savedLanguageCode =
2020-09-21 11:50:26 +00:00
sharedPreferences.getString(PreferencesKey.currentLanguageCode) ??
2020-09-28 15:47:43 +00:00
await LanguageService.localeDetection();
2020-09-21 11:50:26 +00:00
final nodeId = sharedPreferences.getInt(PreferencesKey.currentNodeIdKey);
final bitcoinElectrumServerId = sharedPreferences
.getInt(PreferencesKey.currentBitcoinElectrumSererIdKey);
2020-08-27 16:54:34 +00:00
final moneroNode = nodeSource.get(nodeId);
final bitcoinElectrumServer = nodeSource.get(bitcoinElectrumServerId);
2020-07-06 20:09:03 +00:00
final packageInfo = await PackageInfo.fromPlatform();
return SettingsStore(
sharedPreferences: sharedPreferences,
2020-08-27 16:54:34 +00:00
nodes: {
WalletType.monero: moneroNode,
WalletType.bitcoin: bitcoinElectrumServer
},
2020-07-06 20:09:03 +00:00
appVersion: packageInfo.version,
initialFiatCurrency: currentFiatCurrency,
initialTransactionPriority: currentTransactionPriority,
initialBalanceDisplayMode: currentBalanceDisplayMode,
initialSaveRecipientAddress: shouldSaveRecipientAddress,
initialAllowBiometricalAuthentication: allowBiometricalAuthentication,
initialDarkTheme: savedDarkTheme,
actionlistDisplayMode: actionListDisplayMode,
2020-09-21 11:50:26 +00:00
initialPinLength: pinLength,
2020-09-28 19:02:30 +00:00
initialLanguageCode: savedLanguageCode);
2020-07-06 20:09:03 +00:00
}
2020-09-21 11:50:26 +00:00
2020-09-26 19:17:31 +00:00
Future<void> _saveCurrentNode(Node node, WalletType walletType) async {
2020-09-21 11:50:26 +00:00
switch (walletType) {
case WalletType.bitcoin:
await _sharedPreferences.setInt(
PreferencesKey.currentBitcoinElectrumSererIdKey, node.key as int);
break;
case WalletType.monero:
await _sharedPreferences.setInt(
PreferencesKey.currentNodeIdKey, node.key as int);
break;
default:
break;
}
nodes[walletType] = node;
}
2020-07-06 20:09:03 +00:00
}