fix(agenda): one slot generator for professional and patient

The two calendars each built their own list of hours. The patient's
enforced a 3-hour booking lead time; the professional's did not, so at
15:00 an 08:00-18:00 agenda reported "3 libres" (15, 16, 17) that no
patient could actually take.

Both now go through SlotGenerator. The professional still sees the
near-term hours (blocking the next hour is legitimate) but they are
labelled "Sin reserva" and excluded from the "libres" count, so the
number on his agenda means what the patient sees.

Also in this pass:

- endOf() clamps the slot end at 23:59; a 120-minute slot booked at
  23:00 was sending "25:00" to the backend.
- ScheduleEntity.copyWith can set an hour back to null, so returning a
  day to jornada continua no longer keeps the split hours around.
- Removed the Firebase-era schedule map ('habilitado', 'range1Hour1')
  together with the dead ProfessionalEntity.fromDocument that fed it.
- Settings loads guard on mounted and swallow failures instead of
  calling setState after dispose.
- "Pedir cita" says what is missing instead of doing nothing.

Tests: 9 passing, including the clamp and the empty/inverted ranges.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Lizandro Guarnizo
2026-08-25 13:17:03 -05:00
co-authored by Claude Opus 5
parent 3327220557
commit 0a8e5d11a2
15 changed files with 585 additions and 266 deletions
@@ -1,7 +1,9 @@
import 'package:flutter/material.dart';
import 'package:injector/injector.dart';
import 'package:professional_repository/professional_repository.dart';
import 'package:prosappco/components/general_primary_button.dart';
import 'package:prosappco/screens/professional/components/schedule_item.dart';
import 'package:prosappco/utils/schedule_validation.dart';
class ProfessionalScheduleScreen extends StatefulWidget {
Schedules schedules;
@@ -17,16 +19,85 @@ class ProfessionalScheduleScreen extends StatefulWidget {
class _ProfessionalScheduleScreenState
extends State<ProfessionalScheduleScreen> {
final _repo = Injector.appInstance.get<ApiProfessionalRepository>();
Schedules schedules = Schedules.empty;
bool _saving = false;
bool _dirty = false;
@override
void initState() {
super.initState();
schedules = widget.schedules;
}
void _update(Schedules next) {
setState(() {
schedules = next;
_dirty = true;
});
}
/// Saving now writes to the backend instead of just handing the object back:
/// the old flow depended on the profile screen being saved afterwards, so
/// leaving without that second save silently discarded everything.
Future<void> _save() async {
final problems = validateSchedules(schedules);
if (problems.isNotEmpty) {
ScaffoldMessenger.of(context).clearSnackBars();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(problems.first)),
);
return;
}
setState(() => _saving = true);
try {
await _repo.updateSchedules(schedules);
if (!mounted) return;
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Horario guardado')),
);
Navigator.of(context).pop(schedules);
} catch (e) {
if (!mounted) return;
setState(() => _saving = false);
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text(
'No se pudo guardar el horario. Revisa tu conexión e inténtalo de nuevo.')),
);
}
}
Future<bool> _confirmLeave() async {
if (!_dirty || _saving) return true;
final leave = await showDialog<bool>(
context: context,
builder: (ctx) => AlertDialog(
title: const Text('¿Salir sin guardar?'),
content: const Text('Perderás los cambios que hiciste en tu horario.'),
actions: [
TextButton(
onPressed: () => Navigator.pop(ctx, false),
child: const Text('Seguir editando')),
TextButton(
onPressed: () => Navigator.pop(ctx, true),
child: const Text('Salir sin guardar')),
],
),
);
return leave ?? false;
}
@override
Widget build(BuildContext context) {
return Scaffold(
return PopScope(
canPop: false,
onPopInvoked: (didPop) async {
if (didPop) return;
if (await _confirmLeave() && mounted) Navigator.of(context).pop();
},
child: Scaffold(
appBar: AppBar(
title: const Text('Horario'),
),
@@ -39,63 +110,62 @@ class _ProfessionalScheduleScreenState
label: "Lunes",
schedule: schedules.monday,
onChanged: (s) {
setState(() => schedules = schedules.copyWith(monday: s));
_update(schedules.copyWith(monday: s));
},
),
ScheduleItem(
label: "Martes",
schedule: schedules.tuesday,
onChanged: (s) {
setState(() => schedules = schedules.copyWith(tuesday: s));
_update(schedules.copyWith(tuesday: s));
},
),
ScheduleItem(
label: "Miercoles",
schedule: schedules.wednesday,
onChanged: (s) {
setState(() => schedules = schedules.copyWith(wednesday: s));
_update(schedules.copyWith(wednesday: s));
},
),
ScheduleItem(
label: "Jueves",
schedule: schedules.thursday,
onChanged: (s) {
setState(() => schedules = schedules.copyWith(thursday: s));
_update(schedules.copyWith(thursday: s));
},
),
ScheduleItem(
label: "Viernes",
schedule: schedules.friday,
onChanged: (s) {
setState(() => schedules = schedules.copyWith(friday: s));
_update(schedules.copyWith(friday: s));
},
),
ScheduleItem(
label: "Sabado",
schedule: schedules.saturday,
onChanged: (s) {
setState(() => schedules = schedules.copyWith(saturday: s));
_update(schedules.copyWith(saturday: s));
},
),
ScheduleItem(
label: "Domingo",
schedule: schedules.sunday,
onChanged: (s) {
setState(() => schedules = schedules.copyWith(sunday: s));
_update(schedules.copyWith(sunday: s));
},
),
const SizedBox(height: 20),
GeneralPrimaryButton(
label: "Guardar",
onPressed: () {
Navigator.of(context).pop(schedules);
},
label: _saving ? "Guardando…" : "Guardar",
onPressed: _saving ? () {} : _save,
),
const SizedBox(height: 20),
],
),
),
),
),
);
}
}