passo_mobile_app/lib/bloc/profile/profile_bloc.dart

156 lines
5.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';
2023-09-22 08:07:13 +00:00
import '../../utils/global.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 = [];
2023-08-20 09:36:16 +00:00
List<String> bloodType = ["A+", "B+", "A-", "B-", "AB+", "AB-", "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 {
2023-08-20 09:36:16 +00:00
if (globalProfileInformation == null) {
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) {
2023-09-22 08:07:13 +00:00
if (globalCurrentProfile != null) {
emit(BasicInformationProfileLoaded(
primaryBasicInformation: globalCurrentProfile!));
} else {
2023-09-13 02:45:03 +00:00
currentProfileInformation = event.primaryBasicInformation;
globalCurrentProfile = currentProfileInformation;
2023-09-22 08:07:13 +00:00
emit(BasicInformationProfileLoaded(
primaryBasicInformation: currentProfileInformation!));
2023-09-13 02:45:03 +00:00
}
2023-05-22 02:23:56 +00:00
});
2023-08-20 09:36:16 +00:00
on<LoadBasicPrimaryInfo>((event, emit) {
emit(BasicInformationProfileLoaded(
2023-09-22 08:07:13 +00:00
primaryBasicInformation: globalCurrentProfile!));
2023-05-22 02:23:56 +00:00
});
on<ShowPrimaryInfoEditForm>((event, emit) async {
try {
emit(BasicPrimaryInformationLoadingState());
if (religions.isEmpty) {
religions = await ProfileOtherInfoServices.instace
.getReligions(token: event.token);
2023-08-20 09:36:16 +00:00
religions.insert(
0, ProfileOtherInfo(id: null, name: "NONE", description: null));
2023-05-22 02:23:56 +00:00
}
if (genders.isEmpty) {
genders = await ProfileOtherInfoServices.instace
.getGenders(token: event.token);
2023-08-20 09:36:16 +00:00
genders.insert(
0, ProfileOtherInfo(id: null, name: "NONE", description: null));
2023-05-22 02:23:56 +00:00
}
if (ethnicities.isEmpty) {
ethnicities = await ProfileOtherInfoServices.instace
.getEthnicity(token: event.token);
2023-08-20 09:36:16 +00:00
ethnicities.insert(
0, ProfileOtherInfo(id: null, name: "NONE", description: null));
2023-05-22 02:23:56 +00:00
}
if (disabilities.isEmpty) {
disabilities = await ProfileOtherInfoServices.instace
.getDisability(token: event.token);
2023-08-20 09:36:16 +00:00
disabilities.insert(
0, ProfileOtherInfo(id: null, name: "NONE", description: null));
2023-05-22 02:23:56 +00:00
}
if (indigencies.isEmpty) {
indigencies = await ProfileOtherInfoServices.instace
.getIndigency(token: event.token);
2023-08-20 09:36:16 +00:00
indigencies.insert(
0, ProfileOtherInfo(id: null, name: "NONE", description: null));
2023-05-22 02:23:56 +00:00
}
emit(BasicInformationEditingState(
primaryInformation: globalCurrentProfile!,
2023-05-22 02:23:56 +00:00
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;
2023-09-22 08:07:13 +00:00
globalCurrentProfile = profile;
globalFistname = profile.firstName;
globalLastname = profile.lastName;
globalBday = profile.birthdate;
globalSex = profile.sex;
2023-05-22 02:23:56 +00:00
emit(BasicProfileInfoEditedState(response: status));
}
emit(BasicProfileInfoEditedState(response: status));
} catch (e) {
emit(BasicPrimaryInformationErrorState(message: e.toString()));
}
});
}
}