Skip to content

Commit

Permalink
Handle error for _getRecoveryWords
Browse files Browse the repository at this point in the history
  • Loading branch information
nqhhdev committed Jun 7, 2024
1 parent 90994a1 commit f57dec1
Showing 1 changed file with 76 additions and 33 deletions.
109 changes: 76 additions & 33 deletions lib/pages/bootstrap/tom_bootstrap_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,18 @@ class TomBootstrapDialogState extends State<TomBootstrapDialog>
Future<RecoveryWords?> _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;
},
),
);
}
Expand All @@ -86,48 +96,81 @@ class TomBootstrapDialogState extends State<TomBootstrapDialog>
_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<void> _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 =>
Expand Down

0 comments on commit f57dec1

Please sign in to comment.