1
0
mirror of https://github.com/bspeice/itcs4180 synced 2024-11-13 19:08:20 -05:00

A whole lot of skeleton work for the scavenger hunt

This commit is contained in:
Bradlee Speice 2014-04-13 23:32:39 -04:00
parent dddab46731
commit f4df53da3f
5 changed files with 206 additions and 33 deletions

View File

@ -8,9 +8,21 @@
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="@+id/listLocations"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
<TextView
android:id="@+id/txtNoLocations"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
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..." />
</RelativeLayout>

View File

@ -0,0 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imgIsFound"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/imgIsFound"
android:text="TextView"
android:textSize="20sp" />
<TextView
android:id="@+id/txtRiddle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/txtName"
android:layout_below="@+id/txtName"
android:text="TextView"
android:textColor="@android:color/secondary_text_light"
android:textSize="15sp" />
</RelativeLayout>

View File

@ -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<RestLocation> 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<RestLocation> 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<RestLocation> locations;
public LocationAdapter(List<RestLocation> 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;
}
}

View File

@ -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<RestLocation> 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<RestLocation> locations) {
for (RestLocation l: locations) {
persistLocation(l);
}
}
public void synchronizeLocations(List<RestLocation> locations) {
// TODO: Implement method to save all values in locations, and remove values that aren't there.
}
}

View File

@ -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<String, String> keys = new HashMap<String, String>();
keys.put("key", key);
keys.put("id", String.valueOf(id));
return client.getResult(keys);
}
public List<RestLocation> getLocations() {
try {
// Inline AsyncTask
return new AsyncTask<Void, Void, List<RestLocation>>() {
@Override
protected List<RestLocation> 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<Void, Void, List<RestLocation>> {
LocationService client;
public LocationsDownloader(Context ctx) {
client = getAdapter(ctx);
}
return null;
@Override
protected List<RestLocation> doInBackground(Void... arg0) {
return client.listLocations();
}
}
public static AsyncTask<Void, Void, List<RestLocation>> updateDatabase(Context ctx) {
// Start the downloader and wait until it's finished.
return new LocationsDownloader(ctx) {
@Override
protected void onPostExecute(List<RestLocation> result) {
new LocationDatabaseClient().synchronizeLocations(result);
}
}.execute();
}
}