From 385d53a23a6782c3447e37216a659495b237e934 Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Tue, 15 Apr 2014 21:42:40 -0400 Subject: [PATCH] Refactor existing API to use the new DatabaseHelper --- .../src/edu/uncc/scavenger/MainActivity.java | 4 +-- .../database/LocationDatabaseClient.java | 33 ------------------- .../database/LocationDatabaseHelper.java | 9 +++-- .../uncc/scavenger/rest/LocationClient.java | 10 +++--- 4 files changed, 14 insertions(+), 42 deletions(-) delete mode 100644 UNCCScavenger/src/edu/uncc/scavenger/database/LocationDatabaseClient.java diff --git a/UNCCScavenger/src/edu/uncc/scavenger/MainActivity.java b/UNCCScavenger/src/edu/uncc/scavenger/MainActivity.java index 4798fb7..387b2b4 100644 --- a/UNCCScavenger/src/edu/uncc/scavenger/MainActivity.java +++ b/UNCCScavenger/src/edu/uncc/scavenger/MainActivity.java @@ -11,7 +11,7 @@ import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; -import edu.uncc.scavenger.database.LocationDatabaseClient; +import edu.uncc.scavenger.database.LocationDatabaseHelper; import edu.uncc.scavenger.rest.LocationClient; import edu.uncc.scavenger.rest.RestLocation; @@ -26,7 +26,7 @@ public class MainActivity extends Activity { // Get our list of events loaded locationList = (ListView)findViewById(R.id.listLocations); - List locations = new LocationDatabaseClient().getAllLocations(); + List locations = LocationDatabaseHelper.getInstance(this).fetchAll(); if (locations != null && locations.size() > 0) { LocationAdapter mLocationAdapter = new LocationAdapter(locations); locationList.setAdapter(mLocationAdapter); diff --git a/UNCCScavenger/src/edu/uncc/scavenger/database/LocationDatabaseClient.java b/UNCCScavenger/src/edu/uncc/scavenger/database/LocationDatabaseClient.java deleted file mode 100644 index 32503a5..0000000 --- a/UNCCScavenger/src/edu/uncc/scavenger/database/LocationDatabaseClient.java +++ /dev/null @@ -1,33 +0,0 @@ -package edu.uncc.scavenger.database; - -import java.util.List; - -import edu.uncc.scavenger.rest.RestLocation; - -public class LocationDatabaseClient { - - /* - * Super high-level API for working with locations. - * Also helpful for just drawing out skeletons of code. - */ - - public List getAllLocations() { - // TODO: Implement method to get all locations that exist in the database - return null; - } - - public void persistLocation(RestLocation location) { - // TODO: Save a location to the database - } - - public void persistLocation(List locations) { - for (RestLocation l: locations) { - persistLocation(l); - } - } - - public void synchronizeLocations(List locations) { - // TODO: Implement method to save all values in locations, and remove values that aren't there. - } - -} diff --git a/UNCCScavenger/src/edu/uncc/scavenger/database/LocationDatabaseHelper.java b/UNCCScavenger/src/edu/uncc/scavenger/database/LocationDatabaseHelper.java index 75f5a8e..7db6bda 100644 --- a/UNCCScavenger/src/edu/uncc/scavenger/database/LocationDatabaseHelper.java +++ b/UNCCScavenger/src/edu/uncc/scavenger/database/LocationDatabaseHelper.java @@ -3,6 +3,7 @@ package edu.uncc.scavenger.database; // Design pattern from: http://www.androiddesignpatterns.com/2012/05/correctly-managing-your-sqlite-database.html import java.util.ArrayList; +import java.util.List; import edu.uncc.scavenger.rest.RestLocation; import android.content.ContentValues; @@ -32,7 +33,7 @@ public class LocationDatabaseHelper extends SQLiteOpenHelper { super(ctx, DATABASE_NAME, null, DATABASE_VERSION); } - public LocationDatabaseHelper getInstance(Context ctx) { + public static LocationDatabaseHelper getInstance(Context ctx) { if (helper == null) { helper = new LocationDatabaseHelper(ctx.getApplicationContext()); } @@ -104,5 +105,9 @@ public class LocationDatabaseHelper extends SQLiteOpenHelper { helper.getWritableDatabase().insert(TABLE_NAME, null, values); } - + public void persistAll(List locs) { + for (RestLocation l : locs) { + persist(l); + } + } } diff --git a/UNCCScavenger/src/edu/uncc/scavenger/rest/LocationClient.java b/UNCCScavenger/src/edu/uncc/scavenger/rest/LocationClient.java index 907bcda..2c8883d 100644 --- a/UNCCScavenger/src/edu/uncc/scavenger/rest/LocationClient.java +++ b/UNCCScavenger/src/edu/uncc/scavenger/rest/LocationClient.java @@ -6,7 +6,7 @@ import java.util.Map; import java.util.concurrent.ExecutionException; import edu.uncc.scavenger.R; -import edu.uncc.scavenger.database.LocationDatabaseClient; +import edu.uncc.scavenger.database.LocationDatabaseHelper; import android.content.Context; import android.os.AsyncTask; import retrofit.RestAdapter; @@ -30,15 +30,15 @@ public class LocationClient { public static class LocationsDownloader extends AsyncTask> { - LocationService client; + Context ctx; public LocationsDownloader(Context ctx) { - client = getAdapter(ctx); + this.ctx = ctx; } @Override protected List doInBackground(Void... arg0) { - return client.listLocations(); + return getAdapter(ctx).listLocations(); } } @@ -47,7 +47,7 @@ public class LocationClient { return new LocationsDownloader(ctx) { @Override protected void onPostExecute(List result) { - new LocationDatabaseClient().synchronizeLocations(result); + LocationDatabaseHelper.getInstance(ctx).persistAll(result); } }.execute(); }