cake_wallet/lib/store/settings_store.dart

179 lines
6.8 KiB
Dart
Raw Normal View History

2020-07-06 20:09:03 +00:00
import 'package:cake_wallet/di.dart';
2020-08-27 16:54:34 +00:00
import 'package:cake_wallet/src/domain/common/wallet_type.dart';
2020-07-06 20:09:03 +00:00
import 'package:flutter/foundation.dart';
import 'package:hive/hive.dart';
import 'package:mobx/mobx.dart';
import 'package:devicelocale/devicelocale.dart';
import 'package:package_info/package_info.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:cake_wallet/src/domain/common/language.dart';
import 'package:cake_wallet/src/domain/common/balance_display_mode.dart';
import 'package:cake_wallet/src/domain/common/fiat_currency.dart';
import 'package:cake_wallet/src/domain/common/node.dart';
import 'package:cake_wallet/src/domain/common/transaction_priority.dart';
import 'package:cake_wallet/src/stores/action_list/action_list_display_mode.dart';
part 'settings_store.g.dart';
class SettingsStore = SettingsStoreBase with _$SettingsStore;
abstract class SettingsStoreBase with Store {
SettingsStoreBase(
{@required SharedPreferences sharedPreferences,
@required Box<Node> nodeSource,
@required FiatCurrency initialFiatCurrency,
@required TransactionPriority initialTransactionPriority,
@required BalanceDisplayMode initialBalanceDisplayMode,
@required bool initialSaveRecipientAddress,
@required bool initialAllowBiometricalAuthentication,
@required bool initialDarkTheme,
@required int initialPinLength,
@required String initialLanguageCode,
@required String initialCurrentLocale,
2020-08-27 16:54:34 +00:00
// @required this.node,
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;
defaultPinLength = initialPinLength;
languageCode = initialLanguageCode;
currentLocale = initialCurrentLocale;
itemHeaders = {};
_sharedPreferences = sharedPreferences;
_nodeSource = nodeSource;
2020-08-27 16:54:34 +00:00
_nodes = nodes;
2020-07-06 20:09:03 +00:00
}
static const currentNodeIdKey = 'current_node_id';
2020-08-27 16:54:34 +00:00
static const currentBitcoinElectrumSererIdKey = 'current_node_id_btc';
2020-07-06 20:09:03 +00:00
static const currentFiatCurrencyKey = 'current_fiat_currency';
static const currentTransactionPriorityKey = 'current_fee_priority';
static const currentBalanceDisplayModeKey = 'current_balance_display_mode';
static const shouldSaveRecipientAddressKey = 'save_recipient_address';
static const allowBiometricalAuthenticationKey =
'allow_biometrical_authentication';
static const currentDarkTheme = 'dark_theme';
static const displayActionListModeKey = 'display_list_mode';
static const currentPinLength = 'current_pin_length';
static const currentLanguageCode = 'language_code';
2020-08-27 16:54:34 +00:00
// @observable
// Node node;
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
int defaultPinLength;
@observable
Map<String, String> itemHeaders;
String languageCode;
String currentLocale;
String appVersion;
SharedPreferences _sharedPreferences;
Box<Node> _nodeSource;
2020-08-27 16:54:34 +00:00
Map<WalletType, Node> _nodes;
Node getCurrentNode(WalletType walletType) => _nodes[walletType];
Future<void> setCurrentNode(Node node, WalletType walletType) async {
switch (walletType) {
case WalletType.bitcoin:
await _sharedPreferences.setInt(
currentBitcoinElectrumSererIdKey, node.key as int);
break;
case WalletType.monero:
await _sharedPreferences.setInt(currentNodeIdKey, node.key as int);
break;
default:
break;
}
_nodes[walletType] = node;
}
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(
symbol: sharedPreferences.getString(currentFiatCurrencyKey));
final currentTransactionPriority = TransactionPriority.deserialize(
raw: sharedPreferences.getInt(currentTransactionPriorityKey));
final currentBalanceDisplayMode = BalanceDisplayMode.deserialize(
raw: sharedPreferences.getInt(currentBalanceDisplayModeKey));
final shouldSaveRecipientAddress =
sharedPreferences.getBool(shouldSaveRecipientAddressKey);
final allowBiometricalAuthentication =
sharedPreferences.getBool(allowBiometricalAuthenticationKey) ?? false;
2020-08-25 16:32:40 +00:00
final savedDarkTheme = sharedPreferences.getBool(currentDarkTheme) ?? true;
2020-07-06 20:09:03 +00:00
final actionListDisplayMode = ObservableList<ActionListDisplayMode>();
actionListDisplayMode.addAll(deserializeActionlistDisplayModes(
sharedPreferences.getInt(displayActionListModeKey) ??
11)); // FIXME: Unnamed constant.
final defaultPinLength = sharedPreferences.getInt(currentPinLength) ??
4; // FIXME: Unnamed constant.
final savedLanguageCode =
sharedPreferences.getString(currentLanguageCode) ??
await Language.localeDetection();
final initialCurrentLocale = await Devicelocale.currentLocale;
final nodeId = sharedPreferences.getInt(currentNodeIdKey);
2020-08-27 16:54:34 +00:00
final bitcoinElectrumServerId =
sharedPreferences.getInt(currentBitcoinElectrumSererIdKey);
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
nodeSource: nodeSource,
appVersion: packageInfo.version,
initialFiatCurrency: currentFiatCurrency,
initialTransactionPriority: currentTransactionPriority,
initialBalanceDisplayMode: currentBalanceDisplayMode,
initialSaveRecipientAddress: shouldSaveRecipientAddress,
initialAllowBiometricalAuthentication: allowBiometricalAuthentication,
initialDarkTheme: savedDarkTheme,
actionlistDisplayMode: actionListDisplayMode,
initialPinLength: defaultPinLength,
initialLanguageCode: savedLanguageCode,
initialCurrentLocale: initialCurrentLocale);
}
}