Skip to content

Commit

Permalink
Updated for iOS Contacts
Browse files Browse the repository at this point in the history
*Need to create a complete Contact object on return from save.
*Need to convert from JS Dates to milliseconds and vice versa when
roundtripping to device.
  • Loading branch information
Becky Gibson authored and filmaj committed Mar 15, 2012
1 parent 9011d1e commit 3360df0
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 12 deletions.
4 changes: 2 additions & 2 deletions lib/exec/ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ module.exports = function() {
for (var i = 0; i < actionArgs.length; ++i) {
var arg = actionArgs[i];
if (arg == undefined || arg == null) { // nulls are pushed to the args now (becomes NSNull)
command.arguments.push(arg);
} else if (typeof(arg) == 'object') {
continue; //command.arguments.push(arg);
} else if (typeof(arg) == 'object' && !(arg instanceof Array)) {
command.options = arg;
} else {
command.arguments.push(arg);
Expand Down
76 changes: 66 additions & 10 deletions lib/plugin/Contact.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,50 @@ var Contact = function (id, displayName, name, nickname, phoneNumbers, emails, a
this.displayName = displayName || null;
this.name = name || null; // ContactName
this.nickname = nickname || null;
this.phoneNumbers = phoneNumbers || []; // ContactField[]
this.emails = emails || []; // ContactField[]
this.addresses = addresses || []; // ContactAddress[]
this.ims = ims || []; // ContactField[]
this.organizations = organizations || []; // ContactOrganization[]
this.phoneNumbers = phoneNumbers || null; // ContactField[]
this.emails = emails || null; // ContactField[]
this.addresses = addresses || null; // ContactAddress[]
this.ims = ims || null; // ContactField[]
this.organizations = organizations || null; // ContactOrganization[]
this.birthday = birthday || null;
this.note = note || null;
this.photos = photos || []; // ContactField[]
this.categories = categories || []; // ContactField[]
this.urls = urls || []; // ContactField[]
this.photos = photos || null; // ContactField[]
this.categories = categories || null; // ContactField[]
this.urls = urls || null; // ContactField[]
};
/**
* Converts Complex objects into primitives
* Only conversion at present is for Dates.
**/
Contact.prototype.convertOut = function() {
var value = this.birthday;
if (value != null) {
// try to make it a Date object if it is not already
if (!value instanceof Date){
try {
value = new Date(value);
} catch(exception){
value = null;
}
}
if (value instanceof Date){
value = value.valueOf(); // convert to milliseconds
}
this.birthday = value;
}
};

/**
* Converts primitives into Complex Object
* Currently only used for Date fields
*/
Contact.prototype.convertIn = function() {
var value = this.birthday;
try {
this.birthday = new Date(parseFloat(value));
} catch (exception){
console.log("exception creating date");
}
};

/**
Expand All @@ -50,7 +84,7 @@ Contact.prototype.remove = function(successCB, errorCB) {
errorCB(errorObj);
}
else {
exec(successCB, errorCB, "Contacts", "remove", [this.id]);
exec(successCB, errorCB, "Contacts", "remove", [this.id]);
}
};

Expand Down Expand Up @@ -114,7 +148,29 @@ Contact.prototype.clone = function() {
* @param errorCB error callback
*/
Contact.prototype.save = function(successCB, errorCB) {
exec(successCB, errorCB, "Contacts", "save", [this]);
var success = function(result) {
if (result) {
if (typeof successCB === 'function') {
var fullContact = require('cordova/plugin/contacts').create(result);
fullContact.convertIn();
try {
successCB(fullContact);
}
catch (e) {
console.log('Error invoking callback: ' + e);
}
}
}
else {
// no Entry object returned
errorCB(ContactError.UNKNOWN_ERROR);
}
};
// convert birthday value to milliseconds - don't modify original(this) contact
var dupContact = utils.clone(this);
dupContact.convertOut();

exec(success, errorCB, "Contacts", "save", [dupContact]);
};


Expand Down

0 comments on commit 3360df0

Please sign in to comment.