Permalink
Browse files

Changes to how we do CRUD on locations

  • Loading branch information...
1 parent 8644b3d commit adca581a33edd6837fa58a6a8e187314e6b77c04 @avinash-chugh avinash-chugh committed Jan 16, 2011
View
@@ -3,7 +3,7 @@
package="com.proxime" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:targetSdkVersion="8" />
<supports-screens android:anyDensity="true"/>
- <application android:label="Proxime" android:icon="@drawable/icon" android:debuggable="true">
+ <application android:label="Proxime" android:icon="@drawable/proxime" android:debuggable="true">
<activity android:name=".activities.Events"
android:label="@string/app_name">
<intent-filter>
@@ -18,8 +18,8 @@
<!--</intent-filter>-->
</activity>
<activity android:name=".activities.ViewEvent"/>
- <activity android:name=".activities.PickLocation" android:label="Choose Location" android:theme="@android:style/Theme.Light.NoTitleBar"/>
- <activity android:name=".activities.Locations" android:label="Choose Location"/>
+ <activity android:name=".activities.PickLocation" android:label="Locations" android:theme="@android:style/Theme.Light.NoTitleBar"/>
+ <activity android:name=".activities.Locations" android:label="Locations"/>
<activity android:name=".activities.About" android:label="About Proxime"/>
<activity android:name=".activities.Addresses" android:label="Addresses"/>
<activity android:name=".activities.EditLocation" android:label="Add Location"/>
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:minWidth="200dp"
+ android:padding="10dp">
+
+ <EditText android:id="@+id/location_name"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:maxLines="1"/>
+
+</LinearLayout>
@@ -43,7 +43,7 @@
<Button android:id="@+id/searchLocationButton"
android:layout_width="50dip"
android:layout_height="wrap_content"
- android:text="@string/find_location"
+ android:drawableRight="@drawable/ic_btn_search"
/>
</LinearLayout>
@@ -4,6 +4,10 @@
android:title="@string/menu_edit_location"
android:icon="@android:drawable/ic_menu_edit"/>
+ <item android:id="@+id/rename_location"
+ android:title="@string/menu_rename_location"
+ android:icon="@android:drawable/ic_menu_directions"/>
+
<item android:id="@+id/delete_location"
android:title="@string/menu_delete_location"
android:icon="@android:drawable/ic_menu_delete"/>
View
@@ -7,6 +7,7 @@
<string name="save">Save</string>
<string name="cancel">Cancel</string>
<string name="locations_title">Location</string>
+ <string name="new_location">New Location</string>
<string name="edit_event_add_location">Add</string>
<string name="message_title">Message</string>
<string name="location_name">Name</string>
@@ -21,6 +22,7 @@
<string name="menu_events">Events</string>
<string name="menu_add_location">Add Location</string>
<string name="menu_edit_location">Edit Location</string>
+ <string name="menu_rename_location">Rename Location</string>
<string name="menu_delete_location">Delete Location</string>
<!-- about dialog -->
@@ -16,11 +16,9 @@
import com.proxime.repositories.LocationRepository;
import org.json.JSONException;
-import java.util.Date;
import java.util.List;
-public class EditLocation extends Activity
-{
+public class EditLocation extends Activity {
private boolean useMap;
private Location location;
private LocationRepository locationRepository;
@@ -42,21 +40,15 @@ public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.location_edit);
hookUpListeners();
setDependencies();
- setDefaultLocation();
loadLocation();
}
- private void setDefaultLocation() {
- location = new Location("",1.0,1.0,0);
- }
-
private void loadLocation() {
long id = getIntent().getLongExtra("location_id", -1);
- if (id < 0) return;
-
location = locationRepository.load(id);
-
+
setTitle(location.getName());
+ setText(R.id.userSelectedLocation, location.getAddress());
}
private void setDependencies() {
@@ -71,9 +63,10 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
double longitude = data.getDoubleExtra("longitude", 0);
location.setLatitude(latitude);
location.setLongitude(longitude);
- ((TextView)(findViewById(R.id.userSelectedLocation))).setText((data.getStringExtra("formattedAddress")));
+
+ setText(R.id.userSelectedLocation, data.getStringExtra("formattedAddress"));
((ArrayAdapter<GeoCoderResponse>)((ListView)(findViewById(R.id.searchResultsList))).getAdapter()).clear();
- ((EditText)(findViewById(R.id.searchTextBoxForLocation))).setText("");
+ setText(R.id.searchTextBoxForLocation, "");
}
}
@@ -88,35 +81,30 @@ public void onClick(View view) {
finish();
}
});
- findViewById(R.id.searchLocationButton).setOnClickListener(new View.OnClickListener() {
- public void onClick(View view)
- {
- try
- {
- List<GeoCoderResponse> possibleAddresses = new GeoCoderServiceUtility().getFormattedAddresses(getSearchString());
- ListView eventsView = (ListView) findViewById(R.id.searchResultsList);
- eventsView.setTextFilterEnabled(true);
- ArrayAdapter<GeoCoderResponse> adapter = new ArrayAdapter<GeoCoderResponse>(getBaseContext(),android.R.layout.simple_list_item_1, possibleAddresses);
- eventsView.setAdapter(adapter);
- } catch (JSONException e)
- {
+ findViewById(R.id.searchLocationButton).setOnClickListener(new View.OnClickListener() {
+ public void onClick(View view) {
+ try {
+ List<GeoCoderResponse> possibleAddresses = new GeoCoderServiceUtility().getFormattedAddresses(getSearchString());
+ ListView eventsView = (ListView) findViewById(R.id.searchResultsList);
+ eventsView.setTextFilterEnabled(true);
+ ArrayAdapter<GeoCoderResponse> adapter = new ArrayAdapter<GeoCoderResponse>(getBaseContext(), android.R.layout.simple_list_item_1, possibleAddresses);
+ eventsView.setAdapter(adapter);
+ } catch (JSONException e) {
e.printStackTrace();
- }
+ }
}
});
- ((ListView) findViewById(R.id.searchResultsList)).setOnItemClickListener(new AdapterView.OnItemClickListener()
- {
- public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
- {
+ ((ListView) findViewById(R.id.searchResultsList)).setOnItemClickListener(new AdapterView.OnItemClickListener() {
+ public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
GeoCoderResponse item = (GeoCoderResponse) adapterView.getAdapter().getItem(i);
- try
- {
- ((TextView)(findViewById(R.id.userSelectedLocation))).setText(item.getFormattedAddress());
+ try {
+ String formattedAddress = item.getFormattedAddress();
+ setText(R.id.userSelectedLocation, formattedAddress);
+ location.setAddress(formattedAddress);
location.setLatitude(new Double(item.getLatitude()));
location.setLongitude(new Double(item.getLongitude()));
- } catch (JSONException e)
- {
+ } catch (JSONException e) {
e.printStackTrace();
}
}
@@ -132,24 +120,21 @@ public void onClick(View view)
newIntent.putExtra("longitude", location.getLongitude());
newIntent.putExtra("formattedAddress",((TextView)findViewById(R.id.userSelectedLocation)).getText());
startActivityForResult(newIntent, MAP_REQUEST_CODE);
- }
+ }
});
}
-
-
private void saveLocation() {
- location.setName(new Date().toString());
- location.setSpan(25);
-
locationRepository.save(location);
setResult(RESULT_OK, new Intent().putExtra("location", location));
finish();
}
+ private void setText(int id, String text) {
+ ((TextView) findViewById(id)).setText(text);
+ }
- public String getSearchString()
- {
- return ((TextView)findViewById(R.id.searchTextBoxForLocation)).getText().toString();
+ public String getSearchString() {
+ return ((TextView) findViewById(R.id.searchTextBoxForLocation)).getText().toString();
}
}
@@ -0,0 +1,7 @@
+package com.proxime.activities;
+
+import com.proxime.entities.Location;
+
+public interface LocationChangeListener {
+ void change(Location location, boolean isNew);
+}
@@ -0,0 +1,79 @@
+package com.proxime.activities;
+
+import android.app.Activity;
+import android.app.AlertDialog;
+import android.app.Dialog;
+import android.content.Context;
+import android.content.DialogInterface;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.widget.EditText;
+import com.proxime.R;
+import com.proxime.entities.Location;
+import com.proxime.repositories.LocationRepository;
+
+public class LocationDialog {
+ private Activity activity;
+ private LayoutInflater inflater;
+ private LocationRepository repository;
+ private LocationChangeListener listener;
+
+ public LocationDialog(Activity activity) {
+ this.activity = activity;
+ inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
+ repository = new LocationRepository(activity);
+ }
+
+ public Dialog show(final Location existing) {
+ AlertDialog.Builder builder = new AlertDialog.Builder(activity);
+ builder.setTitle(R.string.new_location);
+
+ final View view = inflater.inflate(R.layout.location_dialog, null);
+ builder.setView(view);
+
+ if (existing != null)
+ {
+ EditText locationText = getEditText(view);
+ locationText.setText(existing.getName());
+ locationText.setSelection(0, existing.getName().length());
+ }
+
+ builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
+ public void onClick(DialogInterface dialog, int which) {
+ activity.removeDialog(Locations.SET_LOCATION_DIALOG);
+ }
+ });
+
+ builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
+ public void onClick(DialogInterface dialog, int which) {
+ String locationName = getEditText(view).getText().toString();
+ if (locationName == null || locationName.length() == 0) return;
+
+ Location location;
+ if (existing == null)
+ {
+ location = new Location(0, locationName);
+ repository.save(location);
+ }
+ else {
+ repository.rename(existing.getId(), locationName);
+ location = repository.load(existing.getId());
+ }
+
+ activity.removeDialog(Locations.SET_LOCATION_DIALOG);
+ if (listener != null) listener.change(location, (existing == null));
+ }
+ });
+
+ return builder.create();
+ }
+
+ public void setLocationChangeListener(LocationChangeListener listener)
+ {
+ this.listener = listener;
+ }
+
+ private EditText getEditText(View view) {
+ return (EditText) view.findViewById(R.id.location_name);
+ }
+}
@@ -20,13 +20,14 @@
import java.util.List;
public class Locations extends Activity {
- private static final int NEW_LOCATION = R.id.add_location;
private static final int ABOUT_DIALOG = R.id.about_proxime;
private static final int EDIT_LOCATION = R.id.edit_location;
+ public static final int SET_LOCATION_DIALOG = R.id.add_location;
private ListView locationsView;
private LocationRepository locationRepository;
private BaseListAdapter<Location> adapter;
+ private Location location;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -52,7 +53,7 @@ private void hookUpEvents() {
Button createButton = (Button) findViewById(R.id.createLocationButton);
createButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
- newLocation();
+ showLocationDialog();
}
});
@@ -73,12 +74,6 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK) return;
switch (requestCode) {
- case NEW_LOCATION: {
- Location location = getResult(data);
- showStatus(R.string.toast_location_created, location);
- adapter.add(location);
- break;
- }
case EDIT_LOCATION: {
Location location = getResult(data);
showStatus(R.string.toast_location_modified, location);
@@ -106,7 +101,7 @@ public boolean onCreateOptionsMenu(Menu menu) {
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.add_location: {
- newLocation();
+ showLocationDialog();
break;
}
case R.id.view_events: {
@@ -131,13 +126,17 @@ public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMen
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
- Location location = adapter.getItem(info.position);
+ location = adapter.getItem(info.position);
switch (item.getItemId()) {
case R.id.edit_location: {
editLocation(location.getId());
break;
}
+ case R.id.rename_location: {
+ showDialog(SET_LOCATION_DIALOG);
+ break;
+ }
case R.id.delete_location: {
deleteLocation(location);
break;
@@ -157,17 +156,34 @@ private void editLocation(long id) {
startActivityForResult(intent, EDIT_LOCATION);
}
+
+ private void showLocationDialog() {
+ location = null;
+ showDialog(SET_LOCATION_DIALOG);
+ }
+
@Override
protected Dialog onCreateDialog(int id, Bundle args) {
switch (id) {
+ case SET_LOCATION_DIALOG: {
+ LocationDialog locationDialog = new LocationDialog(this);
+ locationDialog.setLocationChangeListener(new LocationChangeListener() {
+ public void change(Location location, boolean isNew) {
+ if (isNew) {
+ showStatus(R.string.toast_location_created, location);
+ adapter.add(location);
+ }
+ else {
+ showStatus(R.string.toast_location_modified, location);
+ loadLocations();
+ }
+ }
+ });
+ return locationDialog.show(location);
+ }
case ABOUT_DIALOG:
return new AboutDialog(this).show();
}
return super.onCreateDialog(id, args);
}
-
- private void newLocation() {
- Intent intent = new Intent(this, EditLocation.class);
- startActivityForResult(intent, NEW_LOCATION);
- }
}
Oops, something went wrong.

0 comments on commit adca581

Please sign in to comment.