Skip to content

Adding Address to Contact

Aik Siang edited this page May 8, 2014 · 1 revision

Adding Address to Contact

This feature takes an address from Navigator and adds it to the address field any chosen contact in the device's contact database. When a location is being searched and selected, the menu button will consist of a new button "Add Address" which will bring up the whole list of contacts from the device's contact database. Upon choosing a particular contact, the app will add that location's address into the address field of that contact. After that, an alert will pop up to feedback to the user whether the address was added successfully or not. Currently, it can only be added to contacts without an address field. ie. A contact with an existing address cannot have another address added to it.

How it works

It makes use of the Contacts Plugin provided by Cordova, as well as Contact Chooser Plugin, which is a fork of another repository. This fork is a plugin which essentially retrieves the list of all contacts in native layout, and upon selection it will return the contact ID , contact display name and contact address from the address book in the device. The contact plugin takes the name of the selected contact and does another search to get that contact in the contact object form. Then the id from the contact chooser plugin and the contact object are matched to find the right contact. The address from the selected location is then added to the contact via the Contacts Plugin. Some future implementations are that the contact chooser plugin can be improved to do the work of adding the address to the contact instead of doing it from the contacts plugin. Another improvement is the search should be done only once instead of twice in this case.

Dependencies

This feature requires the Contacts Plugin provided by Cordova, as well as cordova.js, as well as Contact Chooser Plugin which is a fork of another repository.

Setup

If this feature is used as a standalone, add the contacts plugin and the contact chooser plugin using the command line:
cordova plugin add org.apache.cordova.contacts
cordova plugin add https://github.com/aiksiang/com.kolwit.pickcontact
If not, these plugins are included in the list of plugins which does not require any additional actions.

Code

The main chunk of the code can be found in contactchooser.coffee, and contactchooser.js should be included in index.html. Firstly, window.plugins.PickContact.chooseContact calls the plugin function cordova.exec, which passes control to functions in the PickContactPlugin.java. It then creates a new Intent to pick contacts and waits for results. Then the data of the chosen contact is retrieved from the result and put into json format, which is then passed back into the contactInfo parameter of the window.plugins.PickContact.chooseContact method. Then, the navigator.contacts.find method finds the contact using the chosen name. The location is retrieved from the data stored when the "select" method of "Prediction" class is called.
This is from the select method:
$input.data "selected_location", @location.name
Then, retrieval of this data:
curLoc = $('#input-search input').data 'selected_location'
Then, adding this address to the contact:
address.formatted = curLoc
names.addresses = []
names.addresses.push address
names.save()