Got rid of bools in 2fa provider #144

Merged
tech merged 1 commits from SEND014 into main 2025-12-24 16:07:08 +00:00
3 changed files with 25 additions and 30 deletions
Showing only changes of commit 512f25f74f - Show all commits

View File

@@ -1,8 +1,10 @@
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
import 'package:pshared/generated/i18n/ps_localizations.dart';
import 'package:pshared/models/payment/chain_network.dart'; import 'package:pshared/models/payment/chain_network.dart';
import 'package:pshared/generated/i18n/ps_localizations.dart';
/// Localized labels for [ChainNetwork] values. /// Localized labels for [ChainNetwork] values.
extension ChainNetworkL10n on ChainNetwork { extension ChainNetworkL10n on ChainNetwork {
/// Returns a human-readable, localized name for the chain. /// Returns a human-readable, localized name for the chain.

View File

@@ -0,0 +1 @@
enum FlowStatus { idle, submitting, resending, success, error }

View File

@@ -8,6 +8,8 @@ import 'package:pshared/models/auth/pending_login.dart';
import 'package:pshared/provider/account.dart'; import 'package:pshared/provider/account.dart';
import 'package:pshared/service/verification.dart'; import 'package:pshared/service/verification.dart';
import 'package:pweb/models/flow_status.dart';
class TwoFactorProvider extends ChangeNotifier { class TwoFactorProvider extends ChangeNotifier {
static final _logger = Logger('provider.two_factor'); static final _logger = Logger('provider.two_factor');
@@ -15,19 +17,17 @@ class TwoFactorProvider extends ChangeNotifier {
TwoFactorProvider(); TwoFactorProvider();
bool _isSubmitting = false; FlowStatus _status = FlowStatus.idle;
bool _isResending = false;
bool _hasError = false;
bool _verificationSuccess = false;
String? _errorMessage; String? _errorMessage;
String? _currentPendingToken; String? _currentPendingToken;
Timer? _cooldownTimer; Timer? _cooldownTimer;
int _cooldownRemainingSeconds = 0; int _cooldownRemainingSeconds = 0;
bool get isSubmitting => _isSubmitting; FlowStatus get status => _status;
bool get isResending => _isResending; bool get isSubmitting => _status == FlowStatus.submitting;
bool get hasError => _hasError; bool get isResending => _status == FlowStatus.resending;
bool get verificationSuccess => _verificationSuccess; bool get hasError => _status == FlowStatus.error;
bool get verificationSuccess => _status == FlowStatus.success;
String? get errorMessage => _errorMessage; String? get errorMessage => _errorMessage;
int get cooldownRemainingSeconds => _cooldownRemainingSeconds; int get cooldownRemainingSeconds => _cooldownRemainingSeconds;
bool get isCooldownActive => _cooldownRemainingSeconds > 0; bool get isCooldownActive => _cooldownRemainingSeconds > 0;
@@ -45,11 +45,8 @@ class TwoFactorProvider extends ChangeNotifier {
} }
Future<void> submitCode(String code) async { Future<void> submitCode(String code) async {
_isSubmitting = true;
_hasError = false;
_errorMessage = null; _errorMessage = null;
_verificationSuccess = false; _setStatus(FlowStatus.submitting);
notifyListeners();
try { try {
final pending = _accountProvider.pendingLogin; final pending = _accountProvider.pendingLogin;
@@ -61,15 +58,12 @@ class TwoFactorProvider extends ChangeNotifier {
code: code, code: code,
); );
_accountProvider.completePendingLogin(account); _accountProvider.completePendingLogin(account);
_verificationSuccess = true;
_currentPendingToken = null; _currentPendingToken = null;
_setStatus(FlowStatus.success);
} catch (e) { } catch (e) {
_hasError = true;
_errorMessage = e.toString(); _errorMessage = e.toString();
_logger.warning('Failed to verify code: ${e.toString()}', e); _logger.warning('Failed to verify code: ${e.toString()}', e);
} finally { _setStatus(FlowStatus.error);
_isSubmitting = false;
notifyListeners();
} }
} }
@@ -79,24 +73,20 @@ class TwoFactorProvider extends ChangeNotifier {
_logger.warning('No pending login to resend code for'); _logger.warning('No pending login to resend code for');
return; return;
} }
if (_isResending || isCooldownActive) return; if (isResending || isCooldownActive) return;
_isResending = true;
_hasError = false;
_errorMessage = null; _errorMessage = null;
notifyListeners(); _setStatus(FlowStatus.resending);
try { try {
final confirmation = await VerificationService.resendLoginCode(pending); final confirmation = await VerificationService.resendLoginCode(pending);
_accountProvider.updatePendingLogin(confirmation); _accountProvider.updatePendingLogin(confirmation);
_startCooldown(confirmation.cooldownSeconds); _startCooldown(confirmation.cooldownSeconds);
_setStatus(FlowStatus.idle);
} catch (e) { } catch (e) {
_logger.warning('Failed to resend login code', e); _logger.warning('Failed to resend login code', e);
_hasError = true;
_errorMessage = e.toString(); _errorMessage = e.toString();
} finally { _setStatus(FlowStatus.error);
_isResending = false;
notifyListeners();
} }
} }
@@ -106,11 +96,8 @@ class TwoFactorProvider extends ChangeNotifier {
} }
void _resetState() { void _resetState() {
_isSubmitting = false; _status = FlowStatus.idle;
_isResending = false;
_hasError = false;
_errorMessage = null; _errorMessage = null;
_verificationSuccess = false;
_stopCooldown(); _stopCooldown();
notifyListeners(); notifyListeners();
} }
@@ -169,6 +156,11 @@ class TwoFactorProvider extends ChangeNotifier {
} }
} }
void _setStatus(FlowStatus status) {
_status = status;
notifyListeners();
}
@override @override
void dispose() { void dispose() {
_stopCooldown(); _stopCooldown();