fix for resend, cooldown and a few small fixes
This commit is contained in:
@@ -22,6 +22,7 @@ class TwoFactorProvider extends ChangeNotifier {
|
||||
String? _currentPendingToken;
|
||||
Timer? _cooldownTimer;
|
||||
int _cooldownRemainingSeconds = 0;
|
||||
DateTime? _cooldownUntil;
|
||||
|
||||
FlowStatus get status => _status;
|
||||
bool get isSubmitting => _status == FlowStatus.submitting;
|
||||
@@ -108,48 +109,69 @@ class TwoFactorProvider extends ChangeNotifier {
|
||||
return;
|
||||
}
|
||||
|
||||
final remaining = pending.cooldownRemainingSeconds;
|
||||
if (remaining <= 0) {
|
||||
final until = pending.cooldownUntil;
|
||||
if (until == null) {
|
||||
_stopCooldown(notify: _cooldownRemainingSeconds != 0);
|
||||
return;
|
||||
}
|
||||
|
||||
if (_cooldownRemainingSeconds != remaining) {
|
||||
_startCooldown(remaining);
|
||||
if (!_isCooldownActive(until) && _cooldownRemainingSeconds != 0) {
|
||||
_stopCooldown(notify: true);
|
||||
return;
|
||||
}
|
||||
|
||||
if (_cooldownUntil == null || _cooldownUntil != until) {
|
||||
_startCooldownUntil(until);
|
||||
}
|
||||
}
|
||||
|
||||
void _startCooldown(int seconds) {
|
||||
final until = DateTime.now().add(Duration(seconds: seconds));
|
||||
_startCooldownUntil(until);
|
||||
}
|
||||
|
||||
void _startCooldownUntil(DateTime until) {
|
||||
_cooldownTimer?.cancel();
|
||||
_cooldownRemainingSeconds = seconds;
|
||||
_cooldownUntil = until;
|
||||
_cooldownRemainingSeconds = _cooldownRemaining();
|
||||
|
||||
if (_cooldownRemainingSeconds <= 0) {
|
||||
_cooldownTimer = null;
|
||||
_cooldownUntil = null;
|
||||
notifyListeners();
|
||||
return;
|
||||
}
|
||||
|
||||
_cooldownTimer = Timer.periodic(const Duration(seconds: 1), (timer) {
|
||||
if (_cooldownRemainingSeconds <= 1) {
|
||||
_cooldownRemainingSeconds = 0;
|
||||
_cooldownTimer?.cancel();
|
||||
_cooldownTimer = null;
|
||||
notifyListeners();
|
||||
final remaining = _cooldownRemaining();
|
||||
if (remaining <= 0) {
|
||||
_stopCooldown(notify: true);
|
||||
return;
|
||||
}
|
||||
|
||||
_cooldownRemainingSeconds -= 1;
|
||||
notifyListeners();
|
||||
if (remaining != _cooldownRemainingSeconds) {
|
||||
_cooldownRemainingSeconds = remaining;
|
||||
notifyListeners();
|
||||
}
|
||||
});
|
||||
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
bool _isCooldownActive(DateTime until) => until.isAfter(DateTime.now());
|
||||
|
||||
int _cooldownRemaining() {
|
||||
final until = _cooldownUntil;
|
||||
if (until == null) return 0;
|
||||
final remaining = until.difference(DateTime.now()).inSeconds;
|
||||
return remaining < 0 ? 0 : remaining;
|
||||
}
|
||||
|
||||
void _stopCooldown({bool notify = false}) {
|
||||
_cooldownTimer?.cancel();
|
||||
_cooldownTimer = null;
|
||||
final hadCooldown = _cooldownRemainingSeconds != 0;
|
||||
_cooldownRemainingSeconds = 0;
|
||||
_cooldownUntil = null;
|
||||
|
||||
if (notify && hadCooldown) {
|
||||
notifyListeners();
|
||||
|
||||
Reference in New Issue
Block a user