-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
808 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
src/build/ | ||
Examples/Holiday/build/ | ||
Examples/NativeCal/build/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.