2024-08-06 14:59:44 +00:00
|
|
|
import 'dart:async';
|
|
|
|
|
2020-09-21 11:50:26 +00:00
|
|
|
import 'package:cake_wallet/routes.dart';
|
2023-02-20 20:25:54 +00:00
|
|
|
import 'package:cake_wallet/utils/exception_handler.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';
|
|
|
|
|
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;
|
2024-08-06 14:59:44 +00:00
|
|
|
StreamController<dynamic> authenticatedErrorStreamController = StreamController<dynamic>();
|
2020-11-12 20:02:37 +00:00
|
|
|
|
2023-04-14 04:39:08 +00:00
|
|
|
void startAuthenticationStateChange(
|
|
|
|
AuthenticationStore authenticationStore, GlobalKey<NavigatorState> navigatorKey) {
|
2024-08-06 14:59:44 +00:00
|
|
|
authenticatedErrorStreamController.stream.listen((event) {
|
|
|
|
if (authenticationStore.state == AuthenticationState.allowed) {
|
|
|
|
ExceptionHandler.showError(event.toString(), delayInSeconds: 3);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-09-21 11:50:26 +00:00
|
|
|
_onAuthenticationStateChange ??= autorun((_) async {
|
|
|
|
final state = authenticationStore.state;
|
|
|
|
|
|
|
|
if (state == AuthenticationState.installed) {
|
2020-11-12 20:02:37 +00:00
|
|
|
try {
|
|
|
|
await loadCurrentWallet();
|
2023-02-20 20:25:54 +00:00
|
|
|
} catch (error, stack) {
|
|
|
|
loginError = error;
|
|
|
|
ExceptionHandler.onError(FlutterErrorDetails(exception: error, stack: stack));
|
2020-11-12 20:02:37 +00:00
|
|
|
}
|
2020-10-09 18:34:21 +00:00
|
|
|
return;
|
2020-09-21 11:50:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (state == AuthenticationState.allowed) {
|
2022-12-03 21:34:23 +00:00
|
|
|
await navigatorKey.currentState!.pushNamedAndRemoveUntil(Routes.dashboard, (route) => false);
|
2024-08-06 14:59:44 +00:00
|
|
|
if (!(await authenticatedErrorStreamController.stream.isEmpty)) {
|
|
|
|
ExceptionHandler.showError(
|
|
|
|
(await authenticatedErrorStreamController.stream.first).toString());
|
|
|
|
authenticatedErrorStreamController.stream.drain();
|
|
|
|
}
|
2020-10-09 18:34:21 +00:00
|
|
|
return;
|
2020-09-21 11:50:26 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|