diff --git a/lib/services/user_service.dart b/lib/services/user_service.dart index 868993600..91dd9413e 100644 --- a/lib/services/user_service.dart +++ b/lib/services/user_service.dart @@ -609,7 +609,7 @@ class UserService { } } - Future setupTwoFactor(BuildContext context) async { + Future setupTwoFactor(BuildContext context, Completer completer) async { final dialog = createProgressDialog(context, "Please wait..."); await dialog.show(); try { @@ -621,12 +621,14 @@ class UserService { TwoFactorSetupPage( response.data["secretCode"], response.data["qrCode"], + completer, ), ), ); } catch (e) { await dialog.hide(); _logger.severe("Failed to setup tfa", e); + completer.complete(); rethrow; } } diff --git a/lib/ui/account/two_factor_setup_page.dart b/lib/ui/account/two_factor_setup_page.dart index f1b2b4491..14c43247d 100644 --- a/lib/ui/account/two_factor_setup_page.dart +++ b/lib/ui/account/two_factor_setup_page.dart @@ -1,5 +1,6 @@ // @dart=2.9 +import 'dart:async'; import 'dart:ui'; import 'package:flutter/material.dart'; @@ -17,9 +18,14 @@ import 'package:pinput/pin_put/pin_put.dart'; class TwoFactorSetupPage extends StatefulWidget { final String secretCode; final String qrCode; + final Completer completer; - const TwoFactorSetupPage(this.secretCode, this.qrCode, {Key key}) - : super(key: key); + const TwoFactorSetupPage( + this.secretCode, + this.qrCode, + this.completer, { + Key key, + }) : super(key: key); @override State createState() => _TwoFactorSetupPageState(); @@ -260,6 +266,7 @@ class _TwoFactorSetupPageState extends State .enableTwoFactor(context, widget.secretCode, code); if (success) { _showSuccessPage(); + widget.completer.complete(); } } diff --git a/lib/ui/components/toggle_switch_widget.dart b/lib/ui/components/toggle_switch_widget.dart index 24512ff96..54845dcbc 100644 --- a/lib/ui/components/toggle_switch_widget.dart +++ b/lib/ui/components/toggle_switch_widget.dart @@ -14,6 +14,8 @@ typedef FutureValueCallBack = Future Function(); class ToggleSwitchWidget extends StatefulWidget { final FutureValueCallBack value; + + ///Make sure to use completer if onChanged callback has other async functions inside final OnChangedCallBack onChanged; final bool initialValue; const ToggleSwitchWidget({ @@ -32,6 +34,7 @@ class _ToggleSwitchWidgetState extends State { late bool toggleValue; ExecutionState executionState = ExecutionState.idle; final _debouncer = Debouncer(const Duration(milliseconds: 300)); + @override void initState() { futureToggleValue = widget.value.call(); @@ -90,10 +93,6 @@ class _ToggleSwitchWidgetState extends State { widget.value.call().then((newValue) { setState(() { - //if onchanged on toggle is successful - // print('here'); - // print(toggleValue); - // print("newValue : $newValue"); if (toggleValue == newValue) { if (executionState == ExecutionState.inProgress) { executionState = ExecutionState.successful; diff --git a/lib/ui/settings/security_section_widget.dart b/lib/ui/settings/security_section_widget.dart index 35b25fc2c..5c28a9163 100644 --- a/lib/ui/settings/security_section_widget.dart +++ b/lib/ui/settings/security_section_widget.dart @@ -58,6 +58,7 @@ class _SecuritySectionWidgetState extends State { } Widget _getSectionOptions(BuildContext context) { + final Completer completer = Completer(); final List children = []; if (_config.hasConfiguredAccount()) { children.addAll( @@ -69,7 +70,7 @@ class _SecuritySectionWidgetState extends State { ), trailingSwitch: ToggleSwitchWidget( value: () => UserService.instance.fetchTwoFactorStatus(), - initialValue: false, + initialValue: _config.hasEnabledTwoFactor(), onChanged: () async { final hasAuthenticated = await LocalAuthenticationService .instance @@ -81,10 +82,12 @@ class _SecuritySectionWidgetState extends State { await UserService.instance.fetchTwoFactorStatus(); if (hasAuthenticated) { if (isTwoFactorEnabled) { - _disableTwoFactor(); + _disableTwoFactor(completer); } else { - await UserService.instance.setupTwoFactor(context); + await UserService.instance + .setupTwoFactor(context, completer); } + return completer.future; } }, ), @@ -165,7 +168,7 @@ class _SecuritySectionWidgetState extends State { ); } - void _disableTwoFactor() { + void _disableTwoFactor(Completer completer) { final AlertDialog alert = AlertDialog( title: const Text("Disable two-factor"), content: const Text( @@ -181,6 +184,7 @@ class _SecuritySectionWidgetState extends State { ), onPressed: () { Navigator.of(context, rootNavigator: true).pop('dialog'); + completer.complete(); }, ), TextButton( @@ -193,6 +197,7 @@ class _SecuritySectionWidgetState extends State { onPressed: () async { await UserService.instance.disableTwoFactor(context); Navigator.of(context, rootNavigator: true).pop('dialog'); + completer.complete(); }, ), ],