passo_mobile_app/lib/bloc/profile/references/references_bloc.dart

214 lines
8.3 KiB
Dart
Raw Permalink Normal View History

import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:unit2/model/location/address_category.dart';
import 'package:unit2/model/location/barangay.dart';
import 'package:unit2/model/location/city.dart';
import 'package:unit2/model/location/country.dart';
import 'package:unit2/model/location/region.dart';
import 'package:unit2/sevices/profile/references_services.dart';
import '../../../model/location/provinces.dart';
import '../../../model/profile/references.dart';
import '../../../utils/location_utilities.dart';
part 'references_event.dart';
part 'references_state.dart';
class ReferencesBloc extends Bloc<ReferencesEvent, ReferencesState> {
List<PersonalReference> references = [];
List<Country> globalCountries = [];
List<Region> globalRegions = [];
List<AddressCategory> globalCategories = [];
ReferencesBloc() : super(ReferencesInitial()) {
on<GetReferences>((event, emit) async {
emit(ReferencesLoadingState());
try {
2023-06-06 06:54:51 +00:00
if (references.isEmpty) {
List<PersonalReference> refs = await ReferencesServices.instace
2023-06-06 06:54:51 +00:00
.getRefences(event.profileId, event.token);
references = refs;
emit(ReferencesLoadedState(references: references));
}
2023-09-05 06:43:57 +00:00
emit(ReferencesLoadedState(references: references));
} catch (e) {
2023-06-06 06:54:51 +00:00
emit( ReferencesErrorState(message: e.toString()));
}
2023-06-06 06:54:51 +00:00
});
////SHOW FORM FOR ADDING REFERENCES
2023-06-06 06:54:51 +00:00
on<ShowAddReferenceForm>((event, emit) async {
emit(ReferencesLoadingState());
try {
if (globalRegions.isEmpty) {
List<Region> regions = await LocationUtils.instance.getRegions();
globalRegions = regions;
}
if (globalCountries.isEmpty) {
List<Country> countries = await LocationUtils.instance.getCountries();
globalCountries = countries;
}
if (globalCategories.isEmpty) {
List<AddressCategory> categories =
await LocationUtils.instance.getAddressCategory();
globalCategories = categories;
}
emit(AddReferenceState(
countries: globalCountries,
regions: globalRegions,
categories: globalCategories));
} catch (e) {
emit(ReferencesErrorState(message: e.toString()));
}
////SHOW EDIT FORM
});
on<ShowEditReferenceForm>((event, emit) async {
Region? selectedRegion;
Province? selectedProvince;
CityMunicipality? selectedCity;
AddressCategory? selectedCategory;
Country selectedCountry;
Barangay? selectedBarangay;
try {
if (globalRegions.isEmpty) {
List<Region> regions = await LocationUtils.instance.getRegions();
globalRegions = regions;
}
if (globalCountries.isEmpty) {
List<Country> countries = await LocationUtils.instance.getCountries();
globalCountries = countries;
}
if (globalCategories.isEmpty) {
List<AddressCategory> categories =
await LocationUtils.instance.getAddressCategory();
globalCategories = categories;
}
//// checck if address is overseas
bool overseas =
event.personalReference.address!.country!.id! != 175 ? true : false;
2023-06-06 06:54:51 +00:00
selectedCategory = globalCategories.firstWhere(
(AddressCategory element) =>
event.personalReference.address!.addressCategory!.id ==
element.id);
////if address is not overseas set initial values for address
if (!overseas) {
selectedRegion = globalRegions.firstWhere((Region element) =>
event.personalReference.address!.cityMunicipality!.province!
.region!.code ==
element.code);
List<Province> provinces = await LocationUtils.instance
.getProvinces(regionCode: selectedRegion.code.toString());
selectedProvince = provinces.firstWhere((Province province) =>
event.personalReference.address!.cityMunicipality!.province!
.code ==
province.code);
List<CityMunicipality> cities = await LocationUtils.instance
.getCities(code: selectedProvince.code!);
selectedCity = cities.firstWhere((CityMunicipality city) =>
event.personalReference.address!.cityMunicipality!.code ==
city.code);
2023-06-06 06:54:51 +00:00
List<Barangay> barangays = await LocationUtils.instance
.getBarangay(code: selectedCity.code.toString());
if (event.personalReference.address?.barangay != null) {
selectedBarangay = barangays.firstWhere((Barangay barangay) =>
event.personalReference.address?.barangay?.code ==
barangay.code);
} else {
selectedBarangay = null;
}
emit(EditReferenceState(
selectedRegion: selectedRegion,
ref: event.personalReference,
countries: globalCountries,
regions: globalRegions,
barangays: barangays,
categories: globalCategories,
isOverseas: overseas,
provinces: provinces,
selectedProvince: selectedProvince,
cities: cities,
selectedCity: selectedCity,
selectedCategory: selectedCategory,
2023-06-06 06:54:51 +00:00
selectedBarangay: selectedBarangay));
} else {
//// if address is overseas set initial value for country
2023-06-06 06:54:51 +00:00
selectedCountry = globalCountries.firstWhere((Country element) =>
event.personalReference.address!.country!.id == element.id);
emit(EditReferenceState(
2023-06-06 06:54:51 +00:00
selectedCountry: selectedCountry,
selectedCategory: selectedCategory,
selectedRegion: null,
ref: event.personalReference,
countries: globalCountries,
regions: globalRegions,
categories: globalCategories,
isOverseas: overseas));
}
} catch (e) {
emit(ReferencesErrorState(message: e.toString()));
}
});
//// CALL THE ERROR STATE EVEN T
on<CallErrorState>((event, emit) async {
emit(const ReferencesErrorState(
message: "Something went wrong. Please try again"));
//// EDIT REFERENCES EVENT
2023-06-06 06:54:51 +00:00
});
on<EditReference>((event, emit) async {
Map<dynamic, dynamic> status = await ReferencesServices.instace.update(
ref: event.reference, token: event.token, profileId: event.profileId);
if (status['success']) {
PersonalReference ref = PersonalReference.fromJson(status['data']);
references.removeWhere(
(PersonalReference element) => element.id == event.reference.id);
references.add(ref);
emit(ReferenceEditedState(response: status));
} else {
emit(ReferenceEditedState(response: status));
}
});
//// add reference event
on<AddReference>((event, emit) async {
try {
Map<dynamic, dynamic> status = await ReferencesServices.instace
.addReference(
ref: event.reference,
token: event.token,
profileId: event.profileId);
if (status['success']) {
PersonalReference ref = PersonalReference.fromJson(status['data']);
references.add(ref);
2023-06-06 06:54:51 +00:00
emit(ReferencesAddedState(response: status));
} else {
2023-06-06 06:54:51 +00:00
emit(ReferencesAddedState(response: status));
}
} catch (e) {
emit(ReferencesErrorState(message: e.toString()));
}
});
////LOAD REFERENCE
on<LoadReferences>((event, emit) {
emit(ReferencesLoadingState());
emit(ReferencesLoadedState(references: references));
});
2023-06-06 06:54:51 +00:00
////DELETE REFERENCE
on<DeleteReference>((event, emit) async {
try {
final bool success = await ReferencesServices.instace.delete(
profileId: event.profileId, token: event.token, id: event.refId);
if (success) {
event.references.removeWhere(
(PersonalReference element) => element.id == event.refId);
references = event.references;
2023-06-06 06:54:51 +00:00
emit(DeleteReferenceState(success: success));
} else {
2023-04-11 01:27:53 +00:00
emit(DeleteReferenceState(success: success));
}
} catch (e) {
emit(ReferencesErrorState(message: e.toString()));
}
});
}
}