recordFailedAttempt method

Future<LivenessDetectionCooldown> recordFailedAttempt()

Implementation

Future<LivenessDetectionCooldown> recordFailedAttempt() async {
  final currentState = await getCooldownState();

  if (currentState.isInCooldown) {
    return currentState;
  }

  final newFailedAttempts = currentState.failedAttempts + 1;

  LivenessDetectionCooldown newState;

  if (newFailedAttempts >= _maxFailedAttempts) {
    // Start cooldown
    final cooldownEndTime = DateTime.now().add(
      Duration(minutes: _cooldownMinutes),
    );

    newState = LivenessDetectionCooldown(
      failedAttempts: newFailedAttempts,
      cooldownEndTime: cooldownEndTime,
      isInCooldown: true,
    );

    _startCooldownTimer(newState);
  } else {
    newState = LivenessDetectionCooldown(
      failedAttempts: newFailedAttempts,
      cooldownEndTime: null,
      isInCooldown: false,
    );
  }

  await _saveCooldownState(newState);
  _cooldownController.add(newState);
  return newState;
}