From f4df53da3fb2f5de7c382a69feda23329cf549c7 Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Sun, 13 Apr 2014 23:32:39 -0400 Subject: [PATCH] A whole lot of skeleton work for the scavenger hunt --- UNCCScavenger/res/layout/activity_main.xml | 14 ++- UNCCScavenger/res/layout/list_location.xml | 34 +++++++ .../src/edu/uncc/scavenger/MainActivity.java | 96 +++++++++++++++++++ .../database/LocationDatabaseClient.java | 33 +++++++ .../uncc/scavenger/rest/LocationClient.java | 62 ++++++------ 5 files changed, 206 insertions(+), 33 deletions(-) create mode 100644 UNCCScavenger/res/layout/list_location.xml create mode 100644 UNCCScavenger/src/edu/uncc/scavenger/database/LocationDatabaseClient.java diff --git a/UNCCScavenger/res/layout/activity_main.xml b/UNCCScavenger/res/layout/activity_main.xml index af9f093..4fca377 100644 --- a/UNCCScavenger/res/layout/activity_main.xml +++ b/UNCCScavenger/res/layout/activity_main.xml @@ -8,9 +8,21 @@ android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > + + + + android:layout_alignParentLeft="true" + android:layout_alignParentTop="true" + android:visibility="gone" + android:text="There are currently no locations available. Contacting the server to get them now..." /> diff --git a/UNCCScavenger/res/layout/list_location.xml b/UNCCScavenger/res/layout/list_location.xml new file mode 100644 index 0000000..6817015 --- /dev/null +++ b/UNCCScavenger/res/layout/list_location.xml @@ -0,0 +1,34 @@ + + + + + + + + + + diff --git a/UNCCScavenger/src/edu/uncc/scavenger/MainActivity.java b/UNCCScavenger/src/edu/uncc/scavenger/MainActivity.java index a54f674..575f2da 100644 --- a/UNCCScavenger/src/edu/uncc/scavenger/MainActivity.java +++ b/UNCCScavenger/src/edu/uncc/scavenger/MainActivity.java @@ -1,15 +1,58 @@ package edu.uncc.scavenger; +import java.util.List; + +import edu.uncc.scavenger.database.LocationDatabaseClient; +import edu.uncc.scavenger.rest.LocationClient; +import edu.uncc.scavenger.rest.RestLocation; import android.os.Bundle; import android.app.Activity; import android.view.Menu; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.BaseAdapter; +import android.widget.ImageView; +import android.widget.ListAdapter; +import android.widget.ListView; +import android.widget.TextView; public class MainActivity extends Activity { + + ListView locationList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); + + // Get our list of events loaded + List locations = new LocationDatabaseClient().getAllLocations(); + if (locations != null && locations.size() > 0) { + locationList = (ListView)findViewById(R.id.listLocations); + LocationAdapter mLocationAdapter = new LocationAdapter(locations); + locationList.setAdapter(mLocationAdapter); + } else { + // We don't yet have any locations... + ((TextView)findViewById(R.id.txtNoLocations)).setVisibility(View.VISIBLE); + ((ListView)findViewById(R.id.listLocations)).setVisibility(View.GONE); + } + + // And kick off contacting to server to see if there are any new ones + new LocationClient.LocationsDownloader(this) { + @Override + protected void onPostExecute(List result) { + super.onPostExecute(result); + // And update our adapter when done + LocationAdapter mLocationAdapter = new LocationAdapter(result); + locationList.setAdapter(mLocationAdapter); + + // Always show the ListView and hide the TextView + // We're back on the main thread at this point, so it's legal. + ((TextView)findViewById(R.id.txtNoLocations)).setVisibility(View.GONE); + ((ListView)findViewById(R.id.listLocations)).setVisibility(View.VISIBLE); + } + }; } @Override @@ -18,5 +61,58 @@ public class MainActivity extends Activity { getMenuInflater().inflate(R.menu.main, menu); return true; } + + private class LocationAdapter extends BaseAdapter { + private List locations; + + public LocationAdapter(List locations) { + this.locations = locations; + } + @Override + public int getCount() { + return locations.size(); + } + + @Override + public Object getItem(int position) { + return locations.get(position); + } + + @Override + public long getItemId(int position) { + return locations.get(position).getId(); + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + Holder holder = new Holder(); + View v = convertView; + + if (v == null) { + v = getLayoutInflater().inflate(R.layout.list_location, null); + + holder.imgFound = (ImageView)v.findViewById(R.id.imgIsFound); + holder.name = (TextView)v.findViewById(R.id.txtName); + holder.riddle = (TextView)v.findViewById(R.id.txtRiddle); + + v.setTag(holder); + } else { + holder = (Holder)v.getTag(); + } + + RestLocation location = locations.get(position); + holder.name.setText(location.getName()); + holder.riddle.setText(location.getRiddle()); + + return v; + } + + } + + static class Holder { + ImageView imgFound; + TextView name; + TextView riddle; + } } diff --git a/UNCCScavenger/src/edu/uncc/scavenger/database/LocationDatabaseClient.java b/UNCCScavenger/src/edu/uncc/scavenger/database/LocationDatabaseClient.java new file mode 100644 index 0000000..32503a5 --- /dev/null +++ b/UNCCScavenger/src/edu/uncc/scavenger/database/LocationDatabaseClient.java @@ -0,0 +1,33 @@ +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/rest/LocationClient.java b/UNCCScavenger/src/edu/uncc/scavenger/rest/LocationClient.java index 927bd7b..907bcda 100644 --- a/UNCCScavenger/src/edu/uncc/scavenger/rest/LocationClient.java +++ b/UNCCScavenger/src/edu/uncc/scavenger/rest/LocationClient.java @@ -6,51 +6,49 @@ import java.util.Map; import java.util.concurrent.ExecutionException; import edu.uncc.scavenger.R; +import edu.uncc.scavenger.database.LocationDatabaseClient; import android.content.Context; import android.os.AsyncTask; - import retrofit.RestAdapter; public class LocationClient { - - private Context ctx; - - public LocationClient(Context ctx) { - this.ctx = ctx; - } - - public LocationService getAdapter() { + + private static LocationService getAdapter(Context ctx) { String endpoint = ctx.getString(R.string.endpoint); - RestAdapter ra = new RestAdapter.Builder().setEndpoint(endpoint).build(); + RestAdapter ra = new RestAdapter.Builder().setEndpoint(endpoint) + .build(); return ra.create(LocationService.class); } - - public String validateLocation(int id, String key) { - LocationService client = getAdapter(); + + public static String validateLocation(Context ctx, int id, String key) { + LocationService client = getAdapter(ctx); Map keys = new HashMap(); keys.put("key", key); keys.put("id", String.valueOf(id)); return client.getResult(keys); } - - public List getLocations() { - try { - // Inline AsyncTask - return new AsyncTask>() { - @Override - protected List doInBackground(Void... params) { - // Work happens here - return getAdapter().listLocations(); - } - }.get(); - } catch (InterruptedException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (ExecutionException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + + public static class LocationsDownloader extends + AsyncTask> { + LocationService client; + + public LocationsDownloader(Context ctx) { + client = getAdapter(ctx); } - - return null; + + @Override + protected List doInBackground(Void... arg0) { + return client.listLocations(); + } + } + + public static AsyncTask> updateDatabase(Context ctx) { + // Start the downloader and wait until it's finished. + return new LocationsDownloader(ctx) { + @Override + protected void onPostExecute(List result) { + new LocationDatabaseClient().synchronizeLocations(result); + } + }.execute(); } }