mirror of
https://github.com/Such-Software/hash-wallet.git
synced 2026-12-01 12:04:21 +01:00
Part 1
This commit is contained in:
+18
-123
@@ -1,139 +1,34 @@
|
||||
import 'dart:async';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:mobx/mobx.dart';
|
||||
import 'package:cake_wallet/di.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
import 'package:connectivity/connectivity.dart';
|
||||
import 'package:cake_wallet/core/key_service.dart';
|
||||
import 'package:cake_wallet/router.dart';
|
||||
import 'package:cake_wallet/src/domain/common/sync_status.dart';
|
||||
import 'package:cake_wallet/core/wallet_base.dart';
|
||||
import 'package:cake_wallet/core/wallet_service.dart';
|
||||
import 'package:cake_wallet/di.dart';
|
||||
import 'package:cake_wallet/entities/preferences_key.dart';
|
||||
import 'package:cake_wallet/reactions/on_authentication_state_change.dart';
|
||||
import 'package:cake_wallet/reactions/on_current_fiat_change.dart';
|
||||
import 'package:cake_wallet/reactions/on_current_wallet_change.dart';
|
||||
import 'package:cake_wallet/store/app_store.dart';
|
||||
import 'package:cake_wallet/store/settings_store.dart';
|
||||
import 'package:cake_wallet/store/authentication_store.dart';
|
||||
import 'package:cake_wallet/src/domain/common/wallet_type.dart';
|
||||
import 'package:cake_wallet/src/domain/services/fiat_convertation_service.dart';
|
||||
import 'package:cake_wallet/src/domain/common/fiat_currency.dart';
|
||||
import 'package:cake_wallet/store/dashboard/fiat_convertation_store.dart';
|
||||
import 'package:cake_wallet/store/dashboard/fiat_conversion_store.dart';
|
||||
|
||||
// FIXME: move me
|
||||
Future<void> loadCurrentWallet() async {
|
||||
Future<void> bootstrap(GlobalKey<NavigatorState> navigatorKey) async {
|
||||
final appStore = getIt.get<AppStore>();
|
||||
final name = getIt.get<SharedPreferences>().getString('current_wallet_name');
|
||||
final typeRaw =
|
||||
getIt.get<SharedPreferences>().getInt('current_wallet_type') ?? 0;
|
||||
final type = deserializeFromInt(typeRaw);
|
||||
final password =
|
||||
await getIt.get<KeyService>().getWalletPassword(walletName: name);
|
||||
final _service = getIt.get<WalletService>(param1: type);
|
||||
final wallet = await _service.openWallet(name, password);
|
||||
appStore.wallet = wallet;
|
||||
}
|
||||
|
||||
ReactionDisposer _initialAuthReaction;
|
||||
ReactionDisposer _onCurrentWalletChangeReaction;
|
||||
ReactionDisposer _onWalletSyncStatusChangeReaction;
|
||||
ReactionDisposer _onCurrentFiatCurrencyChangeDisposer;
|
||||
Timer _reconnectionTimer;
|
||||
|
||||
Future<void> bootstrap(
|
||||
{FiatConvertationService fiatConvertationService,
|
||||
GlobalKey<NavigatorState> navigatorKey}) async {
|
||||
final authenticationStore = getIt.get<AuthenticationStore>();
|
||||
final settingsStore = getIt.get<SettingsStore>();
|
||||
final fiatConvertationStore = getIt.get<FiatConvertationStore>();
|
||||
final fiatConversionStore = getIt.get<FiatConversionStore>();
|
||||
|
||||
if (authenticationStore.state == AuthenticationState.uninitialized) {
|
||||
authenticationStore.state =
|
||||
getIt.get<SharedPreferences>().getString('current_wallet_name') == null
|
||||
? AuthenticationState.denied
|
||||
: AuthenticationState.installed;
|
||||
authenticationStore.state = getIt
|
||||
.get<SharedPreferences>()
|
||||
.getString(PreferencesKey.currentWalletName) ==
|
||||
null
|
||||
? AuthenticationState.denied
|
||||
: AuthenticationState.installed;
|
||||
}
|
||||
|
||||
_initialAuthReaction ??= autorun((_) async {
|
||||
final state = authenticationStore.state;
|
||||
print(state);
|
||||
|
||||
if (state == AuthenticationState.installed) {
|
||||
await loadCurrentWallet();
|
||||
await navigatorKey.currentState
|
||||
.pushAndRemoveUntil(createLoginRoute(), (_) => false);
|
||||
}
|
||||
|
||||
if (state == AuthenticationState.allowed) {
|
||||
await navigatorKey.currentState
|
||||
.pushAndRemoveUntil(createDashboardRoute(), (_) => false);
|
||||
}
|
||||
|
||||
if (state == AuthenticationState.denied) {
|
||||
await navigatorKey.currentState
|
||||
.pushAndRemoveUntil(createWelcomeRoute(), (_) => false);
|
||||
}
|
||||
});
|
||||
|
||||
_onCurrentWalletChangeReaction ??=
|
||||
reaction((_) => getIt.get<AppStore>().wallet, (WalletBase wallet) async {
|
||||
_onWalletSyncStatusChangeReaction?.reaction?.dispose();
|
||||
_reconnectionTimer?.cancel();
|
||||
_onWalletSyncStatusChangeReaction =
|
||||
reaction((_) => wallet.syncStatus, (SyncStatus status) async {
|
||||
if (status is ConnectedSyncStatus) {
|
||||
await wallet.startSync();
|
||||
}
|
||||
});
|
||||
|
||||
_reconnectionTimer = Timer.periodic(Duration(seconds: 5), (_) async {
|
||||
final connectivityResult = await (Connectivity().checkConnectivity());
|
||||
|
||||
if (connectivityResult == ConnectivityResult.none) {
|
||||
wallet.syncStatus = FailedSyncStatus();
|
||||
return;
|
||||
}
|
||||
|
||||
if (wallet.syncStatus is LostConnectionSyncStatus ||
|
||||
wallet.syncStatus is FailedSyncStatus) {
|
||||
try {
|
||||
final alive =
|
||||
await settingsStore.getCurrentNode(wallet.type).requestNode();
|
||||
|
||||
if (alive) {
|
||||
await wallet.connectToNode(
|
||||
node: settingsStore.getCurrentNode(wallet.type));
|
||||
}
|
||||
} catch (_) {}
|
||||
}
|
||||
});
|
||||
|
||||
await getIt
|
||||
.get<SharedPreferences>()
|
||||
.setString('current_wallet_name', wallet.name);
|
||||
|
||||
await getIt
|
||||
.get<SharedPreferences>()
|
||||
.setInt('current_wallet_type', serializeToInt(wallet.type));
|
||||
|
||||
final node = settingsStore.getCurrentNode(wallet.type);
|
||||
final cryptoCurrency = wallet.currency;
|
||||
final fiatCurrency = settingsStore.fiatCurrency;
|
||||
|
||||
await wallet.connectToNode(node: node);
|
||||
|
||||
final price = await fiatConvertationService.getPrice(
|
||||
crypto: cryptoCurrency, fiat: fiatCurrency);
|
||||
|
||||
fiatConvertationStore.setPrice(price);
|
||||
});
|
||||
|
||||
_onCurrentFiatCurrencyChangeDisposer ??= reaction(
|
||||
(_) => settingsStore.fiatCurrency, (FiatCurrency fiatCurrency) async {
|
||||
final cryptoCurrency = getIt.get<AppStore>().wallet.currency;
|
||||
|
||||
final price = await fiatConvertationService.getPrice(
|
||||
crypto: cryptoCurrency, fiat: fiatCurrency);
|
||||
|
||||
fiatConvertationStore.setPrice(price);
|
||||
});
|
||||
startAuthenticationStateChange(authenticationStore, navigatorKey);
|
||||
startCurrentWalletChangeReaction(
|
||||
appStore, settingsStore, fiatConversionStore);
|
||||
startCurrentFiatChangeReaction(appStore, settingsStore);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:cake_wallet/core/wallet_base.dart';
|
||||
import 'package:cake_wallet/entities/sync_status.dart';
|
||||
import 'package:cake_wallet/store/settings_store.dart';
|
||||
import 'package:connectivity/connectivity.dart';
|
||||
|
||||
Timer _checkConnectionTimer;
|
||||
|
||||
void startCheckConnectionReaction(WalletBase wallet, SettingsStore settingsStore, {int timeInterval = 5}) {
|
||||
_checkConnectionTimer?.cancel();
|
||||
_checkConnectionTimer = Timer.periodic(Duration(seconds: timeInterval), (_) async {
|
||||
final connectivityResult = await (Connectivity().checkConnectivity());
|
||||
|
||||
if (connectivityResult == ConnectivityResult.none) {
|
||||
wallet.syncStatus = FailedSyncStatus();
|
||||
return;
|
||||
}
|
||||
|
||||
if (wallet.syncStatus is LostConnectionSyncStatus ||
|
||||
wallet.syncStatus is FailedSyncStatus) {
|
||||
try {
|
||||
final alive =
|
||||
await settingsStore.getCurrentNode(wallet.type).requestNode();
|
||||
|
||||
if (alive) {
|
||||
await wallet.connectToNode(
|
||||
node: settingsStore.getCurrentNode(wallet.type));
|
||||
}
|
||||
} catch (_) {
|
||||
// FIXME: empty catch clojure
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import 'dart:async';
|
||||
import 'package:cake_wallet/core/fiat_conversion_service.dart';
|
||||
import 'package:cake_wallet/store/app_store.dart';
|
||||
import 'package:cake_wallet/store/dashboard/fiat_conversion_store.dart';
|
||||
import 'package:cake_wallet/store/settings_store.dart';
|
||||
|
||||
Timer _timer;
|
||||
|
||||
Future<void> startFiatRateUpdate(AppStore appStore, SettingsStore settingsStore,
|
||||
FiatConversionStore fiatConversionStore) async {
|
||||
if (_timer != null) {
|
||||
return;
|
||||
}
|
||||
|
||||
fiatConversionStore.price = await FiatConversionService.fetchPrice(
|
||||
appStore.wallet.currency, settingsStore.fiatCurrency);
|
||||
|
||||
_timer = Timer.periodic(
|
||||
Duration(seconds: 30),
|
||||
(_) async => fiatConversionStore.price =
|
||||
await FiatConversionService.fetchPrice(
|
||||
appStore.wallet.currency, settingsStore.fiatCurrency));
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import 'package:cake_wallet/routes.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:mobx/mobx.dart';
|
||||
import 'package:cake_wallet/router.dart';
|
||||
import 'package:cake_wallet/entities/load_current_wallet.dart';
|
||||
import 'package:cake_wallet/store/authentication_store.dart';
|
||||
|
||||
ReactionDisposer _onAuthenticationStateChange;
|
||||
|
||||
void startAuthenticationStateChange(AuthenticationStore authenticationStore,
|
||||
GlobalKey<NavigatorState> navigatorKey) {
|
||||
_onAuthenticationStateChange ??= autorun((_) async {
|
||||
final state = authenticationStore.state;
|
||||
|
||||
if (state == AuthenticationState.installed) {
|
||||
await loadCurrentWallet();
|
||||
// await navigatorKey.currentState
|
||||
// .pushNamedAndRemoveUntil(Routes.login, (_) => false);
|
||||
}
|
||||
|
||||
if (state == AuthenticationState.allowed) {
|
||||
await navigatorKey.currentState
|
||||
.pushNamedAndRemoveUntil(Routes.dashboard, (route) => false);
|
||||
}
|
||||
|
||||
if (state == AuthenticationState.denied) {
|
||||
await navigatorKey.currentState
|
||||
.pushNamedAndRemoveUntil(Routes.welcome, (_) => false);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import 'package:mobx/mobx.dart';
|
||||
import 'package:cake_wallet/store/settings_store.dart';
|
||||
import 'package:cake_wallet/store/app_store.dart';
|
||||
import 'package:cake_wallet/entities/fiat_currency.dart';
|
||||
|
||||
ReactionDisposer _onCurrentFiatCurrencyChangeDisposer;
|
||||
|
||||
void startCurrentFiatChangeReaction(AppStore appStore, SettingsStore settingsStore) {
|
||||
_onCurrentFiatCurrencyChangeDisposer?.reaction?.dispose();
|
||||
_onCurrentFiatCurrencyChangeDisposer = reaction(
|
||||
(_) => settingsStore.fiatCurrency, (FiatCurrency fiatCurrency) async {
|
||||
final cryptoCurrency = appStore.wallet.currency;
|
||||
// final price = await fiatConvertationService.getPrice(
|
||||
// crypto: cryptoCurrency, fiat: fiatCurrency);
|
||||
//
|
||||
// fiatConvertationStore.setPrice(price);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import 'package:mobx/mobx.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:cake_wallet/di.dart';
|
||||
import 'package:cake_wallet/entities/preferences_key.dart';
|
||||
import 'package:cake_wallet/reactions/check_connection.dart';
|
||||
import 'package:cake_wallet/reactions/on_wallet_sync_status_change.dart';
|
||||
import 'package:cake_wallet/store/dashboard/fiat_conversion_store.dart';
|
||||
import 'package:cake_wallet/store/app_store.dart';
|
||||
import 'package:cake_wallet/store/settings_store.dart';
|
||||
import 'package:cake_wallet/core/fiat_conversion_service.dart';
|
||||
import 'package:cake_wallet/core/wallet_base.dart';
|
||||
import 'package:cake_wallet/entities/wallet_type.dart';
|
||||
|
||||
ReactionDisposer _onCurrentWalletChangeReaction;
|
||||
|
||||
void startCurrentWalletChangeReaction(AppStore appStore,
|
||||
SettingsStore settingsStore, FiatConversionStore fiatConversionStore) {
|
||||
_onCurrentWalletChangeReaction?.reaction?.dispose();
|
||||
|
||||
_onCurrentWalletChangeReaction =
|
||||
reaction((_) => appStore.wallet, (WalletBase wallet) async {
|
||||
try {
|
||||
final node = settingsStore.getCurrentNode(wallet.type);
|
||||
startWalletSyncStatusChangeReaction(wallet);
|
||||
startCheckConnectionReaction(wallet, settingsStore);
|
||||
await getIt
|
||||
.get<SharedPreferences>()
|
||||
.setString(PreferencesKey.currentWalletName, wallet.name);
|
||||
await getIt.get<SharedPreferences>().setInt(
|
||||
PreferencesKey.currentWalletType, serializeToInt(wallet.type));
|
||||
await wallet.connectToNode(node: node);
|
||||
|
||||
fiatConversionStore.price = await FiatConversionService.fetchPrice(
|
||||
wallet.currency, settingsStore.fiatCurrency);
|
||||
} catch (e) {
|
||||
print(e.toString());
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import 'package:mobx/mobx.dart';
|
||||
import 'package:cake_wallet/core/wallet_base.dart';
|
||||
import 'package:cake_wallet/entities/sync_status.dart';
|
||||
|
||||
ReactionDisposer _onWalletSyncStatusChangeReaction;
|
||||
|
||||
void startWalletSyncStatusChangeReaction(WalletBase wallet) {
|
||||
_onWalletSyncStatusChangeReaction?.reaction?.dispose();
|
||||
_onWalletSyncStatusChangeReaction =
|
||||
reaction((_) => wallet.syncStatus, (SyncStatus status) async {
|
||||
if (status is ConnectedSyncStatus) {
|
||||
await wallet.startSync();
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user