|
|
|
|
@ -16,33 +16,64 @@ class CalenderUtilsNew {
|
|
|
|
|
final DeviceCalendar calender = DeviceCalendar.instance;
|
|
|
|
|
List<Calendar> writableCalender = [];
|
|
|
|
|
|
|
|
|
|
CalenderUtilsNew._instance() {
|
|
|
|
|
getCalenders();
|
|
|
|
|
}
|
|
|
|
|
/// Completer to ensure getCalenders() only runs once at a time
|
|
|
|
|
Completer<void>? _calendarInitCompleter;
|
|
|
|
|
|
|
|
|
|
CalenderUtilsNew._instance();
|
|
|
|
|
|
|
|
|
|
static final CalenderUtilsNew instance = CalenderUtilsNew._instance();
|
|
|
|
|
|
|
|
|
|
Future<void> getCalenders() async {
|
|
|
|
|
/// Ensures calendars are loaded exactly once. Safe to call multiple times —
|
|
|
|
|
/// subsequent calls await the same future.
|
|
|
|
|
Future<void> ensureCalendarsLoaded() async {
|
|
|
|
|
if (writableCalender.isNotEmpty) return;
|
|
|
|
|
if (_calendarInitCompleter != null) {
|
|
|
|
|
// Already in progress — just wait for it
|
|
|
|
|
return _calendarInitCompleter!.future;
|
|
|
|
|
}
|
|
|
|
|
_calendarInitCompleter = Completer<void>();
|
|
|
|
|
try {
|
|
|
|
|
await _loadCalendars();
|
|
|
|
|
_calendarInitCompleter!.complete();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
_calendarInitCompleter!.completeError(e);
|
|
|
|
|
_calendarInitCompleter = null; // Allow retry on failure
|
|
|
|
|
rethrow;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> _loadCalendars() async {
|
|
|
|
|
print("the get calenders is called");
|
|
|
|
|
CalendarPermissionStatus result = await DeviceCalendar.instance.hasPermissions();
|
|
|
|
|
if (result != CalendarPermissionStatus.granted) {
|
|
|
|
|
await DeviceCalendar.instance.requestPermissions();
|
|
|
|
|
showCommonBottomSheetWithoutHeight(
|
|
|
|
|
title: LocaleKeys.notice.tr(context: GetIt.instance<NavigationService>().navigatorKey.currentContext!),
|
|
|
|
|
GetIt.instance<NavigationService>().navigatorKey.currentContext!,
|
|
|
|
|
child: Utils.getWarningWidget(
|
|
|
|
|
loadingText: LocaleKeys.calendarPermissionAlert.tr(),
|
|
|
|
|
isShowActionButtons: true,
|
|
|
|
|
onCancelTap: () {
|
|
|
|
|
GetIt.instance<NavigationService>().pop();
|
|
|
|
|
},
|
|
|
|
|
onConfirmTap: () async {
|
|
|
|
|
GetIt.instance<NavigationService>().pop();
|
|
|
|
|
openAppSettings();
|
|
|
|
|
}),
|
|
|
|
|
callBackFunc: () {},
|
|
|
|
|
isFullScreen: false,
|
|
|
|
|
isCloseButtonVisible: true,
|
|
|
|
|
);
|
|
|
|
|
result = await DeviceCalendar.instance.hasPermissions();
|
|
|
|
|
// if (result != CalendarPermissionStatus.granted) {
|
|
|
|
|
// print("the permission is not granted");
|
|
|
|
|
// showCommonBottomSheetWithoutHeight(
|
|
|
|
|
// title: LocaleKeys.notice.tr(context: GetIt
|
|
|
|
|
// .instance<NavigationService>()
|
|
|
|
|
// .navigatorKey
|
|
|
|
|
// .currentContext!),
|
|
|
|
|
// GetIt
|
|
|
|
|
// .instance<NavigationService>()
|
|
|
|
|
// .navigatorKey
|
|
|
|
|
// .currentContext!,
|
|
|
|
|
// child: Utils.getWarningWidget(
|
|
|
|
|
// loadingText: LocaleKeys.calendarPermissionAlert.tr(),
|
|
|
|
|
// isShowActionButtons: true,
|
|
|
|
|
// onCancelTap: () {
|
|
|
|
|
// GetIt.instance<NavigationService>().pop();
|
|
|
|
|
// },
|
|
|
|
|
// onConfirmTap: () async {
|
|
|
|
|
// GetIt.instance<NavigationService>().pop();
|
|
|
|
|
// openAppSettings();
|
|
|
|
|
// }),
|
|
|
|
|
// callBackFunc: () {},
|
|
|
|
|
// isFullScreen: false,
|
|
|
|
|
// isCloseButtonVisible: true,
|
|
|
|
|
// );
|
|
|
|
|
// }
|
|
|
|
|
}
|
|
|
|
|
var calenders = await calender.listCalendars();
|
|
|
|
|
calenders.forEach((calender) {
|
|
|
|
|
@ -53,9 +84,7 @@ class CalenderUtilsNew {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FutureOr<bool> createOrUpdateEvent({required String title, required String description, required String location, DateTime? scheduleDateTime, String? eventId, int? reminderMinutes}) async {
|
|
|
|
|
if (writableCalender.isEmpty) {
|
|
|
|
|
await getCalenders();
|
|
|
|
|
}
|
|
|
|
|
await ensureCalendarsLoaded();
|
|
|
|
|
var writableCalendars = writableCalender.first;
|
|
|
|
|
String eventResult = "";
|
|
|
|
|
CalendarPermissionStatus result = await DeviceCalendar.instance.hasPermissions();
|
|
|
|
|
@ -146,9 +175,7 @@ class CalenderUtilsNew {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FutureOr<bool> checkIfEventExist(String admissionId) async {
|
|
|
|
|
if (writableCalender.isEmpty) {
|
|
|
|
|
await getCalenders();
|
|
|
|
|
}
|
|
|
|
|
await ensureCalendarsLoaded();
|
|
|
|
|
List<Event> events = await getEvents();
|
|
|
|
|
if (events.isEmpty) return false;
|
|
|
|
|
for (var event in events) {
|
|
|
|
|
@ -161,7 +188,7 @@ class CalenderUtilsNew {
|
|
|
|
|
|
|
|
|
|
FutureOr<String> getEventIdIfEventExist(String admissionId) async {
|
|
|
|
|
if (writableCalender.isEmpty) {
|
|
|
|
|
await getCalenders();
|
|
|
|
|
await ensureCalendarsLoaded();
|
|
|
|
|
}
|
|
|
|
|
List<Event> events = await getEvents();
|
|
|
|
|
if (events.isEmpty) return "";
|
|
|
|
|
@ -174,7 +201,7 @@ class CalenderUtilsNew {
|
|
|
|
|
|
|
|
|
|
FutureOr<bool> checkAndRemove({required String id}) async {
|
|
|
|
|
if (writableCalender.isEmpty) {
|
|
|
|
|
await getCalenders();
|
|
|
|
|
await ensureCalendarsLoaded();
|
|
|
|
|
}
|
|
|
|
|
List<Event> events = await getEvents();
|
|
|
|
|
if (events.isEmpty) return false;
|
|
|
|
|
@ -191,7 +218,7 @@ class CalenderUtilsNew {
|
|
|
|
|
|
|
|
|
|
FutureOr<bool> checkAndRemoveMultipleItems({required String id}) async {
|
|
|
|
|
if (writableCalender.isEmpty) {
|
|
|
|
|
await getCalenders();
|
|
|
|
|
await ensureCalendarsLoaded();
|
|
|
|
|
}
|
|
|
|
|
List<Event> events = await getEvents();
|
|
|
|
|
if (events.isEmpty) return false;
|
|
|
|
|
|