Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding removeCalendar method #269

Merged
merged 1 commit into from
Apr 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,22 @@ Returns: **Promise**

<br/>

### removeCalendar
Removes a calendar.

```javascript
RNCalendarEvents.removeCalendar(id)
```

Arguments:
- id: String - The id of the calendar to remove.

Returns: **Promise**
- fulfilled: Bool - Successful
- rejected: Error

<br/>

### findEventById
Find calendar event by id.
Returns a promise with fulfilled found events.
Expand Down
21 changes: 21 additions & 0 deletions RNCalendarEvents.m
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,27 @@ - (NSDictionary *)serializeCalendarEvent:(EKEvent *)event
[NSString stringWithFormat:@"Calendar %@ could not be saved", title], error);
}

RCT_EXPORT_METHOD(removeCalendar:(NSString *)calendarId resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
if (![self isCalendarAccessGranted]) {
reject(@"error", @"unauthorized to access calendar", nil);
return;
}


dispatch_async(serialQueue, ^{

EKCalendar *calendar = (EKCalendar *)[self.eventStore calendarWithIdentifier:calendarId];
NSError *error = nil;

BOOL success = [self.eventStore removeCalendar:calendar commit:YES error:&error];
if (error) {
return reject(@"error", [error.userInfo valueForKey:@"NSLocalizedDescription"], nil);
}
return resolve(@(success));
});
}

RCT_EXPORT_METHOD(fetchAllEvents:(NSDate *)startDate endDate:(NSDate *)endDate calendars:(NSArray *)calendars resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
if (![self isCalendarAccessGranted]) {
Expand Down
37 changes: 37 additions & 0 deletions android/src/main/java/com/calendarevents/CalendarEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,22 @@ private int addCalendar(ReadableMap details) throws Exception, SecurityException
return Integer.parseInt(calendarUri.getLastPathSegment());
}

private boolean removeCalendar(String calendarID) {
int rows = 0;

try {
ContentResolver cr = reactContext.getContentResolver();

Uri uri = ContentUris.withAppendedId(CalendarContract.Calendars.CONTENT_URI, (long) Integer.parseInt(calendarID));
rows = cr.delete(uri, null, null);

} catch (Exception e) {
e.printStackTrace();
}

return rows > 0;
}

private WritableNativeArray findAttendeesByEventId(String eventID) {
WritableNativeArray result;
Cursor cursor;
Expand Down Expand Up @@ -1213,6 +1229,27 @@ public void run() {
}
}

@ReactMethod
public void removeCalendar(final String CalendarID, final Promise promise) {
if (this.haveCalendarReadWritePermissions()) {
try {
Thread thread = new Thread(new Runnable(){
@Override
public void run() {
boolean successful = removeCalendar(CalendarID);
promise.resolve(successful);
}
});
thread.start();

} catch (Exception e) {
promise.reject("error removing calendar", e.getMessage());
}
} else {
promise.reject("remove calendar error", "you don't have permissions to remove a calendar");
}

}
@ReactMethod
public void saveEvent(final String title, final ReadableMap details, final ReadableMap options, final Promise promise) {
if (this.haveCalendarReadWritePermissions()) {
Expand Down
4 changes: 4 additions & 0 deletions index.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ export default {
});
},

async removeCalendar (id) {
return CalendarEvents.removeCalendar(id)
},

async findEventById (id) {
return CalendarEvents.findById(id)
},
Expand Down
10 changes: 8 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,14 @@ export default class ReactNativeCalendarEvents {
*/
static saveCalendar(calendar: CalendarOptions): Promise<string>;
/**
* Find calendar event by id.
* @param id - Event ID
* Removes a calendar.
* @param id - The calendar id
* @returns - Promise resolving to boolean to indicate if removal succeeded.
*/
static removeCalendar(id: string): Promise<boolean>;
/**
* Find calendar by id.
* @param id - Calendar ID
*/
static findEventById(id: string): Promise<CalendarEventReadable | null>;
/**
Expand Down
4 changes: 4 additions & 0 deletions index.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ export default {
});
},

removeCalendar (id) {
return RNCalendarEvents.removeCalendar(id)
},

findEventById (id) {
return RNCalendarEvents.findEventById(id);
},
Expand Down