Android Tutorial Android Tutorial Part II

View the Project on GitHub FatFractal/hoodyoodoo

Modify the WouldYaActivity

In this section, we will modify the WouldYaActivity class to include a second celebrity and to use queries to retrieve the Celebrity objects from the API.

Add instance variables

First, add the following instance variables to the WouldYaActivity class:

private Celebrity   m_rightCeleb;
private WouldYa     m_wouldYa;
private ImageView   m_rightCelebImageView;
private TextView    m_rightCelebNameTextView;
private String      m_currentGender = "female";
private ImageButton m_playAgainImageButton;
private ImageButton m_toggleGenderImageButton;

These variables hold information for a second celebrity, a WouldYa object, and button objects to support the UI, which we add in the next section.

Modify loadCelebrities method
loadCelebrities method to use queries instead of filtering in the client. We are also adding m_rightCeleb. You will see that m_leftCeleb is retrieved with the following query:

"(gender eq '" + m_currentGender + "' and guid eq random(guid))"

For m_rightCeleb, we need to also ensure that we do not retrieve the same celebrity as m_leftCeleb, and so we have to get the guid for m_leftCeleb, create a query that will make sure that m_rightCeleb is different. To do this, we create the query string and getting the guid for m_leftCeleb using the getMetaDataForObj method.

String guid = ff.getMetaDataForObj(m_leftCeleb).getGuid();
String q2 = "/Celebrity/(gender eq '" + m_currentGender + "' and guid ne '" + guid + "' and guid eq random(guid))";
celeb = ff.getObjFromUri(q2);

Here is the modified loadCelebrities method:

private void loadCelebrities() {
    // Get a Celebrity Object called leftCeleb synchronously using getObjFrom(String url).
    // Note: this is not generally recommended, you should usually use wrap with AsyncTask as below.
    try {
        String q0 = "/Celebrity/(gender eq '" + m_currentGender + "' and guid eq random(guid))";
        m_leftCeleb = ff.getObjFromUri(q0);
        m_leftCelebNameTextView.setText(m_leftCeleb.getFirstName() + " " + m_leftCeleb.getLastName());
        if (m_leftCeleb.getImageData() != null) {
            Bitmap bm = BitmapFactory.decodeByteArray(m_leftCeleb.getImageData(), 0, m_leftCeleb.getImageData().length);
            m_leftCelebImageView.setImageBitmap(bm);
        }
    } catch (FFException e) {
        e.printStackTrace();
    }
    // Get a Celebrity Object called rightCeleb asynchronously using getObjFrom(String url) wrapped with an AsyncTask.
    new AsyncTask() {
        @Override
        protected Celebrity doInBackground(String... params) {
            try {
                Celebrity celeb = new Celebrity();
                if (m_leftCeleb != null) {
                    String guid = ff.getMetaDataForObj(m_leftCeleb).getGuid();
                    String q2 = "/Celebrity/(gender eq '" + m_currentGender + "' and guid ne '" + guid + "' and guid eq random(guid))";
                    celeb = ff.getObjFromUri(q2);
                }
                return celeb;
            } catch (FFException e) {
                e.printStackTrace();
                return null;
            }
        }
        @Override
        protected void onPostExecute(Celebrity rightCeleb) {
            if (rightCeleb != null) {
                m_rightCeleb = rightCeleb;
                m_rightCelebNameTextView.setText(m_rightCeleb.getFirstName() + " " + m_rightCeleb.getLastName());
                if (m_rightCeleb.getImageData() != null) {
                    Bitmap bm = BitmapFactory.decodeByteArray(m_rightCeleb.getImageData(), 0, m_rightCeleb.getImageData().length);
                    m_rightCelebImageView.setImageBitmap(bm);
                }
            }
        }
    }.execute();
    m_playAgainImageButton.setVisibility(View.INVISIBLE);
}
Add methods

Next, we add the toggleGender method implementation to handle the m_toggleGenderImageButton click:

public void toggleGender(View view) {
    if (m_currentGender.equals("male")) {
        m_currentGender = "female";
        m_toggleGenderImageButton.setImageResource(R.drawable.button_gender_female);
    } else {
        m_currentGender = "male";
        m_toggleGenderImageButton.setImageResource(R.drawable.button_gender_male);
    }
    loadCelebrities();
}

Add the celebrityWasPicked method implementation to handle m_leftCelebImageView or m_rightCelebImageView clicks:

public void celebrityWasPicked(View view) {
    m_wouldYa = new WouldYa();
    if (view == m_leftCelebImageView) {
        m_wouldYa.setSelectedGuid(ff.getMetaDataForObj(m_leftCeleb).getGuid());
        m_wouldYa.setRejectedGuid(ff.getMetaDataForObj(m_rightCeleb).getGuid());
    } else if (view == m_rightCelebImageView) {
        m_wouldYa.setSelectedGuid(ff.getMetaDataForObj(m_rightCeleb).getGuid());
        m_wouldYa.setRejectedGuid(ff.getMetaDataForObj(m_leftCeleb).getGuid());
    }
    if (m_wouldYa != null) {
        persistWouldYa();
    } else System.out.println("WouldYaViewController celebrityWasPicked failed: m_wouldYa is null");
}

Now, add the persistWouldYa method implementation that will persist your WouldYa instance to the API after ensuring that the user is logged in. This is implemented as a separate method to make it easier to use a callback from a login dialog.

public void persistWouldYa() {
    if (!ff.isLoggedIn()) {
        System.out.println("persistWouldYa found ff.isLoggedIn() false");
        Hoodyoodoo hoodyoodoo = new Hoodyoodoo();
        AlertDialog alert = hoodyoodoo.loginDialog(this, "You must be logged in to record a WouldYa selection.");
        alert.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                if (ff.isLoggedIn()) persistWouldYa();
            }
        });
        alert.show();
    } else {
        if (m_wouldYa != null) {
            try {
                m_playAgainImageButton.setVisibility(View.VISIBLE);
                ff.createObjAtUri(m_wouldYa, "/WouldYa");
            } catch (FFException e) {
                e.printStackTrace();
            }
        } else System.out.println("WouldYaActivity persistWouldYa failed: m_wouldYa is null");
    }
}

Finally, add the playAgain method to handle clicks on the m_playAgainImageButton:

public void playAgain(View view) {
    loadCelebrities();
}

NEXT: Adding the UI