passo_mobile_app/lib/bloc/profile/profile_bloc.dart

138 lines
5.1 KiB
Dart
Raw Normal View History

import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:unit2/model/profile/basic_info.dart';
import 'package:unit2/model/profile/basic_information/primary-information.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';
2023-02-23 00:53:14 +00:00
import 'package:unit2/test_data.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';
import '../../model/location/barangay.dart';
part 'profile_event.dart';
part 'profile_state.dart';
class ProfileBloc extends Bloc<ProfileEvent, ProfileState> {
ProfileBloc() : super(ProfileInitial()) {
ProfileInformation? _profileInformation;
2023-02-23 00:53:14 +00:00
List<Country>? _countries;
List<Region>? _regions;
List<Eligibility>? _eligibilities;
List<Province>? _provinces;
List<CityMunicipality>? _cities;
////=========================================================================
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);
_profileInformation = profileInformation;
emit(ProfileLoaded(profileInformation: _profileInformation!));
// } 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());
emit(EligibilityLoaded(eligibilities: event.eligibilities));
});
2023-02-23 00:53:14 +00:00
////====================================================================
on<EditEligibility>((event, emit) async {
Region? currentRegion;
Province? currentProvince;
CityMunicipality? currentCity;
// try{
2023-02-23 00:53:14 +00:00
emit(ProfileLoading());
if (_countries == null) {
List<Country> countries = await LocationUtils.instance.getCountries();
_countries = countries;
}
if (_regions == null) {
List<Region> regions = await LocationUtils.instance.getRegions();
_regions = regions;
}
if (_eligibilities == null) {
List<Eligibility> eligibilities =
await ProfileUtilities.instance.getEligibilities();
_eligibilities = eligibilities;
}
// get current country
Country? currentCountry = _countries!.firstWhere((Country country) =>
country.code == event.eligibityCert.examAddress?.country!.code);
// get current eligibility
Eligibility currentEligibility = _eligibilities!.firstWhere(
(Eligibility eligibility) =>
event.eligibityCert.eligibility!.id == eligibility.id);
2023-02-20 07:48:24 +00:00
bool? isOverseas = event.eligibityCert.overseas;
2023-02-23 00:53:14 +00:00
2023-02-23 05:51:53 +00:00
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-23 00:53:14 +00:00
countries: _countries!,
regions: _regions!,
eligibilities: _eligibilities!));
2023-02-20 07:48:24 +00:00
// }catch(e){
// emit(ProfileErrorState(mesage: e.toString()));
// }
2023-02-23 00:53:14 +00:00
////====================================================================
});
on<AddEligibility>((event, emit) async {
List<CityMunicipality>? cities;
List<Province>? provinces;
Eligibility? currentEligibility;
2023-02-20 07:48:24 +00:00
emit(ProfileLoading());
2023-02-23 00:53:14 +00:00
if (_regions == null) {
List<Region> regions = await LocationUtils.instance.getRegions();
_regions = regions;
}
if (_eligibilities == null) {
List<Eligibility> eligibilities =
await ProfileUtilities.instance.getEligibilities();
_eligibilities = eligibilities;
}
if (_countries == null) {
List<Country> countries = await LocationUtils.instance.getCountries();
_countries = countries;
}
2023-02-23 05:51:53 +00:00
2023-02-23 00:53:14 +00:00
emit(AddEligibilityState(
eligibilities: _eligibilities!,
regions: _regions!,
countries: _countries!));
});
////====================================================================
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
});
}
}