Skip to content

Commit 843d79b

Browse files
committed
Added EventKit example
1 parent d9b40aa commit 843d79b

10 files changed

+808
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
src/build/
22
Examples/Holiday/build/
3+
Examples/NativeCal/build/
34

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright (c) 2010 Keith Lazuka
3+
* License: http://www.opensource.org/licenses/mit-license.html
4+
*/
5+
6+
#import "Kal.h"
7+
8+
@class EKEventStore, EKEvent;
9+
10+
@interface EventKitDataSource : NSObject <KalDataSource>
11+
{
12+
EKEventStore *eventStore;
13+
NSMutableArray *items;
14+
NSMutableArray *events;
15+
}
16+
17+
+ (EventKitDataSource *)dataSource;
18+
- (EKEvent *)eventAtIndexPath:(NSIndexPath *)indexPath; // exposed for client so that it can implement the UITableViewDelegate protocol.
19+
20+
@end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (c) 2010 Keith Lazuka
3+
* License: http://www.opensource.org/licenses/mit-license.html
4+
*/
5+
6+
#import <UIKit/UIKit.h>
7+
8+
/*
9+
* NativeCalAppDelegate
10+
* --------------------
11+
*
12+
* This demo app shows how to use Kal to display events
13+
* from EventKit (Apple's native calendar database).
14+
*
15+
*/
16+
17+
@class KalViewController;
18+
19+
@interface NativeCalAppDelegate : NSObject <UIApplicationDelegate, UITableViewDelegate>
20+
{
21+
UIWindow *window;
22+
UINavigationController *navController;
23+
KalViewController *kal;
24+
id dataSource;
25+
}
26+
27+
@property (nonatomic, retain) IBOutlet UIWindow *window;
28+
29+
@end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright (c) 2010 Keith Lazuka
3+
* License: http://www.opensource.org/licenses/mit-license.html
4+
*/
5+
6+
#import "NativeCalAppDelegate.h"
7+
#import "EventKitDataSource.h"
8+
#import "Kal.h"
9+
10+
#import <EventKit/EventKit.h>
11+
#import <EventKitUI/EventKitUI.h>
12+
13+
@implementation NativeCalAppDelegate
14+
15+
@synthesize window;
16+
17+
- (void)applicationDidFinishLaunching:(UIApplication *)application
18+
{
19+
/*
20+
* Kal Initialization
21+
*
22+
* When the calendar is first displayed to the user, Kal will automatically select today's date.
23+
* If your application requires an arbitrary starting date, use -[KalViewController initWithSelectedDate:]
24+
* instead of -[KalViewController init].
25+
*/
26+
kal = [[KalViewController alloc] init];
27+
28+
/*
29+
* Kal Configuration
30+
*
31+
*/
32+
kal.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Today" style:UIBarButtonItemStyleBordered target:self action:@selector(showAndSelectToday)] autorelease];
33+
kal.delegate = self;
34+
dataSource = [[EventKitDataSource alloc] init];
35+
kal.dataSource = dataSource;
36+
37+
// Setup the navigation stack and display it.
38+
navController = [[UINavigationController alloc] initWithRootViewController:kal];
39+
[window addSubview:navController.view];
40+
[window makeKeyAndVisible];
41+
}
42+
43+
// Action handler for the navigation bar's right bar button item.
44+
- (void)showAndSelectToday
45+
{
46+
[kal showAndSelectDate:[NSDate date]];
47+
}
48+
49+
#pragma mark UITableViewDelegate protocol conformance
50+
51+
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
52+
{
53+
// Display a details screen for the selected event/row.
54+
EKEventViewController *vc = [[[EKEventViewController alloc] init] autorelease];
55+
vc.event = [dataSource eventAtIndexPath:indexPath];
56+
vc.allowsEditing = NO;
57+
[navController pushViewController:vc animated:YES];
58+
}
59+
60+
#pragma mark -
61+
62+
- (void)dealloc
63+
{
64+
[kal release];
65+
[dataSource release];
66+
[window release];
67+
[navController release];
68+
[super dealloc];
69+
}
70+
71+
@end

0 commit comments

Comments
 (0)