Skip to content

Commit

Permalink
- refactor an interface
Browse files Browse the repository at this point in the history
- fix option menu for waypointlistfragment
- nearly fix waypoint handling (#5). Still a refresh problem when deleting first line.
  • Loading branch information
dkm committed Nov 6, 2012
1 parent 5ac64c8 commit 8595a47
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/com/geeksville/gaggle/TopActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
public class TopActivity extends FragmentActivity implements
TabHost.OnTabChangeListener,
ListFlightsFragment.OnFlightSelectedListener,
ListWaypointsFragment.OnWaypointSelectedListener {
ListWaypointsFragment.OnWaypointActionListener {

/**
* Debugging tag
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ public abstract class AbstractDBListFragment
// / Should the user be shown a confirming dialog
protected Boolean isConfirmDeletes = true;

BaseAdapter adapter;
protected BaseAdapter adapter;

@Override
public void onActivityCreated(final Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
registerForContextMenu(getListView());
this.setHasOptionsMenu(true);

myCursor = createCursor();
getActivity().startManagingCursor(myCursor);

Expand Down Expand Up @@ -96,9 +98,9 @@ private void doDelete(MenuItem item) {
myCursor.requery(); // We just deleted a
// row, it seems we need
// to manually refetch the cursor

Toast.makeText(getActivity(), R.string.deleted, Toast.LENGTH_SHORT).show();
}
adapter.notifyDataSetChanged();

// adapter.notifyDataSetChanged(); // this
// doesn't seem to do
Expand Down
14 changes: 11 additions & 3 deletions src/com/geeksville/gaggle/fragments/FlyMapFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
altitudeView = (AltitudeView) v.findViewById(R.id.altitude_view);

addWaypoints();
wptOver.onParentFragmentCreateView();

addPolyoverlay();
perhapsAddFromUri();
perhapsAddExtraTracklog();
Expand All @@ -155,7 +157,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
final int lonE6 = prefs.getMapCenterZoom_Lon();
final int zoom = prefs.getMapCenterZoom_Zoom();
if (latE6 != -1 && lonE6 != -1 && zoom != -1) {
final GeoPoint center = new GeoPoint(latE6, lonE6);
final GeoPoint center = new GeoPoint(latE6, lonE6);
mapView.getController().setCenter(center);
mapView.getController().setZoom(zoom);
}
Expand All @@ -172,6 +174,12 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
return v;
}

@Override
public void onDestroyView(){
wptOver.onParentFragmentDestroyView();
super.onDestroyView();
}

/**
* See if the user wants us to open an IGC file
*/
Expand Down Expand Up @@ -298,7 +306,7 @@ private void disableWeatherStations(){
public void onPause() {
super.onPause();

wptOver.onPause();
// wptOver.onPause();
if (weather_overlay != null){
weather_overlay.unregisterToWeatherUpdate(getActivity());
}
Expand All @@ -323,7 +331,7 @@ public void onResume() {
super.onResume();

Units.instance.setFromPrefs(getActivity());
wptOver.onResume();
// wptOver.onResume();

if (weather_overlay != null){
weather_overlay.registerToWeatherUpdate(getActivity());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public class ListWaypointsFragment extends AbstractDBListFragment implements Obs
private GPSClientStub gps;
private static final String TAG = "ListWaypointsFragment";

public interface OnWaypointSelectedListener {
public interface OnWaypointActionListener {
public void onWaypointSelected(Waypoint wpt);
}

Expand Down
39 changes: 37 additions & 2 deletions src/com/geeksville/location/WaypointDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,33 @@ public class WaypointDB extends Observable implements LocationListener, ServiceC
private Context context;
private IGPSClient gps;

public static enum UpdateEnum{
DELETE_ONE,
ADD_ONE,
CLEAR_ALL,
UPDATE_WPT,
DISTANCE_CHANGE,
};
public static class UpdateData {
public UpdateData(UpdateEnum type, long id){
this(type, id, null);
}
public UpdateData(UpdateEnum type){
this(type, -1, null);
}
public UpdateData(UpdateEnum type, ExtendedWaypoint w){
this(type, -1, w);
}
public UpdateData(UpdateEnum type, long id, ExtendedWaypoint w) {
this.type = type;
this.w = w;
this.id = id;
}
public UpdateEnum type;
public long id;
public ExtendedWaypoint w;
}

/**
* One row for each Waypoint type, one col for each WaypointColor
*/
Expand Down Expand Up @@ -204,7 +231,7 @@ private void setPilotLocation(Location l) {
// has been an appreciable change in
// position)
setChanged();
notifyObservers();
notifyObservers(new UpdateData(UpdateEnum.DISTANCE_CHANGE));
}

public ExtendedWaypoint getNearestLZ() {
Expand Down Expand Up @@ -240,7 +267,7 @@ public long add(ExtendedWaypoint w) {
// can be a problem when the adding component is also an observer (may get
// notified for nothing)
setChanged();
notifyObservers();
notifyObservers(new UpdateData(UpdateEnum.ADD_ONE, w));

return w.id;
}
Expand Down Expand Up @@ -341,6 +368,8 @@ public void deleteAllWaypoints() {
// FIXME - use proper thread safety prims for these containers
wptsById.clear();
wptsByName = null;

notifyObservers(new UpdateData(UpdateEnum.CLEAR_ALL));
}

public void deleteWaypoint(long id) {
Expand All @@ -349,6 +378,9 @@ public void deleteWaypoint(long id) {

wptsById.remove(id);
wptsByName = null;

setChanged();
notifyObservers(new UpdateData(UpdateEnum.DELETE_ONE, id));
}

/**
Expand All @@ -359,6 +391,9 @@ public void deleteWaypoint(long id) {
void updateWaypoint(ExtendedWaypoint w) {
db.updateWaypoint(w.id, w.name, w.description, w.latitude, w.longitude, w.altitude, w.type.ordinal());

setChanged();
notifyObservers(new UpdateData(UpdateEnum.UPDATE_WPT, w));

wptsByName = null;
}

Expand Down
22 changes: 14 additions & 8 deletions src/com/geeksville/maps/WaypointItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ public class WaypointItem extends ExtendedOverlayItem {

private static final String TAG = "WaypointItem";

private ExtendedWaypoint w;
private ExtendedWaypoint extendedWpt;

public ExtendedWaypoint getExtendedWpt() {
return extendedWpt;
}

private CaptionedDrawable marker;
private int width, height;

Expand All @@ -51,8 +56,7 @@ public class WaypointItem extends ExtendedOverlayItem {

public WaypointItem(ExtendedWaypoint w, Paint captionPaint, Context context) {
super(w.name, w.description, w.geoPoint, context);

this.w = w;
this.extendedWpt = w;
curIcon = w.getIcon();
// Note: be careful to call mutate here, because we'll be moving each
// drawable independently
Expand All @@ -73,20 +77,22 @@ public WaypointItem(ExtendedWaypoint w, Paint captionPaint, Context context) {
* @return true if the icon changed
*/
boolean updateIcon() {
Drawable newIcon = w.getIcon();

final Drawable newIcon = extendedWpt.getIcon();
boolean refresh=false;

if (curIcon != newIcon) {
curIcon = newIcon;
marker.setDrawable(newIcon);
return true;
refresh = true;
}


return false;
return refresh;
}

public void handleTap(Activity context) {
String msg = String.format(context.getString(R.string.distance_s_s_glide_s), Units.instance
.metersToDistance(w.distanceFromPilotX), Units.instance.getDistanceUnits(), w
.metersToDistance(extendedWpt.distanceFromPilotX), Units.instance.getDistanceUnits(), extendedWpt
.glideRatioString());

Toast t = Toast.makeText(context, msg, Toast.LENGTH_LONG);
Expand Down
67 changes: 59 additions & 8 deletions src/com/geeksville/maps/WaypointOverlay.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,7 @@ public boolean onLongPress(MotionEvent event, MapView mapView) {
mGeoPoint.getAltitude(), 0,
Waypoint.Type.Unknown.ordinal());
db.add(w);
WaypointItem item = new WaypointItem(w, captionPaint, context);
addItem(item);
populate();
view.postInvalidate();
// item will be added when db update the overlay.
return true;
}
return false;
Expand Down Expand Up @@ -163,12 +160,12 @@ public static Drawable boundCenter(Drawable d) {
// return ItemizedOverlay.boundCenter(d);
}

public void onPause() {
db.deleteObserver(this);
public void onParentFragmentCreateView() {
db.addObserver(this);
}

public void onResume() {
db.addObserver(this);
public void onParentFragmentDestroyView(){
db.deleteObserver(this);
}

/**
Expand Down Expand Up @@ -199,6 +196,60 @@ public void update(Observable observable, Object data) {
// // contents of the CaptionedDrawables
// // and then invalidate only if needed
boolean needRedraw = false;

WaypointDB.UpdateData updatedata = (WaypointDB.UpdateData) data;

switch(updatedata.type){
case UPDATE_WPT:
WaypointItem f1 = null;
for(WaypointItem wi : mItemList){
if (wi.getExtendedWpt().id == updatedata.w.id){
Log.d(TAG, "found wpt to update");
f1 = wi;
break;
}
}
if (f1 == null){
Log.e(TAG, "Could not find waypoint to update");
} else {
removeItem(f1);
WaypointItem item = new WaypointItem(updatedata.w, captionPaint, context);
addItem(item);
}
needRedraw = true;
break;
case ADD_ONE:
WaypointItem item = new WaypointItem(updatedata.w, captionPaint, context);
addItem(item);
needRedraw = true;
break;
case DELETE_ONE:
Log.d(TAG, "delete one");
WaypointItem f2 = null;
for(WaypointItem wi : mItemList){
if (wi.getExtendedWpt().id == updatedata.id){
Log.d(TAG, "found wpt to remove");
f2 = wi;
break;
}
}
if (f2 != null){
if (!removeItem(f2)){
Log.e(TAG, "Could not removing item");
}
} else {
Log.e(TAG, "Could not find waypoint to remove");
}
needRedraw = true;
break;
case CLEAR_ALL:
removeAllItems();
needRedraw = true;
break;
case DISTANCE_CHANGE:
}

Log.d(TAG, "update from Observable");
for (WaypointItem item : mItemList) {
needRedraw |= item.updateIcon();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,15 @@ public void onReceive(final Context context, final Intent intent) {
if ("mobibalises.balisesUpdate".equals(action)) {
final Object[] balises = (Object[])intent.getSerializableExtra("balises");
final String key= (String)intent.getSerializableExtra("key");
Log.d(TAG, "Balise update for key: " + key);

if (!itemsMap.containsKey(key)){
Log.d(TAG, "First time, creating map");
itemsMap.put(key, new HashMap<String, BaliseOverlayItem>());
}
if (balises != null){
final HashSet<String> b_ids = new HashSet<String>();
final HashMap<String, BaliseOverlayItem> map = itemsMap.get(key);

Log.d(TAG, "Balise update for key: " + key);

for (Object bo : balises){
final Balise b = (Balise)bo;
BaliseOverlayItem boi = map.get(b.id);
Expand All @@ -76,18 +75,14 @@ public void onReceive(final Context context, final Intent intent) {
if (boi == null) {
boi = new BaliseOverlayItem(null, b, context);
WeatherStationsOverlay.this.addItem(boi);
Log.d(TAG, "create pin for " + b.nom);
map.put(b.id, boi);
} else {
Log.d(TAG, "pin for " + b.nom + " already here");
}
}
final HashSet<String> to_remove = new HashSet<String>();

for (Map.Entry<String, BaliseOverlayItem> e: map.entrySet()){
if (! b_ids.contains(e.getKey())){
WeatherStationsOverlay.this.removeItem(e.getValue());
Log.d(TAG, "Removing " + e.getValue().bid);
to_remove.add(e.getKey());
}
}
Expand Down

0 comments on commit 8595a47

Please sign in to comment.