-
Notifications
You must be signed in to change notification settings - Fork 0
/
CeleryUIController.m
330 lines (261 loc) · 10.2 KB
/
CeleryUIController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
//
// CeleryUIController.m
// Celery
//
// Created by Mark Powell on 11/3/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "CeleryUIController.h"
@implementation CeleryUIController
- (IBAction)searchForFood:(id)sender {
if(!updating) {
updating = YES;
[searching startAnimation:self];
if(!restManager) {
restManager = [[FatSecretManager alloc] init];
[restManager authenticateWithServer];
[servingCombo setDataSource:self];
[servingCombo setDelegate:self];
}
currentPage = 0;
maxPages = 0;
[restManager searchForFoodWithString:[searchField stringValue] forTarget:self forSelector:@selector(performedMethodLoadForURL:withResponseBody:) pageNumber:currentPage];
}
}
- (IBAction) nextPage:(id)sender {
if(!updating) {
updating = YES;
[searching startAnimation:self];
if(!restManager) {
restManager = [[FatSecretManager alloc] init];
[restManager authenticateWithServer];
}
if(![prevButton isEnabled]) {
[prevButton setEnabled:YES];
}
currentPage++;
if(currentPage == maxPages) {
[nextButton setEnabled:NO];
}
[restManager searchForFoodWithString:[searchField stringValue] forTarget:self forSelector:@selector(performedMethodLoadForURL:withResponseBody:) pageNumber:currentPage];
}
}
- (IBAction) prevPage:(id)sender {
if(!updating) {
updating = YES;
[searching startAnimation:self];
if(!restManager) {
restManager = [[FatSecretManager alloc] init];
[restManager authenticateWithServer];
}
if(![nextButton isEnabled]) {
[nextButton setEnabled:YES];
}
currentPage--;
if(currentPage == 0) {
[prevButton setEnabled:NO];
}
[restManager searchForFoodWithString:[searchField stringValue] forTarget:self forSelector:@selector(performedMethodLoadForURL:withResponseBody:) pageNumber:currentPage];
}
}
- (void)performedMethodLoadForURL:(NSURL *)inMethod withResponseBody:(NSString *)inResponseBody {
id dict = [inResponseBody JSONValue];
id foodDict = [dict objectForKey:@"foods"];
int page = [[foodDict objectForKey:@"max_results"] intValue];
int total = [[foodDict objectForKey:@"total_results"] intValue];
maxPages = total/page;
[countLabel setStringValue:[NSString stringWithFormat:@"%i/%i", currentPage, maxPages]];
id tempFoods = [foodDict objectForKey:@"food"];
NSArray* foods = nil;
if([tempFoods isKindOfClass:[NSDictionary class]]) {
foods = [[[NSArray alloc] initWithObjects:tempFoods,nil] autorelease];
} else {
foods = (NSArray*)tempFoods;
}
if(!foodData) {
foodData = [[TableArrayDelegate alloc] init];
foodData.delegate = self;
[displayTable setDataSource:foodData];
[displayTable setDelegate:foodData];
}
[foodData removeAllData];
for(NSDictionary* foodInfo in foods) {
FoodData* foodItem = [[FoodData alloc] init];
foodItem.foodName = (NSString*)[foodInfo objectForKey:@"food_name"];
foodItem.foodID = [[foodInfo objectForKey:@"food_id"] intValue];
foodItem.brandName = (NSString*)[foodInfo objectForKey:@"brand_name"];
foodItem.shortDescription = (NSString*)[foodInfo objectForKey:@"food_description"];
[foodData addData:foodItem];
[foodItem release];
}
[displayTable reloadData];
[searching stopAnimation:self];
updating = NO;
}
- (void)performedMethodLoadForURL2:(NSURL *)inMethod withResponseBody:(NSString *)inResponseBody {
NSDictionary* dict = (NSDictionary*)[[inResponseBody JSONValue] objectForKey:@"food"];
NSDictionary* servings = [dict objectForKey:@"servings"];
id tempServing = [servings objectForKey:@"serving"];
NSArray* servingList = nil;
if([tempServing isKindOfClass:[NSDictionary class]]) {
servingList = [[NSArray alloc] initWithObjects:tempServing,nil];
} else {
servingList = (NSArray*)tempServing;
}
NSDictionary* serving = [servingList objectAtIndex:0];
NSString* name = [dict objectForKey:@"food_name"];
[self displayDetailForServing:serving forFoodNamed:name];
selectedData.detailedDescription = servingList;
[searching stopAnimation:self];
updating = NO;
}
- (void) displayDetailForServing:(NSDictionary*) serving forFoodNamed:(NSString*) name {
[servingSizeLabel setStringValue:[serving objectForKey:@"serving_description"]];
[servingsPerLabel setStringValue:[serving objectForKey:@"number_of_units"]];
NSString* calories = [serving objectForKey:@"calories"];
if(calories) {
[caloriesLabel setStringValue:calories];
} else {
[caloriesLabel setStringValue:@"0"];
}
NSString* carbohydrate = [serving objectForKey:@"carbohydrate"];
if(carbohydrate) {
[totalCarbohydrateLabel setStringValue:[carbohydrate stringByAppendingString:@"g"]];
double carbValue = [carbohydrate doubleValue];
int percent = (int)((carbValue / 300.0f) * 100);
[totalCarbohydratePercentLabel setStringValue:[NSString stringWithFormat:@"%i%%", percent]];
} else {
[totalCarbohydrateLabel setStringValue:@"0g"];
[totalCarbohydratePercentLabel setStringValue:@"0%"];
}
[proteinLabel setStringValue:[[serving objectForKey:@"protein"] stringByAppendingString:@"g"]];
NSString* fat = [serving objectForKey:@"fat"];
if(fat) {
[totalFatLabel setStringValue:[fat stringByAppendingString:@"g"]];
double fatValue = [fat doubleValue];
double caloriesFromFat = fatValue * 9;
int percent = (int)((fatValue / 65.0f) * 100);
[caloriesFromFatLabel setStringValue:[NSString stringWithFormat:@"%4.2f",caloriesFromFat]];
[totalFatPercentLabel setStringValue:[NSString stringWithFormat:@"%i%%", percent]];
} else {
[totalFatLabel setStringValue:@"0g"];
[caloriesFromFatLabel setStringValue:@"0"];
[totalFatPercentLabel setStringValue:@"0%"];
}
NSString* saturatedFat = [serving objectForKey:@"saturated_fat"];
if(saturatedFat) {
double satFatValue = [saturatedFat doubleValue];
int percent = (int)((satFatValue / 20.0f) * 100);
[saturatedFatLabel setStringValue:[saturatedFat stringByAppendingString:@"g"]];
[saturatedFatPercentLabel setStringValue:[NSString stringWithFormat:@"%i%%", percent]];
} else {
[saturatedFatLabel setStringValue:@"0g"];
[saturatedFatPercentLabel setStringValue:@"0%"];
}
NSString* cholesterol = [serving objectForKey:@"cholesterol"];
if(cholesterol) {
[cholesterolLabel setStringValue:[cholesterol stringByAppendingString:@"mg"]];
double cholValue = [cholesterol doubleValue];
int percent = (int)((cholValue / 300.0f) * 100);
[cholesterolPercentLabel setStringValue:[NSString stringWithFormat:@"%i%%", percent]];
} else {
[cholesterolLabel setStringValue:@"0mg"];
[cholesterolPercentLabel setStringValue:@"0%"];
}
NSString* sodium = [serving objectForKey:@"sodium"];
if(sodium) {
[sodiumLabel setStringValue:[sodium stringByAppendingString:@"mg"]];
double sodiumValue = [sodium doubleValue];
int percent = (int)((sodiumValue / 2400.0f) * 100);
[sodiumPercentLabel setStringValue:[NSString stringWithFormat:@"%i%%", percent]];
} else {
[sodiumLabel setStringValue:@"0mg"];
[sodiumPercentLabel setStringValue:@"0%"];
}
NSString* dietaryFiber = [serving objectForKey:@"fiber"];
if(dietaryFiber) {
[dietaryFiberLabel setStringValue:[dietaryFiber stringByAppendingString:@"g"]];
double fiberValue = [dietaryFiber doubleValue];
int percent = (int)((fiberValue / 25.0f) * 100);
[dietaryFiberPercentLabel setStringValue:[NSString stringWithFormat:@"%i%%", percent]];
} else {
[dietaryFiberLabel setStringValue:@"0g"];
[dietaryFiberPercentLabel setStringValue:@"0%"];
}
[sugarsLabel setStringValue:[[serving objectForKey:@"sugar"] stringByAppendingString:@"g"]];
NSString* vitaminAPercent = [serving objectForKey:@"vitamin_a"];
if(!vitaminAPercent) {
vitaminAPercent = @"0";
}
[vitaminAPercentLabel setStringValue:[vitaminAPercent stringByAppendingString:@"%"]];
NSString* vitaminCPercent = [serving objectForKey:@"vitamin_c"];
if(!vitaminCPercent) {
vitaminCPercent = @"0%";
}
[vitaminCPercentLabel setStringValue:[vitaminCPercent stringByAppendingString:@"%"]];
NSString* calcium = [serving objectForKey:@"calcium"];
if(!calcium) {
calcium = @"0";
}
[calciumLabel setStringValue:[calcium stringByAppendingString:@"%"]];
NSString* iron = [serving objectForKey:@"iron"];
if(!iron) {
iron = @"0";
}
[ironLabel setStringValue:[iron stringByAppendingString:@"%"]];
}
- (void)foodSelected:(FoodData *)data {
if (!updating) {
updating = YES;
[searching startAnimation:self];
selectedData = data;
if(!data.detailedDescription) {
[restManager obtainNutritionInformationWithID:data.foodID forTarget:self forSelector:@selector(performedMethodLoadForURL2:withResponseBody:)];
} else {
NSDictionary* serving = [selectedData.detailedDescription objectAtIndex:0];
[self displayDetailForServing:serving forFoodNamed:data.foodName];
[searching stopAnimation:self];
updating = NO;
}
updating = YES;
[searching startAnimation:self];
if(!infoDrawer) {
NSSize contentSize = NSMakeSize(300, 100);
infoDrawer = [[NSDrawer alloc] initWithContentSize:contentSize preferredEdge:NSMaxXEdge];
[infoDrawer setParentWindow:myParentWindow];
[infoDrawer setMinContentSize:contentSize];
[infoDrawer setContentView:infoView];
}
NSDrawerState state = [infoDrawer state];
if (NSDrawerOpeningState == state || NSDrawerOpenState == state) {
//[infoDrawer close];
} else {
[infoDrawer openOnEdge:NSMaxXEdge];
}
updating = NO;
}
}
- (NSInteger)numberOfItemsInComboBox:(NSComboBox *)aComboBox {
return [selectedData.detailedDescription count];
}
- (id)comboBox:(NSComboBox *)aComboBox objectValueForItemAtIndex:(NSInteger)itemIndex {
NSDictionary * data = [selectedData.detailedDescription objectAtIndex:itemIndex];
return [data objectForKey:@"serving_description"];
}
- (void)comboBoxWillPopUp:(NSNotification *)notification {
}
- (void)comboBoxWillDismiss:(NSNotification *)notification {
}
- (void)comboBoxSelectionDidChange:(NSNotification *)notification {
NSComboBox* box = (NSComboBox*)[notification object];
int selectionIndex = [box indexOfSelectedItem];
NSDictionary* serving = [selectedData.detailedDescription objectAtIndex:selectionIndex];
[self displayDetailForServing:serving forFoodNamed:selectedData.foodName];
}
- (void)comboBoxSelectionIsChanging:(NSNotification *)notification {
}
- (BOOL)control:(NSControl *)control textShouldEndEditing:(NSText *)fieldEditor {
[self searchForFood:self];
return YES;
}
@end