Skip to content

Commit

Permalink
Refactoring names and returning Event objects from HTTPEngine. Cleani…
Browse files Browse the repository at this point in the history
…ng up NSLog statements.
  • Loading branch information
jsakuda committed Jun 2, 2012
1 parent d3c2c62 commit 79ab23a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 33 deletions.
46 changes: 18 additions & 28 deletions HI Capacity/CalendarViewController.m
Expand Up @@ -34,6 +34,8 @@ - (void)viewWillAppear:(BOOL)animated {

- (NSArray*) calendarMonthView:(TKCalendarMonthView*)monthView marksFromDate:(NSDate*)startDate toDate:(NSDate*)lastDate {
if ([lastStartDate isSameDay:startDate] && [lastEndDate isSameDay:lastDate]) {
// Do not query again. The query dates match. This happens if the table is manually reloaded.
// The table will be manually reloaded after the async call to the Google Calendar API is returned.
return dataArray;
}

Expand All @@ -44,7 +46,6 @@ - (NSArray*) calendarMonthView:(TKCalendarMonthView*)monthView marksFromDate:(NS
lastEndDate = lastDate;

[dataArray removeAllObjects]; // clear the array, waiting for new data to return
// NSLog(@"CLEARING DATA ARRAY");

NSMutableDictionary *headerFields = [NSMutableDictionary dictionary];
[headerFields setValue:@"iOS" forKey:@"x-client-identifier"];
Expand All @@ -58,17 +59,15 @@ - (NSArray*) calendarMonthView:(TKCalendarMonthView*)monthView marksFromDate:(NS

NSDate *ds = [cal dateFromComponents:comp];

NSMutableArray *temp = [[NSMutableArray alloc] init];
NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] init];
NSMutableArray *datesArray = [[NSMutableArray alloc] init];
NSMutableDictionary *eventsDictionary = [[NSMutableDictionary alloc] init];

// Init offset components to increment days in the loop by one each time
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setDay:1];


while (YES) {
NSLog(@"!!! %@", ds);

while (YES) {
// Is the date beyond the last date? If so, exit the loop.
// NSOrderedDescending = the left value is greater than the right
if ([ds compare:lastDate] == NSOrderedDescending) {
Expand All @@ -77,31 +76,25 @@ - (NSArray*) calendarMonthView:(TKCalendarMonthView*)monthView marksFromDate:(NS
NSMutableArray *eventsFound = [self getEventsForDate:ds from:returnedEvents];

if ([eventsFound count] > 0) {
// [[self dataArray] addObject:[NSNumber numberWithBool:YES]];
// [[self dataDictionary] setObject:eventsFound forKey:currentDate];
[tempDict setObject:eventsFound forKey:ds];
[eventsDictionary setObject:eventsFound forKey:ds];

[temp addObject:[NSNumber numberWithBool:YES]];
// NSLog(@"%@ - Events found: %d", currentDate, [eventsFound count]);
// NSLog(@"ADDING YES");
[datesArray addObject:[NSNumber numberWithBool:YES]];
}
else {
// no events found
[temp addObject:[NSNumber numberWithBool:NO]];
// [[self dataArray] addObject:[NSNumber numberWithBool:NO]];
// NSLog(@"ADDING NO");
[datesArray addObject:[NSNumber numberWithBool:NO]];
}

// Increment day using offset components (ie, 1 day in this instance)
ds = [cal dateByAddingComponents:offsetComponents toDate:ds options:0];
}
[self setDataArray:temp];
[self setDataDictionary:tempDict];
[[self monthView] reload]; // reload the month, new events in
[self setDataArray:datesArray];
[self setDataDictionary:eventsDictionary];
[[self monthView] reload]; // reload the month view since new events were loaded
}
onError:^(NSError *error) {
NSLog(@"%@", error);
}];
}]; // end of completion block
return dataArray;
}
- (void) calendarMonthView:(TKCalendarMonthView*)monthView didSelectDate:(NSDate*)date{
Expand Down Expand Up @@ -136,15 +129,8 @@ - (UITableViewCell *) tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndex
UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

NSLog(@"%d", indexPath.row);

NSArray *ar = [dataDictionary objectForKey:[self.monthView dateSelected]];
// NSLog(@"%@", ar);
Event *event = [ar objectAtIndex:indexPath.row];
NSLog(@"%@", event);
// NSLog(@"%@", [[self monthView] dateSelected]);

// cell.textLabel.text = [ar objectAtIndex:indexPath.row];
cell.textLabel.text = [event summary];

return cell;
Expand All @@ -155,13 +141,17 @@ - (NSMutableArray *) getEventsForDate:(NSDate *)date from:(NSMutableArray *)quer
NSMutableArray *events = [[NSMutableArray alloc] init];

[queriedEvents enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop) {
Event *e = [[Event alloc] initWithDictionary:[queriedEvents objectAtIndex:index]];
Event *e = [queriedEvents objectAtIndex:index];

NSDate *startTime = [e startTime];
NSLog(@"%@", [e summary]);
if ([date isSameDay:startTime]) {
[events addObject:e];
}
else if ([startTime compare:date] == NSOrderedDescending) {
// events returned from Google Calendar are in ascending order
// if the event time is greater than the date we are checking, we can break out of the enumeration
*stop = YES;
}
}];

return events;
Expand Down
15 changes: 10 additions & 5 deletions HI Capacity/HTTPEngine.m
@@ -1,4 +1,5 @@
#import "HTTPEngine.h"
#import "Event.h"
@implementation HTTPEngine

-(MKNetworkOperation*) posts:
Expand Down Expand Up @@ -45,16 +46,20 @@ -(MKNetworkOperation*) eventsFrom:(NSDate *)startDate
ssl:YES];
[op onCompletion:^(MKNetworkOperation *completedOperation) {
NSDictionary *responseDictionary = [completedOperation responseJSON];
// NSLog(@"%@", [responseDictionary objectForKey:@"items"]);
NSMutableArray *events = [responseDictionary objectForKey:@"items"];
NSMutableArray *jsonEvents = [responseDictionary objectForKey:@"items"];

NSMutableArray *events = [[NSMutableArray alloc] initWithCapacity:[jsonEvents count]];

// NSMutableArray *events = [[NSMutableArray alloc] init];
[jsonEvents enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop) {
Event *e = [[Event alloc] initWithDictionary:[jsonEvents objectAtIndex:index]];
[events addObject:e];
}];

if([completedOperation isCachedResponse]) {
// NSLog(@"Data from cache %@", responseDictionary);
//NSLog(@"Data from cache %@", responseDictionary);
}
else {
// NSLog(@"Data from server %@", responseDictionary);
//NSLog(@"Data from server %@", responseDictionary);
}
completionBlock(events);
}
Expand Down

0 comments on commit 79ab23a

Please sign in to comment.