Skip to content

Commit

Permalink
Added EventKit example
Browse files Browse the repository at this point in the history
  • Loading branch information
klazuka committed Jul 7, 2010
1 parent d9b40aa commit 843d79b
Show file tree
Hide file tree
Showing 10 changed files with 808 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
src/build/
Examples/Holiday/build/
Examples/NativeCal/build/

20 changes: 20 additions & 0 deletions Examples/NativeCal/Classes/EventKitDataSource.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (c) 2010 Keith Lazuka
* License: http://www.opensource.org/licenses/mit-license.html
*/

#import "Kal.h"

@class EKEventStore, EKEvent;

@interface EventKitDataSource : NSObject <KalDataSource>
{
EKEventStore *eventStore;
NSMutableArray *items;
NSMutableArray *events;
}

+ (EventKitDataSource *)dataSource;
- (EKEvent *)eventAtIndexPath:(NSIndexPath *)indexPath; // exposed for client so that it can implement the UITableViewDelegate protocol.

@end
109 changes: 109 additions & 0 deletions Examples/NativeCal/Classes/EventKitDataSource.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Copyright (c) 2010 Keith Lazuka
* License: http://www.opensource.org/licenses/mit-license.html
*/

#import "EventKitDataSource.h"
#import <EventKit/EventKit.h>

static BOOL IsDateBetweenInclusive(NSDate *date, NSDate *begin, NSDate *end)
{
return [date compare:begin] != NSOrderedAscending && [date compare:end] != NSOrderedDescending;
}

@interface EventKitDataSource ()
- (NSArray *)eventsFrom:(NSDate *)fromDate to:(NSDate *)toDate;
- (NSArray *)markedDatesFrom:(NSDate *)fromDate to:(NSDate *)toDate;
@end

@implementation EventKitDataSource

+ (EventKitDataSource *)dataSource
{
return [[[[self class] alloc] init] autorelease];
}

- (id)init
{
if ((self = [super init])) {
eventStore = [[EKEventStore alloc] init];
events = [[NSMutableArray alloc] init];
items = [[NSMutableArray alloc] init];
}
return self;
}

- (EKEvent *)eventAtIndexPath:(NSIndexPath *)indexPath
{
return [items objectAtIndex:indexPath.row];
}

#pragma mark UITableViewDataSource protocol conformance

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"MyCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
}

EKEvent *event = [self eventAtIndexPath:indexPath];
cell.textLabel.text = event.title;
return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [items count];
}

#pragma mark KalDataSource protocol conformance

- (void)presentingDatesFrom:(NSDate *)fromDate to:(NSDate *)toDate delegate:(id<KalDataSourceCallbacks>)delegate
{
[events removeAllObjects];
NSLog(@"Fetching events from EventKit between %@ and %@...", fromDate, toDate);
NSPredicate *predicate = [eventStore predicateForEventsWithStartDate:fromDate endDate:toDate calendars:nil];
[events addObjectsFromArray:[eventStore eventsMatchingPredicate:predicate]];
[delegate loadedDataSource:self];
}

- (NSArray *)markedDatesFrom:(NSDate *)fromDate to:(NSDate *)toDate
{
return [[self eventsFrom:fromDate to:toDate] valueForKeyPath:@"startDate"];
}

- (void)loadItemsFromDate:(NSDate *)fromDate toDate:(NSDate *)toDate
{
[items addObjectsFromArray:[self eventsFrom:fromDate to:toDate]];
}

- (void)removeAllItems
{
[items removeAllObjects];
}

#pragma mark -

- (NSArray *)eventsFrom:(NSDate *)fromDate to:(NSDate *)toDate
{
NSMutableArray *matches = [NSMutableArray array];
for (EKEvent *event in events)
if (IsDateBetweenInclusive(event.startDate, fromDate, toDate))
[matches addObject:event];

return matches;
}

- (void)dealloc
{
[eventStore release];
[items release];
[events release];
[super dealloc];
}

@end
29 changes: 29 additions & 0 deletions Examples/NativeCal/Classes/NativeCalAppDelegate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2010 Keith Lazuka
* License: http://www.opensource.org/licenses/mit-license.html
*/

#import <UIKit/UIKit.h>

/*
* NativeCalAppDelegate
* --------------------
*
* This demo app shows how to use Kal to display events
* from EventKit (Apple's native calendar database).
*
*/

@class KalViewController;

@interface NativeCalAppDelegate : NSObject <UIApplicationDelegate, UITableViewDelegate>
{
UIWindow *window;
UINavigationController *navController;
KalViewController *kal;
id dataSource;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;

@end
71 changes: 71 additions & 0 deletions Examples/NativeCal/Classes/NativeCalAppDelegate.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright (c) 2010 Keith Lazuka
* License: http://www.opensource.org/licenses/mit-license.html
*/

#import "NativeCalAppDelegate.h"
#import "EventKitDataSource.h"
#import "Kal.h"

#import <EventKit/EventKit.h>
#import <EventKitUI/EventKitUI.h>

@implementation NativeCalAppDelegate

@synthesize window;

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
/*
* Kal Initialization
*
* When the calendar is first displayed to the user, Kal will automatically select today's date.
* If your application requires an arbitrary starting date, use -[KalViewController initWithSelectedDate:]
* instead of -[KalViewController init].
*/
kal = [[KalViewController alloc] init];

/*
* Kal Configuration
*
*/
kal.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Today" style:UIBarButtonItemStyleBordered target:self action:@selector(showAndSelectToday)] autorelease];
kal.delegate = self;
dataSource = [[EventKitDataSource alloc] init];
kal.dataSource = dataSource;

// Setup the navigation stack and display it.
navController = [[UINavigationController alloc] initWithRootViewController:kal];
[window addSubview:navController.view];
[window makeKeyAndVisible];
}

// Action handler for the navigation bar's right bar button item.
- (void)showAndSelectToday
{
[kal showAndSelectDate:[NSDate date]];
}

#pragma mark UITableViewDelegate protocol conformance

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Display a details screen for the selected event/row.
EKEventViewController *vc = [[[EKEventViewController alloc] init] autorelease];
vc.event = [dataSource eventAtIndexPath:indexPath];
vc.allowsEditing = NO;
[navController pushViewController:vc animated:YES];
}

#pragma mark -

- (void)dealloc
{
[kal release];
[dataSource release];
[window release];
[navController release];
[super dealloc];
}

@end
Loading

0 comments on commit 843d79b

Please sign in to comment.