Created
August 5, 2012 20:15
-
-
Save mrsidique/3266982 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| - (IBAction)showButtonTapped:(id)sender { | |
| NSMutableDictionary *nutritionPrefs = [[NSUserDefaults standardUserDefaults]objectForKey:@"nutritionPrefs"]; | |
| NSMutableDictionary *cuisinePrefs = [[NSUserDefaults standardUserDefaults]objectForKey:@"cuisinePrefs"]; | |
| NSMutableDictionary *typeOfDishPrefs = [[NSUserDefaults standardUserDefaults]objectForKey:@"typeOfDishPrefs"]; | |
| NSMutableDictionary *ingredientsPrefs = [[NSUserDefaults standardUserDefaults]objectForKey:@"ingredientsPrefs"]; | |
| //Get dining preferences from dictionary and add them to an array | |
| NSMutableArray *diningPrefsArray = [[NSMutableArray alloc]initWithCapacity:1]; | |
| if ([[NSUserDefaults standardUserDefaults]boolForKey:@"DINE IN"]) { | |
| [diningPrefsArray addObject:@"Dine In"]; | |
| } | |
| if ([[NSUserDefaults standardUserDefaults]boolForKey:@"TAKE OUT"]) { | |
| [diningPrefsArray addObject:@"Take Out"]; | |
| } | |
| if ([[NSUserDefaults standardUserDefaults]boolForKey:@"DELIVERY"]) { | |
| [diningPrefsArray addObject:@"Delivery"]; | |
| } | |
| //configure the parameter string based on which filter items have been selected | |
| NSString *params; | |
| if (nutritionPrefs.count > 0) { | |
| for (int i = 0; i < [nutritionPrefs allKeys].count; i++) { | |
| if (i == 0) { | |
| params = [NSString stringWithFormat:@"?diet[]=%@", [[nutritionPrefs allKeys]objectAtIndex:i]]; | |
| } | |
| else { | |
| params = [NSString stringWithFormat:@"%@&diet[]=%@", params,[[nutritionPrefs allKeys]objectAtIndex:i]]; | |
| } | |
| } | |
| } | |
| if (cuisinePrefs.count > 0) { | |
| for (int i = 0; i < [cuisinePrefs allKeys].count; i++) { | |
| if (i == 0) { | |
| if (params.length > 0) { | |
| params = [NSString stringWithFormat:@"%@&cuis[]=%@", params, [[cuisinePrefs allKeys]objectAtIndex:i]]; | |
| } | |
| else { | |
| params = [NSString stringWithFormat:@"?cuis[]=%@", [[cuisinePrefs allKeys]objectAtIndex:i]]; | |
| } | |
| } | |
| else { | |
| params = [NSString stringWithFormat:@"%@&cuis[]=%@", params,[[cuisinePrefs allKeys]objectAtIndex:i]]; | |
| } | |
| } | |
| } | |
| if (typeOfDishPrefs.count > 0) { | |
| for (int i = 0; i < [typeOfDishPrefs allKeys].count; i++) { | |
| if (i == 0) { | |
| if (params.length > 0) { | |
| params = [NSString stringWithFormat:@"%@&typ[]=%@", params, [[typeOfDishPrefs allKeys]objectAtIndex:i]]; | |
| } | |
| else { | |
| params = [NSString stringWithFormat:@"?typ[]=%@", [[typeOfDishPrefs allKeys]objectAtIndex:i]]; | |
| } | |
| } | |
| else { | |
| params = [NSString stringWithFormat:@"%@&typ[]=%@", params,[[typeOfDishPrefs allKeys]objectAtIndex:i]]; | |
| } | |
| } | |
| } | |
| if (ingredientsPrefs.count > 0) { | |
| for (int i = 0; i < [ingredientsPrefs allKeys].count; i++) { | |
| if (i == 0) { | |
| if (params.length > 0) { | |
| params = [NSString stringWithFormat:@"%@&ing[]=%@", params, [[ingredientsPrefs allKeys]objectAtIndex:i]]; | |
| } | |
| else { | |
| params = [NSString stringWithFormat:@"?ing[]=%@", [[ingredientsPrefs allKeys]objectAtIndex:i]]; | |
| } | |
| } | |
| else { | |
| params = [NSString stringWithFormat:@"%@&ing[]=%@", params,[[ingredientsPrefs allKeys]objectAtIndex:i]]; | |
| } | |
| } | |
| } | |
| if (diningPrefsArray.count > 0) { | |
| for (int i = 0; i < diningPrefsArray.count; i++) { | |
| if (i == 0) { | |
| if (params.length > 0) { | |
| params = [NSString stringWithFormat:@"%@&dine[]=%@", params, [diningPrefsArray objectAtIndex:i]]; | |
| } | |
| else { | |
| params = [NSString stringWithFormat:@"?dine[]=%@", [diningPrefsArray objectAtIndex:i]]; | |
| } | |
| } | |
| else { | |
| params = [NSString stringWithFormat:@"%@&dine[]=%@", params,[diningPrefsArray objectAtIndex:i]]; | |
| } | |
| } | |
| } | |
| if (![searchTextField.text isKindOfClass:[NSNull class]] && ![searchTextField.text isEqualToString:@""]) { | |
| if (params.length > 0) { | |
| params = [NSString stringWithFormat:@"%@&srch=%@", params, searchTextField.text]; | |
| } | |
| else { | |
| params = [NSString stringWithFormat:@"?srch=%@", searchTextField.text]; | |
| } | |
| } | |
| /* | |
| //check if user location data is available and if so add it as a parameter | |
| if ([[NSUserDefaults standardUserDefaults]valueForKey:@"latitude"] && [[NSUserDefaults standardUserDefaults]valueForKey:@"longitude"]) { | |
| if (params.length > 0) { | |
| params = [NSString stringWithFormat:@"%@&lat=%@&long=%@", params, [[NSUserDefaults standardUserDefaults]valueForKey:@"latitude"], [[NSUserDefaults standardUserDefaults]valueForKey:@"longitude"]]; | |
| } | |
| else { | |
| params = [NSString stringWithFormat:@"?lat=%@&long=%@", [[NSUserDefaults standardUserDefaults]valueForKey:@"latitude"], [[NSUserDefaults standardUserDefaults]valueForKey:@"longitude"]]; | |
| } | |
| }*/ | |
| //URL encode the parameters | |
| params = [params stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]; | |
| NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@menu_items%@", [[NSUserDefaults standardUserDefaults]objectForKey:@"baseUrl"], params]]; | |
| NSLog(@"%@", url); | |
| NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30]; | |
| [request setHTTPMethod:@"GET"]; | |
| //Send request to server | |
| [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { | |
| NSMutableArray* jsonObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; | |
| if (jsonObject.count > 0) { | |
| [self setContentList:jsonObject]; | |
| self.navigationItem.leftBarButtonItem = backButton; | |
| _filterView = NO; | |
| [searchTextField resignFirstResponder]; | |
| [self hideSearchView]; | |
| [_tableView setFrame:CGRectMake(0, 87, 320, 332)]; | |
| [_tableView reloadData]; | |
| } | |
| else { | |
| UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Healthy Out" message:@"No items found." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; | |
| [alert show]; | |
| } | |
| }]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment