2023-03-06 08:29:55 +00:00
|
|
|
import 'package:bloc/bloc.dart';
|
|
|
|
import 'package:equatable/equatable.dart';
|
|
|
|
import 'package:unit2/sevices/profile/family_services.dart';
|
|
|
|
|
2023-03-07 02:31:28 +00:00
|
|
|
import '../../../model/profile/family_backround.dart';
|
2023-03-06 08:29:55 +00:00
|
|
|
|
|
|
|
part 'family_event.dart';
|
|
|
|
part 'family_state.dart';
|
|
|
|
|
|
|
|
class FamilyBloc extends Bloc<FamilyEvent, FamilyState> {
|
|
|
|
FamilyBloc() : super(FamilyInitial()) {
|
2023-05-11 00:04:53 +00:00
|
|
|
List<FamilyBackground> families = [];
|
|
|
|
on<GetFamilies>((event, emit) async {
|
2023-03-06 08:29:55 +00:00
|
|
|
emit(FamilyLoadingState());
|
2023-05-11 00:04:53 +00:00
|
|
|
try {
|
2023-06-06 06:54:51 +00:00
|
|
|
|
|
|
|
if (families.isEmpty) {
|
|
|
|
List<FamilyBackground> family = await FamilyService.instance
|
|
|
|
.getFamilies(event.profileId, event.token);
|
|
|
|
families = family;
|
|
|
|
}
|
2023-03-06 08:29:55 +00:00
|
|
|
emit(FamilyLoaded(families: families));
|
2023-05-11 00:04:53 +00:00
|
|
|
} catch (e) {
|
|
|
|
emit(FamilyErrorState(message: e.toString()));
|
|
|
|
}
|
2023-06-06 06:54:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
});
|
2023-05-11 00:04:53 +00:00
|
|
|
////Load
|
|
|
|
on<LoadFamily>((event, emit) {
|
|
|
|
emit(FamilyLoaded(families: families));
|
|
|
|
});
|
2023-06-06 06:54:51 +00:00
|
|
|
////Add Family
|
2023-05-11 00:04:53 +00:00
|
|
|
on<AddFamily>((event, emit) async {
|
|
|
|
try {
|
|
|
|
emit(FamilyLoadingState());
|
|
|
|
Map<dynamic, dynamic> 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<AddEmergencyEvent>((event, emit) async {
|
|
|
|
try {
|
|
|
|
emit(FamilyLoadingState());
|
|
|
|
Map<dynamic, dynamic> status = await FamilyService.instance
|
|
|
|
.addEmergency(
|
2023-06-06 06:54:51 +00:00
|
|
|
requestType: event.requestType,
|
2023-05-11 00:04:53 +00:00
|
|
|
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);
|
2023-06-06 06:54:51 +00:00
|
|
|
emit(EmergencyContactEditedState(response: status));
|
2023-05-11 00:04:53 +00:00
|
|
|
} else {
|
2023-06-06 06:54:51 +00:00
|
|
|
emit(EmergencyContactEditedState(response: status));
|
2023-05-11 00:04:53 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
emit(FamilyErrorState(message: e.toString()));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
////update
|
|
|
|
on<Updatefamily>((event, emit) async {
|
|
|
|
try {
|
2023-06-06 06:54:51 +00:00
|
|
|
emit(FamilyLoadingState());
|
2023-05-11 00:04:53 +00:00
|
|
|
Map<dynamic, dynamic> 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<DeleteFamily>((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) {
|
2023-03-06 08:29:55 +00:00
|
|
|
emit(FamilyErrorState(message: e.toString()));
|
|
|
|
}
|
|
|
|
});
|
2023-06-21 00:22:43 +00:00
|
|
|
on<CallErrorState>((event,emit){
|
|
|
|
emit(FamilyErrorState(message: state.toString()));
|
|
|
|
});
|
2023-03-06 08:29:55 +00:00
|
|
|
}
|
|
|
|
}
|