Skip to content

Commit

Permalink
tweaks to contacts and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
filmaj committed Mar 15, 2012
1 parent 3360df0 commit 94b7fb0
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 76 deletions.
97 changes: 38 additions & 59 deletions lib/plugin/Contact.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,23 @@ var exec = require('cordova/exec'),
ContactError = require('cordova/plugin/ContactError'),
utils = require('cordova/utils');

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


// convert birthday value to milliseconds - don't modify original(this) contact

/**
* Contains information about a single contact.
* @constructor
Expand Down Expand Up @@ -38,53 +55,21 @@ var Contact = function (id, displayName, name, nickname, phoneNumbers, emails, a
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");
}
};

/**
* Removes contact from device storage.
* @param successCB success callback
* @param errorCB error callback
*/
Contact.prototype.remove = function(successCB, errorCB) {
var fail = function(code) {
errorCB(new ContactError(code));
};
if (this.id === null) {
var errorObj = new ContactError(ContactError.UNKNOWN_ERROR);
errorCB(errorObj);
fail(ContactError.UNKNOWN_ERROR);
}
else {
exec(successCB, errorCB, "Contacts", "remove", [this.id]);
exec(successCB, fail, "Contacts", "remove", [this.id]);
}
};

Expand Down Expand Up @@ -148,29 +133,23 @@ Contact.prototype.clone = function() {
* @param errorCB error callback
*/
Contact.prototype.save = function(successCB, errorCB) {
var fail = function(code) {
errorCB(new ContactError(code));
};
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]);
if (result) {
if (typeof successCB === 'function') {
var fullContact = require('cordova/plugin/contacts').create(result);
successCB(convertIn(fullContact));
}
}
else {
// no Entry object returned
fail(ContactError.UNKNOWN_ERROR);
}
};
var dupContact = this.clone();
exec(success, fail, "Contacts", "save", [dupContact]);
};


Expand Down
34 changes: 17 additions & 17 deletions test/test.contact.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ describe("Contact", function () {
expect(c.displayName).toBe(null);
expect(c.name).toBe(null);
expect(c.nickname).toBe(null);
expect(c.phoneNumbers).toEqual([]);
expect(c.emails).toEqual([]);
expect(c.addresses).toEqual([]);
expect(c.ims).toEqual([]);
expect(c.organizations).toEqual([]);
expect(c.phoneNumbers).toEqual(null);
expect(c.emails).toEqual(null);
expect(c.addresses).toEqual(null);
expect(c.ims).toEqual(null);
expect(c.organizations).toEqual(null);
expect(c.birthday).toBe(null);
expect(c.note).toBe(null);
expect(c.photos).toEqual([]);
expect(c.categories).toEqual([]);
expect(c.urls).toEqual([]);
expect(c.photos).toEqual(null);
expect(c.categories).toEqual(null);
expect(c.urls).toEqual(null);
});

it("overrides default values with the arguments", function () {
Expand Down Expand Up @@ -67,14 +67,14 @@ describe("Contact", function () {
c.id = 1;
c.rawId = 1;

c.phoneNumbers.push({id: 1});
c.emails.push({id: 1});
c.addresses.push({id: 1});
c.ims.push({id: 1});
c.organizations.push({id: 1});
c.categories.push({id: 1});
c.photos.push({id: 1});
c.urls.push({id: 1});
c.phoneNumbers = [{id: 1}];
c.emails = [{id: 1}];
c.addresses = [{id: 1}];
c.ims = [{id: 1}];
c.organizations = [{id: 1}];
c.categories = [{id: 1}];
c.photos = [{id: 1}];
c.urls = [{id: 1}];

var clone = c.clone();

Expand All @@ -98,7 +98,7 @@ describe("Contact", function () {
e = jasmine.createSpy();

c.save(s, e);
expect(exec).toHaveBeenCalledWith(s, e, "Contacts", "save", [c]);
expect(exec).toHaveBeenCalledWith(jasmine.any(Function), jasmine.any(Function), "Contacts", "save", [c]);
});
});
});

0 comments on commit 94b7fb0

Please sign in to comment.