import 'package:bloc/bloc.dart'; import 'package:equatable/equatable.dart'; import 'package:unit2/sevices/profile/family_services.dart'; import '../../../model/profile/family_backround.dart'; part 'family_event.dart'; part 'family_state.dart'; class FamilyBloc extends Bloc { FamilyBloc() : super(FamilyInitial()) { List families = []; on((event, emit) async { emit(FamilyLoadingState()); try { if (families.isEmpty) { List family = await FamilyService.instance .getFamilies(event.profileId, event.token); families = family; } emit(FamilyLoaded(families: families)); } catch (e) { emit(FamilyErrorState(message: e.toString())); } }); ////Load on((event, emit) { emit(FamilyLoaded(families: families)); }); ////Add Family on((event, emit) async { try { emit(FamilyLoadingState()); Map status = await FamilyService.instance.add( family: event.familyBackground, relationshipId: event.relationshipId, profileId: event.profileId, token: event.token); if (status['success']) { FamilyBackground familyBackground = FamilyBackground.fromJson(status['data']); families.add(familyBackground); emit(FamilyAddedState(response: status)); } else { emit(FamilyAddedState(response: status)); } } catch (e) { emit(FamilyErrorState(message: e.toString())); } }); //// Add Emergency on((event, emit) async { try { emit(FamilyLoadingState()); Map status = await FamilyService.instance .addEmergency( requestType: event.requestType, relatedPersonId: event.relatedPersonId, numberMail: event.numberMail, contactInfoId: event.contactInfoId, profileId: event.profileId, token: event.token); if (status['success']) { families.removeWhere( (element) => element.relatedPerson!.id == event.relatedPersonId); FamilyBackground familyBackground = FamilyBackground.fromJson(status['data']); families.add(familyBackground); emit(EmergencyContactEditedState(response: status)); } else { emit(EmergencyContactEditedState(response: status)); } } catch (e) { emit(FamilyErrorState(message: e.toString())); } }); ////update on((event, emit) async { try { emit(FamilyLoadingState()); Map status = await FamilyService.instance.update( family: event.familyBackground, relationshipId: event.relationshipId, profileId: event.profileId, token: event.token); if (status['success']) { families.removeWhere((element) => element.relatedPerson!.id == event.familyBackground.relatedPerson!.id); FamilyBackground familyBackground = FamilyBackground.fromJson(status['data']); families.add(familyBackground); emit(FamilyEditedState(response: status)); } else { emit(FamilyEditedState(response: status)); } } catch (e) { emit(FamilyErrorState(message: e.toString())); } }); ////Delete on((event, emit) async { try { final bool success = await FamilyService.instance.delete( personRelatedId: event.id, profileId: event.profileId, token: event.token); if (success) { families .removeWhere((element) => element.relatedPerson!.id == event.id); emit(DeletedState(success: success)); } else { emit(DeletedState(success: success)); } } catch (e) { emit(FamilyErrorState(message: e.toString())); } }); on((event,emit){ emit(FamilyErrorState(message: state.toString())); }); } }