2023-08-30 01:55:03 +00:00
|
|
|
import 'dart:io';
|
|
|
|
|
2023-03-02 05:28:33 +00:00
|
|
|
import 'package:bloc/bloc.dart';
|
|
|
|
import 'package:equatable/equatable.dart';
|
2023-08-30 01:55:03 +00:00
|
|
|
import 'package:fluttertoast/fluttertoast.dart';
|
|
|
|
import 'package:path_provider/path_provider.dart';
|
|
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
|
|
import 'package:share_plus/share_plus.dart';
|
|
|
|
import 'package:unit2/screens/profile/components/eligibility/eligibility_view_attachment.dart';
|
2023-08-07 06:33:38 +00:00
|
|
|
import 'package:unit2/utils/attachment_services.dart';
|
2023-03-07 02:31:28 +00:00
|
|
|
import '../../../model/location/city.dart';
|
|
|
|
import '../../../model/location/country.dart';
|
|
|
|
import '../../../model/location/provinces.dart';
|
|
|
|
import '../../../model/location/region.dart';
|
2023-08-01 08:20:38 +00:00
|
|
|
import '../../../model/profile/attachment.dart';
|
2023-03-07 02:31:28 +00:00
|
|
|
import '../../../model/profile/eligibility.dart';
|
|
|
|
import '../../../model/utils/eligibility.dart';
|
|
|
|
import '../../../sevices/profile/eligibility_services.dart';
|
|
|
|
import '../../../utils/location_utilities.dart';
|
|
|
|
import '../../../utils/profile_utilities.dart';
|
2023-08-30 01:55:03 +00:00
|
|
|
import '../../../utils/request_permission.dart';
|
2023-08-27 08:38:05 +00:00
|
|
|
import '../../../utils/urls.dart';
|
2023-03-02 05:28:33 +00:00
|
|
|
part 'eligibility_event.dart';
|
|
|
|
part 'eligibility_state.dart';
|
|
|
|
|
|
|
|
class EligibilityBloc extends Bloc<EligibilityEvent, EligibilityState> {
|
|
|
|
EligibilityBloc() : super(EligibilityInitial()) {
|
2023-04-25 07:50:36 +00:00
|
|
|
List<Country> globalCountries = [];
|
|
|
|
List<Region> globalRegions = [];
|
2023-04-11 01:27:53 +00:00
|
|
|
List<Eligibility> globalEligibilities = [];
|
|
|
|
List<EligibityCert> eligibilities = [];
|
2023-08-01 08:20:38 +00:00
|
|
|
List<AttachmentCategory> attachmentCategories = [];
|
2023-04-11 01:27:53 +00:00
|
|
|
//// LOAD ELIGIBILTY
|
2023-03-02 05:28:33 +00:00
|
|
|
on<LoadEligibility>((event, emit) {
|
|
|
|
emit(EligibilityLoadingState());
|
2023-08-01 08:20:38 +00:00
|
|
|
emit(EligibilityLoaded(
|
|
|
|
eligibilities: eligibilities,
|
|
|
|
attachmentCategory: attachmentCategories));
|
2023-04-11 01:27:53 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
//// DELETE
|
|
|
|
on<DeleteEligibility>((event, emit) async {
|
|
|
|
try {
|
|
|
|
final bool success = await EligibilityService.instance.delete(
|
|
|
|
eligibilityId: event.eligibilityId,
|
|
|
|
profileId: int.parse(event.profileId),
|
|
|
|
token: event.token);
|
|
|
|
if (success) {
|
2023-06-06 06:54:51 +00:00
|
|
|
eligibilities.removeWhere(
|
2023-04-11 01:27:53 +00:00
|
|
|
((EligibityCert element) => element.id == event.eligibilityId));
|
2023-08-07 06:33:38 +00:00
|
|
|
emit(EligibilityDeletedState(
|
2023-04-11 01:27:53 +00:00
|
|
|
success: success,
|
|
|
|
));
|
|
|
|
} else {
|
2023-08-07 06:33:38 +00:00
|
|
|
emit(EligibilityDeletedState(success: success));
|
2023-04-11 01:27:53 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
emit(EligibilityErrorState(message: e.toString()));
|
|
|
|
}
|
2023-03-02 05:28:33 +00:00
|
|
|
});
|
2023-04-11 01:27:53 +00:00
|
|
|
|
|
|
|
//// GET ELIGIBILITY
|
2023-03-02 05:28:33 +00:00
|
|
|
on<GetEligibilities>((event, emit) async {
|
2023-08-27 08:38:05 +00:00
|
|
|
emit(EligibilityLoadingState());
|
2023-03-02 05:28:33 +00:00
|
|
|
try {
|
2023-08-01 08:20:38 +00:00
|
|
|
if (attachmentCategories.isEmpty) {
|
|
|
|
attachmentCategories =
|
|
|
|
await AttachmentServices.instance.getCategories();
|
|
|
|
}
|
2023-04-11 01:27:53 +00:00
|
|
|
if (eligibilities.isNotEmpty) {
|
2023-08-01 08:20:38 +00:00
|
|
|
emit(EligibilityLoaded(
|
|
|
|
eligibilities: eligibilities,
|
|
|
|
attachmentCategory: attachmentCategories));
|
2023-03-02 05:28:33 +00:00
|
|
|
} else {
|
|
|
|
emit(EligibilityLoadingState());
|
|
|
|
eligibilities = await EligibilityService.instance
|
|
|
|
.getEligibilities(event.profileId, event.token);
|
2023-08-01 08:20:38 +00:00
|
|
|
emit(EligibilityLoaded(
|
|
|
|
eligibilities: eligibilities,
|
|
|
|
attachmentCategory: attachmentCategories));
|
2023-03-02 05:28:33 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
emit(EligibilityErrorState(message: e.toString()));
|
|
|
|
}
|
|
|
|
});
|
2023-04-11 01:27:53 +00:00
|
|
|
//// SHOW EDIT FORM
|
2023-03-02 05:28:33 +00:00
|
|
|
on<ShowEditEligibilityForm>((event, emit) async {
|
|
|
|
try {
|
2023-04-25 07:50:36 +00:00
|
|
|
if (globalCountries.isEmpty) {
|
2023-03-02 05:28:33 +00:00
|
|
|
List<Country> countries = await LocationUtils.instance.getCountries();
|
|
|
|
globalCountries = countries;
|
|
|
|
}
|
2023-04-25 07:50:36 +00:00
|
|
|
if (globalRegions.isEmpty) {
|
2023-03-02 05:28:33 +00:00
|
|
|
List<Region> regions = await LocationUtils.instance.getRegions();
|
|
|
|
globalRegions = regions;
|
|
|
|
}
|
2023-04-11 01:27:53 +00:00
|
|
|
if (globalEligibilities.isEmpty) {
|
2023-03-02 05:28:33 +00:00
|
|
|
List<Eligibility> eligibilities =
|
|
|
|
await ProfileUtilities.instance.getEligibilities();
|
|
|
|
globalEligibilities = eligibilities;
|
|
|
|
}
|
2023-04-11 01:27:53 +00:00
|
|
|
Eligibility currentEligibility = globalEligibilities.firstWhere(
|
2023-03-02 05:28:33 +00:00
|
|
|
(Eligibility eligibility) =>
|
|
|
|
event.eligibityCert.eligibility!.id == eligibility.id);
|
|
|
|
bool? isOverseas = event.eligibityCert.overseas;
|
2023-06-06 06:54:51 +00:00
|
|
|
Country currentCountry = globalCountries.firstWhere((Country country) =>
|
|
|
|
event.eligibityCert.examAddress!.country!.code == country.code);
|
2023-03-02 05:28:33 +00:00
|
|
|
if (event.eligibityCert.examAddress?.cityMunicipality?.province
|
|
|
|
?.region !=
|
|
|
|
null) {
|
2023-04-25 07:50:36 +00:00
|
|
|
Region currrentRegion = globalRegions.firstWhere((Region region) =>
|
2023-03-02 05:28:33 +00:00
|
|
|
event.eligibityCert.examAddress!.cityMunicipality!.province!
|
|
|
|
.region!.code ==
|
|
|
|
region.code);
|
|
|
|
List<Province> provinces = await LocationUtils.instance
|
|
|
|
.getProvinces(regionCode: currrentRegion.code.toString());
|
|
|
|
Province currentProvince = provinces.firstWhere((Province province) =>
|
|
|
|
event.eligibityCert.examAddress!.cityMunicipality!.province!
|
|
|
|
.code ==
|
|
|
|
province.code);
|
|
|
|
List<CityMunicipality> cities = await LocationUtils.instance
|
|
|
|
.getCities(code: currentProvince.code.toString());
|
|
|
|
CityMunicipality currentCity = cities.firstWhere(
|
|
|
|
(CityMunicipality cityMunicipality) =>
|
|
|
|
event.eligibityCert.examAddress!.cityMunicipality!.code ==
|
|
|
|
cityMunicipality.code);
|
|
|
|
|
|
|
|
emit(EditEligibilityState(
|
|
|
|
currentCity: currentCity,
|
|
|
|
selectedCountry: currentCountry,
|
|
|
|
currentProvince: currentProvince,
|
|
|
|
currentRegion: currrentRegion,
|
|
|
|
currentEligibility: currentEligibility,
|
|
|
|
provinces: provinces,
|
|
|
|
cities: cities,
|
|
|
|
isOverseas: isOverseas!,
|
|
|
|
eligibityCert: event.eligibityCert,
|
2023-04-25 07:50:36 +00:00
|
|
|
countries: globalCountries,
|
|
|
|
regions: globalRegions,
|
2023-04-11 01:27:53 +00:00
|
|
|
eligibilities: globalEligibilities));
|
2023-03-02 05:28:33 +00:00
|
|
|
} else {
|
|
|
|
emit(EditEligibilityState(
|
|
|
|
selectedCountry: currentCountry,
|
|
|
|
currentCity: null,
|
|
|
|
currentProvince: null,
|
|
|
|
currentRegion: null,
|
|
|
|
provinces: null,
|
|
|
|
cities: null,
|
|
|
|
currentEligibility: currentEligibility,
|
|
|
|
isOverseas: isOverseas!,
|
|
|
|
eligibityCert: event.eligibityCert,
|
2023-04-25 07:50:36 +00:00
|
|
|
countries: globalCountries,
|
|
|
|
regions: globalRegions,
|
2023-04-11 01:27:53 +00:00
|
|
|
eligibilities: globalEligibilities));
|
2023-03-02 05:28:33 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
emit(EligibilityErrorState(message: e.toString()));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-04-11 01:27:53 +00:00
|
|
|
//// UPDATE
|
2023-03-02 05:28:33 +00:00
|
|
|
on<UpdateEligibility>((event, emit) async {
|
|
|
|
try {
|
|
|
|
Map<dynamic, dynamic> status = await EligibilityService.instance.update(
|
|
|
|
eligibityCert: event.eligibityCert,
|
|
|
|
token: event.token,
|
|
|
|
profileId: int.parse(event.profileId),
|
|
|
|
oldEligibility: event.oldEligibility);
|
|
|
|
if (status['success']) {
|
|
|
|
EligibityCert newEligibility = EligibityCert.fromJson(status['data']);
|
2023-04-11 01:27:53 +00:00
|
|
|
eligibilities.removeWhere(
|
2023-03-02 05:28:33 +00:00
|
|
|
(EligibityCert element) => element.id == event.eligibityCert.id);
|
2023-04-11 01:27:53 +00:00
|
|
|
eligibilities.add(newEligibility);
|
|
|
|
emit(EligibilityEditedState(response: status));
|
2023-03-02 05:28:33 +00:00
|
|
|
} else {
|
2023-04-11 01:27:53 +00:00
|
|
|
emit(EligibilityEditedState(response: status));
|
2023-03-02 05:28:33 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
emit(EligibilityErrorState(message: e.toString()));
|
|
|
|
}
|
|
|
|
});
|
2023-04-11 01:27:53 +00:00
|
|
|
//// SHOW ADD FORM
|
2023-03-02 05:28:33 +00:00
|
|
|
on<ShowAddEligibilityForm>((event, emit) async {
|
|
|
|
emit(EligibilityLoadingState());
|
2023-06-06 06:54:51 +00:00
|
|
|
try {
|
|
|
|
if (globalRegions.isEmpty) {
|
|
|
|
List<Region> regions = await LocationUtils.instance.getRegions();
|
|
|
|
globalRegions = regions;
|
|
|
|
}
|
|
|
|
if (globalEligibilities.isEmpty) {
|
|
|
|
List<Eligibility> eligibilities =
|
|
|
|
await ProfileUtilities.instance.getEligibilities();
|
|
|
|
globalEligibilities = eligibilities;
|
|
|
|
}
|
|
|
|
if (globalCountries.isEmpty) {
|
|
|
|
List<Country> countries = await LocationUtils.instance.getCountries();
|
|
|
|
globalCountries = countries;
|
|
|
|
}
|
2023-03-02 05:28:33 +00:00
|
|
|
|
2023-06-06 06:54:51 +00:00
|
|
|
emit(AddEligibilityState(
|
|
|
|
eligibilities: globalEligibilities,
|
|
|
|
regions: globalRegions,
|
|
|
|
countries: globalCountries));
|
|
|
|
} catch (e) {
|
|
|
|
emit(EligibilityErrorState(message: e.toString()));
|
|
|
|
}
|
2023-03-02 05:28:33 +00:00
|
|
|
});
|
2023-04-11 01:27:53 +00:00
|
|
|
|
|
|
|
//// ADD
|
2023-03-02 05:28:33 +00:00
|
|
|
on<AddEligibility>(
|
|
|
|
(event, emit) async {
|
|
|
|
try {
|
|
|
|
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']);
|
2023-04-11 01:27:53 +00:00
|
|
|
eligibilities.add(eligibityCert);
|
|
|
|
emit(EligibilityAddedState(response: status));
|
2023-03-02 05:28:33 +00:00
|
|
|
} else {
|
2023-04-11 01:27:53 +00:00
|
|
|
emit(EligibilityAddedState(response: status));
|
2023-03-02 05:28:33 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
emit(EligibilityErrorState(message: e.toString()));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
on<CallErrorState>((event, emit) {
|
|
|
|
emit(const EligibilityErrorState(
|
|
|
|
message: "Something went wrong. Please try again"));
|
|
|
|
});
|
2023-08-01 08:20:38 +00:00
|
|
|
////Add attachment
|
2023-08-07 06:33:38 +00:00
|
|
|
on<AddEligibiltyAttachment>((event, emit) async {
|
2023-08-01 08:20:38 +00:00
|
|
|
emit(EligibilityLoadingState());
|
2023-08-07 06:33:38 +00:00
|
|
|
List<Attachment> attachments = [];
|
|
|
|
EligibityCert eligibityCert = eligibilities.firstWhere(
|
|
|
|
(element) => element.id.toString() == event.attachmentModule);
|
2023-08-01 08:20:38 +00:00
|
|
|
try {
|
|
|
|
Map<dynamic, dynamic> status = await AttachmentServices.instance
|
|
|
|
.attachment(
|
|
|
|
categoryId: event.categoryId,
|
|
|
|
module: event.attachmentModule,
|
|
|
|
paths: event.filePaths,
|
|
|
|
token: event.token,
|
|
|
|
profileId: event.profileId);
|
|
|
|
if (status['success']) {
|
2023-08-07 06:33:38 +00:00
|
|
|
status['data'].forEach((element) {
|
|
|
|
Attachment newAttachment = Attachment.fromJson(element);
|
|
|
|
attachments.add(newAttachment);
|
|
|
|
});
|
|
|
|
eligibityCert.attachments == null
|
|
|
|
? eligibityCert.attachments = attachments
|
|
|
|
: eligibityCert.attachments = [
|
|
|
|
...eligibityCert.attachments!,
|
|
|
|
...attachments
|
|
|
|
];
|
|
|
|
emit(EligibilityAttachmentAddedState(response: status));
|
2023-08-01 08:20:38 +00:00
|
|
|
} else {
|
2023-08-07 06:33:38 +00:00
|
|
|
emit(EligibilityAttachmentAddedState(response: status));
|
2023-08-01 08:20:38 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
emit(EligibilityErrorState(message: e.toString()));
|
|
|
|
}
|
|
|
|
});
|
2023-08-07 06:33:38 +00:00
|
|
|
|
|
|
|
on<DeleteEligibyAttachment>((event, emit) async {
|
|
|
|
emit(EligibilityLoadingState());
|
2023-08-27 08:38:05 +00:00
|
|
|
try {
|
2023-08-07 06:33:38 +00:00
|
|
|
final bool success = await AttachmentServices.instance.deleteAttachment(
|
2023-08-01 08:20:38 +00:00
|
|
|
attachment: event.attachment,
|
2023-08-07 06:33:38 +00:00
|
|
|
moduleId: int.parse(event.moduleId),
|
|
|
|
profileId: event.profileId.toString(),
|
2023-08-01 08:20:38 +00:00
|
|
|
token: event.token);
|
|
|
|
if (success) {
|
2023-08-07 06:33:38 +00:00
|
|
|
final EligibityCert eligibityCert = eligibilities
|
|
|
|
.firstWhere((element) => element.id.toString() == event.moduleId);
|
|
|
|
eligibityCert.attachments
|
|
|
|
?.removeWhere((element) => element.id == event.attachment.id);
|
|
|
|
eligibilities.removeWhere(
|
|
|
|
(element) => element.id.toString() == event.moduleId);
|
|
|
|
eligibilities.add(eligibityCert);
|
|
|
|
emit(EligibilitytAttachmentDeletedState(success: success));
|
2023-08-01 08:20:38 +00:00
|
|
|
} else {
|
2023-08-07 06:33:38 +00:00
|
|
|
emit(EligibilitytAttachmentDeletedState(success: success));
|
2023-08-01 08:20:38 +00:00
|
|
|
}
|
2023-08-27 08:38:05 +00:00
|
|
|
} catch (e) {
|
|
|
|
emit(EligibilityErrorState(message: e.toString()));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
on<EligibiltyViewAttachmentEvent>((event,emit){
|
2023-09-05 08:44:48 +00:00
|
|
|
String fileUrl = '${Url.instance.prefixHost()}://${Url.instance.host()}/media/${event.source}';
|
2023-08-30 01:55:03 +00:00
|
|
|
emit(EligibilityAttachmentViewState(fileUrl: fileUrl,fileName: event.filename));
|
|
|
|
});
|
|
|
|
on<ShareAttachment>((event, emit) async {
|
|
|
|
emit(EligibilityLoadingState());
|
|
|
|
Directory directory;
|
|
|
|
String? appDocumentPath;
|
|
|
|
if (await requestPermission(Permission.storage)) {
|
|
|
|
directory = await getApplicationDocumentsDirectory();
|
|
|
|
appDocumentPath = directory.path;
|
|
|
|
}
|
|
|
|
try{
|
|
|
|
final bool success = await AttachmentServices.instance.downloadAttachment(
|
|
|
|
filename: event.fileName,
|
|
|
|
source: event.source,
|
|
|
|
downLoadDir: appDocumentPath!);
|
|
|
|
if (success) {
|
|
|
|
final result = await Share.shareXFiles(
|
|
|
|
[XFile("$appDocumentPath/${event.fileName}")]);
|
|
|
|
if (result.status == ShareResultStatus.success) {
|
|
|
|
Fluttertoast.showToast(msg: "Attachment shared successful");
|
|
|
|
emit(EligibilityAttachmentViewState(
|
|
|
|
fileUrl: event.source, fileName: event.fileName));
|
|
|
|
} else {
|
|
|
|
Fluttertoast.showToast(msg: "Attachment shared unsuccessful");
|
|
|
|
emit(EligibilityAttachmentViewState(
|
|
|
|
fileUrl: event.source, fileName: event.fileName));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
emit(EligibilityAttachmentViewState(
|
|
|
|
fileUrl: event.source, fileName: event.fileName));
|
|
|
|
}
|
|
|
|
}catch(e){
|
|
|
|
emit(EligibilityErrorState(message: e.toString()));
|
|
|
|
}
|
2023-08-01 08:20:38 +00:00
|
|
|
});
|
2023-03-02 05:28:33 +00:00
|
|
|
}
|
|
|
|
}
|