passo_mobile_app/lib/bloc/profile/profile_bloc.dart

138 lines
4.5 KiB
Dart
Raw Normal View History

import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:unit2/model/profile/profileInfomation.dart';
2023-05-22 02:23:56 +00:00
import 'package:unit2/sevices/profile/profile_other_info.dart';
import 'package:unit2/sevices/profile/profile_service.dart';
2023-05-22 02:23:56 +00:00
import '../../model/profile/basic_information/primary-information.dart';
import '../../screens/profile/components/basic_information/profile_other_info.dart';
part 'profile_event.dart';
part 'profile_state.dart';
class ProfileBloc extends Bloc<ProfileEvent, ProfileState> {
ProfileBloc() : super(ProfileInitial()) {
2023-05-22 02:23:56 +00:00
Profile? currentProfileInformation;
List<ProfileOtherInfo> religions = [];
List<ProfileOtherInfo> ethnicities = [];
List<ProfileOtherInfo> indigencies = [];
List<ProfileOtherInfo> disabilities = [];
List<ProfileOtherInfo> genders = [];
List<String> bloodType = [
"A+",
"B+",
"A-",
"B-",
"AB+",
"AB-",
2023-06-06 06:54:51 +00:00
"O+",
"O-"
2023-05-22 02:23:56 +00:00
];
List<String> nameExtensions = [
"NONE",
"N/A",
"SR.",
"JR.",
"I",
"II",
"III",
"IV",
"V",
"VI",
"VII",
"VIII",
"IX",
"X"
];
List<String> sexes = ["MALE", "FEMALE"];
List<String> civilStatus = [
"NONE",
"SINGLE",
"MARRIED",
"SEPARATED",
"WIDOWED"
];
2023-02-27 06:26:27 +00:00
ProfileInformation? globalProfileInformation;
on<LoadProfile>((event, emit) async {
2023-05-22 02:23:56 +00:00
emit(ProfileLoading());
try {
ProfileInformation? profileInformation =
await ProfileService.instance.getProfile(event.token, event.userID);
globalProfileInformation = profileInformation;
emit(ProfileLoaded(profileInformation: globalProfileInformation!));
} catch (e) {
emit(ProfileErrorState(mesage: e.toString()));
}
});
2023-05-22 02:23:56 +00:00
on<GetPrimaryBasicInfo>((event, emit) {
currentProfileInformation = event.primaryBasicInformation;
emit(BasicInformationProfileLoaded(
primaryBasicInformation: event.primaryBasicInformation));
});
on<LoadBasicPrimaryInfo>((event,emit){
emit(BasicInformationProfileLoaded(
primaryBasicInformation: currentProfileInformation!));
});
on<ShowPrimaryInfoEditForm>((event, emit) async {
try {
emit(BasicPrimaryInformationLoadingState());
if (religions.isEmpty) {
religions = await ProfileOtherInfoServices.instace
.getReligions(token: event.token);
}
if (genders.isEmpty) {
genders = await ProfileOtherInfoServices.instace
.getGenders(token: event.token);
}
if (ethnicities.isEmpty) {
ethnicities = await ProfileOtherInfoServices.instace
.getEthnicity(token: event.token);
}
if (disabilities.isEmpty) {
disabilities = await ProfileOtherInfoServices.instace
.getDisability(token: event.token);
}
if (indigencies.isEmpty) {
indigencies = await ProfileOtherInfoServices.instace
.getIndigency(token: event.token);
}
emit(BasicInformationEditingState(
primaryInformation: currentProfileInformation!,
extensions: nameExtensions,
sexes: sexes,
bloodTypes: bloodType,
genders: genders,
civilStatus: civilStatus,
disability: disabilities,
ethnicity: ethnicities,
indigenous: indigencies,
religion: religions));
} catch (e) {
emit(BasicPrimaryInformationErrorState(message: e.toString()));
}
});
on<EditBasicProfileInformation>((event, emit) async {
try {
Map<dynamic, dynamic> status = await ProfileService.instance
.updateBasicProfileInfo(
token: event.token,
profileId: event.profileId,
profileInfo: event.profileInformation,
genderId: event.genderId,
indigencyId: event.indigencyId,
disabilityId: event.disabilityId,
ethnicityId: event.ethnicityId,
reqligionId: event.religionId);
if (status['success']) {
Profile profile = Profile.fromJson(status['data']);
currentProfileInformation = profile;
emit(BasicProfileInfoEditedState(response: status));
}
emit(BasicProfileInfoEditedState(response: status));
} catch (e) {
emit(BasicPrimaryInformationErrorState(message: e.toString()));
}
});
}
}