Calender Event Fixes

merge-update-with-lab-changes
Aamir.Muhammad 2 years ago
parent 76f5fc2ef8
commit 515896c265

@ -5,6 +5,8 @@ import 'package:diplomaticquarterapp/locator.dart';
import 'package:diplomaticquarterapp/services/permission/permission_service.dart'; import 'package:diplomaticquarterapp/services/permission/permission_service.dart';
import 'package:diplomaticquarterapp/theme/colors.dart'; import 'package:diplomaticquarterapp/theme/colors.dart';
import 'package:diplomaticquarterapp/uitl/CalendarUtils.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/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/uitl/utils.dart'; import 'package:diplomaticquarterapp/uitl/utils.dart';
import 'package:diplomaticquarterapp/uitl/utils_new.dart'; import 'package:diplomaticquarterapp/uitl/utils_new.dart';
@ -19,16 +21,16 @@ Future<Map<Permission, PermissionStatus>> requestPermissions() async {
} }
showReminderDialog(BuildContext context, DateTime dateTime, String doctorName, String eventId, String appoDateFormatted, String appoTimeFormatted, 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 (Platform.isAndroid) {
if (await PermissionService.isCalendarPermissionEnabled()) { if (await PermissionService.isCalendarPermissionEnabled()) {
_showReminderDialog(context, dateTime, doctorName, eventId, appoDateFormatted, appoTimeFormatted, _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 { } else {
Utils.showPermissionConsentDialog(context, TranslationBase.of(context).calendarPermission, () async { Utils.showPermissionConsentDialog(context, TranslationBase.of(context).calendarPermission, () async {
if (await Permission.calendarFullAccess.request().isGranted) { if (await Permission.calendarFullAccess.request().isGranted) {
_showReminderDialog(context, dateTime, doctorName, eventId, appoDateFormatted, appoTimeFormatted, _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.calendarWriteOnly.request().isGranted) {
if (await Permission.calendarFullAccess.request().isGranted) { if (await Permission.calendarFullAccess.request().isGranted) {
_showReminderDialog(context, dateTime, doctorName, eventId, appoDateFormatted, appoTimeFormatted, _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<void> _showReminderDialog(BuildContext context, DateTime dateTime, String doctorName, String eventId, String appoDateFormatted, String appoTimeFormatted, Future<void> _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<void>( return showDialog<void>(
context: context, context: context,
barrierDismissible: true, // user must tap button! barrierDismissible: true, // user must tap button!
@ -75,22 +77,18 @@ Future<void> _showReminderDialog(BuildContext context, DateTime dateTime, String
dateTime = Jiffy.parseFromDateTime(dateTime).subtract(hours: 2).dateTime; dateTime = Jiffy.parseFromDateTime(dateTime).subtract(hours: 2).dateTime;
text = "2 hours"; text = "2 hours";
} }
if (!isMultiAllowed!) {
if (onMultiDateSuccess.call(i) == null) { if (onMultiDateSuccess == null) {
try {
CalendarUtils calendarUtils = await CalendarUtils.getInstance(); CalendarUtils calendarUtils = await CalendarUtils.getInstance();
await calendarUtils.createOrUpdateEvent( await calendarUtils.createOrUpdateEvent(
title: title ?? TranslationBase.of(context).reminderTitle + " " + doctorName, title: title ?? TranslationBase.of(context).reminderTitle + " " + doctorName,
description: description ?? "At " + appoDateFormatted + " " + appoTimeFormatted, description: description ?? "At " + appoDateFormatted + " " + appoTimeFormatted,
scheduleDateTime: dateTime, scheduleDateTime: dateTime,
eventId: eventId); eventId: eventId);
onSuccess(); onSuccess();
} catch (e) {
print(e);
} }
} else { } else {
onMultiDateSuccess(i); onMultiDateSuccess!(i);
} }
locator<GAnalytics>().appointment.appointment_reminder_time(reminde_before: text); locator<GAnalytics>().appointment.appointment_reminder_time(reminde_before: text);
}, },

@ -222,6 +222,7 @@ class _PrescriptionDetailsPageState extends State<PrescriptionDetailsPage> {
onSuccess: () { onSuccess: () {
AppToast.showSuccessToast(message: TranslationBase.of(context).reminderSuccess); AppToast.showSuccessToast(message: TranslationBase.of(context).reminderSuccess);
}, },
isMultiAllowed: true,
onMultiDateSuccess: (int selectedIndex) { onMultiDateSuccess: (int selectedIndex) {
setCalender(context, widget.prescriptionReport!.itemID.toString(), selectedIndex); setCalender(context, widget.prescriptionReport!.itemID.toString(), selectedIndex);
}, },
@ -245,21 +246,27 @@ class _PrescriptionDetailsPageState extends State<PrescriptionDetailsPage> {
checkIfHasReminder() async { checkIfHasReminder() async {
CalendarUtils calendarUtils = await CalendarUtils.getInstance(); CalendarUtils calendarUtils = await CalendarUtils.getInstance();
print("If Has Reminder");
DateTime startEventsDate = Jiffy.parseFromDateTime(DateTime.now()).subtract(days: 30).dateTime; DateTime startEventsDate = Jiffy.parseFromDateTime(DateTime.now()).subtract(days: 30).dateTime;
DateTime endEventsDate = Jiffy.parseFromDateTime(DateTime.now()).add(days: 120).dateTime; DateTime endEventsDate = Jiffy.parseFromDateTime(DateTime.now()).add(days: 120).dateTime;
RetrieveEventsParams params = new RetrieveEventsParams(startDate: startEventsDate, endDate: endEventsDate); RetrieveEventsParams params = new RetrieveEventsParams(startDate: startEventsDate, endDate: endEventsDate);
if (calendarUtils.calendars != null) { if (calendarUtils.calendars != null) {
await calendarUtils.retrieveEvents(calendarUtils.calendars[0].id!, params).then((value) { print(calendarUtils.calendars);
Future.forEach(
calendarUtils.calendars,
(Calendar calender) => {
calendarUtils.retrieveEvents(calender.id!, params).then((value) {
Result<UnmodifiableListView<Event>> events = value; Result<UnmodifiableListView<Event>> events = value;
events.data!.forEach((element) { events.data!.forEach((element) {
print(widget.prescriptionReport!.itemDescriptionN!);
if (element.title!.contains(widget.prescriptionReport!.itemDescriptionN!)) if (element.title!.contains(widget.prescriptionReport!.itemDescriptionN!))
setState(() { setState(() {
hasReminder = true; hasReminder = true;
}); });
}); });
}),
}); });
} }
} }
@ -269,15 +276,25 @@ class _PrescriptionDetailsPageState extends State<PrescriptionDetailsPage> {
DateTime startEventsDate = Jiffy.parseFromDateTime(DateTime.now()).subtract(days: 30).dateTime; DateTime startEventsDate = Jiffy.parseFromDateTime(DateTime.now()).subtract(days: 30).dateTime;
DateTime endEventsDate = Jiffy.parseFromDateTime(DateTime.now()).add(days: 120).dateTime; DateTime endEventsDate = Jiffy.parseFromDateTime(DateTime.now()).add(days: 120).dateTime;
RetrieveEventsParams params = RetrieveEventsParams(startDate: startEventsDate, endDate: endEventsDate);
RetrieveEventsParams params = new RetrieveEventsParams(startDate: startEventsDate, endDate: endEventsDate); Future.forEach(
calendarUtils.calendars,
await calendarUtils.retrieveEvents(calendarUtils.calendars[0].id!, params).then((value) { (Calendar calender) => {
calendarUtils.retrieveEvents(calender.id!, params).then((value) {
Result<UnmodifiableListView<Event>> events = value; Result<UnmodifiableListView<Event>> events = value;
events.data!.forEach((element) { events.data!.forEach((element) {
if (element.title!.contains(widget.prescriptionReport!.itemDescriptionN!)) calendarUtils.deleteEvent(calendarUtils.calendars[0], element); print(widget.prescriptionReport!.itemDescriptionN!);
if (element.title!.contains(widget.prescriptionReport!.itemDescriptionN!)) calendarUtils.deleteEvent(calender, element);
}); });
}),
}); });
AppToast.showSuccessToast(message: TranslationBase.of(context).reminderCancelSuccess); AppToast.showSuccessToast(message: TranslationBase.of(context).reminderCancelSuccess);
setState(() { setState(() {
hasReminder = false; hasReminder = false;
@ -285,16 +302,21 @@ class _PrescriptionDetailsPageState extends State<PrescriptionDetailsPage> {
} }
setCalender(BuildContext context, String eventId, int reminderIndex) async { 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
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 = 2; //Some time frequency number is null so by default will be 2
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);
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); GifLoaderDialogUtils.showMyDialog(context);
try {
for (int i = 0; i < remainingDays; i++) { for (int i = 0; i < remainingDays; i++) {
//event for number of days. //event for number of days.
for (int j = 0; j < widget.prescriptionReport!.frequencyNumber!; j++) { for (int j = 0; j < widget.prescriptionReport!.frequencyNumber!; j++) {
@ -320,21 +342,27 @@ class _PrescriptionDetailsPageState extends State<PrescriptionDetailsPage> {
// dateTime.add(new Duration(minutes: -120)); // dateTime.add(new Duration(minutes: -120));
actualDate = Jiffy.parseFromDateTime(actualDate).subtract(hours: 2).dateTime; actualDate = Jiffy.parseFromDateTime(actualDate).subtract(hours: 2).dateTime;
} }
calendarUtils
.createOrUpdateEvent( calendarUtils.createOrUpdateEvent(
title: "${widget.prescriptionReport!.itemDescriptionN} Prescription Reminder", title: "${widget.prescriptionReport!.itemDescriptionN} Prescription Reminder",
description: "${widget.prescriptionReport!.itemDescriptionN} ${widget.prescriptionReport!.frequencyN} ${widget.prescriptionReport!.routeN} ", description: "${widget.prescriptionReport!.itemDescriptionN} ${widget.prescriptionReport!.frequencyN} ${widget.prescriptionReport!.routeN} ",
scheduleDateTime: actualDate, scheduleDateTime: actualDate,
eventId: eventId + (i.toString() + j.toString()), //event id with varitions eventId: eventId + (i.toString() + j.toString()), //event id with varitions
) );
.then((value) {});
actualDate = DateTime(actualDate.year, actualDate.month, actualDate.day, 8, 0); actualDate = DateTime(actualDate.year, actualDate.month, actualDate.day, 8, 0);
} }
actualDate = Jiffy.parseFromDateTime(actualDate).add(days: 1).dateTime; actualDate = Jiffy.parseFromDateTime(actualDate).add(days: 1).dateTime;
print(actualDate); print(actualDate);
} }
} catch (ex) {
print("catch:$ex");
}
AppToast.showSuccessToast(message: TranslationBase.of(context).reminderSuccess); AppToast.showSuccessToast(message: TranslationBase.of(context).reminderSuccess);
Future.delayed(Duration(seconds: 2), () {
GifLoaderDialogUtils.hideDialog(context); GifLoaderDialogUtils.hideDialog(context);
});
setState(() { setState(() {
hasReminder = true; hasReminder = true;
}); });

@ -17,27 +17,48 @@ class CalendarUtils {
CalendarUtils._(this.calendars); CalendarUtils._(this.calendars);
static Future<CalendarUtils> getInstance() async { // static Future<CalendarUtils> getInstance() async {
_completer = null; // if (_completer == null) {
// _completer = Completer<CalendarUtils>();
// 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;
// }
if (_completer == null) { static Future<CalendarUtils> getInstance() async {
_completer = await Completer<CalendarUtils>(); if (_completer != null) {
return _completer!.future;
}
_completer = Completer<CalendarUtils>();
try { try {
final dynamic calendarsResult; final dynamic calendarsResult;
if (Platform.isIOS) { if (Platform.isIOS) {
calendarsResult = await _myPlugin.getCalendars(); calendarsResult = await _myPlugin.getCalendars();
_completer?.complete(await CalendarUtils._(await calendarsResult!)); _completer!.complete(CalendarUtils._(calendarsResult));
} else { } else {
calendarsResult = await deviceCalendarPlugin.retrieveCalendars(); calendarsResult = await deviceCalendarPlugin.retrieveCalendars();
print(jsonEncode(calendarsResult.data!)); _completer!.complete(CalendarUtils._(calendarsResult.data));
_completer?.complete(await CalendarUtils._(await calendarsResult.data!));
} }
} on Exception catch (e) { } catch (e) {
_completer!.completeError(e); _completer!.completeError(e);
final Future<CalendarUtils> sharedPrefsFuture = _completer!.future;
_completer = null;
return sharedPrefsFuture;
}
} }
return _completer!.future; return _completer!.future;
@ -87,19 +108,18 @@ class CalendarUtils {
TZDateTime scheduleDateTimeUTZ = TZDateTime.from(scheduleDateTime!, _currentLocation); TZDateTime scheduleDateTimeUTZ = TZDateTime.from(scheduleDateTime!, _currentLocation);
print("eventId: " + "hhgvhg");
// print("writableCalendars-name: " + writableCalendars.name); // print("writableCalendars-name: " + writableCalendars.name);
// print("writableCalendars-Id: " + writableCalendars.id); // print("writableCalendars-Id: " + writableCalendars.id);
print("writableCalendarsToString: " + writableCalendars.toString()); print("writableCalendarsToString: " + writableCalendars.toString());
print("writableCalendarsToString: " + writableCalendars!.id!); print("writableCalendarsToString: " + writableCalendars!.id!);
Event event = await Event( Event event = Event(
writableCalendars!.id, writableCalendars!.id,
start: scheduleDateTimeUTZ, start: scheduleDateTimeUTZ,
end: scheduleDateTimeUTZ.add(Duration(minutes: 30)), end: scheduleDateTimeUTZ.add(Duration(minutes: 30)),
title: title, title: title,
description: description, description: description,
); );
deviceCalendarPlugin.createOrUpdateEvent(event).catchError((e) { await deviceCalendarPlugin.createOrUpdateEvent(event).catchError((e) {
print("catchError " + e.toString()); print("catchError " + e.toString());
}).whenComplete(() { }).whenComplete(() {
print("whenComplete " + eventId!); print("whenComplete " + eventId!);

Loading…
Cancel
Save