Skip to content

Instantly share code, notes, and snippets.

@samueleastdev
Last active October 12, 2016 11:15
Show Gist options
  • Save samueleastdev/8e735c8783e314873b4ecacc2795fa3b to your computer and use it in GitHub Desktop.
Save samueleastdev/8e735c8783e314873b4ecacc2795fa3b to your computer and use it in GitHub Desktop.
Cross Platform Appcelerator Calendar Permissions With Calendar Select
var cals = require("permissions");
cals.addCalendarEvent({
iosEvent: {
title: 'Sample Event',
notes: 'This is a test event which has some values assigned to it.',
location: 'Appcelerator Inc',
begin: new Date(new Date().getTime() + 3000),
end: new Date(new Date().getTime() + 900000),
availability: Ti.Calendar.AVAILABILITY_FREE,
allDay: false,
hasAlarm: true
},
iosEventExtraOptions: {
defaultCalendar: true,
frequency: true,
frequencyDuration: Titanium.Calendar.RECURRENCEFREQUENCY_DAILY // WEEKLY MONTHLY YEARLY
},
androidEvent: {
title: 'Sample Event',
description: 'This is a test event which has some values assigned to it.',
location: 'Appcelerator Inc',
begin: new Date(new Date().getTime() + 3000),
end: new Date(new Date().getTime() + 900000),
hasAlarm: true
},
androidEventExtraOptions: {
method: Titanium.Calendar.METHOD_ALERT, // DEFAULT EMAIL SMS
}
}, function(_response) {
console.log("ADD EVENT RESPONSE", _response);
if (_response.success) {
alert(_response.message);
} else {
var dialog = Ti.UI.createAlertDialog({
cancel: 1,
buttonNames: ['Settings', 'Cancel'],
message: _response.message,
title: 'Error'
});
dialog.addEventListener('click', function(e) {
if (e.index === e.source.cancel) {
Ti.API.info('The cancel button was clicked');
return;
}
cals.editPermissions();
});
dialog.show();
}
});
/*
* Crossplatform calendat permissions
*/
function requestCalendarPermissions(callback) {
// The new cross-platform way to check permissions
var hasCalendarPermissions = Ti.Calendar.hasCalendarPermissions();
if (hasCalendarPermissions) {
return callback({
success: true
});
}
// On iOS we can get information on the reason why we might not have permission
if (OS_IOS) {
// Map constants to names
var map = {};
map[Ti.Calendar.AUTHORIZATION_AUTHORIZED] = 'AUTHORIZATION_AUTHORIZED';
map[Ti.Calendar.AUTHORIZATION_DENIED] = 'AUTHORIZATION_DENIED';
map[Ti.Calendar.AUTHORIZATION_RESTRICTED] = 'AUTHORIZATION_RESTRICTED';
map[Ti.Calendar.AUTHORIZATION_UNKNOWN] = 'AUTHORIZATION_UNKNOWN';
// Available since Ti 3.1.0
var eventsAuthorization = Ti.Calendar.eventsAuthorization;
if (eventsAuthorization === Ti.Calendar.AUTHORIZATION_RESTRICTED) {
return callback({
success: false,
message: 'Because permission are restricted by some policy which you as user cannot change, we don\'t request as that might also cause issues.'
});
} else if (eventsAuthorization === Ti.Calendar.AUTHORIZATION_DENIED) {
return callback({
success: false,
message: 'You denied permission before. We don\'t request again as that won\'t show the dialog anyway. Instead, press Yes to open the Settings App to grant permission there.'
});
}
}
// The new cross-platform way to request permissions
Ti.Calendar.requestCalendarPermissions(function(e) {
if (e.success) {
// Instead, probably call the same method you call if hasCalendarPermissions() is true
return callback({
success: true,
message: 'You granted permission.'
});
} else if (OS_ANDROID) {
return callback({
success: false,
message: 'You don\'t have the required uses-permissions in tiapp.xml or you denied permission for now, forever or the dialog did not show at all because you denied forever before.'
});
} else {
// We already check AUTHORIZATION_DENIED earlier so we can be sure it was denied now and not before
return callback({
success: false,
message: 'You denied permission.'
});
}
});
}
/*
* Select a calendar to add the event to
*/
function selectCalendar(callback) {
var calendars = (OS_IOS) ? Ti.Calendar.allEditableCalendars : Ti.Calendar.selectableCalendars;
if (calendars && calendars.length > 0) {
var deviceCalendars = [];
var calendarsOptions = [];
for (var i = 0; i < calendars.length; i++) {
deviceCalendars.push(calendars[i]);
calendarsOptions.push(calendars[i].name);
}
calendarsOptions.push("Cancel");
var opts = {
cancel: (calendarsOptions.length - 1),
options: calendarsOptions,
destructive: (calendarsOptions.length - 1),
title: 'Select a Calendar?'
};
var dialog = Ti.UI.createOptionDialog(opts);
dialog.addEventListener('click', function(_response) {
callback({
success: true,
id: deviceCalendars[_response.index].id,
message: 'Returning Calendars.'
});
});
dialog.show();
} else {
callback({
success: false,
message: 'No Calendar on this device.'
});
}
}
/**
* Event listener added in the view.
* Fired when user taps on edit-button to open the app settings.
*/
exports.editPermissions = function() {
if (OS_IOS) {
Ti.Platform.openURL(Ti.App.iOS.applicationOpenSettingsURL);
}
if (OS_ANDROID) {
var intent = Ti.Android.createIntent({
action: 'android.settings.APPLICATION_SETTINGS',
});
intent.addFlags(Ti.Android.FLAG_ACTIVITY_NEW_TASK);
Ti.Android.currentActivity.startActivity(intent);
}
};
/*
* Add the event
*/
exports.addCalendarEvent = function(data, callback) {
// this must be run after the ui has loaded
requestCalendarPermissions(function(_response) {
console.log("requestCalendarPermissions", _response);
if (_response.success) {
// select calendars
selectCalendar(function(_response) {
console.log("androidSelectCalendar", _response);
if (_response.success) {
if (OS_IOS) {
var calendar = Ti.Calendar.getCalendarById(_response.id);
if (calendar) {
var event1 = calendar.createEvent(data.iosEvent);
// Set a start date
var alert1 = event1.createAlert({
absoluteDate: new Date(new Date().getTime() - (1000 * 60 * 20))
});
// Set a end date
var alert2 = event1.createAlert({
relativeOffset: -(60 * 15)
});
var allAlerts = new Array(alert1, alert2);
event1.alerts = allAlerts;
if (data.iosEventExtraOptions.frequency) {
var newRule = event1.createRecurrenceRule({
frequency: data.iosEventExtraOptions.frequencyDuration,
interval: 1,
/*daysOfTheWeek : [{
dayOfWeek : 1,
week : 2
}, {
dayOfWeek : 2
}],
end : {
occurrenceCount : 10
}*/
});
event1.recurrenceRules = [newRule];
}
event1.save(Ti.Calendar.SPAN_THISEVENT);
callback({
success: true,
data: {
id: event1.id
},
message: "Successfully added event"
});
} else {
callback({
success: false,
message: "Error adding to this calendar"
});
}
}
if (OS_ANDROID) {
var calendar = Ti.Calendar.getCalendarById(_response.id);
var event1 = calendar.createEvent(data.androidEvent);
var reminderDetails = {
minutes: 10,
method: data.androidEventExtraOptions.method
};
event1.createReminder(reminderDetails);
callback({
success: true,
data: {
id: event1.id
},
message: "Successfully added event"
});
}
} else {
callback({
success: false,
message: _response.message
});
}
});
} else {
callback({
success: false,
message: _response.message
});
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment