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>
123 lines
4.3 KiB
Dart
123 lines
4.3 KiB
Dart
import 'dart:developer';
|
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:equatable/equatable.dart';
|
|
import 'package:professional_repository/professional_repository.dart';
|
|
import 'package:prosappco/blocs/professional_list_bloc/professional_list_bloc.dart';
|
|
import 'package:user_repository/user_repository.dart';
|
|
|
|
part 'professional_event.dart';
|
|
part 'professional_state.dart';
|
|
|
|
class ProfessionalBloc extends Bloc<ProfessionalEvent, ProfessionalState> {
|
|
final ApiProfessionalRepository _professionalRepository;
|
|
final UserRepository _userRepository;
|
|
|
|
ProfessionalBloc(
|
|
{required ApiProfessionalRepository professionalRepository,
|
|
required UserRepository userRepository})
|
|
: _professionalRepository = professionalRepository,
|
|
_userRepository = userRepository,
|
|
super(ProfessionalInitial()) {
|
|
bool isProModeActive = _professionalRepository.isProModeActive;
|
|
final proInfo = _professionalRepository.lastProInfo();
|
|
emit(LoadedModeProState(isProModeActive, proInfo));
|
|
if (isProModeActive && proInfo == null) {
|
|
_fetchMyProInfo();
|
|
}
|
|
|
|
_professionalRepository.streamProInfo().listen((proInfo) {
|
|
if (proInfo == null) {
|
|
emit(const ProfessionalStateFailure());
|
|
} else {
|
|
final isProModeActive = _professionalRepository.isProModeActive;
|
|
emit(LoadedModeProState(isProModeActive, proInfo));
|
|
}
|
|
});
|
|
|
|
_professionalRepository.sreamIsProModeActive().listen((isProModeActive) {
|
|
add(UpdateProfessionalEvent(isProModeActive: isProModeActive));
|
|
});
|
|
|
|
on<SwitchProModeEvent>((event, emit) async {
|
|
await _professionalRepository.switchProMode();
|
|
});
|
|
|
|
on<ResetProfessionalApplicationEvent>((event, emit) async {
|
|
emit(ResetProfessionalApplicationLoading());
|
|
try {
|
|
await _professionalRepository.deleteProfessionalInfo();
|
|
emit(ResetProfessionalApplicationSuccess());
|
|
} catch (e) {
|
|
log(e.toString());
|
|
emit(ProfessionalStateFailure(message: e.toString()));
|
|
}
|
|
});
|
|
|
|
on<UpdateProfessionalEvent>((event, emit) async {
|
|
final proInfo = _professionalRepository.lastProInfo();
|
|
emit(LoadedModeProState(event.isProModeActive, proInfo));
|
|
if (event.isProModeActive && proInfo == null) {
|
|
await _fetchMyProInfo();
|
|
}
|
|
});
|
|
|
|
on<SendProfessionalToReviewEvent>((event, emit) async {
|
|
emit(SendProfessionalToReviewLoading());
|
|
final myUser = await _userRepository.lastUser();
|
|
if (myUser == null) {
|
|
emit(const SendProfessionalToReviewFailure(
|
|
message: 'No se pudo identificar tu usuario'));
|
|
return;
|
|
}
|
|
try {
|
|
final identificationPdfUrl = await _professionalRepository
|
|
.uploadPdfCedula(event.identificationPicture, myUser.id);
|
|
|
|
final certificatePdfUrl = await _professionalRepository
|
|
.uploadPdfCertificado(event.certificatePicture, myUser.id);
|
|
|
|
List<String> specializationsPdfUrls = [];
|
|
if (event.specializationsPictures.isNotEmpty) {
|
|
specializationsPdfUrls =
|
|
await _professionalRepository.uploadPdfsEspecializaciones(
|
|
event.specializationsPictures, myUser.id);
|
|
} else {
|
|
specializationsPdfUrls = [];
|
|
}
|
|
|
|
await _professionalRepository.saveProfessionalInfo(ProfessionalEntity(
|
|
id: event.id,
|
|
identification: event.identification,
|
|
identificationPicture: identificationPdfUrl,
|
|
address: '',
|
|
aditionalAddress: '',
|
|
profession: event.profession,
|
|
specializations: event.specializations,
|
|
specializationsPictures: specializationsPdfUrls,
|
|
certificatePicture: certificatePdfUrl,
|
|
latitude: 0,
|
|
longitude: 0,
|
|
ratePreferences: false,
|
|
rate: '',
|
|
locationPreferences: LocationPreferences.office,
|
|
bannerPicture: '',
|
|
schedules: Schedules.empty,
|
|
paymentMethods: PaymentMethodEntity.empty,
|
|
));
|
|
|
|
emit(SendProfessionalToReviewSuccess());
|
|
} catch (e) {
|
|
log('error acceso a pro ${e.toString()}');
|
|
emit(SendProfessionalToReviewFailure(message: e.toString()));
|
|
}
|
|
});
|
|
}
|
|
|
|
Future<void> _fetchMyProInfo() async {
|
|
final myUser = await _userRepository.lastUser();
|
|
if (myUser == null) return;
|
|
await _professionalRepository.updateFromFirebase(userId: myUser.id);
|
|
}
|
|
}
|