From 515896c26574371eca6f467199322f6a70a04134 Mon Sep 17 00:00:00 2001 From: "Aamir.Muhammad" <> Date: Sun, 30 Jun 2024 16:42:36 +0300 Subject: [PATCH] Calender Event Fixes --- .../widgets/reminder_dialog.dart | 22 ++- .../prescription_details_page.dart | 148 +++++++++++------- lib/uitl/CalendarUtils.dart | 64 +++++--- 3 files changed, 140 insertions(+), 94 deletions(-) diff --git a/lib/pages/BookAppointment/widgets/reminder_dialog.dart b/lib/pages/BookAppointment/widgets/reminder_dialog.dart index 25e3a14c..a16876db 100644 --- a/lib/pages/BookAppointment/widgets/reminder_dialog.dart +++ b/lib/pages/BookAppointment/widgets/reminder_dialog.dart @@ -5,6 +5,8 @@ import 'package:diplomaticquarterapp/locator.dart'; import 'package:diplomaticquarterapp/services/permission/permission_service.dart'; import 'package:diplomaticquarterapp/theme/colors.dart'; import 'package:diplomaticquarterapp/uitl/CalendarUtils.dart'; +import 'package:diplomaticquarterapp/uitl/date_uitl.dart'; +import 'package:diplomaticquarterapp/uitl/gif_loader_dialog_utils.dart'; import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart'; import 'package:diplomaticquarterapp/uitl/utils.dart'; import 'package:diplomaticquarterapp/uitl/utils_new.dart'; @@ -19,16 +21,16 @@ Future> requestPermissions() async { } showReminderDialog(BuildContext context, DateTime dateTime, String doctorName, String eventId, String appoDateFormatted, String appoTimeFormatted, - {required Function onSuccess, String? title, String? description, Function(int)? onMultiDateSuccess}) async { + {required Function() onSuccess, String? title, String? description, Function(int)? onMultiDateSuccess, bool isMultiAllowed = false}) async { if (Platform.isAndroid) { if (await PermissionService.isCalendarPermissionEnabled()) { _showReminderDialog(context, dateTime, doctorName, eventId, appoDateFormatted, appoTimeFormatted, - onSuccess: onSuccess, title: title, description: description, onMultiDateSuccess: onMultiDateSuccess ?? (int) {}); + onSuccess: onSuccess, title: title, description: description, onMultiDateSuccess: onMultiDateSuccess, isMultiAllowed: isMultiAllowed); } else { Utils.showPermissionConsentDialog(context, TranslationBase.of(context).calendarPermission, () async { if (await Permission.calendarFullAccess.request().isGranted) { _showReminderDialog(context, dateTime, doctorName, eventId, appoDateFormatted, appoTimeFormatted, - onSuccess: onSuccess, title: title, description: description, onMultiDateSuccess: onMultiDateSuccess ?? (int) {}); + onSuccess: onSuccess, title: title, description: description, onMultiDateSuccess: onMultiDateSuccess, isMultiAllowed: isMultiAllowed); } }); } @@ -36,14 +38,14 @@ showReminderDialog(BuildContext context, DateTime dateTime, String doctorName, S if (await Permission.calendarWriteOnly.request().isGranted) { if (await Permission.calendarFullAccess.request().isGranted) { _showReminderDialog(context, dateTime, doctorName, eventId, appoDateFormatted, appoTimeFormatted, - onSuccess: onSuccess, title: title, description: description, onMultiDateSuccess: onMultiDateSuccess ?? (int) {}); + onSuccess: onSuccess, title: title, description: description, onMultiDateSuccess: onMultiDateSuccess, isMultiAllowed: isMultiAllowed); } } } } Future _showReminderDialog(BuildContext context, DateTime dateTime, String doctorName, String eventId, String appoDateFormatted, String appoTimeFormatted, - {required Function onSuccess, String? title, String? description, required Function(int) onMultiDateSuccess}) async { + {required Function onSuccess, String? title, String? description, Function(int)? onMultiDateSuccess, bool? isMultiAllowed}) async { return showDialog( context: context, barrierDismissible: true, // user must tap button! @@ -75,22 +77,18 @@ Future _showReminderDialog(BuildContext context, DateTime dateTime, String dateTime = Jiffy.parseFromDateTime(dateTime).subtract(hours: 2).dateTime; text = "2 hours"; } - - if (onMultiDateSuccess.call(i) == null) { - try { + if (!isMultiAllowed!) { + if (onMultiDateSuccess == null) { CalendarUtils calendarUtils = await CalendarUtils.getInstance(); await calendarUtils.createOrUpdateEvent( title: title ?? TranslationBase.of(context).reminderTitle + " " + doctorName, description: description ?? "At " + appoDateFormatted + " " + appoTimeFormatted, scheduleDateTime: dateTime, eventId: eventId); - onSuccess(); - } catch (e) { - print(e); } } else { - onMultiDateSuccess(i); + onMultiDateSuccess!(i); } locator().appointment.appointment_reminder_time(reminde_before: text); }, diff --git a/lib/pages/medical/prescriptions/prescription_details_page.dart b/lib/pages/medical/prescriptions/prescription_details_page.dart index ea2f0e67..7455f030 100644 --- a/lib/pages/medical/prescriptions/prescription_details_page.dart +++ b/lib/pages/medical/prescriptions/prescription_details_page.dart @@ -30,7 +30,7 @@ class PrescriptionDetailsPage extends StatefulWidget { final PrescriptionReport? prescriptionReport; final Prescriptions? prescriptions; - PrescriptionDetailsPage({Key? key, this.prescriptionReport, this.prescriptions}); + PrescriptionDetailsPage({Key? key, this.prescriptionReport, this.prescriptions}); @override _PrescriptionDetailsPageState createState() => _PrescriptionDetailsPageState(); @@ -222,6 +222,7 @@ class _PrescriptionDetailsPageState extends State { onSuccess: () { AppToast.showSuccessToast(message: TranslationBase.of(context).reminderSuccess); }, + isMultiAllowed: true, onMultiDateSuccess: (int selectedIndex) { setCalender(context, widget.prescriptionReport!.itemID.toString(), selectedIndex); }, @@ -245,22 +246,28 @@ class _PrescriptionDetailsPageState extends State { checkIfHasReminder() async { 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); if (calendarUtils.calendars != null) { - await calendarUtils.retrieveEvents(calendarUtils.calendars[0].id!, params).then((value) { - Result> events = value; - events.data!.forEach((element) { - if (element.title!.contains(widget.prescriptionReport!.itemDescriptionN!)) - setState(() { - hasReminder = true; - }); - }); - }); + print(calendarUtils.calendars); + Future.forEach( + calendarUtils.calendars, + (Calendar calender) => { + calendarUtils.retrieveEvents(calender.id!, params).then((value) { + Result> events = value; + events.data!.forEach((element) { + print(widget.prescriptionReport!.itemDescriptionN!); + if (element.title!.contains(widget.prescriptionReport!.itemDescriptionN!)) + setState(() { + hasReminder = true; + }); + }); + }), + }); } } @@ -269,15 +276,25 @@ class _PrescriptionDetailsPageState extends State { 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); + + Future.forEach( + calendarUtils.calendars, + (Calendar calender) => { + calendarUtils.retrieveEvents(calender.id!, params).then((value) { + Result> events = value; + events.data!.forEach((element) { + print(widget.prescriptionReport!.itemDescriptionN!); + if (element.title!.contains(widget.prescriptionReport!.itemDescriptionN!)) calendarUtils.deleteEvent(calender, element); + + }); + }), + }); + + + - RetrieveEventsParams params = new RetrieveEventsParams(startDate: startEventsDate, endDate: endEventsDate); - await calendarUtils.retrieveEvents(calendarUtils.calendars[0].id!, params).then((value) { - Result> events = value; - events.data!.forEach((element) { - if (element.title!.contains(widget.prescriptionReport!.itemDescriptionN!)) calendarUtils.deleteEvent(calendarUtils.calendars[0], element); - }); - }); AppToast.showSuccessToast(message: TranslationBase.of(context).reminderCancelSuccess); setState(() { hasReminder = false; @@ -285,56 +302,67 @@ class _PrescriptionDetailsPageState extends State { } setCalender(BuildContext context, String eventId, int reminderIndex) async { - CalendarUtils calendarUtils = await CalendarUtils.getInstance(); + DateTime actualDate = DateTime(DateTime.now().year, DateTime.now().month, DateTime.now().day, 8, 0); + print("Frequency ${widget.prescriptionReport?.frequencyNumber}"); //Time will start at 8:00 AM from starting date + if (widget.prescriptionReport?.frequencyNumber == null) widget.prescriptionReport!.frequencyNumber = 2; //Some time frequency number is null so by default will be 2 - DateTime actualDate = DateTime(DateTime.now().year, DateTime.now().month, DateTime.now().day, 8, 0); //Time will start at 8:00 AM from starting date - if (widget.prescriptionReport?.frequencyNumber == null) widget.prescriptionReport!.frequencyNumber = 1; //Some time frequency number is null so by default will be 2 + int remainingDays = widget.prescriptionReport!.days!; + // - + // (Jiffy.parseFromDateTime(DateTime.now()).diff(Jiffy.parseFromDateTime(DateUtil.convertStringToDate(widget.prescriptionReport!.orderDate!)), unit: Unit.day) as int); + if (remainingDays.isNegative) { + Utils.showErrorToast("Prescription date has been already passed you can not add a reminder for this prescription."); + return; + } + CalendarUtils calendarUtils = await CalendarUtils.getInstance(); + GifLoaderDialogUtils.showMyDialog(context); - int remainingDays = widget.prescriptionReport!.days! - - (Jiffy.parseFromDateTime(DateTime.now()).diff(Jiffy.parseFromDateTime(DateUtil.convertStringToDate(widget.prescriptionReport!.orderDate!)), unit: Unit.day) as int); + try { + for (int i = 0; i < remainingDays; i++) { + //event for number of days. + for (int j = 0; j < widget.prescriptionReport!.frequencyNumber!; j++) { + // event for number of times per day. + if (j != 0) { + actualDate.add(new Duration(hours: 8)); // 8 hours addition for daily dose. + } + //Time subtraction from actual reminder time. like before 30, or 1 hour. + if (reminderIndex == 0) { + // Before 30 mints + actualDate = Jiffy.parseFromDateTime(actualDate).subtract(minutes: 30).dateTime; + // dateTime.add(new Duration(minutes: -30)); + } else if (reminderIndex == 1) { + // Before 1 hour + // dateTime.add(new Duration(minutes: -60)); + actualDate = Jiffy.parseFromDateTime(actualDate).subtract(hours: 1).dateTime; + } else if (reminderIndex == 2) { + // Before 1 hour and 30 mints + // dateTime.add(new Duration(minutes: -90)); + actualDate = Jiffy.parseFromDateTime(actualDate).subtract(hours: 1, minutes: 30).dateTime; + } else if (reminderIndex == 3) { + // Before 2 hours + // dateTime.add(new Duration(minutes: -120)); + actualDate = Jiffy.parseFromDateTime(actualDate).subtract(hours: 2).dateTime; + } - GifLoaderDialogUtils.showMyDialog(context); + calendarUtils.createOrUpdateEvent( + title: "${widget.prescriptionReport!.itemDescriptionN} Prescription Reminder", + description: "${widget.prescriptionReport!.itemDescriptionN} ${widget.prescriptionReport!.frequencyN} ${widget.prescriptionReport!.routeN} ", + scheduleDateTime: actualDate, + eventId: eventId + (i.toString() + j.toString()), //event id with varitions + ); - for (int i = 0; i < remainingDays; i++) { - //event for number of days. - for (int j = 0; j < widget.prescriptionReport!.frequencyNumber!; j++) { - // event for number of times per day. - if (j != 0) { - actualDate.add(new Duration(hours: 8)); // 8 hours addition for daily dose. - } - //Time subtraction from actual reminder time. like before 30, or 1 hour. - if (reminderIndex == 0) { - // Before 30 mints - actualDate = Jiffy.parseFromDateTime(actualDate).subtract(minutes: 30).dateTime; - // dateTime.add(new Duration(minutes: -30)); - } else if (reminderIndex == 1) { - // Before 1 hour - // dateTime.add(new Duration(minutes: -60)); - actualDate = Jiffy.parseFromDateTime(actualDate).subtract(hours: 1).dateTime; - } else if (reminderIndex == 2) { - // Before 1 hour and 30 mints - // dateTime.add(new Duration(minutes: -90)); - actualDate = Jiffy.parseFromDateTime(actualDate).subtract(hours: 1, minutes: 30).dateTime; - } else if (reminderIndex == 3) { - // Before 2 hours - // dateTime.add(new Duration(minutes: -120)); - actualDate = Jiffy.parseFromDateTime(actualDate).subtract(hours: 2).dateTime; + actualDate = DateTime(actualDate.year, actualDate.month, actualDate.day, 8, 0); } - calendarUtils - .createOrUpdateEvent( - title: "${widget.prescriptionReport!.itemDescriptionN} Prescription Reminder", - description: "${widget.prescriptionReport!.itemDescriptionN} ${widget.prescriptionReport!.frequencyN} ${widget.prescriptionReport!.routeN} ", - scheduleDateTime: actualDate, - eventId: eventId + (i.toString() + j.toString()), //event id with varitions - ) - .then((value) {}); - actualDate = DateTime(actualDate.year, actualDate.month, actualDate.day, 8, 0); + actualDate = Jiffy.parseFromDateTime(actualDate).add(days: 1).dateTime; + print(actualDate); } - actualDate = Jiffy.parseFromDateTime(actualDate).add(days: 1).dateTime; - print(actualDate); + } catch (ex) { + print("catch:$ex"); } + AppToast.showSuccessToast(message: TranslationBase.of(context).reminderSuccess); - GifLoaderDialogUtils.hideDialog(context); + Future.delayed(Duration(seconds: 2), () { + GifLoaderDialogUtils.hideDialog(context); + }); setState(() { hasReminder = true; }); diff --git a/lib/uitl/CalendarUtils.dart b/lib/uitl/CalendarUtils.dart index f98adc2b..be78a0c1 100644 --- a/lib/uitl/CalendarUtils.dart +++ b/lib/uitl/CalendarUtils.dart @@ -17,27 +17,48 @@ class CalendarUtils { CalendarUtils._(this.calendars); + // static Future getInstance() async { + // if (_completer == null) { + // _completer = Completer(); + // print(_completer!.isCompleted); + // try { + // final dynamic calendarsResult; + // if (Platform.isIOS) { + // calendarsResult = await _myPlugin.getCalendars(); + // if (!_completer!.isCompleted) { + // _completer?.complete(CalendarUtils._(await calendarsResult!)); + // } + // } else { + // calendarsResult = await deviceCalendarPlugin.retrieveCalendars(); + // if (!_completer!.isCompleted) { + // _completer?.complete(CalendarUtils._(await calendarsResult.data!)); + // } + // } + // } on Exception catch (e) { + // if (!_completer!.isCompleted) { + // _completer!.completeError(e); + // } + // } + // } + // return _completer!.future; + // } + static Future getInstance() async { - _completer = null; - - if (_completer == null) { - _completer = await Completer(); - try { - final dynamic calendarsResult; - if (Platform.isIOS) { - calendarsResult = await _myPlugin.getCalendars(); - _completer?.complete(await CalendarUtils._(await calendarsResult!)); - } else { - calendarsResult = await deviceCalendarPlugin.retrieveCalendars(); - print(jsonEncode(calendarsResult.data!)); - _completer?.complete(await CalendarUtils._(await calendarsResult.data!)); - } - } on Exception catch (e) { - _completer!.completeError(e); - final Future sharedPrefsFuture = _completer!.future; - _completer = null; - return sharedPrefsFuture; + if (_completer != null) { + return _completer!.future; + } + _completer = Completer(); + try { + final dynamic calendarsResult; + if (Platform.isIOS) { + calendarsResult = await _myPlugin.getCalendars(); + _completer!.complete(CalendarUtils._(calendarsResult)); + } else { + calendarsResult = await deviceCalendarPlugin.retrieveCalendars(); + _completer!.complete(CalendarUtils._(calendarsResult.data)); } + } catch (e) { + _completer!.completeError(e); } return _completer!.future; @@ -87,19 +108,18 @@ class CalendarUtils { TZDateTime scheduleDateTimeUTZ = TZDateTime.from(scheduleDateTime!, _currentLocation); - print("eventId: " + "hhgvhg"); // print("writableCalendars-name: " + writableCalendars.name); // print("writableCalendars-Id: " + writableCalendars.id); print("writableCalendarsToString: " + writableCalendars.toString()); print("writableCalendarsToString: " + writableCalendars!.id!); - Event event = await Event( + Event event = Event( writableCalendars!.id, start: scheduleDateTimeUTZ, end: scheduleDateTimeUTZ.add(Duration(minutes: 30)), title: title, description: description, ); - deviceCalendarPlugin.createOrUpdateEvent(event).catchError((e) { + await deviceCalendarPlugin.createOrUpdateEvent(event).catchError((e) { print("catchError " + e.toString()); }).whenComplete(() { print("whenComplete " + eventId!);