diff --git a/frontend/pshared/lib/utils/l10n/chain.dart b/frontend/pshared/lib/utils/l10n/chain.dart index b7ca083..08244b9 100644 --- a/frontend/pshared/lib/utils/l10n/chain.dart +++ b/frontend/pshared/lib/utils/l10n/chain.dart @@ -1,8 +1,10 @@ import 'package:flutter/widgets.dart'; -import 'package:pshared/generated/i18n/ps_localizations.dart'; import 'package:pshared/models/payment/chain_network.dart'; +import 'package:pshared/generated/i18n/ps_localizations.dart'; + + /// Localized labels for [ChainNetwork] values. extension ChainNetworkL10n on ChainNetwork { /// Returns a human-readable, localized name for the chain. diff --git a/frontend/pweb/lib/models/flow_status.dart b/frontend/pweb/lib/models/flow_status.dart new file mode 100644 index 0000000..2da3920 --- /dev/null +++ b/frontend/pweb/lib/models/flow_status.dart @@ -0,0 +1 @@ +enum FlowStatus { idle, submitting, resending, success, error } \ No newline at end of file diff --git a/frontend/pweb/lib/providers/two_factor.dart b/frontend/pweb/lib/providers/two_factor.dart index 33f8612..85cefca 100644 --- a/frontend/pweb/lib/providers/two_factor.dart +++ b/frontend/pweb/lib/providers/two_factor.dart @@ -8,6 +8,8 @@ import 'package:pshared/models/auth/pending_login.dart'; import 'package:pshared/provider/account.dart'; import 'package:pshared/service/verification.dart'; +import 'package:pweb/models/flow_status.dart'; + class TwoFactorProvider extends ChangeNotifier { static final _logger = Logger('provider.two_factor'); @@ -15,19 +17,17 @@ class TwoFactorProvider extends ChangeNotifier { TwoFactorProvider(); - bool _isSubmitting = false; - bool _isResending = false; - bool _hasError = false; - bool _verificationSuccess = false; + FlowStatus _status = FlowStatus.idle; String? _errorMessage; String? _currentPendingToken; Timer? _cooldownTimer; int _cooldownRemainingSeconds = 0; - bool get isSubmitting => _isSubmitting; - bool get isResending => _isResending; - bool get hasError => _hasError; - bool get verificationSuccess => _verificationSuccess; + FlowStatus get status => _status; + bool get isSubmitting => _status == FlowStatus.submitting; + bool get isResending => _status == FlowStatus.resending; + bool get hasError => _status == FlowStatus.error; + bool get verificationSuccess => _status == FlowStatus.success; String? get errorMessage => _errorMessage; int get cooldownRemainingSeconds => _cooldownRemainingSeconds; bool get isCooldownActive => _cooldownRemainingSeconds > 0; @@ -45,11 +45,8 @@ class TwoFactorProvider extends ChangeNotifier { } Future submitCode(String code) async { - _isSubmitting = true; - _hasError = false; _errorMessage = null; - _verificationSuccess = false; - notifyListeners(); + _setStatus(FlowStatus.submitting); try { final pending = _accountProvider.pendingLogin; @@ -61,15 +58,12 @@ class TwoFactorProvider extends ChangeNotifier { code: code, ); _accountProvider.completePendingLogin(account); - _verificationSuccess = true; _currentPendingToken = null; + _setStatus(FlowStatus.success); } catch (e) { - _hasError = true; _errorMessage = e.toString(); _logger.warning('Failed to verify code: ${e.toString()}', e); - } finally { - _isSubmitting = false; - notifyListeners(); + _setStatus(FlowStatus.error); } } @@ -79,24 +73,20 @@ class TwoFactorProvider extends ChangeNotifier { _logger.warning('No pending login to resend code for'); return; } - if (_isResending || isCooldownActive) return; + if (isResending || isCooldownActive) return; - _isResending = true; - _hasError = false; _errorMessage = null; - notifyListeners(); + _setStatus(FlowStatus.resending); try { final confirmation = await VerificationService.resendLoginCode(pending); _accountProvider.updatePendingLogin(confirmation); _startCooldown(confirmation.cooldownSeconds); + _setStatus(FlowStatus.idle); } catch (e) { _logger.warning('Failed to resend login code', e); - _hasError = true; _errorMessage = e.toString(); - } finally { - _isResending = false; - notifyListeners(); + _setStatus(FlowStatus.error); } } @@ -106,11 +96,8 @@ class TwoFactorProvider extends ChangeNotifier { } void _resetState() { - _isSubmitting = false; - _isResending = false; - _hasError = false; + _status = FlowStatus.idle; _errorMessage = null; - _verificationSuccess = false; _stopCooldown(); notifyListeners(); } @@ -169,6 +156,11 @@ class TwoFactorProvider extends ChangeNotifier { } } + void _setStatus(FlowStatus status) { + _status = status; + notifyListeners(); + } + @override void dispose() { _stopCooldown();