--- android/TrainInfo/src/dk/thoerup/traininfo/StationList.java 2009/10/11 07:07:29 439 +++ android/TrainInfo/src/dk/thoerup/traininfo/StationList.java 2009/10/27 13:39:28 473 @@ -55,11 +55,18 @@ public static final int DLG_PROGRESS = 3001; public static final int DLG_STATIONNAME = 3002; - /** Called when the activity is first created. */ + static enum LookupMethod { + ByLocation, + ByName, + ByList, + MethodNone + } + + String dialogMessage = ""; ProgressDialog dialog; LocationLookup locator = null; - LocatorTask locatorTask; + FindStationsTask findStationsTask; StationsFetchedHandler stationsFetched = new StationsFetchedHandler(); GeoPair location = new GeoPair(); @@ -74,12 +81,7 @@ FavoritesMenu contextMenu = new FavoritesMenu(); IntSet favorites = new IntSet(); - static enum LookupMethod { - ByLocation, - ByName, - ByList, - MethodNone - } + boolean showingFavorites = false; SharedPreferences prefs; @@ -307,21 +309,21 @@ dialogMessage = "Finding stations by name"; showDialog(DLG_PROGRESS); - locatorTask = new LocatorTask(); - locatorTask.searchByName(name, locator.getLocation()); - locatorTask.execute(); + findStationsTask = new FindStationsTask(); + findStationsTask.searchByName(name, locator.getLocation()); + findStationsTask.execute(); } public void startFavoriteLookup() { - if (favorites.toString().length() > 0) { + if (favorites.size() > 0) { dialogMessage = "Loading favorites"; showDialog(DLG_PROGRESS); - locatorTask = new LocatorTask(); - locatorTask.searchByIds(favorites.toString(), locator.getLocation()); - locatorTask.execute(); + findStationsTask = new FindStationsTask(); + findStationsTask.searchByIds(favorites.toString(), locator.getLocation()); + findStationsTask.execute(); } else { MessageBox.showMessage(this, "Favorite list is empty"); } @@ -334,9 +336,9 @@ dialogMessage = "Finding nearby stations"; showDialog(DLG_PROGRESS); - locatorTask = new LocatorTask(); - locatorTask.searchByLocation( locator.getLocation() ); - locatorTask.execute(); + findStationsTask = new FindStationsTask(); + findStationsTask.searchByLocation( locator.getLocation() ); + findStationsTask.execute(); } @@ -421,7 +423,7 @@ }; - class LocatorTask extends AsyncTask { + class FindStationsTask extends AsyncTask { LookupMethod method = LookupMethod.MethodNone; boolean success; @@ -458,15 +460,21 @@ @Override protected Void doInBackground(Void... params) { - - if (method.equals(LookupMethod.ByLocation)) + + switch (method) { + case ByLocation: success = stationProvider.lookupStations(loc); - - if (method.equals(LookupMethod.ByName)) + break; + case ByName: success = stationProvider.lookupStationsByName(name); - - if (method.equals(LookupMethod.ByList)) + break; + case ByList: success = stationProvider.lookupStationsByIds(ids); + break; + default: + success = false; // not possible + } + Location dummy = new Location("gps"); List stations = stationProvider.getStations(); @@ -490,11 +498,32 @@ super.onPostExecute(result); dialog.dismiss(); + //set title + String title; + switch (method) { + case ByLocation: + title = "Traininfo DK - Nearby stations"; + break; + case ByName: + title = "Traininfo DK - Search"; + break; + case ByList: + title = "Traininfo DK - Favorites"; + break; + default: + title = "";//not possible + } + + StationList.this.setTitle(title); + //set title end + if (success) { if (stationProvider.getStations().size() == 0) MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?! stations = stationProvider.getStations(); - adapter.setStations( stations ); + adapter.setStations( stations ); + + showingFavorites = method.equals(LookupMethod.ByList); } else { //communication or parse errors AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); @@ -537,7 +566,7 @@ selectedPosition = info.position; int stationID = stations.get(selectedPosition).getId(); - if (!favorites.hasInt(stationID)) { + if (!favorites.contains(stationID)) { menu.add(0, FAVORITES_ADD, 0, "Add to favorites"); } else { menu.add(0, FAVORITES_REMOVE, 0, "Remove from favorites"); @@ -553,8 +582,14 @@ favorites.add(stationID); Toast.makeText(StationList.this, "Station added", Toast.LENGTH_SHORT).show(); } else { + favorites.remove(stationID); Toast.makeText(StationList.this, "Station removed", Toast.LENGTH_SHORT).show(); + + if (showingFavorites) { + stations.remove(selectedPosition); + adapter.notifyDataSetChanged(); + } } Editor ed = prefs.edit(); ed.putString("favorites", favorites.toString());