Calender Event Fixes Function Changes

dev_v3.13.6_BLE_Faiz
Aamir.Muhammad 1 year ago
parent e9404f1656
commit be4022cee5

@ -44,7 +44,7 @@ class _PrescriptionDetailsPageState extends State<PrescriptionDetailsPage> {
@override
void initState() {
checkIfHasReminder();
checkAndRemove(delete: false);
locationUtils = new LocationUtils(isShowConfirmDialog: true, context: context);
WidgetsBinding.instance.addPostFrameCallback((_) => locationUtils.getCurrentLocation());
super.initState();
@ -208,7 +208,7 @@ class _PrescriptionDetailsPageState extends State<PrescriptionDetailsPage> {
hasReminder ? TranslationBase.of(context).cancelReminder : TranslationBase.of(context).addReminder,
() {
if (hasReminder) {
cancelReminders();
checkAndRemove(delete: true);
} else {
DateTime startDate = DateTime.now();
DateTime endDate = DateTime(startDate.year, startDate.month, startDate.day + widget.prescriptionReport!.days!);
@ -246,97 +246,151 @@ class _PrescriptionDetailsPageState extends State<PrescriptionDetailsPage> {
);
}
checkIfHasReminder() async {
// checkAndRemove({bool delete = false}) async {
// final ios.CalendarPlugin _myPlugin = ios.CalendarPlugin();
// CalendarUtils calendarUtils = await CalendarUtils.getInstance();
// DateTime startEventsDate = Jiffy.parseFromDateTime(DateTime.now()).subtract(days: 30).dateTime;
// DateTime endEventsDate = Jiffy.parseFromDateTime(DateTime.now()).add(days: 120).dateTime;
// RetrieveEventsParams params = new RetrieveEventsParams(startDate: startEventsDate, endDate: endEventsDate);
//
// if (calendarUtils.calendars != null) {
// print(calendarUtils.calendars);
// if (Platform.isAndroid) {
// Future.forEach(
// calendarUtils.calendars,
// (Calendar calender) => {
// calendarUtils.retrieveEvents(calender.id!, params).then((value) {
// Result<UnmodifiableListView<Event>> events = value;
// events.data!.forEach((element) {
// if (delete) {
// if (element.title!.contains(widget.prescriptionReport!.itemDescriptionN!)) calendarUtils.deleteEvent(calender, element);
// AppToast.showSuccessToast(message: TranslationBase.of(context).reminderCancelSuccess);
// setState(() {
// hasReminder = false;
// });
// } else {
// if (element.title!.contains(widget.prescriptionReport!.itemDescriptionN!))
// setState(() {
// hasReminder = true;
// });
// }
// });
// }),
// });
// } else {
// List<ios.Calendar>? iosCall = await _myPlugin.getCalendars();
// Future.forEach(
// iosCall!,
// (ios.Calendar cal) async => {
// print(cal),
// calendarUtils.retrieveEvents(cal.id!, params).then((value) {
// Result<UnmodifiableListView<Event>> events = value;
// events.data!.forEach((element) {
// if (delete) {
// if (element.title!.contains(widget.prescriptionReport!.itemDescriptionN!)) calendarUtils.deleteEvent(Calendar(id: cal.id, name: cal.name, accountName: cal.accountName), element);
// AppToast.showSuccessToast(message: TranslationBase.of(context).reminderCancelSuccess);
// setState(() {
// hasReminder = false;
// });
// } else {
// if (element.title!.contains(widget.prescriptionReport!.itemDescriptionN!))
// setState(() {
// hasReminder = true;
// });
// }
// });
// }),
// },
// );
// }
// }
// }
Future<void> checkAndRemove({bool delete = false}) async {
final ios.CalendarPlugin _myPlugin = ios.CalendarPlugin();
CalendarUtils calendarUtils = await CalendarUtils.getInstance();
print("If Has Reminder");
DateTime startEventsDate = Jiffy.parseFromDateTime(DateTime.now()).subtract(days: 30).dateTime;
DateTime endEventsDate = Jiffy.parseFromDateTime(DateTime.now()).add(days: 120).dateTime;
RetrieveEventsParams params = new RetrieveEventsParams(startDate: startEventsDate, endDate: endEventsDate);
RetrieveEventsParams params = RetrieveEventsParams(startDate: startEventsDate, endDate: endEventsDate);
if (calendarUtils.calendars != null) {
print(calendarUtils.calendars);
if (Platform.isAndroid) {
Future.forEach(
calendarUtils.calendars,
(Calendar calender) => {
calendarUtils.retrieveEvents(calender.id!, params).then((value) {
Result<UnmodifiableListView<Event>> events = value;
events.data!.forEach((element) {
if (element.title!.contains(widget.prescriptionReport!.itemDescriptionN!))
setState(() {
hasReminder = true;
});
});
}),
});
await processEvents(calendarUtils.calendars, calendarUtils, params, delete);
} else {
List<ios.Calendar>? iosCall = await _myPlugin.getCalendars();
Future.forEach(
iosCall!,
(ios.Calendar cal) async => {
print(cal),
calendarUtils.retrieveEvents(cal.id!, params).then((value) {
Result<UnmodifiableListView<Event>> events = value;
events.data!.forEach((element) {
if (element.title!.contains(widget.prescriptionReport!.itemDescriptionN!))
setState(() {
hasReminder = true;
});
});
}),
},
);
List<ios.Calendar>? iosCalendars = await _myPlugin.getCalendars();
if (iosCalendars != null) {
await processEvents(iosCalendars.map((cal) => Calendar(id: cal.id, name: cal.name, accountName: cal.accountName)).toList(), calendarUtils, params, delete);
}
}
}
}
cancelReminders() async {
CalendarUtils calendarUtils = await CalendarUtils.getInstance();
final ios.CalendarPlugin _myPlugin = ios.CalendarPlugin();
DateTime startEventsDate = Jiffy.parseFromDateTime(DateTime.now()).subtract(days: 30).dateTime;
DateTime endEventsDate = Jiffy.parseFromDateTime(DateTime.now()).add(days: 120).dateTime;
RetrieveEventsParams params = RetrieveEventsParams(startDate: startEventsDate, endDate: endEventsDate);
if (Platform.isAndroid) {
Future.forEach(
calendarUtils.calendars,
(Calendar calender) => {
calendarUtils.retrieveEvents(calender.id!, params).then((value) {
Result<UnmodifiableListView<Event>> events = value;
events.data!.forEach((element) {
if (element.title!.contains(widget.prescriptionReport!.itemDescriptionN!)) calendarUtils.deleteEvent(calender, element);
});
}),
});
} else {
List<ios.Calendar>? iosCal = await _myPlugin.getCalendars();
Future.forEach(
iosCal!,
(ios.Calendar cale) async => {
calendarUtils.retrieveEvents(cale.id!, params).then((value) {
Result<UnmodifiableListView<Event>> events = value;
events.data!.forEach((element) {
print(element.title);
if (element.title!.contains(widget.prescriptionReport!.itemDescriptionN!))
calendarUtils.deleteEvent(
Calendar(
id: cale.id,
name: cale.name,
accountName: cale.accountName,
),
element);
});
}),
});
AppToast.showSuccessToast(message: TranslationBase.of(context).reminderCancelSuccess);
setState(() {
hasReminder = false;
});
Future<void> processEvents(List<Calendar> calendars, calendarUtils, params, delete) async {
for (var calendar in calendars) {
Result<UnmodifiableListView<Event>> events = await calendarUtils.retrieveEvents(calendar.id!, params);
for (var event in events.data!) {
if (event.title!.contains(widget.prescriptionReport!.itemDescriptionN!)) {
if (delete) {
await calendarUtils.deleteEvent(calendar, event);
AppToast.showSuccessToast(message: TranslationBase.of(context).reminderCancelSuccess);
setState(() {
hasReminder = false;
});
} else {
setState(() {
hasReminder = true;
});
}
}
}
}
}
// cancelReminders() async {
// CalendarUtils calendarUtils = await CalendarUtils.getInstance();
// final ios.CalendarPlugin _myPlugin = ios.CalendarPlugin();
// DateTime startEventsDate = Jiffy.parseFromDateTime(DateTime.now()).subtract(days: 30).dateTime;
// DateTime endEventsDate = Jiffy.parseFromDateTime(DateTime.now()).add(days: 120).dateTime;
// RetrieveEventsParams params = RetrieveEventsParams(startDate: startEventsDate, endDate: endEventsDate);
//
// if (Platform.isAndroid) {
// Future.forEach(
// calendarUtils.calendars,
// (Calendar calender) => {
// calendarUtils.retrieveEvents(calender.id!, params).then((value) {
// Result<UnmodifiableListView<Event>> events = value;
// events.data!.forEach((element) {
// if (element.title!.contains(widget.prescriptionReport!.itemDescriptionN!)) calendarUtils.deleteEvent(calender, element);
// });
// }),
// });
// } else {
// List<ios.Calendar>? iosCal = await _myPlugin.getCalendars();
// Future.forEach(
// iosCal!,
// (ios.Calendar cale) async => {
// calendarUtils.retrieveEvents(cale.id!, params).then((value) {
// Result<UnmodifiableListView<Event>> events = value;
// events.data!.forEach((element) {
// print(element.title);
// if (element.title!.contains(widget.prescriptionReport!.itemDescriptionN!))
// calendarUtils.deleteEvent(
// Calendar(
// id: cale.id,
// name: cale.name,
// accountName: cale.accountName,
// ),
// element);
// });
// }),
// });
// AppToast.showSuccessToast(message: TranslationBase.of(context).reminderCancelSuccess);
// setState(() {
// hasReminder = false;
// });
// }
// }
setCalender(BuildContext context, String eventId, int reminderIndex) async {
DateTime actualDate = DateTime(DateTime.now().year, DateTime.now().month, DateTime.now().day, 8, 0);
if (widget.prescriptionReport?.frequencyNumber == null) widget.prescriptionReport!.frequencyNumber = 2; //Some time frequency number is null so by default will be 2

Loading…
Cancel
Save