Supporting Online and Offline - Android

Now that the concrete classes have been written, it's time to provide the user with functionality to swap between being online and offline.

I've added a new static final integer in the EventManager class: public static final int EM_TOGGLE_OFFLINE = 140;

I've updated the TopViewController class to add an additional menu item:

  1. public TopViewController(Activity activity, Context context, int layoutId) {
  2. super(context, layoutId);
  3. this.activity = activity;
  4. menuEntries = new MenuEntry[]{
  5. new MenuEntry("Locations (Map)", 0,EventManager.EM_NAV_LOCAL_SPOTS_MAP),
  6. new MenuEntry("Locations (List)",0, EventManager.EM_NAV_LOCAL_SPOTS_LIST),
  7. new MenuEntry("View Catches",0, EventManager.EM_NAV_LIST_CATCHES),
  8. new MenuEntry("Record a Catch",0, EventManager.EM_NAV_CAPTURE_CATCH),
  9. new MenuEntry("Go offline",0, EventManager.EM_TOGGLE_OFFLINE),
  10. new MenuEntry("Log Out", R.drawable.com_facebook_icon, EventManager.EM_USER_LOGOUT)
  11. };
  12. inflateView();
  13. viewDidLoad();
  14. }

And I've updated the onItemClick() inner class method to toggle the menu item text based on the previous value. (This is part of the viewDidLoad() method:

  1. // ...
  2. drawerListView.setOnItemClickListener(new OnItemClickListener() {
  3.  
  4. @Override
  5. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  6. MenuEntry mEntry = menuEntries[position];
  7. if (mEntry.message == EventManager.EM_TOGGLE_OFFLINE) {
  8. TextView tv = (TextView)view;
  9. String newStat = mEntry.text.equals("Go offline") ? "Go online" : "Go offline";
  10. tv.setText(newStat);
  11. mEntry.text = (newStat);
  12. }
  13. drawerLayout.closeDrawer(drawerListView);
  14. if (eventManager != null) {
  15. eventManager.handleEvent(mEntry.message, null);
  16. }
  17. }
  18. });
  19. // ...

In Main.java I've added code to the handleEvent() method to react to the new event:

  1. /*
  2. * Implementation of the EventManager interface. The method helps navigate between the various
  3. * view controllers. This should probably be migrated to it's own class, because it's getting quite large in size.
  4. * @see com.crowleyworks.futilefishing.EventManager#handleEvent(int, java.lang.Object)
  5. */
  6. @Override
  7. public void handleEvent(int eventID, Object data) {
  8. switch(eventID) {
  9.  
  10. // ...
  11.  
  12. case EventManager.EM_TOGGLE_OFFLINE:
  13. boolean curState = ServiceFactory.isOnline();
  14. ServiceFactory.setOnline(!curState);
  15. break;
  16.  
  17. // ...
  18.  
  19. }

And lastly, I've updated the ServiceFactory class to return different classes based on the online / offline mode:

  1. public static LocationService getLocationService() {
  2. LocationService ls = (online) ? new LocationServiceWeb() : new LocationServiceDB();
  3. return ls;
  4. }

There are no additional changes to make to the application; the new persistence framework makes all of the alterations transparent to the remaining code.

When the application is launched, it's initially in online mode. Displaying locations shows the following:

To go offline, select "Go Offline" from the menu:

Now, displaying locations shows the following:

This completes the Android implementation of offline mode.