Use the Persistence Framework - Android

The persistence framework is made available to the mobile app via the ServiceFactory class. The implementation is as follows:

ServiceFactory.java

  1. package com.crowleyworks.futilefishing.service;
  2.  
  3. public class ServiceFactory {
  4.  
  5. private static boolean online = true;
  6. public static boolean isOnline() { return online; }
  7. public static void setOnline(boolean online) { ServiceFactory.online = online; }
  8.  
  9. public static LocationService getLocationService() {
  10. LocationService ls = (online) ? new LocationServiceWeb() : new LocationServiceDB();
  11. return ls;
  12. }
  13.  
  14. }

Comments:

  • Lines 5 - 7: The online / offline status is managed by a static variable named online and two helper methods.
  • Lines 9 - 12: Based on the online / offline status, return either a web-based location service, or a db-based location service. (Note: The implementation of the class LocationServiceDB will be described in the next article.)

The only remaining task is to update the mobile app code to leverage the new service. For purposes of simplicity, I'll describe the replacement code for the LocationListViewController class:

  1. // ...
  2.  
  3. /*
  4. * When the view is about to appear, request fresh data from the server
  5. * via the LocationService
  6. */
  7. @Override
  8. public void viewWillAppear() {
  9. super.viewWillAppear();
  10. // Load the content that will be displayed
  11. LocationService ls = ServiceFactory.getLocationService();
  12. ls.setDelegate(this);
  13. try {
  14. ls.getMany("");
  15. } catch (ServiceException e) {
  16. e.printStackTrace();
  17. }
  18. }
  19.  
  20. /*
  21. * This class manages the CRUD operations for locations, and this method is called when
  22. * a response is received from the server.
  23. */
  24. @Override
  25. public void contentLoaded(ContentResponse<FishingSpot> response) {
  26. if (response.getRc() < 0) {
  27. MiscUtils.showAlert(context, "Error loading data", "Error "
  28. + response.getRc() + ": " + response.getDescr());
  29. return;
  30. }
  31.  
  32. // At this point, we should have content returned
  33. if (response.getAction() == ContentBroker.LIST) {
  34. adapter.clear();
  35. adapter.addAll(response.getItems());
  36. } else if (response.getAction() == ContentBroker.CREATE_JSON) {
  37. adapter.addAll(response.getItems());
  38. } else if (response.getAction() == ContentBroker.UPDATE) {
  39. // Remove the old version, and replace it with the new one
  40. FishingSpot fs = response.getItems().get(0);
  41. for(int i = 0; i < adapter.getCount(); i++) {
  42. FishingSpot orig = adapter.getItem(i);
  43. if (orig.getId() == fs.getId()) {
  44. adapter.remove(orig);
  45. adapter.addAll(fs);
  46. adapter.notifyDataSetChanged();
  47. return;
  48. }
  49. }
  50. } else if (response.getAction() == ContentBroker.DELETE) {
  51. // If successful, the return code will be the id of the object deleted
  52. for(int i = 0; i < adapter.getCount(); i++) {
  53. FishingSpot orig = adapter.getItem(i);
  54. if (orig.getId() == response.getRc()) {
  55. adapter.remove(orig);
  56. adapter.notifyDataSetChanged();
  57. return;
  58. }
  59. }
  60.  
  61. }
  62. }
  63.  
  64. // ...

Comments:

  • Line 11: The ServiceFactory is used to retrieve a LocationService.
  • Line 12: The current class is specified as the delegate for the service.
  • Lines 13 - 17: Attempt to retrieve a list of all FishingSpot objects. If successful, contentLoaded() will be invoked. If not successful, print an error.
  • Lines 20 - 62: This method is invoked when data has been retrieved, and the contents have been parsed. Based on the operation completed, update the list view appropriately.

This completes the Android rewrite of the web-based persistence framework.