|
| 1 | +/* |
| 2 | + * Copyright (c) 2010 Keith Lazuka |
| 3 | + * License: http://www.opensource.org/licenses/mit-license.html |
| 4 | + */ |
| 5 | + |
| 6 | +#import "EventKitDataSource.h" |
| 7 | +#import <EventKit/EventKit.h> |
| 8 | + |
| 9 | +static BOOL IsDateBetweenInclusive(NSDate *date, NSDate *begin, NSDate *end) |
| 10 | +{ |
| 11 | + return [date compare:begin] != NSOrderedAscending && [date compare:end] != NSOrderedDescending; |
| 12 | +} |
| 13 | + |
| 14 | +@interface EventKitDataSource () |
| 15 | +- (NSArray *)eventsFrom:(NSDate *)fromDate to:(NSDate *)toDate; |
| 16 | +- (NSArray *)markedDatesFrom:(NSDate *)fromDate to:(NSDate *)toDate; |
| 17 | +@end |
| 18 | + |
| 19 | +@implementation EventKitDataSource |
| 20 | + |
| 21 | ++ (EventKitDataSource *)dataSource |
| 22 | +{ |
| 23 | + return [[[[self class] alloc] init] autorelease]; |
| 24 | +} |
| 25 | + |
| 26 | +- (id)init |
| 27 | +{ |
| 28 | + if ((self = [super init])) { |
| 29 | + eventStore = [[EKEventStore alloc] init]; |
| 30 | + events = [[NSMutableArray alloc] init]; |
| 31 | + items = [[NSMutableArray alloc] init]; |
| 32 | + } |
| 33 | + return self; |
| 34 | +} |
| 35 | + |
| 36 | +- (EKEvent *)eventAtIndexPath:(NSIndexPath *)indexPath |
| 37 | +{ |
| 38 | + return [items objectAtIndex:indexPath.row]; |
| 39 | +} |
| 40 | + |
| 41 | +#pragma mark UITableViewDataSource protocol conformance |
| 42 | + |
| 43 | +- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath |
| 44 | +{ |
| 45 | + static NSString *identifier = @"MyCell"; |
| 46 | + UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; |
| 47 | + if (!cell) { |
| 48 | + cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease]; |
| 49 | + cell.selectionStyle = UITableViewCellSelectionStyleNone; |
| 50 | + cell.imageView.contentMode = UIViewContentModeScaleAspectFill; |
| 51 | + } |
| 52 | + |
| 53 | + EKEvent *event = [self eventAtIndexPath:indexPath]; |
| 54 | + cell.textLabel.text = event.title; |
| 55 | + return cell; |
| 56 | +} |
| 57 | + |
| 58 | +- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section |
| 59 | +{ |
| 60 | + return [items count]; |
| 61 | +} |
| 62 | + |
| 63 | +#pragma mark KalDataSource protocol conformance |
| 64 | + |
| 65 | +- (void)presentingDatesFrom:(NSDate *)fromDate to:(NSDate *)toDate delegate:(id<KalDataSourceCallbacks>)delegate |
| 66 | +{ |
| 67 | + [events removeAllObjects]; |
| 68 | + NSLog(@"Fetching events from EventKit between %@ and %@...", fromDate, toDate); |
| 69 | + NSPredicate *predicate = [eventStore predicateForEventsWithStartDate:fromDate endDate:toDate calendars:nil]; |
| 70 | + [events addObjectsFromArray:[eventStore eventsMatchingPredicate:predicate]]; |
| 71 | + [delegate loadedDataSource:self]; |
| 72 | +} |
| 73 | + |
| 74 | +- (NSArray *)markedDatesFrom:(NSDate *)fromDate to:(NSDate *)toDate |
| 75 | +{ |
| 76 | + return [[self eventsFrom:fromDate to:toDate] valueForKeyPath:@"startDate"]; |
| 77 | +} |
| 78 | + |
| 79 | +- (void)loadItemsFromDate:(NSDate *)fromDate toDate:(NSDate *)toDate |
| 80 | +{ |
| 81 | + [items addObjectsFromArray:[self eventsFrom:fromDate to:toDate]]; |
| 82 | +} |
| 83 | + |
| 84 | +- (void)removeAllItems |
| 85 | +{ |
| 86 | + [items removeAllObjects]; |
| 87 | +} |
| 88 | + |
| 89 | +#pragma mark - |
| 90 | + |
| 91 | +- (NSArray *)eventsFrom:(NSDate *)fromDate to:(NSDate *)toDate |
| 92 | +{ |
| 93 | + NSMutableArray *matches = [NSMutableArray array]; |
| 94 | + for (EKEvent *event in events) |
| 95 | + if (IsDateBetweenInclusive(event.startDate, fromDate, toDate)) |
| 96 | + [matches addObject:event]; |
| 97 | + |
| 98 | + return matches; |
| 99 | +} |
| 100 | + |
| 101 | +- (void)dealloc |
| 102 | +{ |
| 103 | + [eventStore release]; |
| 104 | + [items release]; |
| 105 | + [events release]; |
| 106 | + [super dealloc]; |
| 107 | +} |
| 108 | + |
| 109 | +@end |
0 commit comments