2023-09-23 11:37:50 +00:00
|
|
|
import 'package:cake_wallet/di.dart';
|
|
|
|
import 'package:cake_wallet/entities/preferences_key.dart';
|
2020-09-21 11:50:26 +00:00
|
|
|
import 'package:cake_wallet/routes.dart';
|
2023-10-03 08:09:40 +00:00
|
|
|
import 'package:cake_wallet/store/app_store.dart';
|
2023-02-20 20:25:54 +00:00
|
|
|
import 'package:cake_wallet/utils/exception_handler.dart';
|
2023-09-23 11:37:50 +00:00
|
|
|
import 'package:cw_core/wallet_type.dart';
|
2020-09-21 11:50:26 +00:00
|
|
|
import 'package:flutter/widgets.dart';
|
|
|
|
import 'package:mobx/mobx.dart';
|
|
|
|
import 'package:cake_wallet/entities/load_current_wallet.dart';
|
|
|
|
import 'package:cake_wallet/store/authentication_store.dart';
|
2023-09-23 11:37:50 +00:00
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
2020-09-21 11:50:26 +00:00
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
ReactionDisposer? _onAuthenticationStateChange;
|
2020-09-21 11:50:26 +00:00
|
|
|
|
2020-11-12 20:02:37 +00:00
|
|
|
dynamic loginError;
|
|
|
|
|
2023-10-03 08:09:40 +00:00
|
|
|
void startAuthenticationStateChange(AuthenticationStore authenticationStore,
|
|
|
|
GlobalKey<NavigatorState> navigatorKey, AppStore appStore) {
|
2023-09-23 11:37:50 +00:00
|
|
|
_onAuthenticationStateChange ??= autorun(
|
|
|
|
(_) async {
|
|
|
|
final state = authenticationStore.state;
|
2020-09-21 11:50:26 +00:00
|
|
|
|
2023-09-23 11:37:50 +00:00
|
|
|
if (state == AuthenticationState.installed) {
|
2023-09-27 00:33:39 +00:00
|
|
|
await _loadCurrentWallet();
|
2023-09-23 11:37:50 +00:00
|
|
|
return;
|
2020-11-12 20:02:37 +00:00
|
|
|
}
|
2020-09-21 11:50:26 +00:00
|
|
|
|
2023-09-23 11:37:50 +00:00
|
|
|
if (state == AuthenticationState.allowed) {
|
2023-10-03 08:09:40 +00:00
|
|
|
await _navigateBasedOnWalletType(navigatorKey, appStore);
|
2023-09-23 11:37:50 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
2020-09-21 11:50:26 +00:00
|
|
|
}
|
2023-09-27 00:33:39 +00:00
|
|
|
|
|
|
|
Future<void> _loadCurrentWallet() async {
|
|
|
|
try {
|
|
|
|
await loadCurrentWallet();
|
|
|
|
} catch (error, stack) {
|
|
|
|
loginError = error;
|
|
|
|
ExceptionHandler.onError(FlutterErrorDetails(exception: error, stack: stack));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-03 08:09:40 +00:00
|
|
|
Future<void> _navigateBasedOnWalletType(
|
|
|
|
GlobalKey<NavigatorState> navigatorKey, AppStore appStore) async {
|
2023-09-27 00:33:39 +00:00
|
|
|
final typeRaw = getIt.get<SharedPreferences>().getInt(PreferencesKey.currentWalletType) ?? 0;
|
|
|
|
final type = deserializeFromInt(typeRaw);
|
|
|
|
|
|
|
|
if (type == WalletType.haven) {
|
2023-10-03 08:09:40 +00:00
|
|
|
final wallet = appStore.wallet;
|
|
|
|
|
|
|
|
await navigatorKey.currentState!.pushNamed(Routes.havenRemovalNoticePage, arguments: wallet);
|
|
|
|
|
2023-09-27 00:33:39 +00:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
await navigatorKey.currentState!.pushNamedAndRemoveUntil(Routes.dashboard, (route) => false);
|
2023-10-03 08:09:40 +00:00
|
|
|
return;
|
2023-09-27 00:33:39 +00:00
|
|
|
}
|
|
|
|
}
|