From 1b4f2d5adc5b81692d88e1fa3b73f6e6417b93de Mon Sep 17 00:00:00 2001 From: tanveer Date: Sun, 2 Apr 2023 03:04:45 +0530 Subject: [PATCH] ExeptionHandle --- .../com/calendarevents/RNCalendarEvents.java | 39 ++++++++++--------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/android/src/main/java/com/calendarevents/RNCalendarEvents.java b/android/src/main/java/com/calendarevents/RNCalendarEvents.java index ef3d499..5dacf26 100644 --- a/android/src/main/java/com/calendarevents/RNCalendarEvents.java +++ b/android/src/main/java/com/calendarevents/RNCalendarEvents.java @@ -472,7 +472,7 @@ private WritableNativeMap findEventInstanceById(String eventID) { return result; } - private long addEvent(String title, ReadableMap details, ReadableMap options) throws ParseException { + private long addEvent(String title, ReadableMap details, ReadableMap options, final Promise promise) throws ParseException { String dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"; SimpleDateFormat sdf = new SimpleDateFormat(dateFormat); boolean skipTimezone = false; @@ -673,7 +673,7 @@ private long addEvent(String title, ReadableMap details, ReadableMap options) th } if (details.hasKey("alarms")) { - createRemindersForEvent(cr, Long.parseLong(details.getString("id")), details.getArray("alarms")); + createRemindersForEvent(cr, Long.parseLong(details.getString("id")), details.getArray("alarms"), promise); } if (details.hasKey("attendees")) { @@ -715,7 +715,7 @@ private long addEvent(String title, ReadableMap details, ReadableMap options) th eventID = Long.parseLong(rowId); if (details.hasKey("alarms")) { - createRemindersForEvent(cr, eventID, details.getArray("alarms")); + createRemindersForEvent(cr, eventID, details.getArray("alarms"), promise); } if (details.hasKey("attendees")) { @@ -842,7 +842,7 @@ private void createAttendeesForEvent(ContentResolver resolver, long eventID, Rea //endregion //region Reminders - private void createRemindersForEvent(ContentResolver resolver, long eventID, ReadableArray reminders) { + private void createRemindersForEvent(ContentResolver resolver, long eventID, ReadableArray reminders, final Promise promise) { Cursor cursor = null; if (resolver != null) { @@ -865,20 +865,23 @@ private void createRemindersForEvent(ContentResolver resolver, long eventID, Rea if (cursor != null) { cursor.close(); } - - for (int i = 0; i < reminders.size(); i++) { - ReadableMap reminder = reminders.getMap(i); - ReadableType type = reminder.getType("date"); - if (type == ReadableType.Number) { - int minutes = reminder.getInt("date"); - ContentValues reminderValues = new ContentValues(); - - reminderValues.put(CalendarContract.Reminders.EVENT_ID, eventID); - reminderValues.put(CalendarContract.Reminders.MINUTES, minutes); - reminderValues.put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT); - - resolver.insert(CalendarContract.Reminders.CONTENT_URI, reminderValues); + try { + for (int i = 0; i < reminders.size(); i++) { + ReadableMap reminder = reminders.getMap(i); + ReadableType type = reminder.getType("date"); + if (type == ReadableType.Number) { + int minutes = reminder.getInt("date"); + ContentValues reminderValues = new ContentValues(); + + reminderValues.put(CalendarContract.Reminders.EVENT_ID, eventID); + reminderValues.put(CalendarContract.Reminders.MINUTES, minutes); + reminderValues.put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT); + + resolver.insert(CalendarContract.Reminders.CONTENT_URI, reminderValues); + } } + } catch (Exception e){ + promise.reject("add event error", "Unable to save event"); } } @@ -1349,7 +1352,7 @@ public void saveEvent(final String title, final ReadableMap details, final Reada public void run() { long eventId; try { - eventId = addEvent(title, details, options); + eventId = addEvent(title, details, options, promise); if (eventId > -1) { promise.resolve(Long.toString(eventId)); } else {