Overview
DisponibilidadCubit manages the state for searching available tables, viewing business hours, and creating reservations. It handles the entire customer-facing reservation flow including table search by zone, time slot selection, and SMS-verified reservation creation.
Location: lib/presentacion/disponibilidad/disponibilidad_cubit.dart:13
Purpose
This Cubit coordinates:- Loading restaurant data (business info, tables, operating hours)
- Searching for available tables in specific zones
- Managing time slot availability
- Creating SMS-verified reservations
- Caching business information for quick access
State Classes
The Cubit uses the following states defined indisponibilidad_estados_de_cubit.dart:
DisponibilidadInicial
Initial state before any data is loaded.DisponibilidadCargando
Emitted when loading data or searching for tables.DisponibilidadExitosa
Emitted when tables and business data are successfully loaded.mesasDisponibles: All tables in the restaurantnegocio: Business entity with configurationhorariosServicio: Map of day names to operating hours (e.g., “Lunes”: “12:00 - 23:00”)- Computed properties provide business rules configuration
MesaEncontrada
Emitted when an available table is found in the requested zone.mesa: The available table foundzona: Zone name where the table is locatedduracionPromedioMinutos: Reservation duration from business config
ProcesandoReserva
Emitted while creating the reservation after SMS verification.ReservaCreada
Emitted when reservation is successfully created.DisponibilidadConError
Emitted when any error occurs.Constructor
DisponibilidadInicial.
Key Methods
cargarTodasLasMesas
Loads all tables, business info, and operating hours for a restaurant.negocioId(optional): Restaurant ID. If null, loads the first available restaurant.
DisponibilidadCargando→ Loading dataDisponibilidadExitosa→ Success with dataDisponibilidadConError→ If error occurs
disponibilidad_cubit.dart:33
Implementation Details:
- Loads business, tables, and hours in parallel using
Future.wait - Caches business info in
_negocioActual - Converts operating hours to display format
buscarMesaEnZona
Searches for an available table in a specific zone at the requested time.zona: Zone name to search in (e.g., “Terraza”, “Salón”)fecha: Reservation datehora: Reservation timenumeroPersonas: Party size
DisponibilidadCargando→ SearchingMesaEncontrada→ Table found with zone and duration infoDisponibilidadConError→ No tables available with helpful message
disponibilidad_cubit.dart:96
crearReservaVerificadaPorSMS
Creates a confirmed reservation for a customer who has verified their phone via SMS.emailCliente: Customer email for confirmationtelefonoVerificado: Phone number verified via SMSnombreCliente: Customer name (optional)mesaId: Table ID to reservefecha: Reservation datehora: Reservation timenumeroPersonas: Party size
ProcesandoReserva→ ProcessingReservaCreada→ Success with confirmation messageDisponibilidadConError→ If creation fails
EstadoReserva.confirmada status since SMS verification is already complete. The CrearReserva use case handles:
- Business rule validation
- Firestore persistence
- Email notifications to customer and owner
disponibilidad_cubit.dart:156
obtenerZonasDisponibles
Retrieves available zones in the restaurant.disponibilidad_cubit.dart:85
obtenerIntervalosHorarioNegocio
Gets available time slots for a specific date based on operating hours.fecha: Date to get time slots for
disponibilidad_cubit.dart:138
Properties
negocioActual
Cached business entity for quick access.cargarTodasLasMesas is called.
State Transition Diagram
Usage Example
Fromdisponibilidad_screen.dart:22:
Integration Points
With Repositories
MesaRepositorio: Table queries and availability checksNegocioRepositorio: Business information and configurationHorarioAperturaRepositorio: Operating hours and time slots
With Use Cases
CrearReserva: Reservation creation with business rules and notifications
With UI Components
DisponibilidadScreen: Main customer reservation interface- Zone selector displays zones from
obtenerZonasDisponibles() - Time picker shows slots from
obtenerIntervalosHorarioNegocio() - SMS verification dialog triggers
crearReservaVerificadaPorSMS()
Error Handling
Errors are captured and emitted asDisponibilidadConError with user-friendly messages:
- No tables available: Suggests trying different zone/time
- Closed hours: Shows operating hours
- Network errors: Generic error message
State Management Pattern
This Cubit follows these patterns:- Loading States: Always emit loading state before async operations
- Error Recovery: Errors don’t crash the app; emit error states
- Data Caching: Business info cached to avoid redundant loads
- Immutable States: Each state is a new instance
- Single Responsibility: Each method handles one specific operation
Related Documentation
- MisReservasCubit - Managing customer reservations
- PantallaDuenoCubit - Restaurant owner dashboard
- Mesa Entity - Table entity
- Negocio Entity - Business entity
- CrearReserva Use Case - Reservation creation logic