|
|
|
|
@ -1,6 +1,7 @@
|
|
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
|
|
import 'package:device_calendar_plus/device_calendar_plus.dart';
|
|
|
|
|
import 'package:hmg_patient_app_new/core/utils/date_util.dart';
|
|
|
|
|
import 'package:jiffy/jiffy.dart' show Jiffy;
|
|
|
|
|
|
|
|
|
|
class CalenderUtilsNew {
|
|
|
|
|
@ -25,34 +26,86 @@ class CalenderUtilsNew {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FutureOr<bool> createOrUpdateEvent({required String title, required String description, required String location, DateTime? scheduleDateTime, String? eventId, int? reminderMinutes}) async {
|
|
|
|
|
print("the reminder minutes are $reminderMinutes");
|
|
|
|
|
if (writableCalender.isEmpty) {
|
|
|
|
|
await getCalenders();
|
|
|
|
|
}
|
|
|
|
|
var writableCalendars = writableCalender.first;
|
|
|
|
|
|
|
|
|
|
print("writableCalendars-name: " + writableCalendars.name);
|
|
|
|
|
print("writableCalendars-Id: " + writableCalendars.id);
|
|
|
|
|
print("writableCalendarsToString: " + writableCalendars.toString());
|
|
|
|
|
print("writableCalendarsToString: " + writableCalendars!.id!);
|
|
|
|
|
|
|
|
|
|
String eventResult = "";
|
|
|
|
|
CalendarPermissionStatus result = await DeviceCalendar.instance.hasPermissions();
|
|
|
|
|
if(result != CalendarPermissionStatus.granted) await DeviceCalendar.instance.requestPermissions();
|
|
|
|
|
print(result);
|
|
|
|
|
String eventResult = await DeviceCalendar.instance.createEvent(
|
|
|
|
|
calendarId: writableCalendars!.id,
|
|
|
|
|
title: title,
|
|
|
|
|
description: description,
|
|
|
|
|
startDate: scheduleDateTime!,
|
|
|
|
|
endDate: scheduleDateTime!.add(Duration(minutes: 30)),
|
|
|
|
|
reminderMinutes: reminderMinutes
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
print("the event Result is ${eventResult}");
|
|
|
|
|
return eventResult.isNotEmpty;
|
|
|
|
|
|
|
|
|
|
// String eventId = await getEventIdIfEventExist(title!.split("#").last);
|
|
|
|
|
// if (eventId.isEmpty) {
|
|
|
|
|
eventResult = await DeviceCalendar.instance.createEvent(
|
|
|
|
|
calendarId: writableCalendars!.id,
|
|
|
|
|
title: title,
|
|
|
|
|
description: description,
|
|
|
|
|
startDate: scheduleDateTime!,
|
|
|
|
|
endDate: scheduleDateTime!.add(Duration(minutes: 30)),
|
|
|
|
|
reminderMinutes: reminderMinutes);
|
|
|
|
|
return eventResult.isNotEmpty;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// await DeviceCalendar.instance.updateEvent(
|
|
|
|
|
// eventId: eventId,
|
|
|
|
|
// title: title,
|
|
|
|
|
// description: description,
|
|
|
|
|
// startDate: scheduleDateTime!,
|
|
|
|
|
// endDate: scheduleDateTime!.add(Duration(minutes: 30)),
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// return eventId.isNotEmpty;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FutureOr<bool> createMultipleEvents(
|
|
|
|
|
{required int reminderMinutes,
|
|
|
|
|
int? frequencyNumber,
|
|
|
|
|
required int days,
|
|
|
|
|
required String orderDate,
|
|
|
|
|
required String itemDescriptionN,
|
|
|
|
|
required String route,
|
|
|
|
|
Function(String)? onFailure,
|
|
|
|
|
String? prescriptionNumber}) async {
|
|
|
|
|
DateTime currentDay = DateTime.now();
|
|
|
|
|
DateTime actualDate = DateTime(DateTime.now().year, DateTime.now().month, DateTime.now().day, 8, 0);
|
|
|
|
|
print("the frequency is $frequencyNumber");
|
|
|
|
|
frequencyNumber ??= 2; //Some time frequency number is null so by default will be 2
|
|
|
|
|
int interval = calculateIntervalAsPerFrequency(frequencyNumber);
|
|
|
|
|
// int remainingDays = days - (Jiffy.parseFromDateTime(DateTime.now()).diff(Jiffy.parseFromDateTime(DateUtil.convertStringToDate(orderDate)), unit: Unit.day) as int);
|
|
|
|
|
Duration difference = (actualDate.difference(DateUtil.convertStringToDate(orderDate)));
|
|
|
|
|
int remainingDays = 5;
|
|
|
|
|
// if (remainingDays.isNegative) {
|
|
|
|
|
// onFailure?.call("Prescription date has been already passed you can not add a reminder for this prescription.");
|
|
|
|
|
// return false;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
bool statusOfOperation = false;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < remainingDays; i++) {
|
|
|
|
|
//event for number of days.
|
|
|
|
|
for (int j = 0; j < frequencyNumber; j++) {
|
|
|
|
|
statusOfOperation = await createOrUpdateEvent(
|
|
|
|
|
title: "$itemDescriptionN} Medication about to due for prescription , #$prescriptionNumber",
|
|
|
|
|
description: "$itemDescriptionN $frequencyNumber $route ",
|
|
|
|
|
scheduleDateTime: actualDate,
|
|
|
|
|
location: '', //event id with varitions
|
|
|
|
|
reminderMinutes: reminderMinutes
|
|
|
|
|
);
|
|
|
|
|
if (!statusOfOperation) return false;
|
|
|
|
|
|
|
|
|
|
actualDate = actualDate.add(Duration(hours: interval));
|
|
|
|
|
// if (actualDate.difference(currentDay).inDays == 1) break;
|
|
|
|
|
}
|
|
|
|
|
// actualDate = actualDate.add(Duration(days: 1));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return statusOfOperation;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int calculateIntervalAsPerFrequency(int frequencyNumber) {
|
|
|
|
|
return 24 ~/ frequencyNumber;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FutureOr<List<Event>> getEvents() async {
|
|
|
|
|
var availableCalender = writableCalender.first;
|
|
|
|
|
@ -64,24 +117,41 @@ class CalenderUtilsNew {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
FutureOr<bool> checkIfEventExist(String admissionId) async {
|
|
|
|
|
if(writableCalender.isEmpty)return false;
|
|
|
|
|
if (writableCalender.isEmpty) {
|
|
|
|
|
await getCalenders();
|
|
|
|
|
}
|
|
|
|
|
List<Event> events = await getEvents();
|
|
|
|
|
if(events.isEmpty) return false;
|
|
|
|
|
for(var event in events){
|
|
|
|
|
List<String> title = event.title!.split("#");
|
|
|
|
|
print("the splitted admission id is ${title}");
|
|
|
|
|
|
|
|
|
|
if(title.contains(admissionId)) return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FutureOr<String> getEventIdIfEventExist(String admissionId) async {
|
|
|
|
|
if (writableCalender.isEmpty) {
|
|
|
|
|
await getCalenders();
|
|
|
|
|
}
|
|
|
|
|
List<Event> events = await getEvents();
|
|
|
|
|
if (events.isEmpty) return "";
|
|
|
|
|
for (var event in events) {
|
|
|
|
|
List<String> title = event.title!.split("#");
|
|
|
|
|
if (title.contains(admissionId)) return event.eventId;
|
|
|
|
|
}
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FutureOr<bool> checkAndRemove({required String id}) async {
|
|
|
|
|
if(writableCalender.isEmpty)return false;
|
|
|
|
|
if (writableCalender.isEmpty) {
|
|
|
|
|
await getCalenders();
|
|
|
|
|
}
|
|
|
|
|
List<Event> events = await getEvents();
|
|
|
|
|
if(events.isEmpty) return false;
|
|
|
|
|
for(var event in events){
|
|
|
|
|
List<String> title = event.title!.split("#");
|
|
|
|
|
print("the splitted admission id is ${title}");
|
|
|
|
|
|
|
|
|
|
if(title.contains(id)) {
|
|
|
|
|
calender.deleteEvent(eventId: event.eventId);
|
|
|
|
|
return true;
|
|
|
|
|
@ -90,5 +160,21 @@ class CalenderUtilsNew {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FutureOr<bool> checkAndRemoveMultipleItems({required String id}) async {
|
|
|
|
|
if (writableCalender.isEmpty) {
|
|
|
|
|
await getCalenders();
|
|
|
|
|
}
|
|
|
|
|
List<Event> events = await getEvents();
|
|
|
|
|
if(events.isEmpty) return false;
|
|
|
|
|
bool statusOfOperation = false;
|
|
|
|
|
for(var event in events){
|
|
|
|
|
List<String> title = event.title.split("#");
|
|
|
|
|
|
|
|
|
|
if(title.contains(id)) {
|
|
|
|
|
calender.deleteEvent(eventId: event.eventId);
|
|
|
|
|
statusOfOperation = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return statusOfOperation;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|