From f57dec12ef883f9f5a23819ff220911cf402483d Mon Sep 17 00:00:00 2001 From: HuyNguyen Date: Wed, 5 Jun 2024 12:17:22 +0700 Subject: [PATCH] Handle error for `_getRecoveryWords` --- lib/pages/bootstrap/tom_bootstrap_dialog.dart | 109 ++++++++++++------ 1 file changed, 76 insertions(+), 33 deletions(-) diff --git a/lib/pages/bootstrap/tom_bootstrap_dialog.dart b/lib/pages/bootstrap/tom_bootstrap_dialog.dart index ab80759f37..a118f5c27b 100644 --- a/lib/pages/bootstrap/tom_bootstrap_dialog.dart +++ b/lib/pages/bootstrap/tom_bootstrap_dialog.dart @@ -69,8 +69,18 @@ class TomBootstrapDialogState extends State Future _getRecoveryWords() async { return await _getRecoveryWordsInteractor.execute().then( (either) => either.fold( - (failure) => null, - (success) => success.words, + (failure) { + Logs().e( + 'TomBootstrapDialog::_getRecoveryWords(): $failure', + ); + return null; + }, + (success) { + Logs().d( + 'TomBootstrapDialog::_getRecoveryWords(): ${success.words}', + ); + return success.words; + }, ), ); } @@ -86,48 +96,81 @@ class TomBootstrapDialogState extends State _uploadRecoveryKeyState = UploadRecoveryKeyState.checkingRecoveryWork; }); await _getRecoveryKeyState(); - bootstrap = - widget.client.encryption!.bootstrap(onUpdate: (_) => setState(() {})); + if (widget.client.encryption != null) { + WidgetsBinding.instance.addPostFrameCallback((_) async { + bootstrap = widget.client.encryption!.bootstrap( + onUpdate: (_) { + if (mounted) { + setState(() {}); + } + }, + ); + }); + } else { + Logs().e( + 'TomBootstrapDialog::_loadingData(): encryption is null', + ); + WidgetsBinding.instance.addPostFrameCallback((_) async { + Navigator.pop(context); + await BootstrapDialog(client: widget.client).show(); + }); + } } Future _getRecoveryKeyState() async { - await widget.client.onSync.stream.first; - await widget.client.initCompleter?.future; + try { + await widget.client.onSync.stream.first; + await widget.client.initCompleter?.future; - // Display first login bootstrap if enabled - if (widget.client.encryption?.keyManager.enabled == true) { - Logs().d( - 'TomBootstrapDialog::_initializeRecoveryKeyState: Showing bootstrap dialog when encryption is enabled', - ); - if (await widget.client.encryption?.keyManager.isCached() == false || - await widget.client.encryption?.crossSigning.isCached() == false || - widget.client.isUnknownSession && mounted) { + // Display first login bootstrap if enabled + if (widget.client.encryption?.keyManager.enabled == true) { + Logs().d( + 'TomBootstrapDialog::_initializeRecoveryKeyState: Showing bootstrap dialog when encryption is enabled', + ); + if (await widget.client.encryption?.keyManager.isCached() == false || + await widget.client.encryption?.crossSigning.isCached() == false || + widget.client.isUnknownSession && mounted) { + final recoveryWords = await _getRecoveryWords(); + if (recoveryWords != null) { + setState(() { + _recoveryWords = recoveryWords; + _uploadRecoveryKeyState = UploadRecoveryKeyState.useExisting; + }); + } else { + Logs().d( + 'TomBootstrapDialog::_initializeRecoveryKeyState(): no recovery existed then call bootstrap', + ); + WidgetsBinding.instance.addPostFrameCallback((_) async { + Navigator.pop(context); + await BootstrapDialog(client: widget.client).show(); + }); + } + } + } else { + Logs().d( + 'TomBootstrapDialog::_initializeRecoveryKeyState(): encryption is not enabled', + ); final recoveryWords = await _getRecoveryWords(); + _wipe = recoveryWords != null; if (recoveryWords != null) { - _recoveryWords = recoveryWords; - _uploadRecoveryKeyState = UploadRecoveryKeyState.useExisting; + setState(() { + _uploadRecoveryKeyState = UploadRecoveryKeyState.wipeRecovery; + }); } else { - Logs().d( - 'TomBootstrapDialog::_initializeRecoveryKeyState(): no recovery existed then call bootstrap', - ); - Navigator.pop(context); - await BootstrapDialog(client: widget.client).show(); + setState(() { + _uploadRecoveryKeyState = UploadRecoveryKeyState.initial; + }); } } - } else { - Logs().d( - 'TomBootstrapDialog::_initializeRecoveryKeyState(): encryption is not enabled', + } catch (e) { + Logs().e( + 'TomBootstrapDialog::_initializeRecoveryKeyState(): $e', ); - final recoveryWords = await _getRecoveryWords(); - _wipe = recoveryWords != null; - if (recoveryWords != null) { - _uploadRecoveryKeyState = UploadRecoveryKeyState.wipeRecovery; - } else { - _uploadRecoveryKeyState = UploadRecoveryKeyState.initial; - } + WidgetsBinding.instance.addPostFrameCallback((_) async { + Navigator.pop(context); + await BootstrapDialog(client: widget.client).show(); + }); } - - setState(() {}); } bool get isDataLoadingState =>