deleteTime method

Future<void> deleteTime(
  1. BuildContext context
)

Elimina el tiempo actual de la base de datos.

Este método elimina un tiempo almacenado en la base de datos a partir de su scramble y idSession. Después de la eliminación, actualiza el estado global y se muestra un mensaje de éxito o error.

Características:

  • Se obtiene el tiempo actual de currentTime.
  • Busca el idDeleteTime a partir del scramble y idSession del tiempo actual.
  • Si se encuentra el idDeleteTime, intenta eliminar el tiempo de la base de datos.
  • Si la eliminación es exitosa, se actualiza el estado global y se pone en 0.0 el tiempo.
  • Si ocurre un error, se muestra un mensaje de error.

Implementation

Future<void> deleteTime(BuildContext context) async {
  if (_timeTraining == null) return;

  // OBTENER EL ID DEL TIEMPO A ELIMINAR
  int idDeleteTime = await _timeTrainingDaoSb.getIdByTime(
      timeTraining!.scramble, timeTraining!.idSession!);

  if (idDeleteTime == -1) {
    // SI OCURRIO UN ERROR, MUESTRA UN SNACKBAR
    AlertUtil.showSnackBarInformation(context, "delete_time_error");
    DatabaseHelper.logger
        .e("No se obtuvo el tiempo por scramble e idSession: $idDeleteTime");
    return;
  } // VERIFICAR SI SE HA OBTENIDO EL ID DEL TIEMPO

  try {
    // ELIMINAR EL TIEMPO
    final isDeleted = await _timeTrainingDaoSb.deleteTime(idDeleteTime);

    if (isDeleted) {
      // SI SE ELIMINO CORRECTAMENTE SE MUESTRA UN SNACKBAR
      AlertUtil.showSnackBarInformation(context, "delete_time_correct");
      // ACTUALIZAR EL TIEMPO ACTUAL EN NULO EN EL ESTADO GLOBAL
      setResetTimeTraining();
    } else {
      // SI OCURRIO UN ERROR MUESTRA UN SNACKBAR
      AlertUtil.showSnackBarInformation(context, "delete_time_error");
      DatabaseHelper.logger.e("No se pudo eliminar: $isDeleted");
    } // VERIFICAR SI SE HA ELIMINADO CORRECTAMENTE
  } catch (e) {
    // CAPTURAR CUALQUIER ERROR DURANTE LA ELIMINACION
    AlertUtil.showSnackBarInformation(context, "delete_time_error");
    DatabaseHelper.logger.e("Error al eliminar el tiempo: $e");
  }
  notifyListeners();
}