import 'package:bloc/bloc.dart'; import 'package:equatable/equatable.dart'; import 'package:unit2/model/profile/educational_background.dart'; import 'package:unit2/sevices/profile/education_services.dart'; import '../../../model/profile/attachment.dart'; import '../../../utils/attachment_categories.dart'; part 'education_event.dart'; part 'education_state.dart'; class EducationBloc extends Bloc { List educationalBackgrounds = []; List schools = []; List programs = []; List honors = []; List attachmentCategories = []; EducationBloc() : super(EducationInitial()) { on((event, emit) async { emit(EducationalBackgroundLoadingState()); try { if (attachmentCategories.isEmpty) { attachmentCategories = await AttachmentServices.instance.getCategories(); } if (educationalBackgrounds.isEmpty) { List educations = await EducationService .instace .getEducationalBackground(event.profileId, event.token); educationalBackgrounds = educations; emit(EducationalBackgroundLoadedState( educationalBackground: educationalBackgrounds, attachmentCategory: attachmentCategories)); } else { emit(EducationalBackgroundLoadedState( educationalBackground: educationalBackgrounds,attachmentCategory: attachmentCategories)); } } catch (e) { emit(EducationalBackgroundErrorState(message: e.toString())); } }); //// SHOW ADD FORM on((event, emit) async { emit(EducationalBackgroundLoadingState()); try { if (schools.isEmpty) { List newSchools = await EducationService.instace.getSchools(); schools = newSchools; } if (programs.isEmpty) { List newPrograms = await EducationService.instace.getPrograms(); programs = newPrograms; } if (honors.isEmpty) { List newHonors = await EducationService.instace.getHonors(); honors = newHonors; } emit(AddEducationState( schools: schools, programs: programs, honors: honors)); } catch (e) { emit(EducationalBackgroundErrorState(message: e.toString())); } }); ////Add on((event, emit) async { Map status = await EducationService.instace.add( honors: event.honors, educationalBackground: event.educationalBackground, token: event.token, profileId: event.profileId); if (status['success']) { EducationalBackground educationalBackground = EducationalBackground.fromJson(status['data']); educationalBackgrounds.add(educationalBackground); emit(EducationAddedState(response: status)); } else { emit(EducationAddedState(response: status)); } }); ////Update on((event, emit) async { Map status = await EducationService.instace.edit( honors: event.honors, educationalBackground: event.educationalBackground, token: event.token, profileId: event.profileId); if (status['success']) { educationalBackgrounds.removeWhere((element) => event.educationalBackground.id == element.id); EducationalBackground educationalBackground = EducationalBackground.fromJson(status['data']); educationalBackgrounds.add(educationalBackground); emit(EditedEducationState(response: status)); } else { emit(EditedEducationState(response: status)); } }); ////LOAD on((event, emit) { emit(EducationalBackgroundLoadedState( educationalBackground: educationalBackgrounds,attachmentCategory: attachmentCategories)); }); //// SHOW EDIT FORM on((event, emit) async { try { if (schools.isEmpty) { List newSchools = await EducationService.instace.getSchools(); schools = newSchools; } if (programs.isEmpty) { List newPrograms = await EducationService.instace.getPrograms(); programs = newPrograms; } if (honors.isEmpty) { List newHonors = await EducationService.instace.getHonors(); honors = newHonors; } emit(EditEducationState( schools: schools, programs: programs, honors: honors, educationalBackground: event.educationalBackground)); } catch (e) { emit(EducationalBackgroundErrorState(message: e.toString())); } }); ////delete on((event, emit) async { try { final bool success = await EducationService.instace.delete( profileId: event.profileId, token: event.token, educationalBackground: event.educationalBackground); if (success) { educationalBackgrounds.removeWhere( (element) => element.id == event.educationalBackground.id); emit(EducationDeletedState(success: success)); } else { emit(EducationDeletedState(success: success)); } } catch (e) { emit(EducationalBackgroundErrorState(message: e.toString())); } }); ////Add attachment on((event, emit) async { emit(EducationalBackgroundLoadingState()); try { Map status = await AttachmentServices.instance .attachment( categoryId: event.categoryId, module: event.attachmentModule, paths: event.filePaths, token: event.token, profileId: event.profileId); if (status['success']) { emit(EducationAddedState(response: status)); } else { emit(EducationAddedState(response: status)); } } catch (e) { emit(EducationalBackgroundErrorState(message: e.toString())); } }); } }