mirror of
				https://github.com/bspeice/itcs4180
				synced 2025-11-04 02:10:32 -05:00 
			
		
		
		
	A whole lot of skeleton work for the scavenger hunt
This commit is contained in:
		@ -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;
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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.
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
}
 | 
			
		||||
@ -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();
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user