Contacts API


#1

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
});


#2

We don’t have contacts on the priority list right now. But hopefully someone in the community can help you make this. It should be very simple with foreign code.


#3

Here is a skeleton of an API that you could use to implement the things you are requesting.

https://github.com/bolav/fuse-contacts


#4

@Bolav thanks, but sadly I don’t understand the majority of that uno code to expand it. I haven’t tested this code yet, so thanks for the contacts.getAll() method I’ll have to try it out soon


#5

Heads up! The Contacts module has been fixed and improved a little: https://github.com/bolav/fuse-contacts

On Android, it now returns only those contacts you’ve entered in Address Book (as opposed to random entries from all connected accounts on device) and calls the Permission request correctly (thanks to Fuse team!).

On both platforms, a new getPage() function has been implemented to provide a way to work around memory consumption with big contact lists.


#6

Hi guys,

I am having massive issues with this contacts uno code. It is all implemented and it will work the first time I open the app… but the second time I open the app (or the page reloads) - it will fail with an error that “authorise” is not a function…

After a bit of investigation it looks like the permissions code is crashing - I have a feeling that Android might be rejecting multiple requests to the permission, even after its approved.

Thanks
Michael


#7

Hey guys,

I tried to get the fuse-contacts module to work, but when I try to call the authorize() function after require(“Contacts”) I get the following error …

[Viewport]: Error: TypeError: contacts.authorize is not a function: Name: TypeError: contacts.authorize is not a function
Error message: Uncaught TypeError: contacts.authorize is not a function

JS stack trace: TypeError: contacts.authorize is not a function
at Object._tempMethod (MainView.ux:20:23)
in Fuse.Scripting.DiagnosticSubjectMainView.ux:20

I’m relatively new to Fuse, so maybe I’m missing something. But I followed all the steps in the README. Installed it via fusepm (the unoproj file is adapted automatically), did a whole bunch of restarts of the preview tool, etc.

Who can help?

Thanks,
Tobi


#8

Debugged a little and solved the issue by removing <Contacts ux:Global="Contacts" /> from UX code.

Since a good while now, Uno classes can be instantiated directly in JavaScript, and you don’t need to include them in UX the old way. Just do var contacts = require('Contacts'); in your JS and you should be all set.


#9

Hey Uldis,

thanks for your quick response. Your reply helped me to get the module working. Thanks also to Bolav for providing it.

Cheere, Tobi


#10

This solution not running anymore.


#11

I fixed the dependencies and permission issue, Android & iOS works! ~ https://github.com/rbtech/fuse-contacts