It’d be really awesome to have a Contacts API, for the app I’m working on I want to do something like SnapChat does with Contacts, where you can add someone by your Contacts list.
So an example API I have in mind (Naively proposing I’m not sure what’s allowed on each OS when it comes to messing with Contacts, perhaps a read only api is possible):
Reading Contacts
var contacts = require('FuseJS/Contacts');
contacts.getAll().then(function(contactsArr) {
// where contactsArr is an Array of contact objects
});
contacts.searchByName('edwin').then(function(contactsArr) {
// where contactsArr is an Array of contact objects with the either firstName or lastName including the string edwin (fuzzysearched)
});
contacts.searchByFirstName('edwin').then(function(contactsArr) {
// where contactsArr is an Array of contact objects with only the firstName including the string edwin (fuzzysearched)
});
contacts.searchByLastName('reynoso').then(function(contactsArr) {
// where contactsArr is an Array of contact objects with only the lastName including the string reynoso (fuzzysearched)
});
contacts.searchByNumber('347').then(function(contactsArr) {
// where contactsArr is an Array of contact objects with their number including the number 347
});
contacts.search({ field: 'FirstName', query: 'edwin' }).then(function(contactsArr) {
// returns same thing as searchByFirstName, however search method takes an object which specifies the field and what to query
});
Writing Contacts
contacts.createNewContact({
firstName: 'Edwin',
lastName: 'Reynoso',
number: '347 ... ....'
... // etc
});
contacts.update(contactObject, {
// values to update
});
contacts.updateAll([...contactObjects], {
// values to update
});
Or just pass an Array of Contact Objects to the update
method, however I think its a bad idea to even be able to update a bunch of contacts like so
Contact Object
var contact = contactsArr[0];
contact.fullName; // 'Edwin Reynoso'
contact.firstName; // 'Edwin'
contact.lastName; // 'Reynoso'
contact.number; // '347 ... ....'
// or use methods?
contact.getFullName(); // 'Edwin Reynoso'
contact.getFirstName(); // 'Edwin'
contact.getLastName(); // 'Reynoso'
contact.getNumber(); // '347 ... ....'
contact.get('FirstName'); // 'Edwin'
The only reason for using methods or getters would be for not having to read each contact right away, so it could asynchronously be done
regarding getters it’d synchrounosly read the attribute of the contact I suppose, yet that may be bad so perhaps the methods could return a promise:
contact.getFullName().then(function(firstName) {
// firstName === 'Edwin'
});
...// etc
In all honesty one big read should be enough, and make everything else easier
Contact Objects could have writing methods?:
contact.update({
// values to update
});