You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cake_wallet/lib/store/settings_store.dart

320 lines
12 KiB

import 'package:cake_wallet/bitcoin/bitcoin.dart';
4 years ago
import 'package:cake_wallet/entities/preferences_key.dart';
import 'package:cw_core/transaction_priority.dart';
import 'package:cake_wallet/themes/theme_base.dart';
import 'package:cake_wallet/themes/theme_list.dart';
import 'package:flutter/foundation.dart';
4 years ago
import 'package:flutter/material.dart';
import 'package:hive/hive.dart';
import 'package:mobx/mobx.dart';
import 'package:package_info/package_info.dart';
4 years ago
import 'package:cake_wallet/di.dart';
import 'package:cw_core/wallet_type.dart';
import 'package:shared_preferences/shared_preferences.dart';
4 years ago
import 'package:cake_wallet/entities/language_service.dart';
4 years ago
import 'package:cake_wallet/entities/balance_display_mode.dart';
import 'package:cake_wallet/entities/fiat_currency.dart';
import 'package:cw_core/node.dart';
import 'package:cake_wallet/monero/monero.dart';
4 years ago
import 'package:cake_wallet/entities/action_list_display_mode.dart';
import 'package:cake_wallet/.secrets.g.dart' as secrets;
part 'settings_store.g.dart';
class SettingsStore = SettingsStoreBase with _$SettingsStore;
abstract class SettingsStoreBase with Store {
SettingsStoreBase(
{@required SharedPreferences sharedPreferences,
@required FiatCurrency initialFiatCurrency,
@required BalanceDisplayMode initialBalanceDisplayMode,
@required bool initialSaveRecipientAddress,
@required bool initialAllowBiometricalAuthentication,
@required ThemeBase initialTheme,
@required int initialPinLength,
@required String initialLanguageCode,
4 years ago
// @required String initialCurrentLocale,
@required this.appVersion,
4 years ago
@required Map<WalletType, Node> nodes,
3 years ago
@required TransactionPriority initialBitcoinTransactionPriority,
@required TransactionPriority initialMoneroTransactionPriority,
@required this.shouldShowYatPopup,
@required this.isBitcoinBuyEnabled,
this.actionlistDisplayMode}) {
fiatCurrency = initialFiatCurrency;
balanceDisplayMode = initialBalanceDisplayMode;
shouldSaveRecipientAddress = initialSaveRecipientAddress;
allowBiometricalAuthentication = initialAllowBiometricalAuthentication;
currentTheme = initialTheme;
4 years ago
pinCodeLength = initialPinLength;
languageCode = initialLanguageCode;
3 years ago
priority = ObservableMap<WalletType, TransactionPriority>.of({
WalletType.monero: initialMoneroTransactionPriority,
WalletType.bitcoin: initialBitcoinTransactionPriority
});
this.nodes = ObservableMap<WalletType, Node>.of(nodes);
_sharedPreferences = sharedPreferences;
4 years ago
reaction(
(_) => fiatCurrency,
(FiatCurrency fiatCurrency) => sharedPreferences.setString(
PreferencesKey.currentFiatCurrencyKey, fiatCurrency.serialize()));
reaction(
(_) => shouldShowYatPopup,
(bool shouldShowYatPopup) => sharedPreferences
.setBool(PreferencesKey.shouldShowYatPopup, shouldShowYatPopup));
3 years ago
priority.observe((change) {
final key = change.key == WalletType.monero
? PreferencesKey.moneroTransactionPriority
: PreferencesKey.bitcoinTransactionPriority;
sharedPreferences.setInt(key, change.newValue.serialize());
});
4 years ago
reaction(
(_) => shouldSaveRecipientAddress,
(bool shouldSaveRecipientAddress) => sharedPreferences.setBool(
PreferencesKey.shouldSaveRecipientAddressKey,
shouldSaveRecipientAddress));
reaction(
(_) => currentTheme,
(ThemeBase theme) =>
sharedPreferences.setInt(PreferencesKey.currentTheme, theme.raw));
4 years ago
reaction(
(_) => allowBiometricalAuthentication,
(bool biometricalAuthentication) => sharedPreferences.setBool(
4 years ago
PreferencesKey.allowBiometricalAuthenticationKey,
biometricalAuthentication));
reaction(
(_) => pinCodeLength,
(int pinLength) => sharedPreferences.setInt(
PreferencesKey.currentPinLength, pinLength));
4 years ago
4 years ago
reaction(
(_) => languageCode,
(String languageCode) => sharedPreferences.setString(
PreferencesKey.currentLanguageCode, languageCode));
4 years ago
reaction(
(_) => balanceDisplayMode,
(BalanceDisplayMode mode) => sharedPreferences.setInt(
PreferencesKey.currentBalanceDisplayModeKey, mode.serialize()));
this
.nodes
.observe((change) => _saveCurrentNode(change.newValue, change.key));
}
4 years ago
static const defaultPinLength = 4;
static const defaultActionsMode = 11;
@observable
FiatCurrency fiatCurrency;
@observable
bool shouldShowYatPopup;
@observable
ObservableList<ActionListDisplayMode> actionlistDisplayMode;
@observable
BalanceDisplayMode balanceDisplayMode;
@observable
bool shouldSaveRecipientAddress;
@observable
bool allowBiometricalAuthentication;
@observable
ThemeBase currentTheme;
@observable
4 years ago
int pinCodeLength;
4 years ago
@computed
ThemeData get theme => currentTheme.themeData;
4 years ago
@observable
String languageCode;
3 years ago
@observable
ObservableMap<WalletType, TransactionPriority> priority;
String appVersion;
SharedPreferences _sharedPreferences;
ObservableMap<WalletType, Node> nodes;
4 years ago
Node getCurrentNode(WalletType walletType) => nodes[walletType];
4 years ago
bool isBitcoinBuyEnabled;
bool get shouldShowReceiveWarning =>
_sharedPreferences.getBool(PreferencesKey.shouldShowReceiveWarning) ?? true;
Future<void> setShouldShowReceiveWarning(bool value) async =>
_sharedPreferences.setBool(PreferencesKey.shouldShowReceiveWarning, value);
static Future<SettingsStore> load(
{@required Box<Node> nodeSource,
@required bool isBitcoinBuyEnabled,
FiatCurrency initialFiatCurrency = FiatCurrency.usd,
TransactionPriority initialMoneroTransactionPriority,
TransactionPriority initialBitcoinTransactionPriority,
BalanceDisplayMode initialBalanceDisplayMode =
BalanceDisplayMode.availableBalance}) async {
if (initialBitcoinTransactionPriority == null) {
initialBitcoinTransactionPriority = bitcoin?.getMediumTransactionPriority();
}
if (initialMoneroTransactionPriority == null) {
initialMoneroTransactionPriority = monero?.getDefaultTransactionPriority();
}
final sharedPreferences = await getIt.getAsync<SharedPreferences>();
final currentFiatCurrency = FiatCurrency(
4 years ago
symbol:
sharedPreferences.getString(PreferencesKey.currentFiatCurrencyKey));
3 years ago
final savedMoneroTransactionPriority =
monero?.deserializeMoneroTransactionPriority(
3 years ago
raw: sharedPreferences
.getInt(PreferencesKey.moneroTransactionPriority));
final savedBitcoinTransactionPriority =
bitcoin?.deserializeBitcoinTransactionPriority(sharedPreferences
3 years ago
.getInt(PreferencesKey.bitcoinTransactionPriority));
final moneroTransactionPriority =
savedMoneroTransactionPriority ?? initialMoneroTransactionPriority;
final bitcoinTransactionPriority =
savedBitcoinTransactionPriority ?? initialBitcoinTransactionPriority;
final currentBalanceDisplayMode = BalanceDisplayMode.deserialize(
4 years ago
raw: sharedPreferences
.getInt(PreferencesKey.currentBalanceDisplayModeKey));
final shouldSaveRecipientAddress =
4 years ago
sharedPreferences.getBool(PreferencesKey.shouldSaveRecipientAddressKey);
final allowBiometricalAuthentication = sharedPreferences
.getBool(PreferencesKey.allowBiometricalAuthenticationKey) ??
false;
final legacyTheme =
4 years ago
(sharedPreferences.getBool(PreferencesKey.isDarkThemeLegacy) ?? false)
? ThemeType.dark.index
: ThemeType.bright.index;
final savedTheme = ThemeList.deserialize(
raw: sharedPreferences.getInt(PreferencesKey.currentTheme) ??
legacyTheme ??
0);
final actionListDisplayMode = ObservableList<ActionListDisplayMode>();
actionListDisplayMode.addAll(deserializeActionlistDisplayModes(
4 years ago
sharedPreferences.getInt(PreferencesKey.displayActionListModeKey) ??
defaultActionsMode));
4 years ago
var pinLength = sharedPreferences.getInt(PreferencesKey.currentPinLength);
// If no value
if (pinLength == null || pinLength == 0) {
pinLength = defaultPinLength;
}
final savedLanguageCode =
4 years ago
sharedPreferences.getString(PreferencesKey.currentLanguageCode) ??
4 years ago
await LanguageService.localeDetection();
4 years ago
final nodeId = sharedPreferences.getInt(PreferencesKey.currentNodeIdKey);
final bitcoinElectrumServerId = sharedPreferences
.getInt(PreferencesKey.currentBitcoinElectrumSererIdKey);
final litecoinElectrumServerId = sharedPreferences
.getInt(PreferencesKey.currentLitecoinElectrumSererIdKey);
4 years ago
final moneroNode = nodeSource.get(nodeId);
final bitcoinElectrumServer = nodeSource.get(bitcoinElectrumServerId);
final litecoinElectrumServer = nodeSource.get(litecoinElectrumServerId);
final packageInfo = await PackageInfo.fromPlatform();
final shouldShowYatPopup =
sharedPreferences.getBool(PreferencesKey.shouldShowYatPopup) ?? true;
return SettingsStore(
sharedPreferences: sharedPreferences,
4 years ago
nodes: {
WalletType.monero: moneroNode,
WalletType.bitcoin: bitcoinElectrumServer,
WalletType.litecoin: litecoinElectrumServer
4 years ago
},
appVersion: packageInfo.version,
isBitcoinBuyEnabled: isBitcoinBuyEnabled,
initialFiatCurrency: currentFiatCurrency,
initialBalanceDisplayMode: currentBalanceDisplayMode,
initialSaveRecipientAddress: shouldSaveRecipientAddress,
initialAllowBiometricalAuthentication: allowBiometricalAuthentication,
initialTheme: savedTheme,
actionlistDisplayMode: actionListDisplayMode,
4 years ago
initialPinLength: pinLength,
3 years ago
initialLanguageCode: savedLanguageCode,
initialMoneroTransactionPriority: moneroTransactionPriority,
initialBitcoinTransactionPriority: bitcoinTransactionPriority,
shouldShowYatPopup: shouldShowYatPopup);
}
4 years ago
4 years ago
Future<void> reload(
{@required Box<Node> nodeSource,
FiatCurrency initialFiatCurrency = FiatCurrency.usd,
TransactionPriority initialMoneroTransactionPriority,
TransactionPriority initialBitcoinTransactionPriority,
4 years ago
BalanceDisplayMode initialBalanceDisplayMode =
BalanceDisplayMode.availableBalance}) async {
if (initialBitcoinTransactionPriority == null) {
initialBitcoinTransactionPriority = bitcoin?.getMediumTransactionPriority();
}
if (initialMoneroTransactionPriority == null) {
initialMoneroTransactionPriority = monero?.getDefaultTransactionPriority();
}
final isBitcoinBuyEnabled = (secrets.wyreSecretKey?.isNotEmpty ?? false) &&
(secrets.wyreApiKey?.isNotEmpty ?? false) &&
(secrets.wyreAccountId?.isNotEmpty ?? false);
4 years ago
final settings = await SettingsStoreBase.load(
nodeSource: nodeSource,
isBitcoinBuyEnabled: isBitcoinBuyEnabled,
4 years ago
initialBalanceDisplayMode: initialBalanceDisplayMode,
initialFiatCurrency: initialFiatCurrency,
3 years ago
initialMoneroTransactionPriority: initialMoneroTransactionPriority,
initialBitcoinTransactionPriority: initialBitcoinTransactionPriority);
4 years ago
fiatCurrency = settings.fiatCurrency;
actionlistDisplayMode = settings.actionlistDisplayMode;
3 years ago
priority[WalletType.monero] = initialMoneroTransactionPriority;
priority[WalletType.bitcoin] = initialBitcoinTransactionPriority;
4 years ago
balanceDisplayMode = settings.balanceDisplayMode;
shouldSaveRecipientAddress = settings.shouldSaveRecipientAddress;
allowBiometricalAuthentication = settings.allowBiometricalAuthentication;
currentTheme = settings.currentTheme;
pinCodeLength = settings.pinCodeLength;
languageCode = settings.languageCode;
appVersion = settings.appVersion;
shouldShowYatPopup = settings.shouldShowYatPopup;
4 years ago
}
4 years ago
Future<void> _saveCurrentNode(Node node, WalletType walletType) async {
4 years ago
switch (walletType) {
case WalletType.bitcoin:
await _sharedPreferences.setInt(
PreferencesKey.currentBitcoinElectrumSererIdKey, node.key as int);
break;
case WalletType.litecoin:
await _sharedPreferences.setInt(
PreferencesKey.currentLitecoinElectrumSererIdKey, node.key as int);
break;
4 years ago
case WalletType.monero:
await _sharedPreferences.setInt(
PreferencesKey.currentNodeIdKey, node.key as int);
break;
default:
break;
}
nodes[walletType] = node;
}
}