Rework the Donation-android activities to support signup and donate activities using the REST service.
First introduce a new attribute in the DonationApp class:
public boolean donationServiceAvailable = false;
Every time we connect, we will set the last know status of our connection to the service here:/
Currently, the app loads the users from the service in the Login activity. This might not bee such a good idea. Lets move it to the welcome screen, so we will have preloaded the user list before starting the login activity
Here is an alternative version of the Welcome Activity to do this:
package app.activities;
import java.util.List;
import app.donation.R;
import app.http.Response;
import app.main.DonationApp;
import app.models.DonationServiceAPI;
import app.models.User;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class Welcome extends Activity implements Response<User>
{
DonationApp app;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
app = (DonationApp) getApplication();
DonationServiceAPI.getUsers(this, this, "Retrieving list of users");
}
void serviceUnavailableMessage()
{
Toast toast = Toast.makeText(this, "Donation Service Unavailable. Try again later", Toast.LENGTH_LONG);
toast.show();
}
public void loginPressed (View view)
{
if (app.donationServiceAvailable)
{
startActivity (new Intent(this, Login.class));
}
else
{
serviceUnavailableMessage();
}
}
public void signupPressed (View view)
{
if (app.donationServiceAvailable)
{
startActivity (new Intent(this, Signup.class));
}
else
{
serviceUnavailableMessage();
}
}
@Override
public void setResponse(List<User> aList)
{
app.users = aList;
app.donationServiceAvailable = true;
}
@Override
public void errorOccurred(Exception e)
{
app.donationServiceAvailable = false;
serviceUnavailableMessage();
}
@Override
public void setResponse(User anObject)
{}
}
Read the above class carefully - and note how we make use of the `donationServiceAvailable' flag in DonationApp.
Login can now be simplified to its original form:
package app.activities;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import app.donation.R;
import app.main.DonationApp;
public class Login extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
public void signinPressed (View view)
{
DonationApp app = (DonationApp) getApplication();
TextView email = (TextView) findViewById(R.id.loginEmail);
TextView password = (TextView) findViewById(R.id.loginPassword);
if (app.validUser(email.getText().toString(), password.getText().toString()))
{
startActivity (new Intent(this, Donate.class));
}
else
{
Toast toast = Toast.makeText(this, "Invalid Credentials", Toast.LENGTH_SHORT);
toast.show();
}
}
}
We will only be reaching this activity if we have successfully connected to the service.
This is a revised version of Signup, which makes use of the service:
package app.activities;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import app.donation.R;
import app.http.Response;
import app.main.DonationApp;
import app.models.DonationServiceAPI;
import app.models.User;
public class Signup extends Activity implements Response<User>
{
private DonationApp app;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
app = (DonationApp) getApplication();
}
public void registerPressed (View view)
{
TextView firstName = (TextView) findViewById(R.id.firstName);
TextView lastName = (TextView) findViewById(R.id.lastName);
TextView email = (TextView) findViewById(R.id.Email);
TextView password = (TextView) findViewById(R.id.Password);
User user = new User (firstName.getText().toString(), lastName.getText().toString(), email.getText().toString(), password.getText().toString());
DonationServiceAPI.createUser(this, this, "Registering new user", user);
}
@Override
public void setResponse(List<User> aList)
{
}
@Override
public void setResponse(User user)
{
app.users.add(user);
startActivity (new Intent(this, Welcome.class));
}
@Override
public void errorOccurred(Exception e)
{
app.donationServiceAvailable = false;
Toast toast = Toast.makeText(this, "Donation Service Unavailable. Try again later", Toast.LENGTH_LONG);
toast.show();
startActivity (new Intent(this, Welcome.class));
}
}
Check now that you can sign up for the donation service - and that a new user is in fact created. This is most easily checked using postman, or just browse to:
http://localhost:9000/api/users
If you are running the app locally.
This takes a bit more work, and involved refactoring the donate button handler to only make the donation in the callback method.
Look carefully at the following - and compare it with the current version. In particular, examine the role of the setResponse method.
package app.activities;
import java.util.List;
import app.donation.R;
import app.http.Response;
import app.main.DonationApp;
import app.models.Donation;
import app.models.DonationServiceAPI;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.NumberPicker;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class Donate extends Activity implements Response<Donation>
{
private RadioGroup paymentMethod;
private ProgressBar progressBar;
private NumberPicker amountPicker;
private TextView amountText;
private TextView amountTotal;
private DonationApp app;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_donate);
app = (DonationApp) getApplication();
paymentMethod = (RadioGroup) findViewById(R.id.paymentMethod);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
amountPicker = (NumberPicker) findViewById(R.id.amountPicker);
amountText = (TextView) findViewById(R.id.amountText);
amountTotal = (TextView) findViewById(R.id.amountTotal);
amountPicker.setMinValue(0);
amountPicker.setMaxValue(1000);
progressBar.setMax(app.target);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.donate, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menuReport : startActivity (new Intent(this, Report.class));
break;
case R.id.menuLogout : startActivity (new Intent(this, Welcome.class));
break;
}
return true;
}
public void donateButtonPressed (View view)
{
String method = paymentMethod.getCheckedRadioButtonId() == R.id.PayPal ? "PayPal" : "Direct";
int donatedAmount = amountPicker.getValue();
if (donatedAmount == 0)
{
String text = amountText.getText().toString();
if (!text.equals(""))
donatedAmount = Integer.parseInt(text);
}
if (donatedAmount > 0)
{
DonationServiceAPI.createDonation(this, this, "Registering new donation...", new Donation(donatedAmount, method));
}
}
@Override
public void setResponse(Donation acceptedDonation)
{
Toast toast = Toast.makeText(this, "Donation Accepteed", Toast.LENGTH_SHORT);
toast.show();
app.newDonation(acceptedDonation);
progressBar.setProgress(app.totalDonated);
String totalDonatedStr = "$" + app.totalDonated;
amountTotal.setText(totalDonatedStr);
amountText.setText("");
amountPicker.setValue(0);
}
@Override
public void errorOccurred(Exception e)
{
Toast toast = Toast.makeText(this, "Donation Service Unavailable. Try again later", Toast.LENGTH_LONG);
toast.show();
}
@Override
public void setResponse(List<Donation> aList)
{}
}
Archive of the lab so far:
Does it make sense to download the full list of users when we load the Welcome activity? Perhaps it might be better if we just attempted to reach donation-service, and indicate to the user if it is available or not.
Consider how you might do this.
When Login is launched, it might be better to just check that the user supplied credentials are valid - with the donation-service app, and not by inspecting our local list of users.
How would this be done?
In the Singup activity - we should really check to see if the new user email is not already taken.
How would we do this?