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

Possibility to add an alert for iOS #58

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,17 @@ eventConfig object:
| allDay | boolean | |
| url | String | iOS only |
| notes | String | The notes (iOS) or description (Android) associated with the event. |
| alert | String | Allow the user to set an alert, could be "0", "1", "2", "3", or none|

The dates passed to this module are strings. If you use moment, you may get the right format via `momentInUTC.format('YYYY-MM-DDTHH:mm:ss.SSS[Z]')` the string may look eg. like this: `'2017-09-25T08:00:00.000Z'`.

For the alert field, here is the specs :
- If you set `alert: "0"` => will add an alert at the startDate.
- If you set `alert: "1"` => will add an alert 5 minutes before the startDate.
- If you set `alert: "2"` => will add an alert 30 minutes before the startDate.
- If you set `alert: "3"` => will add an alert 60 minutes before the startDate.
- If alert is not set in the eventConfig, no alert will be set.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The options "0" ... "3" seem rather random - why not just pass the number of minutes from the JS side?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, yeah I was quite agree but because I didn't see that react-native-calendar-events already managed that. I'll have a look to see how I can eventually implement it here ! Thank's !


More options can be easily added, PRs are welcome!

### Editing an event
Expand Down
1 change: 1 addition & 0 deletions example/EventDemo/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export default class EventDemo extends Component {
startDate: utcDateToString(startDateUTC),
endDate: utcDateToString(moment.utc(startDateUTC).add(1, 'hours')),
notes: 'tasty!',
alert: "1", //it's gonna add an alarm 5 minutes before
navigationBarIOS: {
tintColor: 'orange',
backgroundColor: 'green',
Expand Down
9 changes: 9 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ declare module "react-native-add-calendar-event" {
*/
notes?: string;
navigationBarIOS?: NavigationBarIOS;
/*
* Alert string, could be "0", "1", "2" or "3"
* 0 => set an alarm at the original startDate
* 1 => set an alarm 5 minutes before startDate
* 2 => set an alarm 30 minutes before startDate
* 3 => set an alarm 60 minutes before startDate
* if no alert is set in the config, then no alert is set on the reminder
*/
alert?: string;
}

/**
Expand Down
28 changes: 28 additions & 0 deletions ios/AddCalendarEvent.m
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ - (NSDictionary *)constantsToExport
static NSString *const _notes = @"notes";
static NSString *const _url = @"url";
static NSString *const _allDay = @"allDay";
static NSString *const _alert = @"alert";

static NSString *const MODULE_NAME= @"AddCalendarEvent";

Expand Down Expand Up @@ -272,6 +273,33 @@ - (EKEvent *)createNewEventInstance {
if (options[_allDay]) {
event.allDay = [RCTConvert BOOL:options[_allDay]];
}
if (options[_alert]) {
NSDate *originalDate = [RCTConvert NSDate:options[_startDate]];

if ([[RCTConvert NSString:options[_alert]] caseInsensitiveCompare:@"0"] == NSOrderedSame)
{
EKAlarm * alarm = [EKAlarm alarmWithAbsoluteDate:originalDate];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're using alarmWithAbsoluteDate - let's consider the following scenario: user

  1. creates an event for 2pm and adds an alert that should fire 30 minutes before
  2. moves the event to 1:30pm
  3. when does the alert fire and is it what the user expects?

I think this might be useful.

event.alarms = @[alarm];
}
if ([[RCTConvert NSString:options[_alert]] caseInsensitiveCompare:@"1"] == NSOrderedSame)
{
NSDate *alertReminder = [originalDate dateByAddingTimeInterval:-60*5];
EKAlarm * alarm = [EKAlarm alarmWithAbsoluteDate:alertReminder];
event.alarms = @[alarm];
}
if ([[RCTConvert NSString:options[_alert]] caseInsensitiveCompare:@"2"] == NSOrderedSame)
{
NSDate *alertReminder = [originalDate dateByAddingTimeInterval:-60*30];
EKAlarm * alarm = [EKAlarm alarmWithAbsoluteDate:alertReminder];
event.alarms = @[alarm];
}
if ([[RCTConvert NSString:options[_alert]] caseInsensitiveCompare:@"3"] == NSOrderedSame)
{
NSDate *alertReminder = [originalDate dateByAddingTimeInterval:-60*60];
EKAlarm * alarm = [EKAlarm alarmWithAbsoluteDate:alertReminder];
event.alarms = @[alarm];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

take a good look at the code: there is a lot of duplicates - the ifs look more or less the same, with just small differences. It would be better to remove the duplications and perhaps extract this functionality into a separate function :)

}
}
return event;
}

Expand Down