With the revised service implemented and deployed, upgrade donation-android to use this new API.
First some improvements in the way we are initializing the connection to the donation service.
In the DonationApp class, we need to keep track of the currently logged in user:
public User currentUser;
Which we maintain in the validUser method:
public boolean validUser (String email, String password)
{
for (User user : users)
{
if (user.email.equals(email) && user.password.equals(password))
{
currentUser = user;
return true;
}
}
return false;
}
In the Welcome activity - whenever we resume the activity, we can set this user to null:
@Override
public void onResume()
{
super.onResume();
app.currentUser = null;
DonationServiceAPI.getUsers(this, this, "Retrieving list of users");
}
This means the app effectively logs out if it goes into the background.
In the models package, we will must introduce new methods for retrieving and creating donations:
public static void getDonations(Context context, User user, Response<Donation> response, String dialogMesssage)
{
new GetDonations(context, user, response, dialogMesssage).execute();
}
public static void createDonation(Context context, User user, Response<Donation> response, String dialogMesssage, Donation donation)
{
new CreateDonation(context, user, response, dialogMesssage).execute(donation);
}
THese go into the DonatinsServiceAPI class.
They rely on the following classes, appended to the same java file:
class GetDonations extends Request
{
private User user;
public GetDonations(Context context, User user, Response<Donation> callback, String message)
{
super(context, callback, message);
this.user = user;
}
@Override
protected List<Donation> doRequest(Object... params) throws Exception
{
String response = Rest.get("/api/users/" + user.id + "/donations");
List<Donation> donationList = JsonParsers.json2Donations(response);
return donationList;
}
}
class CreateDonation extends Request
{
private User user;
public CreateDonation(Context context, User user, Response<Donation> callback, String message)
{
super(context, callback, message);
this.user = user;
}
@Override
protected Donation doRequest(Object... params) throws Exception
{
String response = Rest.post ("/api/users/" + user.id + "/donations", JsonParsers.donation2Json(params[0]));
return JsonParsers.json2Donation(response);
}
}
We can now rework this method on the Donate activity:
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, app.currentUser, this, "Registering new donation...", new Donation(donatedAmount, method));
}
}
There is only one small change - the call to createDonation will require the currently logged in user.
Similarly for the Report Activity:
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_report);
app = (DonationApp) getApplication();
listView = (ListView) findViewById(R.id.reportList);
adapter = new DonationAdapter (this, app.donations);
listView.setAdapter(adapter);
DonationServiceAPI.getDonations(this, app.currentUser, this, "Downloading Donations List..");
}
Getting donations will require the current user as well.
This should all compile and run now. Make sure the play service is running.
This is an archive of the two projects so far:
Deploy the play project to heroku. Verify that the android app works against the deployed version.
If you had a go at the exercises from the last, you might have developed an API for one or more of the following: