passo_mobile_app/lib/bloc/profile/profile_bloc.dart

141 lines
5.3 KiB
Dart
Raw Normal View History

import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
2023-02-15 03:40:12 +00:00
import 'package:unit2/model/profile/eligibility.dart';
import 'package:unit2/model/profile/profileInfomation.dart';
2023-02-20 07:48:24 +00:00
import 'package:unit2/model/utils/eligibility.dart';
2023-02-23 00:53:14 +00:00
import 'package:unit2/sevices/profile/eligibility_services.dart';
import 'package:unit2/sevices/profile/profile_service.dart';
import 'package:unit2/utils/location_utilities.dart';
import 'package:unit2/utils/profile_utilities.dart';
2023-02-20 07:48:24 +00:00
import '../../model/location/country.dart';
import '../../model/location/region.dart';
import '../../model/location/provinces.dart';
import '../../model/location/city.dart';
part 'profile_event.dart';
part 'profile_state.dart';
class ProfileBloc extends Bloc<ProfileEvent, ProfileState> {
ProfileBloc() : super(ProfileInitial()) {
2023-02-27 06:26:27 +00:00
ProfileInformation? globalProfileInformation;
List<Country>? globalCountries;
List<Region>? globalRegions;
List<Eligibility>? globalEligibilities;
List<EligibityCert>? eligibilities;
2023-02-23 00:53:14 +00:00
////=========================================================================
on<LoadProfile>((event, emit) async {
// try {
2023-02-15 05:23:06 +00:00
emit(ProfileLoading());
ProfileInformation? profileInformation =
await ProfileService.instance.getProfile(event.token, event.userID);
2023-02-27 06:26:27 +00:00
globalProfileInformation = profileInformation;
emit(ProfileLoaded(profileInformation: globalProfileInformation!));
// } catch (e) {
// emit(ProfileErrorState(mesage: e.toString()));
// }
});
2023-02-23 00:53:14 +00:00
////=====================================================================
2023-02-15 05:23:06 +00:00
on<LoadEligibility>((event, emit) {
emit(ProfileLoading());
2023-02-27 06:26:27 +00:00
eligibilities = event.eligibilities;
2023-02-15 05:23:06 +00:00
emit(EligibilityLoaded(eligibilities: event.eligibilities));
});
2023-02-23 00:53:14 +00:00
////====================================================================
on<EditEligibility>((event, emit) async {
// try{
2023-02-23 00:53:14 +00:00
emit(ProfileLoading());
2023-02-27 06:26:27 +00:00
if (globalCountries == null) {
2023-02-23 00:53:14 +00:00
List<Country> countries = await LocationUtils.instance.getCountries();
2023-02-27 06:26:27 +00:00
globalCountries = countries;
2023-02-23 00:53:14 +00:00
}
2023-02-27 06:26:27 +00:00
if (globalRegions == null) {
2023-02-23 00:53:14 +00:00
List<Region> regions = await LocationUtils.instance.getRegions();
2023-02-27 06:26:27 +00:00
globalRegions = regions;
2023-02-23 00:53:14 +00:00
}
2023-02-27 06:26:27 +00:00
if (globalEligibilities == null) {
2023-02-23 00:53:14 +00:00
List<Eligibility> eligibilities =
await ProfileUtilities.instance.getEligibilities();
2023-02-27 06:26:27 +00:00
globalEligibilities = eligibilities;
2023-02-23 00:53:14 +00:00
}
2023-02-20 07:48:24 +00:00
bool? isOverseas = event.eligibityCert.overseas;
2023-02-23 00:53:14 +00:00
emit(EditEligibilityState(
2023-02-20 07:48:24 +00:00
isOverseas: isOverseas!,
eligibityCert: event.eligibityCert,
2023-02-27 06:26:27 +00:00
countries: globalCountries!,
regions: globalRegions!,
eligibilities: globalEligibilities!));
2023-02-20 07:48:24 +00:00
// }catch(e){
// emit(ProfileErrorState(mesage: e.toString()));
// }
2023-02-23 00:53:14 +00:00
////====================================================================
});
2023-02-27 06:26:27 +00:00
on<ShowAddEligibilityForm>((event, emit) async {
2023-02-20 07:48:24 +00:00
emit(ProfileLoading());
2023-02-27 06:26:27 +00:00
if (globalRegions == null) {
2023-02-23 00:53:14 +00:00
List<Region> regions = await LocationUtils.instance.getRegions();
2023-02-27 06:26:27 +00:00
globalRegions = regions;
2023-02-23 00:53:14 +00:00
}
2023-02-27 06:26:27 +00:00
if (globalEligibilities == null) {
2023-02-23 00:53:14 +00:00
List<Eligibility> eligibilities =
await ProfileUtilities.instance.getEligibilities();
2023-02-27 06:26:27 +00:00
globalEligibilities = eligibilities;
2023-02-23 00:53:14 +00:00
}
2023-02-27 06:26:27 +00:00
if (globalCountries == null) {
2023-02-23 00:53:14 +00:00
List<Country> countries = await LocationUtils.instance.getCountries();
2023-02-27 06:26:27 +00:00
globalCountries = countries;
2023-02-23 00:53:14 +00:00
}
2023-02-27 06:26:27 +00:00
2023-02-23 00:53:14 +00:00
emit(AddEligibilityState(
2023-02-27 06:26:27 +00:00
eligibilities: globalEligibilities!,
regions: globalRegions!,
countries: globalCountries!));
2023-02-23 00:53:14 +00:00
});
////====================================================================
on<DeleteEligibility>((event, emit) async {
emit(ProfileLoading());
try {
final bool success = await EligibilityService.instance.delete(
eligibilityId: event.eligibilityId,
profileId: int.parse(event.profileId),
token: event.token);
if (success) {
event.eligibilities.removeWhere(
((EligibityCert element) => element.id == event.eligibilityId));
List<EligibityCert> eligibilities = event.eligibilities;
emit(DeletedState(success: success, eligibilities: eligibilities));
} else {
emit(DeletedState(
success: success, eligibilities: event.eligibilities));
}
} catch (e) {
emit(ProfileErrorState(mesage: e.toString()));
}
2023-02-15 03:40:12 +00:00
});
2023-02-27 06:26:27 +00:00
on<AddEligibility>(
(event, emit) async {
try {
emit(ProfileLoading());
Map<dynamic, dynamic> status = await EligibilityService.instance.add(
eligibityCert: event.eligibityCert,
token: event.token,
profileId: int.parse(event.profileId));
if (status['success']) {
EligibityCert? eligibityCert =
EligibityCert.fromJson(status['data']);
eligibilities!.add(eligibityCert);
emit(EligibilityAddedState(
eligibilities: eligibilities!, response: status));
} else {
emit(EligibilityAddedState(
eligibilities: eligibilities!, response: status));
}
} catch (e) {
emit(ProfileErrorState(mesage: e.toString()));
}
},
);
}
}