Files
prosappco/lib/screens/professional/components/schedule_item.dart
T
Lizandro GuarnizoandClaude Opus 5 0a8e5d11a2 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>
2026-08-25 13:17:03 -05:00

186 lines
5.5 KiB
Dart

import 'package:flutter/material.dart';
import 'package:professional_repository/professional_repository.dart';
class ScheduleItem extends StatelessWidget {
ScheduleEntity schedule;
String label;
final ValueChanged<ScheduleEntity> onChanged;
ScheduleItem(
{super.key,
required this.label,
required this.schedule,
required this.onChanged});
@override
Widget build(BuildContext context) {
return Column(
children: [
Row(
children: [
Expanded(
child: Text(
label,
style: const TextStyle(
fontSize: 17,
fontWeight: FontWeight.w500,
color: Colors.black,
),
),
),
Switch(
value: schedule.enabled,
onChanged: (value) {
// Turning a day on used to leave every hour empty, so the day
// read as "open" and offered zero slots. Seed a normal
// workday; the professional can adjust it right below.
if (value && schedule.range1Hour1 == null) {
onChanged.call(schedule.copyWith(
enabled: true,
continuousDay: true,
range1Hour1: const TimeOfDay(hour: 8, minute: 0),
range2Hour2: const TimeOfDay(hour: 18, minute: 0),
));
return;
}
onChanged.call(schedule.copyWith(enabled: value));
},
),
],
),
Visibility(
visible: schedule.enabled,
child: Row(
children: [
const Expanded(
child: Text(
'Jornada continua',
style: TextStyle(
fontSize: 17,
fontWeight: FontWeight.w500,
color: Colors.black,
),
),
),
Switch(
value: schedule.continuousDay,
onChanged: (value) {
// Same reasoning as the day switch: splitting the day
// reveals two new empty fields, and a half-filled day
// generates no slots at all.
if (!value && schedule.range1Hour2 == null) {
onChanged.call(schedule.copyWith(
continuousDay: false,
range1Hour2: const TimeOfDay(hour: 12, minute: 0),
range2Hour1: const TimeOfDay(hour: 14, minute: 0),
));
return;
}
onChanged.call(schedule.copyWith(continuousDay: value));
},
),
],
)),
Visibility(
visible: schedule.enabled,
child: hoursRangesBody(context),
),
const Divider(),
],
);
}
hoursRangesBody(BuildContext context) {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
timePickerField(
context: context,
text: ScheduleEntity.getFormatTime(schedule.range1Hour1),
onPick: (pickedTime) {
onChanged.call(schedule.copyWith(range1Hour1: pickedTime));
},
),
Center(
child: Container(
width: 10,
height: 1,
color: Colors.black,
),
),
Visibility(
visible: !schedule.continuousDay,
child: timePickerField(
context: context,
text: ScheduleEntity.getFormatTime(schedule.range1Hour2),
onPick: (pickedTime) {
onChanged.call(schedule.copyWith(range1Hour2: pickedTime));
},
),
),
Visibility(
visible: !schedule.continuousDay,
child: const SizedBox(
width: 10,
),
),
Visibility(
visible: !schedule.continuousDay,
child: timePickerField(
context: context,
text: ScheduleEntity.getFormatTime(schedule.range2Hour1),
onPick: (pickedTime) {
onChanged.call(schedule.copyWith(range2Hour1: pickedTime));
},
),
),
Visibility(
visible: !schedule.continuousDay,
child: Center(
child: Container(
width: 10,
height: 1,
color: Colors.black,
),
),
),
timePickerField(
context: context,
text: ScheduleEntity.getFormatTime(schedule.range2Hour2),
onPick: (pickedTime) {
onChanged.call(schedule.copyWith(range2Hour2: pickedTime));
},
),
],
);
}
timePickerField(
{required BuildContext context,
required String? text,
required Function(TimeOfDay) onPick}) {
return Expanded(
child: TextField(
readOnly: true,
controller: TextEditingController(
text: text,
),
textAlign: TextAlign.center,
onTap: () async {
final TimeOfDay? pickedTime = await showTimePicker(
context: context,
initialTime: TimeOfDay.now(),
);
if (pickedTime != null) {
onPick.call(pickedTime);
}
},
decoration: const InputDecoration(
hintText: 'Hora',
border: UnderlineInputBorder(),
),
),
);
}
}