-
Notifications
You must be signed in to change notification settings - Fork 344
/
Copy pathKalViewController.m
228 lines (187 loc) · 5.59 KB
/
KalViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
* Copyright (c) 2009 Keith Lazuka
* License: http://www.opensource.org/licenses/mit-license.html
*/
#import "KalViewController.h"
#import "KalLogic.h"
#import "KalDataSource.h"
#import "KalDate.h"
#import "KalPrivate.h"
#define PROFILER 0
#if PROFILER
#include <mach/mach_time.h>
#include <time.h>
#include <math.h>
void mach_absolute_difference(uint64_t end, uint64_t start, struct timespec *tp)
{
uint64_t difference = end - start;
static mach_timebase_info_data_t info = {0,0};
if (info.denom == 0)
mach_timebase_info(&info);
uint64_t elapsednano = difference * (info.numer / info.denom);
tp->tv_sec = elapsednano * 1e-9;
tp->tv_nsec = elapsednano - (tp->tv_sec * 1e9);
}
#endif
NSString *const KalDataSourceChangedNotification = @"KalDataSourceChangedNotification";
@interface KalViewController ()
@property (nonatomic, retain, readwrite) NSDate *initialDate;
@property (nonatomic, retain, readwrite) NSDate *selectedDate;
- (KalView*)calendarView;
@end
@implementation KalViewController
@synthesize dataSource, delegate, initialDate, selectedDate;
- (id)initWithSelectedDate:(NSDate *)date
{
if ((self = [super init])) {
logic = [[KalLogic alloc] initForDate:date];
self.initialDate = date;
self.selectedDate = date;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(significantTimeChangeOccurred) name:UIApplicationSignificantTimeChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadData) name:KalDataSourceChangedNotification object:nil];
}
return self;
}
- (id)init
{
return [self initWithSelectedDate:[NSDate date]];
}
- (KalView*)calendarView { return (KalView*)self.view; }
- (void)setDataSource:(id<KalDataSource>)aDataSource
{
if (dataSource != aDataSource) {
dataSource = aDataSource;
tableView.dataSource = dataSource;
}
}
- (void)setDelegate:(id<UITableViewDelegate>)aDelegate
{
if (delegate != aDelegate) {
delegate = aDelegate;
tableView.delegate = delegate;
}
}
- (void)clearTable
{
[dataSource removeAllItems];
[tableView reloadData];
}
- (void)reloadData
{
[dataSource presentingDatesFrom:logic.fromDate to:logic.toDate delegate:self];
}
- (void)significantTimeChangeOccurred
{
[[self calendarView] jumpToSelectedMonth];
[self reloadData];
}
// -----------------------------------------
#pragma mark KalViewDelegate protocol
- (void)didSelectDate:(KalDate *)date
{
self.selectedDate = [date NSDate];
NSDate *from = [[date NSDate] cc_dateByMovingToBeginningOfDay];
NSDate *to = [[date NSDate] cc_dateByMovingToEndOfDay];
[self clearTable];
[dataSource loadItemsFromDate:from toDate:to];
[tableView reloadData];
[tableView flashScrollIndicators];
}
- (void)showPreviousMonth
{
[self clearTable];
[logic retreatToPreviousMonth];
[[self calendarView] slideDown];
[self reloadData];
}
- (void)showFollowingMonth
{
[self clearTable];
[logic advanceToFollowingMonth];
[[self calendarView] slideUp];
[self reloadData];
}
// -----------------------------------------
#pragma mark KalDataSourceCallbacks protocol
- (void)loadedDataSource:(id<KalDataSource>)theDataSource;
{
NSArray *markedDates = [theDataSource markedDatesFrom:logic.fromDate to:logic.toDate];
NSMutableArray *dates = [[markedDates mutableCopy] autorelease];
for (int i=0; i<[dates count]; i++)
[dates replaceObjectAtIndex:i withObject:[KalDate dateFromNSDate:[dates objectAtIndex:i]]];
[[self calendarView] markTilesForDates:dates];
[self didSelectDate:self.calendarView.selectedDate];
}
// ---------------------------------------
#pragma mark -
- (void)showAndSelectDate:(NSDate *)date
{
if ([[self calendarView] isSliding])
return;
[logic moveToMonthForDate:date];
#if PROFILER
uint64_t start, end;
struct timespec tp;
start = mach_absolute_time();
#endif
[[self calendarView] jumpToSelectedMonth];
#if PROFILER
end = mach_absolute_time();
mach_absolute_difference(end, start, &tp);
printf("[[self calendarView] jumpToSelectedMonth]: %.1f ms\n", tp.tv_nsec / 1e6);
#endif
[[self calendarView] selectDate:[KalDate dateFromNSDate:date]];
[self reloadData];
}
- (NSDate *)selectedDate
{
return [self.calendarView.selectedDate NSDate];
}
// -----------------------------------------------------------------------------------
#pragma mark UIViewController
- (void)didReceiveMemoryWarning
{
self.initialDate = self.selectedDate; // must be done before calling super
[super didReceiveMemoryWarning];
}
- (void)loadView
{
if (!self.title)
self.title = @"Calendar";
KalView *kalView = [[[KalView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] delegate:self logic:logic] autorelease];
self.view = kalView;
tableView = kalView.tableView;
tableView.dataSource = dataSource;
tableView.delegate = delegate;
[tableView retain];
[kalView selectDate:[KalDate dateFromNSDate:self.initialDate]];
[self reloadData];
}
- (void)viewDidUnload
{
[super viewDidUnload];
[tableView release];
tableView = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[tableView reloadData];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[tableView flashScrollIndicators];
}
#pragma mark -
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationSignificantTimeChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:KalDataSourceChangedNotification object:nil];
[initialDate release];
[selectedDate release];
[logic release];
[tableView release];
[super dealloc];
}
@end