diff --git a/lib/pages/medical/prescriptions/prescription_details_page.dart b/lib/pages/medical/prescriptions/prescription_details_page.dart index 5090348a..4ee85086 100644 --- a/lib/pages/medical/prescriptions/prescription_details_page.dart +++ b/lib/pages/medical/prescriptions/prescription_details_page.dart @@ -44,7 +44,7 @@ class _PrescriptionDetailsPageState extends State { @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 { 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 { ); } - 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> 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? iosCall = await _myPlugin.getCalendars(); + // Future.forEach( + // iosCall!, + // (ios.Calendar cal) async => { + // print(cal), + // calendarUtils.retrieveEvents(cal.id!, params).then((value) { + // Result> 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 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> 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? iosCall = await _myPlugin.getCalendars(); - Future.forEach( - iosCall!, - (ios.Calendar cal) async => { - print(cal), - calendarUtils.retrieveEvents(cal.id!, params).then((value) { - Result> events = value; - events.data!.forEach((element) { - if (element.title!.contains(widget.prescriptionReport!.itemDescriptionN!)) - setState(() { - hasReminder = true; - }); - }); - }), - }, - ); + List? 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> events = value; - events.data!.forEach((element) { - if (element.title!.contains(widget.prescriptionReport!.itemDescriptionN!)) calendarUtils.deleteEvent(calender, element); - }); - }), - }); - } else { - List? iosCal = await _myPlugin.getCalendars(); - Future.forEach( - iosCal!, - (ios.Calendar cale) async => { - calendarUtils.retrieveEvents(cale.id!, params).then((value) { - Result> 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 processEvents(List calendars, calendarUtils, params, delete) async { + for (var calendar in calendars) { + Result> 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> events = value; + // events.data!.forEach((element) { + // if (element.title!.contains(widget.prescriptionReport!.itemDescriptionN!)) calendarUtils.deleteEvent(calender, element); + // }); + // }), + // }); + // } else { + // List? iosCal = await _myPlugin.getCalendars(); + // Future.forEach( + // iosCal!, + // (ios.Calendar cale) async => { + // calendarUtils.retrieveEvents(cale.id!, params).then((value) { + // Result> 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