2021-01-27 13:51:51 +00:00
|
|
|
import 'package:cake_wallet/bitcoin/bitcoin_transaction_priority.dart';
|
2021-06-01 18:03:35 +00:00
|
|
|
import 'package:cake_wallet/buy/moonpay/moonpay_buy_provider.dart';
|
2020-09-21 11:50:26 +00:00
|
|
|
import 'package:cake_wallet/entities/preferences_key.dart';
|
2021-01-27 13:51:51 +00:00
|
|
|
import 'package:cake_wallet/entities/transaction_priority.dart';
|
2020-12-15 19:43:50 +00:00
|
|
|
import 'package:cake_wallet/themes/theme_base.dart';
|
|
|
|
import 'package:cake_wallet/themes/theme_list.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';
|
2021-01-27 13:51:51 +00:00
|
|
|
import 'package:cake_wallet/entities/monero_transaction_priority.dart';
|
2020-09-21 11:50:26 +00:00
|
|
|
import 'package:cake_wallet/entities/action_list_display_mode.dart';
|
2021-06-01 18:03:35 +00:00
|
|
|
import 'package:cake_wallet/.secrets.g.dart' as secrets;
|
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 BalanceDisplayMode initialBalanceDisplayMode,
|
|
|
|
@required bool initialSaveRecipientAddress,
|
|
|
|
@required bool initialAllowBiometricalAuthentication,
|
2020-12-15 19:30:16 +00:00
|
|
|
@required ThemeBase initialTheme,
|
2020-07-06 20:09:03 +00:00
|
|
|
@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,
|
2021-01-27 13:51:51 +00:00
|
|
|
@required TransactionPriority initialBitcoinTransactionPriority,
|
|
|
|
@required TransactionPriority initialMoneroTransactionPriority,
|
2021-03-16 13:20:46 +00:00
|
|
|
@required this.isBitcoinBuyEnabled,
|
2021-06-01 18:03:35 +00:00
|
|
|
@required this.isMoonPayEnabled,
|
2020-07-06 20:09:03 +00:00
|
|
|
this.actionlistDisplayMode}) {
|
|
|
|
fiatCurrency = initialFiatCurrency;
|
|
|
|
balanceDisplayMode = initialBalanceDisplayMode;
|
|
|
|
shouldSaveRecipientAddress = initialSaveRecipientAddress;
|
|
|
|
allowBiometricalAuthentication = initialAllowBiometricalAuthentication;
|
2020-12-10 17:53:40 +00:00
|
|
|
currentTheme = initialTheme;
|
2020-09-21 11:50:26 +00:00
|
|
|
pinCodeLength = initialPinLength;
|
2020-07-06 20:09:03 +00:00
|
|
|
languageCode = initialLanguageCode;
|
2021-01-27 13:51:51 +00:00
|
|
|
priority = ObservableMap<WalletType, TransactionPriority>.of({
|
|
|
|
WalletType.monero: initialMoneroTransactionPriority,
|
|
|
|
WalletType.bitcoin: initialBitcoinTransactionPriority
|
|
|
|
});
|
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()));
|
|
|
|
|
2021-01-27 13:51:51 +00:00
|
|
|
priority.observe((change) {
|
|
|
|
final key = change.key == WalletType.monero
|
|
|
|
? PreferencesKey.moneroTransactionPriority
|
|
|
|
: PreferencesKey.bitcoinTransactionPriority;
|
|
|
|
|
|
|
|
sharedPreferences.setInt(key, change.newValue.serialize());
|
|
|
|
});
|
2020-09-28 19:02:30 +00:00
|
|
|
|
|
|
|
reaction(
|
|
|
|
(_) => shouldSaveRecipientAddress,
|
|
|
|
(bool shouldSaveRecipientAddress) => sharedPreferences.setBool(
|
|
|
|
PreferencesKey.shouldSaveRecipientAddressKey,
|
|
|
|
shouldSaveRecipientAddress));
|
|
|
|
|
|
|
|
reaction(
|
2020-12-10 17:53:40 +00:00
|
|
|
(_) => currentTheme,
|
2020-12-18 19:30:43 +00:00
|
|
|
(ThemeBase theme) =>
|
|
|
|
sharedPreferences.setInt(PreferencesKey.currentTheme, theme.raw));
|
2020-09-28 19:02:30 +00:00
|
|
|
|
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
|
|
|
|
2020-09-28 19:02:30 +00:00
|
|
|
reaction(
|
|
|
|
(_) => languageCode,
|
|
|
|
(String languageCode) => sharedPreferences.setString(
|
|
|
|
PreferencesKey.currentLanguageCode, languageCode));
|
2020-12-15 16:29:10 +00:00
|
|
|
|
2021-01-15 17:41:30 +00:00
|
|
|
reaction(
|
|
|
|
(_) => balanceDisplayMode,
|
|
|
|
(BalanceDisplayMode mode) => sharedPreferences.setInt(
|
|
|
|
PreferencesKey.currentBalanceDisplayModeKey, mode.serialize()));
|
2020-12-18 19:42:00 +00:00
|
|
|
|
2020-12-15 16:29:10 +00:00
|
|
|
this
|
|
|
|
.nodes
|
|
|
|
.observe((change) => _saveCurrentNode(change.newValue, change.key));
|
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
|
|
|
|
BalanceDisplayMode balanceDisplayMode;
|
|
|
|
|
|
|
|
@observable
|
|
|
|
bool shouldSaveRecipientAddress;
|
|
|
|
|
|
|
|
@observable
|
|
|
|
bool allowBiometricalAuthentication;
|
|
|
|
|
|
|
|
@observable
|
2020-12-15 19:30:16 +00:00
|
|
|
ThemeBase currentTheme;
|
2020-07-06 20:09:03 +00:00
|
|
|
|
|
|
|
@observable
|
2020-09-21 11:50:26 +00:00
|
|
|
int pinCodeLength;
|
2020-07-06 20:09:03 +00:00
|
|
|
|
2020-09-28 15:47:43 +00:00
|
|
|
@computed
|
2020-12-10 17:53:40 +00:00
|
|
|
ThemeData get theme => currentTheme.themeData;
|
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
|
|
|
|
2021-01-27 13:51:51 +00:00
|
|
|
@observable
|
|
|
|
ObservableMap<WalletType, TransactionPriority> priority;
|
|
|
|
|
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
|
|
|
|
2021-03-16 13:20:46 +00:00
|
|
|
bool isBitcoinBuyEnabled;
|
|
|
|
|
2021-06-01 18:03:35 +00:00
|
|
|
bool isMoonPayEnabled;
|
|
|
|
|
2020-07-06 20:09:03 +00:00
|
|
|
static Future<SettingsStore> load(
|
|
|
|
{@required Box<Node> nodeSource,
|
2021-05-07 07:36:38 +00:00
|
|
|
@required bool isBitcoinBuyEnabled,
|
2021-06-01 18:03:35 +00:00
|
|
|
@required bool isMoonPayEnabled,
|
2020-07-06 20:09:03 +00:00
|
|
|
FiatCurrency initialFiatCurrency = FiatCurrency.usd,
|
2021-01-27 13:51:51 +00:00
|
|
|
MoneroTransactionPriority initialMoneroTransactionPriority =
|
|
|
|
MoneroTransactionPriority.slow,
|
|
|
|
BitcoinTransactionPriority initialBitcoinTransactionPriority =
|
|
|
|
BitcoinTransactionPriority.medium,
|
2020-07-06 20:09:03 +00:00
|
|
|
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));
|
2021-01-27 13:51:51 +00:00
|
|
|
final savedMoneroTransactionPriority =
|
|
|
|
MoneroTransactionPriority.deserialize(
|
|
|
|
raw: sharedPreferences
|
|
|
|
.getInt(PreferencesKey.moneroTransactionPriority));
|
|
|
|
final savedBitcoinTransactionPriority =
|
|
|
|
BitcoinTransactionPriority.deserialize(
|
|
|
|
raw: sharedPreferences
|
|
|
|
.getInt(PreferencesKey.bitcoinTransactionPriority));
|
|
|
|
final moneroTransactionPriority =
|
|
|
|
savedMoneroTransactionPriority ?? initialMoneroTransactionPriority;
|
|
|
|
final bitcoinTransactionPriority =
|
|
|
|
savedBitcoinTransactionPriority ?? initialBitcoinTransactionPriority;
|
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;
|
2020-12-18 19:30:43 +00:00
|
|
|
final legacyTheme =
|
2021-01-15 17:41:30 +00:00
|
|
|
(sharedPreferences.getBool(PreferencesKey.isDarkThemeLegacy) ?? false)
|
2020-12-18 19:30:43 +00:00
|
|
|
? ThemeType.dark.index
|
|
|
|
: ThemeType.bright.index;
|
2020-12-15 19:30:16 +00:00
|
|
|
final savedTheme = ThemeList.deserialize(
|
2020-12-18 19:30:43 +00:00
|
|
|
raw: sharedPreferences.getInt(PreferencesKey.currentTheme) ??
|
|
|
|
legacyTheme ??
|
|
|
|
0);
|
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);
|
2021-05-07 07:36:38 +00:00
|
|
|
final litecoinElectrumServerId = sharedPreferences
|
|
|
|
.getInt(PreferencesKey.currentLitecoinElectrumSererIdKey);
|
2020-08-27 16:54:34 +00:00
|
|
|
final moneroNode = nodeSource.get(nodeId);
|
|
|
|
final bitcoinElectrumServer = nodeSource.get(bitcoinElectrumServerId);
|
2021-05-07 07:36:38 +00:00
|
|
|
final litecoinElectrumServer = nodeSource.get(litecoinElectrumServerId);
|
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,
|
2021-05-07 07:36:38 +00:00
|
|
|
WalletType.bitcoin: bitcoinElectrumServer,
|
|
|
|
WalletType.litecoin: litecoinElectrumServer
|
2020-08-27 16:54:34 +00:00
|
|
|
},
|
2020-07-06 20:09:03 +00:00
|
|
|
appVersion: packageInfo.version,
|
2021-03-16 13:20:46 +00:00
|
|
|
isBitcoinBuyEnabled: isBitcoinBuyEnabled,
|
2021-06-01 18:03:35 +00:00
|
|
|
isMoonPayEnabled: isMoonPayEnabled,
|
2020-07-06 20:09:03 +00:00
|
|
|
initialFiatCurrency: currentFiatCurrency,
|
|
|
|
initialBalanceDisplayMode: currentBalanceDisplayMode,
|
|
|
|
initialSaveRecipientAddress: shouldSaveRecipientAddress,
|
|
|
|
initialAllowBiometricalAuthentication: allowBiometricalAuthentication,
|
2020-12-10 17:53:40 +00:00
|
|
|
initialTheme: savedTheme,
|
2020-07-06 20:09:03 +00:00
|
|
|
actionlistDisplayMode: actionListDisplayMode,
|
2020-09-21 11:50:26 +00:00
|
|
|
initialPinLength: pinLength,
|
2021-01-27 13:51:51 +00:00
|
|
|
initialLanguageCode: savedLanguageCode,
|
|
|
|
initialMoneroTransactionPriority: moneroTransactionPriority,
|
|
|
|
initialBitcoinTransactionPriority: bitcoinTransactionPriority);
|
2020-07-06 20:09:03 +00:00
|
|
|
}
|
2020-09-21 11:50:26 +00:00
|
|
|
|
2021-01-15 17:41:30 +00:00
|
|
|
Future<void> reload(
|
|
|
|
{@required Box<Node> nodeSource,
|
|
|
|
FiatCurrency initialFiatCurrency = FiatCurrency.usd,
|
2021-01-27 13:51:51 +00:00
|
|
|
MoneroTransactionPriority initialMoneroTransactionPriority =
|
|
|
|
MoneroTransactionPriority.slow,
|
|
|
|
BitcoinTransactionPriority initialBitcoinTransactionPriority =
|
|
|
|
BitcoinTransactionPriority.medium,
|
2021-01-15 17:41:30 +00:00
|
|
|
BalanceDisplayMode initialBalanceDisplayMode =
|
|
|
|
BalanceDisplayMode.availableBalance}) async {
|
2021-06-01 18:03:35 +00:00
|
|
|
final isBitcoinBuyEnabled = (secrets.wyreSecretKey?.isNotEmpty ?? false) &&
|
|
|
|
(secrets.wyreApiKey?.isNotEmpty ?? false) &&
|
|
|
|
(secrets.wyreAccountId?.isNotEmpty ?? false);
|
|
|
|
|
2021-06-02 17:09:39 +00:00
|
|
|
/*var isMoonPayEnabled = false;
|
2021-06-02 13:52:58 +00:00
|
|
|
try {
|
|
|
|
final locale = await Devicelocale.currentLocale;
|
|
|
|
final deviceCountryCode = locale.split('_').last;
|
|
|
|
isMoonPayEnabled = await MoonPayBuyProvider.onEnabled(deviceCountryCode);
|
|
|
|
} catch (e) {
|
|
|
|
isMoonPayEnabled = false;
|
|
|
|
print(e.toString());
|
2021-06-02 17:09:39 +00:00
|
|
|
}*/
|
2021-06-01 18:03:35 +00:00
|
|
|
|
2021-01-15 17:41:30 +00:00
|
|
|
final settings = await SettingsStoreBase.load(
|
|
|
|
nodeSource: nodeSource,
|
2021-06-01 18:03:35 +00:00
|
|
|
isBitcoinBuyEnabled: isBitcoinBuyEnabled,
|
2021-06-02 17:09:39 +00:00
|
|
|
isMoonPayEnabled: true,
|
2021-01-15 17:41:30 +00:00
|
|
|
initialBalanceDisplayMode: initialBalanceDisplayMode,
|
|
|
|
initialFiatCurrency: initialFiatCurrency,
|
2021-01-27 13:51:51 +00:00
|
|
|
initialMoneroTransactionPriority: initialMoneroTransactionPriority,
|
|
|
|
initialBitcoinTransactionPriority: initialBitcoinTransactionPriority);
|
2021-01-15 17:41:30 +00:00
|
|
|
fiatCurrency = settings.fiatCurrency;
|
|
|
|
actionlistDisplayMode = settings.actionlistDisplayMode;
|
2021-01-27 13:51:51 +00:00
|
|
|
priority[WalletType.monero] = initialMoneroTransactionPriority;
|
|
|
|
priority[WalletType.bitcoin] = initialBitcoinTransactionPriority;
|
2021-01-15 17:41:30 +00:00
|
|
|
balanceDisplayMode = settings.balanceDisplayMode;
|
|
|
|
shouldSaveRecipientAddress = settings.shouldSaveRecipientAddress;
|
|
|
|
allowBiometricalAuthentication = settings.allowBiometricalAuthentication;
|
|
|
|
currentTheme = settings.currentTheme;
|
|
|
|
pinCodeLength = settings.pinCodeLength;
|
|
|
|
languageCode = settings.languageCode;
|
|
|
|
appVersion = settings.appVersion;
|
|
|
|
}
|
|
|
|
|
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;
|
2021-05-07 07:36:38 +00:00
|
|
|
case WalletType.litecoin:
|
|
|
|
await _sharedPreferences.setInt(
|
|
|
|
PreferencesKey.currentLitecoinElectrumSererIdKey, node.key as int);
|
|
|
|
break;
|
2020-09-21 11:50:26 +00:00
|
|
|
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
|
|
|
}
|