Contacts Example

Smartphones all have a built-in PIM (Personal Information Management) Contacts application that allows end users to store phone numbers and addresses. Smartphone platforms allow applications to access those contacts through APIs that differ per platform, but generally offer the same capabilities. In this section, we will step through writing a Rhodes application that will allow you to show and edit native PIM contacts using Rhodes APIs on both the iPhone and Android. This example is written using Rhodes 2.0.2.

The complete source code to the completed application is available online at: http://github.com/VGraupera/Rho-Contacts-Sample.

Generate a skeleton application using the rhogen command as shown in Listing 6-7. Listing 6-7. Creating the Contacts application using the rhogen command

> rhogen app Contacts Generating with app generator:

[ADDED] Contacts/rhoconfig.txt

[ADDED] Contacts/build.yml

[ADDED] Contacts/app/application.rb

[ADDED] Contacts/app/index.erb

[ADDED] Contacts/app/index.bb.erb

[ADDED] Contacts/app/layout.erb

[ADDED] Contacts/app/loading.html

[ADDED] Contacts/Rakefile

[ADDED] Contacts/app/helpers

[ADDED] Contacts/icon

[ADDED] Contacts/app/Settings

[ADDED] Contacts/public

> cd Contacts/

> rhogen model Contact first_name,last_name,email_addressJbusiness_number Generating with model generator:

[ADDED] app/Contact/index.erb

[ADDED] app/Contact/edit.erb

[ADDED] app/Contact/new.erb

[ADDED] app/Contact/show.erb

[ADDED] app/Contact/index.bb.erb

[ADDED] app/Contact/edit.bb.erb

[ADDED] app/Contact/new.bb.erb

[ADDED] app/Contact/show.bb.erb

[ADDED] app/Contact/contact_controller.rb

[ADDED] app/Contact/contact.rb

[ADDED] app/test/contact_spec.rb

Edit your rhoconfig.txt file and change start_path = '/app'

to start_path = '/app/Contact'

Edit your contact_controller.rb file to look like Listing 6-8.

Listing 6-8. Contacts/app/Contacts/contact_controller. rb require 'rho/rhocontroller' require 'rho/rhocontact'

require 'helpers/browser_helper'

class ContactController < Rho::RhoController include BrowserHelper

#GET /Contact def index

^contacts = Rho::RhoContact.find(:all)

(9contacts.to_a.sort! {|x,y| x[1]['first_name'] <=> y[1]['first_name'] } if ^contacts end

^contact = Rho::RhoContact.find(@params['id']) end

# GET /Contact/new def new end

^contact = Rho::RhoContact.find(@params['id']) end

# POST /Contact/create def create

^contact = Rho::RhoContact.create!(@params['contact']) redirect :action => :index end

# POST /Contact/{1}/update def update

Rho::RhoContact.update_attributes(@params['contact']) redirect :action => :index end

# POST /Contact/{1}/delete def delete

Rho::RhoContact.destroy(@params['id']) redirect :action => :index end end

You require 'rho/rhocontact' to load the Rhodes PIM contact API.

In the index action, you create an array of all the contacts on the device using Rho::RhoContact.find(:all) and assign to an instance variable

^contacts = Rho::RhoContact.find(:all)

Rho::RhoContact.find(:all) will return all the contacts on the device. Unfortunately, there is no way to limit it to a certain number of contacts or to specify a sort order, so in the next line you sort the contacts array manually in Ruby by first_name provided, of course, there are any contacts at all.

Next, edit the Contact/index.erb to look like Listing 6-9.

Listing 6-9. Contacts/app/Contact/index.erb

<div id="pageTitle">

<div id="toolbar">

<div id="leftItem" class="regularButton"><%= link_to "Home", Rho::RhoConfig.start_path %></div>

<div id="rightItem" class="regularButton"><%= link_to "New", :controller => :Contact, :action => :new %></div> </div>

<a href="<%= url_for :action => :show, :id => obj[1]['id'] %>"> <span class="title"><%= "#{obj[1]['first_name']} #{obj[1]['last_name']}" %></span>

<span class="disclosure_indicator"></span>

</a> </li> <% end %> </ul> </div>

In this ERB template, you iterate through the array of contacts and output each one in a list. Because of the special HTML CSS classes you use, the list will look like a native iPhone table.

Now, build and run the application for the iPhone simulator:

> rake run:iphone

Before launching our Rhodes Contacts application in the simulator, open the native Contacts application in the simulator and add some contacts. By default the iPhone simulator address book is empty. I added two contacts: John Doe and Abraham Lincoln.

Now launch our Rhodes Contacts application, and you will see the same contacts (Figure 6-9).

John Doe

Abraham Lincoln

John Doe

Abraham Lincoln

Figure 6-9. Rhodes Contacts application

Figure 6-9. Rhodes Contacts application

Similarly, rebuild the application for the Android using

> rake run:android

Again, add some contacts to the native Contacts application in the Android emulator and then launch our Rhodes Contacts application.

The Rhodes Contacts API also allows you to create, update and delete native contacts. The controller methods for these actions can be seen back in Table 6-4. These will work with the standard views that were generated using the rhogen model command.

In this section, we will step through writing an application that will allow you to take pictures using the camera and also pick images that are already on the smartphone using Rhodes APIs on both the iPhone and Android. This example requires Rhodes 1.5.

The full source code to the completed application is available online at:

http://github.com/VGraupera/Rho-Photos-Sample

Generate a skeleton application using rhogen by typing the following into the command line:

> rhogen app Photos

Then create a model for the Photos using

Was this article helpful?

0 0
iPhone and iPad App Cash

iPhone and iPad App Cash

Discover how a single application could generate $1000's of dollars, instantly. The simple, low cost system for outsourcing app development to freelancers. How to quickly evaluate applications so you can create a high profit one of your very own. The fastest way to leverage the popularity of iPhone and iPad apps to skyrocket your income. Top methods for promoting your application for maximum exposure.

Get My Free Ebook


Post a comment