diff --git a/lib/pages/BookAppointment/widgets/reminder_dialog.dart b/lib/pages/BookAppointment/widgets/reminder_dialog.dart index 43038658..723bf538 100644 --- a/lib/pages/BookAppointment/widgets/reminder_dialog.dart +++ b/lib/pages/BookAppointment/widgets/reminder_dialog.dart @@ -14,13 +14,15 @@ Future> requestPermissions() async { } showReminderDialog(BuildContext context, DateTime dateTime, String doctorName, String eventId, String appoDateFormatted, String appoTimeFormatted, - {Function onSuccess, String title, String description}) async { + {Function onSuccess, String title, String description, Function(int) onMultiDateSuccess}) async { if (await Permission.calendar.request().isGranted) { - _showReminderDialog(context, dateTime, doctorName, eventId, appoDateFormatted, appoTimeFormatted, onSuccess: onSuccess, title: title, description: description); + _showReminderDialog(context, dateTime, doctorName, eventId, appoDateFormatted, appoTimeFormatted, + onSuccess: onSuccess, title: title, description: description, onMultiDateSuccess: onMultiDateSuccess); } else { requestPermissions().then((results) { if (results[Permission.calendar].isGranted) { - _showReminderDialog(context, dateTime, doctorName, eventId, appoDateFormatted, appoTimeFormatted, onSuccess: onSuccess, title: title, description: description); + _showReminderDialog(context, dateTime, doctorName, eventId, appoDateFormatted, appoTimeFormatted, + onSuccess: onSuccess, title: title, description: description, onMultiDateSuccess: onMultiDateSuccess); } }); } @@ -29,7 +31,7 @@ showReminderDialog(BuildContext context, DateTime dateTime, String doctorName, S final CalendarPlugin _myPlugin = CalendarPlugin(); Future _showReminderDialog(BuildContext context, DateTime dateTime, String doctorName, String eventId, String appoDateFormatted, String appoTimeFormatted, - {Function onSuccess, String title, String description}) async { + {Function onSuccess, String title, String description, Function(int) onMultiDateSuccess}) async { return showDialog( context: context, barrierDismissible: true, // user must tap button! @@ -56,16 +58,20 @@ Future _showReminderDialog(BuildContext context, DateTime dateTime, String // dateTime.add(new Duration(minutes: -120)); dateTime = Jiffy(dateTime).subtract(hours: 2); } - CalendarUtils calendarUtils = await CalendarUtils.getInstance(); - calendarUtils - .createOrUpdateEvent( - title: title ?? TranslationBase.of(context).reminderTitle + " " + doctorName, - description: description ?? "At " + appoDateFormatted + " " + appoTimeFormatted, - scheduleDateTime: dateTime, - eventId: eventId) - .then((value) {}); + if (onMultiDateSuccess == null) { + CalendarUtils calendarUtils = await CalendarUtils.getInstance(); + calendarUtils + .createOrUpdateEvent( + title: title ?? TranslationBase.of(context).reminderTitle + " " + doctorName, + description: description ?? "At " + appoDateFormatted + " " + appoTimeFormatted, + scheduleDateTime: dateTime, + eventId: eventId) + .then((value) {}); - onSuccess(); + onSuccess(); + } else { + onMultiDateSuccess(i); + } }, ), ); @@ -144,12 +150,14 @@ class _ReminderDialogState extends State { }); }, ), - Text(TranslationBase.of(context).appoReminder30, + Text( + TranslationBase.of(context).appoReminder30, style: TextStyle( fontSize: 12, fontWeight: FontWeight.w600, letterSpacing: -0.48, - ),), + ), + ), ], ), Row( @@ -163,12 +171,14 @@ class _ReminderDialogState extends State { }); }, ), - Text(TranslationBase.of(context).appoReminder60, + Text( + TranslationBase.of(context).appoReminder60, style: TextStyle( fontSize: 12, fontWeight: FontWeight.w600, letterSpacing: -0.48, - ),), + ), + ), ], ), Row( @@ -182,12 +192,14 @@ class _ReminderDialogState extends State { }); }, ), - Text(TranslationBase.of(context).appoReminder90, + Text( + TranslationBase.of(context).appoReminder90, style: TextStyle( fontSize: 12, fontWeight: FontWeight.w600, letterSpacing: -0.48, - ),), + ), + ), ], ), Row( diff --git a/lib/pages/medical/prescriptions/prescription_details_page.dart b/lib/pages/medical/prescriptions/prescription_details_page.dart index ca17947a..d20fbe87 100644 --- a/lib/pages/medical/prescriptions/prescription_details_page.dart +++ b/lib/pages/medical/prescriptions/prescription_details_page.dart @@ -6,6 +6,7 @@ import 'package:diplomaticquarterapp/models/header_model.dart'; import 'package:diplomaticquarterapp/pages/BookAppointment/widgets/reminder_dialog.dart'; import 'package:diplomaticquarterapp/pages/MyAppointments/widgets/reminder_dialog_prescription.dart'; import 'package:diplomaticquarterapp/pages/medical/prescriptions/pharmacy_for_prescriptions_page.dart'; +import 'package:diplomaticquarterapp/uitl/CalendarUtils.dart'; import 'package:diplomaticquarterapp/uitl/LocalNotification.dart'; import 'package:diplomaticquarterapp/uitl/app_toast.dart'; import 'package:diplomaticquarterapp/uitl/date_uitl.dart'; @@ -19,6 +20,7 @@ import 'package:diplomaticquarterapp/widgets/show_zoom_image_dialog.dart'; import 'package:diplomaticquarterapp/widgets/transitions/fade_page.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; +import 'package:jiffy/jiffy.dart'; import 'package:provider/provider.dart'; class PrescriptionDetailsPage extends StatelessWidget { @@ -197,24 +199,26 @@ class PrescriptionDetailsPage extends StatelessWidget { child: DefaultButton( TranslationBase.of(context).addReminder, () { - setTheNotificatoin(context); - // DateTime startDate = DateTime.now(); - // DateTime endDate = DateTime(startDate.year, startDate.month, startDate.day + prescriptionReport.days); - // - // showReminderDialog( - // context, - // endDate, - // "", - // prescriptionReport.itemID.toString(), - // "", - // "", - // title: "${prescriptionReport.itemDescriptionN} Prescription Reminder", - // description: "${prescriptionReport.itemDescriptionN} ${prescriptionReport.frequencyN} ${prescriptionReport.routeN} ", - // onSuccess: () { - // AppToast.showSuccessToast(message: TranslationBase.of(context).reminderSuccess); - // }, - // ); - // return; + DateTime startDate = DateTime.now(); + DateTime endDate = DateTime(startDate.year, startDate.month, startDate.day + prescriptionReport.days); + + showReminderDialog( + context, + endDate, + "", + prescriptionReport.itemID.toString(), + "", + "", + title: "${prescriptionReport.itemDescriptionN} Prescription Reminder", + description: "${prescriptionReport.itemDescriptionN} ${prescriptionReport.frequencyN} ${prescriptionReport.routeN} ", + onSuccess: () { + AppToast.showSuccessToast(message: TranslationBase.of(context).reminderSuccess); + }, + onMultiDateSuccess: (int selectedIndex) { + setCalender(context, prescriptionReport.itemID.toString(), selectedIndex); + }, + ); + return; }, iconData: Icons.notifications_active, color: Color(0xffD02127), @@ -229,24 +233,74 @@ class PrescriptionDetailsPage extends StatelessWidget { ); } - setTheNotificatoin(BuildContext context) { + setCalender(BuildContext context, String eventId, int reminderIndex) async { + CalendarUtils calendarUtils = await CalendarUtils.getInstance(); + DateTime startDate = DateUtil.convertStringToDate(prescriptionReport.startDate); - DateTime actualDate = DateTime(startDate.year, startDate.month, startDate.day, 8, 0); - if(prescriptionReport?.frequencyNumber==null) - prescriptionReport.frequencyNumber=2; + DateTime actualDate = DateTime(startDate.year, startDate.month, startDate.day, 8, 0); //Time will start at 8:00 AM from starting date + if (prescriptionReport?.frequencyNumber == null) prescriptionReport.frequencyNumber = 2; //Some time frequency number is null so by default will be 2 for (int i = 0; i < prescriptionReport.days; i++) { - for (int j = 0; j < prescriptionReport.frequencyNumber??2; j++) { + //event for number of days. + for (int j = 0; j < prescriptionReport.frequencyNumber ?? 2; j++) { + // event for number of times per day. + if (j != 0) { - actualDate.add(new Duration(hours: 8)); + 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(actualDate).subtract(minutes: 30); + // dateTime.add(new Duration(minutes: -30)); + } else if (reminderIndex == 1) { + // Before 1 hour + // dateTime.add(new Duration(minutes: -60)); + actualDate = Jiffy(actualDate).subtract(hours: 1); + } else if (reminderIndex == 2) { + // Before 1 hour and 30 mints + // dateTime.add(new Duration(minutes: -90)); + actualDate = Jiffy(actualDate).subtract(hours: 1, minutes: 30); + } else if (reminderIndex == 3) { + // Before 2 hours + // dateTime.add(new Duration(minutes: -120)); + actualDate = Jiffy(actualDate).subtract(hours: 2); } - LocalNotification.getInstance().scheduleNotification( - scheduledNotificationDateTime: actualDate, - title: "${prescriptionReport.itemDescriptionN} Prescription Reminder", - description: "${prescriptionReport.itemDescriptionN} ${prescriptionReport.frequencyN} ${prescriptionReport.routeN} "); + calendarUtils + .createOrUpdateEvent( + title: "${prescriptionReport.itemDescriptionN} Prescription Reminder", + description: "${prescriptionReport.itemDescriptionN} ${prescriptionReport.frequencyN} ${prescriptionReport.routeN} ", + scheduleDateTime: actualDate, + eventId: eventId + (i.toString() + j.toString()), //event id with varitions + ) + .then( + (value) {}, + ); } actualDate.add(new Duration(days: i + 1)); } AppToast.showSuccessToast(message: TranslationBase.of(context).reminderSuccess); } + + //schedual local notifications + // setTheNotificatoin(BuildContext context) { + // DateTime startDate = DateUtil.convertStringToDate(prescriptionReport.startDate); + // DateTime actualDate = DateTime(startDate.year, startDate.month, startDate.day, 8, 0); + // if (prescriptionReport?.frequencyNumber == null) prescriptionReport.frequencyNumber = 2; + // + // for (int i = 0; i < prescriptionReport.days; i++) { + // for (int j = 0; j < prescriptionReport.frequencyNumber ?? 2; j++) { + // if (j != 0) { + // actualDate.add(new Duration(hours: 8)); + // } + // LocalNotification.getInstance().scheduleNotification( + // scheduledNotificationDateTime: actualDate, + // title: "${prescriptionReport.itemDescriptionN} Prescription Reminder", + // description: "${prescriptionReport.itemDescriptionN} ${prescriptionReport.frequencyN} ${prescriptionReport.routeN} "); + // } + // actualDate.add(new Duration(days: i + 1)); + // } + // AppToast.showSuccessToast(message: TranslationBase.of(context).reminderSuccess); + // } }