Skip to content

Commit

Permalink
#14 New feature: allow specifying a calendar color enhancement
Browse files Browse the repository at this point in the history
#13 New feature: deleteCalendar enhancement
#6 Android: Cannot delete custom local calendar bug enhancement
  • Loading branch information
EddyVerbruggen committed Nov 22, 2016
1 parent b476468 commit 05c8fb8
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 13 deletions.
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,14 @@ If you want a quickstart, [clone our demo app](https://github.com/EddyVerbruggen
endDate: new Date(new Date().getTime() + (10*24*60*60*1000)) // 10 days
};

// Want to use a custom calendar for your app? Pass in the 'id' or 'name'.
// Want to use a custom calendar for your app? Pass in the 'name'.
// If the name doesn't yet exist the plugin will create it for you.
options.calendar = {
// id: 3,
name: "NativeScript Cal"
// the color, in this case red
color: "#FF0000",
// Can be used on Android to group the calendars. Examples: Your app name, or an emailaddress
accountName: "My App Name"
};

Calendar.createEvent(options).then(
Expand Down Expand Up @@ -185,3 +188,15 @@ Usage is largely the same as findEvents, only the result is a bit different ;)
}
)
```

### deleteCalendar

##### TypeScript
```js
Calendar.deleteCalendar({
name: "My Calendar name"
}).then((id) => {
// id may be null if none was deleted
console.log("Deleted Calendar with id " + id);
});
```
60 changes: 53 additions & 7 deletions calendar.android.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var application = require("application");
var utils = require("utils/utils");
var Color = require("color").Color;
var Calendar = require("./calendar-common");

Calendar._fields = {
Expand Down Expand Up @@ -331,22 +332,26 @@ Calendar.createEvent = function(arg) {
// create it
var calUri = android.provider.CalendarContract.Calendars.CONTENT_URI;
var calendarContentValues = new android.content.ContentValues();
var accountName = settings.calendar.accountName || settings.calendar.name;
calendarContentValues.put("account_name", accountName);
calendarContentValues.put("account_type", "LOCAL");
calendarContentValues.put("name", settings.calendar.name);
calendarContentValues.put("calendar_displayName", settings.calendar.name);
calendarContentValues.put("calendar_access_level", new java.lang.Integer(700)); // "owner"
if (settings.calendar.color && Color.isValid(settings.calendar.color)) {
var androidColor = new Color(settings.calendar.color).android;
calendarContentValues.put("calendar_color", new java.lang.Integer(androidColor));
}
calendarContentValues.put("visible", new java.lang.Integer(1));
calendarContentValues.put("account_type", "LOCAL");
calendarContentValues.put("name", settings.calendar.name);
calendarContentValues.put("account_name", settings.calendar.name);
calendarContentValues.put("ownerAccount", settings.calendar.name);
// calendarContentValues.put("calendar_color", android.graphics.Color.RED);
calendarContentValues.put("sync_events", new java.lang.Integer(1));

calUri = calUri.buildUpon()
.appendQueryParameter("caller_is_syncadapter", "true")
.appendQueryParameter("account_name", settings.calendar.name)
.appendQueryParameter("account_name", accountName)
.appendQueryParameter("account_type", "LOCAL")
.build();
ContentResolver.insert(calUri, calendarContentValues);
// retrieve the calendar we just created
// retrieve the calendar we've' just created
var cals = Calendar._findCalendars(settings.calendar.name);
if (cals.length > 0) {
calendarId = cals[0].id;
Expand Down Expand Up @@ -411,4 +416,45 @@ Calendar.createEvent = function(arg) {
});
};

Calendar.deleteCalendar = function(arg) {
return new Promise(function (resolve, reject) {
try {
if (!arg.name) {
reject("name is mandatory");
return;
}

var onPermissionGranted = function() {
var calendars = Calendar._findCalendars(arg.name);
var deletedCalId = null;

if (calendars.length > 0) {
var calUri = android.provider.CalendarContract.Calendars.CONTENT_URI;
var ContentResolver = utils.ad.getApplicationContext().getContentResolver();

// syntactically this is a loop but there's most likely only 1 item
for (var c in calendars) {
var calendar = calendars[c];
var deleteUri = android.content.ContentUris.withAppendedId(calUri, calendar.id);
ContentResolver.delete(deleteUri, null, null);
deletedCalId = calendar.id;
}
}

resolve(deletedCalId);
};

if (!Calendar._hasWritePermission()) {
Calendar._requestWritePermission(onPermissionGranted, reject);
return;
}

onPermissionGranted();
} catch (ex) {
console.log("Error in Calendar.deleteCalendar: " + ex);
reject(ex);
}
});
};

module.exports = Calendar;
40 changes: 37 additions & 3 deletions calendar.ios.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
var Calendar = require("./calendar-common");
var application = require("application");
var Color = require("color").Color;

Calendar._eventStore = null;

Expand Down Expand Up @@ -287,9 +289,9 @@ Calendar.createEvent = function (arg) {
// create it
calendar = EKCalendar.calendarForEntityTypeEventStore(EKEntityTypeEvent, Calendar._eventStore);
calendar.title = settings.calendar.name;
if (false && settings.calendar.color !== null) {
// TODO hex to UIColor
// calendar.CGColor = settings.calendar.color;
if (settings.calendar.color && Color.isValid(settings.calendar.color)) {
var iosColor = new Color(settings.calendar.color).ios;
calendar.CGColor = iosColor;
}
calendar.source = Calendar._findEKSource();
Calendar._eventStore.saveCalendarCommitError(calendar, true, null);
Expand Down Expand Up @@ -395,4 +397,36 @@ Calendar.deleteEvents = function (arg) {
});
};

Calendar.deleteCalendar = function (arg) {
return new Promise(function (resolve, reject) {
try {
if (!arg.name) {
reject("name is mandatory");
return;
}

var onPermissionGranted = function() {
var calendars = Calendar._findCalendars(arg.name);
var deletedCalId = null;

if (calendars.length > 0) {
// syntactically this is a loop but there's most likely only 1 item
for (var c in calendars) {
var calendar = calendars[c];
Calendar._eventStore.removeCalendarCommitError(calendar, true, null);
deletedCalId = calendar.calendarIdentifier;
}
}

resolve(deletedCalId);
};

Calendar._invokeFunctionOnEventStore(onPermissionGranted, reject);
} catch (ex) {
console.log("Error in Calendar.deleteCalendar: " + ex);
reject(ex);
}
});
};

module.exports = Calendar;
18 changes: 18 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ declare module "nativescript-calendar" {
*/
calendar: {
name: string;
/**
* Example, red: "#FF0000"
*/
color?: string;
/**
* Can be used on Android to group the calendars.
* Examples: Your app name, or an emailaddress.
*/
accountName?: string;
};

/**
Expand Down Expand Up @@ -128,6 +137,10 @@ declare module "nativescript-calendar" {
export interface DeleteEventsOptions extends FindOrDeleteEventsOptions {
}

export interface DeleteCalendarOptions {
name: string;
}

export interface Calendar {
id: string;
name: string;
Expand Down Expand Up @@ -184,6 +197,11 @@ declare module "nativescript-calendar" {
*/
export function listCalendars(): Promise<Calendar[]>;

/**
* Returns the ID of the deleted calendar (or null if none was deleted).
*/
export function deleteCalendar(options: DeleteCalendarOptions): Promise<string>;

/**
* No real reason to use this as it's all handled automatically for you.
*/
Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nativescript-calendar",
"version": "1.1.4",
"version": "1.2.0",
"description": "Interact with the native calendar. Add, Update, Read, you name it.",
"main": "calendar.js",
"typings": "index.d.ts",
Expand All @@ -17,7 +17,11 @@
"keywords": [
"ecosystem:nativescript",
"NativeScript",
"iOS",
"Android",
"Calendar",
"Events",
"Event",
"Agenda"
],
"author": "Telerik / Eddy Verbruggen <eddyverbruggen@gmail.com>",
Expand Down

0 comments on commit 05c8fb8

Please sign in to comment.