Skip to content

Commit

Permalink
Fix restauraunts search
Browse files Browse the repository at this point in the history
  • Loading branch information
amcereijo committed Dec 6, 2012
1 parent 6aa8314 commit 3cd4a96
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 139 deletions.
180 changes: 88 additions & 92 deletions src/com/trcardmanager/action/RestaurantInfoAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.trcardmanager.R;
import com.trcardmanager.application.TRCardManagerApplication;
Expand Down Expand Up @@ -91,111 +90,108 @@ protected void onPostExecute(Void result) {
progressDialog.cancel();
}
if(!error){
dialog = new AlertDialog.Builder(activity).create();
dialog.setInverseBackgroundForced(true);
View view = createAndFillDataMovementLayout(restaurant,position);
dialog.setView(view);
dialog.show();
dialog.setOnDismissListener(new DialogInterface.OnDismissListener(){
public void onDismiss(DialogInterface dialog) {
dialog.cancel();
}
});
}else if(error){
Toast.makeText(activity, "Error", Toast.LENGTH_LONG).show();
restaurant.setCompleteDataLoaded(Boolean.TRUE);
}
dialog = new AlertDialog.Builder(activity).create();
dialog.setInverseBackgroundForced(true);
View view = createAndFillDataMovementLayout(restaurant,position);
dialog.setView(view);
dialog.show();
dialog.setOnDismissListener(new DialogInterface.OnDismissListener(){
public void onDismiss(DialogInterface dialog) {
dialog.cancel();
}
});
}


private RelativeLayout createAndFillDataMovementLayout(RestaurantDao restaurant, int position){
RelativeLayout relativeMovementLayout = (RelativeLayout)inflater.inflate(
R.layout.restaurant_data, null,false);

RelativeLayout relativeMovementInfoLayout = (RelativeLayout)relativeMovementLayout.findViewById(R.id.restaurant_data_info_layout);

((TextView)relativeMovementInfoLayout.findViewById(R.id.restaurant_data_name)).setText(restaurant.getRetaurantName());
((TextView)relativeMovementInfoLayout.findViewById(R.id.restaurant_data_direction)).setText(restaurant.getRestaurantDisplayDirection());
String foodType = getFoodType(restaurant);
((TextView)relativeMovementInfoLayout.findViewById(R.id.restaurant_data_type)).setText(foodType);
((TextView)relativeMovementInfoLayout.findViewById(R.id.restaurant_data_phone)).setText(restaurant.getPhoneNumber());

RelativeLayout buttonsLayout = (RelativeLayout)relativeMovementLayout.findViewById(R.id.restaurant_data_apps_layout);
buttonsLayout.setId(position);

if(wazeInstalled){
ImageButton wazeButton = (ImageButton)buttonsLayout.findViewById(R.id.restaurant_data_waze_image);
wazeButton.setVisibility(View.VISIBLE);
wazeButton.setOnTouchListener(new TouchElementsListener<ImageButton>());
wazeButton.setOnClickListener(new WazeClickListener(restaurant));
}

TextView restaurantDataClose = (TextView)relativeMovementInfoLayout.findViewById(R.id.restaurant_data_close);
restaurantDataClose.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.hide();
dialog.cancel();
}
});

ImageButton iButtonMaps = (ImageButton)buttonsLayout.findViewById(R.id.restaurant_data_maps);
iButtonMaps.setOnTouchListener(new TouchElementsListener<ImageButton>());
iButtonMaps.setOnClickListener(new GMapsClickListener(restaurant));

restaurant.setCompleteDataLoaded(Boolean.TRUE);

return relativeMovementLayout;
}

private String getFoodType(RestaurantDao restaurant) {
String foodTypeTitle = activity.getResources().getText(R.string.restaurant_data_text_type).toString();
String foodType = restaurant.getFoodType();
if(foodTypeWithoutTitleWithValue(foodTypeTitle, foodType) ){
foodType = foodTypeTitle+" "+foodType;
}
return foodType;
}


private boolean foodTypeWithoutTitleWithValue(String foodTypeTitle, String foodType) {
return foodType != null && !"".equals(foodType) &&
(!foodType.toLowerCase().contains(foodTypeTitle.toLowerCase()));
}
RelativeLayout relativeMovementLayout = (RelativeLayout)inflater.inflate(
R.layout.restaurant_data, null,false);

RelativeLayout relativeMovementInfoLayout = (RelativeLayout)relativeMovementLayout.findViewById(R.id.restaurant_data_info_layout);

((TextView)relativeMovementInfoLayout.findViewById(R.id.restaurant_data_name)).setText(restaurant.getRetaurantName());
((TextView)relativeMovementInfoLayout.findViewById(R.id.restaurant_data_direction)).setText(restaurant.getRestaurantDisplayDirection());
String foodType = getFoodType(restaurant);
((TextView)relativeMovementInfoLayout.findViewById(R.id.restaurant_data_type)).setText(foodType);
((TextView)relativeMovementInfoLayout.findViewById(R.id.restaurant_data_phone)).setText(restaurant.getPhoneNumber());

private void setWazeInstalled(){
Intent intent = new Intent( Intent.ACTION_VIEW, Uri.parse( WAZE_APP_URL ) );
List<ResolveInfo> list = activity.getPackageManager().queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
wazeInstalled = (list.size()>0);
}

RelativeLayout buttonsLayout = (RelativeLayout)relativeMovementLayout.findViewById(R.id.restaurant_data_apps_layout);
buttonsLayout.setId(position);

if(wazeInstalled){
ImageButton wazeButton = (ImageButton)buttonsLayout.findViewById(R.id.restaurant_data_waze_image);
wazeButton.setVisibility(View.VISIBLE);
wazeButton.setOnTouchListener(new TouchElementsListener<ImageButton>());
wazeButton.setOnClickListener(new WazeClickListener(restaurant));
}

private class WazeClickListener implements OnClickListener{
private RestaurantDao restaurantDao;
public WazeClickListener(RestaurantDao restaurantDao){
this.restaurantDao = restaurantDao;
}
TextView restaurantDataClose = (TextView)relativeMovementInfoLayout.findViewById(R.id.restaurant_data_close);
restaurantDataClose.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
LocationDao location = restaurantDao.getLocation();
String urlwaze = String.format(URL_WAZE_APP,location.getLatitude(),location.getLongitude());
Intent intent = new Intent( Intent.ACTION_VIEW, Uri.parse( urlwaze ) );
activity.startActivity(intent);

dialog.hide();
dialog.cancel();
}
});

ImageButton iButtonMaps = (ImageButton)buttonsLayout.findViewById(R.id.restaurant_data_maps);
iButtonMaps.setOnTouchListener(new TouchElementsListener<ImageButton>());
iButtonMaps.setOnClickListener(new GMapsClickListener(restaurant));

return relativeMovementLayout;
}

private String getFoodType(RestaurantDao restaurant) {
String foodTypeTitle = activity.getResources().getText(R.string.restaurant_data_text_type).toString();
String foodType = restaurant.getFoodType();
if(foodTypeWithoutTitleWithValue(foodTypeTitle, foodType) ){
foodType = foodTypeTitle+" "+foodType;
}
return foodType;
}

private class GMapsClickListener implements OnClickListener{
private RestaurantDao restaurantDao;
public GMapsClickListener(RestaurantDao restaurantDao){
this.restaurantDao = restaurantDao;
}
public void onClick(View v) {
String uri = String.format(URI_TO_OPEN_MAPS,
ZOOM_LEVEL,restaurantDao.getRestaurantDisplayDirection());
activity.startActivity(new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uri)));
}

private boolean foodTypeWithoutTitleWithValue(String foodTypeTitle, String foodType) {
return foodType != null && !"".equals(foodType) &&
(!foodType.toLowerCase().contains(foodTypeTitle.toLowerCase()));
}



private void setWazeInstalled(){
Intent intent = new Intent( Intent.ACTION_VIEW, Uri.parse( WAZE_APP_URL ) );
List<ResolveInfo> list = activity.getPackageManager().queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
wazeInstalled = (list.size()>0);
}



private class WazeClickListener implements OnClickListener{
private RestaurantDao restaurantDao;
public WazeClickListener(RestaurantDao restaurantDao){
this.restaurantDao = restaurantDao;
}
public void onClick(View v) {
LocationDao location = restaurantDao.getLocation();
String urlwaze = String.format(URL_WAZE_APP,location.getLatitude(),location.getLongitude());
Intent intent = new Intent( Intent.ACTION_VIEW, Uri.parse( urlwaze ) );
activity.startActivity(intent);

}
}

private class GMapsClickListener implements OnClickListener{
private RestaurantDao restaurantDao;
public GMapsClickListener(RestaurantDao restaurantDao){
this.restaurantDao = restaurantDao;
}
public void onClick(View v) {
String uri = String.format(URI_TO_OPEN_MAPS,
ZOOM_LEVEL,restaurantDao.getRestaurantDisplayDirection());
activity.startActivity(new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uri)));
}
}

}
19 changes: 11 additions & 8 deletions src/com/trcardmanager/action/SearchRestaurantsAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,9 @@ protected void onPostExecute(Void result) {
if(restaurantSearchDao.getSearchViewType() == SearchViewType.MAP_VIEW){
printMapView();
}else{
showDirectionOfSearch();
cleanRestaurantsView();
createAdapterAndSetAndListView();
showDirectionOfSearch();
cleanRestaurantsView();
createAdapterAndSetAndListView();
}
TRCardManagerApplication.setRestaurantSearchDao(restaurantSearchDao);
}else{
Expand Down Expand Up @@ -191,9 +191,8 @@ private void printMapView(){

List<RestaurantDao> restaurants = restaurantSearchDao.getRestaurantList();
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = activity.getResources().getDrawable(R.drawable.map_marker);
Drawable drawable = activity.getResources().getDrawable(R.drawable.map_marker);
for(RestaurantDao r : restaurants){

RestaurantItemOverlay itemizedoverlay = createOverLayItemForMap(
drawable, r);
mapOverlays.add(itemizedoverlay);
Expand All @@ -207,7 +206,8 @@ private RestaurantItemOverlay createOverLayItemForMap(Drawable drawable,

GeoPoint restaurantPoint = new GeoPoint((int)(r.getLocation().getLatitude()*1E6),
(int)(r.getLocation().getLongitude()*1E6));

Log.d(TAG, "---Creado restaurante "+r.getRetaurantName()+" en : "+r.getLocation().getLatitude()+","+
r.getLocation().getLongitude());
RestaurantOverlayItemDao overlayitem = new RestaurantOverlayItemDao(restaurantPoint, r.getRetaurantName(),
r.getStreet()+", "+r.getLocality()+", "+r.getArea(), r);

Expand Down Expand Up @@ -263,7 +263,7 @@ private void searchLocation() throws IOException{
private void searchRestaurantList() throws IOException {
TRCardManagerHttpAction httpAction = new TRCardManagerHttpAction();
List<RestaurantDao> restaurants = httpAction.getRestaurants(restaurantSearchDao);
//TODO remove?
//TODO remove if do inside "httpAction.getRestaurants"
addFoundRestaurantsToList(restaurants);
}

Expand All @@ -274,7 +274,9 @@ private void addFoundRestaurantsToList(List<RestaurantDao> restaurants){
restaurantSearchDao.setRestaurantList(restaurants);
}else{
lastViewPosition = actualRestaurants.size()-1;
actualRestaurants.remove(actualRestaurants.size()-1);
if(lastViewPosition>=0){
actualRestaurants.remove(actualRestaurants.size()-1);
}
actualRestaurants.addAll(restaurants);
}
if(restaurantSearchDao.getCurrentPage()<=restaurantSearchDao.getNumberOfPages()){
Expand All @@ -289,6 +291,7 @@ private void createAdapterAndSetAndListView() {
restaurantsListView.setAdapter(adapter);
restaurantsListView.refreshDrawableState();
}else{
//TODO delete if not implement more search
//Delete last("load more" view)
RestaurantDao r = adapter.getItem(adapter.getCount()-1);
adapter.remove(r);
Expand Down
Loading

0 comments on commit 3cd4a96

Please sign in to comment.