fix: port 7 web features and repair the endless-loading screens

Root cause behind most "stuck loading" reports: the backend changed shape
(schedules became an array, location_preferences a string) while the mobile
parser still hard-cast to Map/int. The TypeError was swallowed by a silent
catch that returned null, and screens only handled the success state, so a
parse failure rendered as a permanent spinner. Same class of bug appeared
across service lists via non-null map lookups and a total absence of
request timeouts.

Ported from prosappweb:
- in-app suggestions (POST /suggestions)
- policies/terms from GET /settings/policies
- configurable appointment length (slot_duration_minutes)
- block/unblock calendar slots (POST /services/block)
- GPS city detection on the profile (Nominatim)
- server-side professional search with haversine distance
- retry cooldown after a rejected professional application

Reliability:
- parse schedules array (day_of_week 0=Mon) and string location_preferences
- read times as wall clock, so 08:00 stays 08:00 across timezones
- carry minutes into hours in TimeOfDay.add; a minute-based step used to
  loop forever and freeze the calendar (covered by test/time_slots_test.dart)
- semver update check instead of string equality, which blocked every build
  that did not exactly match the configured version
- request timeouts across all repositories
- surface HTTP >= 400 instead of reporting failed writes as success
- error states with retry instead of an indefinite shimmer

Includes pre-existing uncommitted work from the UI redesign.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Lizandro Guarnizo
2026-08-24 16:45:42 -05:00
co-authored by Claude Opus 5
parent 06a89df690
commit 8631e6f729
86 changed files with 5470 additions and 5291 deletions
@@ -6,6 +6,7 @@ import 'package:intl/intl.dart';
import 'package:professional_repository/professional_repository.dart';
import 'package:prosappco/blocs/service_bloc/service_bloc.dart';
import 'package:prosappco/screens/service/professional_service_screen.dart';
import 'package:prosappco/utils/time_of_day_extension.dart';
import 'package:prosappco/utils/time_of_day_utils.dart';
import 'package:service_repository/service_repository.dart';
import 'package:table_calendar/table_calendar.dart';
@@ -44,6 +45,7 @@ class _ProfessionalCalendarScreenState
List<ServiceEntity>? _services;
CalendarFormat _calendarFormat = CalendarFormat.month;
bool isLoading = false;
bool _loadFailed = false;
@override
void initState() {
@@ -54,9 +56,17 @@ class _ProfessionalCalendarScreenState
}
void _loadServices() {
setState(() => _loadFailed = false);
serviceRepository
.getServicesForProfessionalforCalendar(widget.userProfessional.id)
.then((services) => setState(() => _services = services));
.then((services) {
if (!mounted) return;
setState(() => _services = services);
}).catchError((e) {
if (!mounted) return;
// Never fall back to an empty list: booked slots would render as free.
setState(() => _loadFailed = true);
});
}
void _onDaySelected(DateTime day, DateTime focusedDay) {
@@ -87,18 +97,33 @@ class _ProfessionalCalendarScreenState
child: BlocConsumer<ServiceBloc, ServiceState>(
listener: (context, state) {
if (state is CreateServiceLoading) isLoading = true;
if (state is CreateServiceFailure) isLoading = false;
if (state is CreateServiceFailure) {
isLoading = false;
ScaffoldMessenger.of(context).clearSnackBars();
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('No se pudo completar la acción')),
);
}
if (state is CreateServiceSuccess || state is ServiceStatusUpdated) {
isLoading = false;
_loadServices();
}
},
builder: (context, state) {
return ListView(
padding: const EdgeInsets.only(bottom: 32),
children: [
_calendarCard(context),
_dayHeader(context, schedule, slots.length, occupied, available),
if (slots.isEmpty)
_emptyState(context)
else
..._slotCards(context, slots, state),
if (_loadFailed)
_loadErrorState(context)
else ...[
_dayHeader(
context, schedule, slots.length, occupied, available),
if (slots.isEmpty)
_emptyState(context)
else
..._slotCards(context, slots, state),
],
],
);
},
@@ -337,9 +362,7 @@ class _ProfessionalCalendarScreenState
for (final event in _services!) {
if (today.toString() == event.day && time == event.range1Hour1) {
if (event.userId == event.professionalId) {
ScaffoldMessenger.of(context).clearSnackBars();
ScaffoldMessenger.of(context)
.showSnackBar(const SnackBar(content: Text('Horario ocupado por ti')));
_confirmUnblock(event.id!, time);
} else {
Navigator.push(
context,
@@ -353,6 +376,35 @@ class _ProfessionalCalendarScreenState
}
}
void _confirmUnblock(String serviceId, TimeOfDay time) {
showDialog(
context: context,
builder: (dialogContext) => AlertDialog(
title: const Text('Desbloquear horario'),
content: Text(
'¿Quieres liberar el horario de las '
'${ScheduleEntity.getFormatTime(time)} '
'del ${DateFormat('dd-MM-yyyy').format(today)}?',
),
actions: [
TextButton(
onPressed: () => Navigator.pop(dialogContext),
child: const Text('Cancelar'),
),
TextButton(
onPressed: () {
Navigator.pop(dialogContext);
context
.read<ServiceBloc>()
.add(UpdateServiceStatus(serviceId, ServiceStatus.cancelled));
},
child: const Text('Desbloquear'),
),
],
),
);
}
void _onAvailable(BuildContext context, TimeOfDay time, ServiceState state) {
showDialog(
context: context,
@@ -387,7 +439,8 @@ class _ProfessionalCalendarScreenState
createdAt: DateTime.now().toIso8601String(),
description: '',
range1Hour1: time,
range1Hour2: time.replacing(hour: time.hour + 2),
range1Hour2: time.add(
minute: widget.userProfessional.slotDurationMinutes),
rate: '0',
location: ServiceLocationPreferences.office,
status: ServiceStatus.selfBooked,
@@ -395,11 +448,61 @@ class _ProfessionalCalendarScreenState
},
child: const Text('Reservar'),
),
TextButton(
onPressed: () {
Navigator.pop(dialogContext);
context.read<ServiceBloc>().add(
BlockSlot(day: today.toString(), hour1: time),
);
},
child: const Text('Bloquear horario'),
),
],
),
);
}
Widget _loadErrorState(BuildContext context) {
return Container(
margin: const EdgeInsets.fromLTRB(16, 12, 16, 0),
padding: const EdgeInsets.symmetric(vertical: 40, horizontal: 24),
decoration: BoxDecoration(
color: context.card,
borderRadius: BorderRadius.circular(16),
boxShadow: [
BoxShadow(
color: context.shadowSm,
blurRadius: 12,
offset: const Offset(0, 2))
],
),
child: Column(children: [
Container(
width: 64,
height: 64,
decoration: BoxDecoration(
color: _kOccupied.withOpacity(0.1), shape: BoxShape.circle),
child: const Icon(Icons.wifi_off_outlined,
size: 32, color: _kOccupied),
),
const SizedBox(height: 16),
Text('No se pudo cargar tu agenda',
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.w700,
color: context.muted)),
const SizedBox(height: 6),
Text(
'No mostramos horarios para evitar que reserves\nsobre una cita ya agendada.',
textAlign: TextAlign.center,
style: TextStyle(fontSize: 13, color: context.subtle, height: 1.5)),
const SizedBox(height: 16),
OutlinedButton(
onPressed: _loadServices, child: const Text('Reintentar')),
]),
);
}
Widget _emptyState(BuildContext context) {
return Container(
margin: const EdgeInsets.fromLTRB(16, 12, 16, 0),
@@ -451,13 +554,17 @@ class _ProfessionalCalendarScreenState
if (s == null || !s.enabled || s.range1Hour1 == null || s.range2Hour2 == null) {
return [];
}
final stepMinutes = widget.userProfessional.slotDurationMinutes;
if (s.continuousDay) {
return TimeOfDayUtils.genRanges(s.range1Hour1!, s.range2Hour2!);
return TimeOfDayUtils.genRanges(s.range1Hour1!, s.range2Hour2!,
stepMinutes: stepMinutes);
}
if (s.range1Hour2 == null || s.range2Hour1 == null) return [];
return [
...TimeOfDayUtils.genRanges(s.range1Hour1!, s.range1Hour2!),
...TimeOfDayUtils.genRanges(s.range2Hour1!, s.range2Hour2!),
...TimeOfDayUtils.genRanges(s.range1Hour1!, s.range1Hour2!,
stepMinutes: stepMinutes),
...TimeOfDayUtils.genRanges(s.range2Hour1!, s.range2Hour2!,
stepMinutes: stepMinutes),
];
}