-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalendarViewController.m
248 lines (209 loc) · 8.3 KB
/
CalendarViewController.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
//
// CalendarViewController.m
// DailyCalendar
//
// Created by Tom Ryan on 14/08/10.
// Copyright 2010 MVSICHA•COM. All rights reserved.
//
#import "CalendarViewController.h"
#import "AppointmentView.h"
#import "AppointmentCell.h"
#import "NSDate+Extras.h"
#define kStartHour 8
#define kEndHour 21
@interface CalendarViewController(Private)
- (void)constructHours;
@end
@implementation CalendarViewController
@synthesize managedObjectContext;
@synthesize dayMatrix, hours, hoursArrayController;
@synthesize selectedHours;
- (void)loadView {
[super loadView];
[self constructHours];
[self.dayMatrix setCellClass:[AppointmentCell class]];
[self.dayMatrix removeRow:0];
[self.dayMatrix setAutosizesCells:NO];
[self.dayMatrix setBackgroundColor:[NSColor yellowColor]];
[self.hoursArrayController addObserver:self forKeyPath:@"selectedObjects" options:0 context:NULL];
[self loadAppointments];
}
- (void)constructHours {
NSMutableArray *tempAry = [NSMutableArray array];
//starting 15 minutes earlier than what we want because of dateWithAddedMinutes.
NSDate *theDate = [[NSDate date] timeWithHour:kStartHour-1 andMinute:45];
int totalNumberOfCells = (kEndHour-(kStartHour)) * 4;//because we are in 15 minute increments
NSSize cellSize = [self.dayMatrix cellSize];
for(int i=0;i<totalNumberOfCells;i++) {
float cellX = (cellSize.height * i) - cellSize.height;
NSDictionary *cellDict = [NSDictionary dictionaryWithObjectsAndKeys:
theDate, @"date",
[NSNumber numberWithFloat:cellX], @"xvalue",
[NSNumber numberWithInt:i], @"index",
nil];
[tempAry addObject:cellDict];
theDate = [theDate dateWithAddedMinutes:15];
}
self.hours = tempAry;
}
#pragma mark Appointments
- (void)loadAppointments {
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"Appointment" inManagedObjectContext:self.managedObjectContext]];
NSSortDescriptor *sd = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObject:sd]];
[sd release];
NSError *error = nil;
NSArray *appts = [self.managedObjectContext executeFetchRequest:request error:&error];
[request release];
for(NSManagedObject *a in appts) {
[self drawAppointment:a];
}
}
- (void)drawAppointment:(NSManagedObject *)appt {
CGSize cellSize = [dayMatrix cellSize];
NSDate *date = [appt valueForKey:@"date"];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"date=%@", date];
NSArray *dates = [self.hours filteredArrayUsingPredicate:pred];
float x = [[[dates valueForKey:@"xvalue"] lastObject] floatValue];
float y = cellSize.height * [[appt valueForKey:@"blockCount"] floatValue];
NSRect r = NSMakeRect(50, x , cellSize.width-50, y);
AppointmentView *v = [[[AppointmentView alloc] initWithFrame:r] autorelease];
v.appointment = appt;
[v setAutoresizingMask:NSViewWidthSizable|NSViewMaxXMargin];
v.eventDelegate = self;
[dayMatrix addSubview:v];
[v setNeedsDisplay:YES];
}
#pragma mark Selection
/*
Think of this as the mouse handler for a new appointment.
*/
- (void)setSelectedHours:(NSMutableArray *)someHours {
if(![selectedHours isEqualToArray:someHours]) {
[selectedHours release];
selectedHours = [someHours retain];
}
NSArray *matrixSelectedCells = [self.dayMatrix selectedCells];
[self.dayMatrix deselectAllCells];
int firstIndex = [[self.dayMatrix cells] indexOfObject:[matrixSelectedCells objectAtIndex:0]];
int lastIndex = [[self.dayMatrix cells] indexOfObject:[matrixSelectedCells lastObject]];
/* we don't want just a single selection */
if(firstIndex == lastIndex) return;
float appointmentPadding = 50.0f;
NSRect appointmentRect = [self.dayMatrix frame];
float cellHeight = [self.dayMatrix cellSize].height;
float appointmentY = (firstIndex * cellHeight);
float appointmentHeight = [matrixSelectedCells count] * cellHeight;
appointmentRect.origin.y = appointmentY;
appointmentRect.size.height = appointmentHeight;
appointmentRect.origin.x = appointmentPadding;
appointmentRect.size.width -= appointmentPadding;
AppointmentView *appointmentView = [[[AppointmentView alloc] initWithFrame:appointmentRect] autorelease];
[appointmentView setAutoresizingMask:NSViewWidthSizable|NSViewMaxXMargin];
appointmentView.eventDelegate = self;
// create the appointment
NSManagedObject *appointment = [NSEntityDescription insertNewObjectForEntityForName:@"Appointment" inManagedObjectContext:self.managedObjectContext];
NSDictionary *selectedDate = [[self.hours objectAtIndex:firstIndex+1] objectForKey:@"date"];
[appointment setValue:selectedDate forKey:@"date"];
[appointment setValue:[NSNumber numberWithInt:lastIndex-(firstIndex-1)] forKey:@"blockCount"];
appointmentView.appointment = appointment;
[self.dayMatrix addSubview:appointmentView];
}
#pragma mark AppointmentEventDelegate
- (void)mouseDown:(NSEvent *)anEvent withEventView:(AppointmentView *)eventView {
// handle selection
NSPredicate *apptPredicate = [NSPredicate predicateWithFormat:@"class=%@", [AppointmentView class]];
NSArray *appts = [[self.dayMatrix subviews] filteredArrayUsingPredicate:apptPredicate];
for(AppointmentView *a in appts) {
if(![a isEqual:eventView])
a.selected = NO;
}
[eventView toggleSelected];
}
- (void)moveAppointment:(AppointmentView *)apptView withEvent:(NSEvent *)anEvent {
NSPoint newLocation;
NSPoint dragLocation = [apptView convertPoint:[anEvent locationInWindow] fromView:nil];
while(1) {
anEvent = [[apptView window] nextEventMatchingMask:NSLeftMouseDraggedMask|NSLeftMouseUpMask];
newLocation = [self.dayMatrix convertPoint:[anEvent locationInWindow] fromView:nil];
float y = newLocation.y-dragLocation.y;
y = y < 0 ? 0 : y; // make sure we're >= 0
CGRect appointmentFrame = apptView.frame;
appointmentFrame.origin.y = y;
apptView.frame = appointmentFrame;
if([anEvent type] == NSLeftMouseUp) {
float fy = appointmentFrame.origin.y;
NSSize cellSize = [dayMatrix cellSize];
for(int i=0;i<[self.hours count];i++) {
NSDictionary *d = [self.hours objectAtIndex:i];
float df = [[d objectForKey:@"xvalue"] floatValue];
if(df >= fy && df <=fy+cellSize.height) {
appointmentFrame.origin.y = df;
apptView.frame = appointmentFrame;
NSDictionary *d0 = [self.hours objectAtIndex:i];
NSDate *cellDate = [d0 objectForKey:@"date"];
[apptView.appointment setValue:cellDate forKey:@"date"];
[self saveEvent:nil];
break;
}
}
break;
}
}
}
- (void)resizeAppointment:(AppointmentView *)apptView withEvent:(NSEvent *)event resizing:(MVResizing)resizing {
NSPoint newLocation;
while(1) {
event = [[apptView window] nextEventMatchingMask:NSLeftMouseDraggedMask|NSLeftMouseUpMask];
newLocation = [apptView convertPoint:[event locationInWindow] fromView:nil];
CGRect appointmentFrame = apptView.frame;
if(resizing == MVResizingUp) {
appointmentFrame.size.height = newLocation.y;
} else if (resizing == MVResizingDown) {
appointmentFrame.size.height -= newLocation.y;
appointmentFrame.origin.y += newLocation.y;
}
apptView.frame = appointmentFrame;
if([event type] == NSLeftMouseUp) {
float fy = appointmentFrame.origin.y;
float blockSize = ceil(appointmentFrame.size.height/20.0f);
NSSize cellSize = [dayMatrix cellSize];
for(int i=0;i<[self.hours count];i++) {
NSDictionary *d = [self.hours objectAtIndex:i];
float df = [[d objectForKey:@"xvalue"] floatValue];
if(df >= fy && df <=fy+cellSize.height) {
appointmentFrame.origin.y = df;
apptView.frame = appointmentFrame;
NSDictionary *d0 = [self.hours objectAtIndex:i];
NSDate *cellDate = [d0 objectForKey:@"date"];
[apptView.appointment setValue:cellDate forKey:@"date"];
[apptView.appointment setValue:[NSNumber numberWithInt:blockSize] forKey:@"blockCount"];
[apptView setNeedsDisplay:YES];
break;
}
}
break;
}
}
}
- (IBAction)saveEvent:(id)sender {
NSError *error = nil;
if(![self.managedObjectContext save:&error]) {
NSLog(@"Could not save appointment!", error);
}
}
- (IBAction)deleteEvent:(id)sender {
AppointmentView *ev = (AppointmentView *)sender;
[self.managedObjectContext deleteObject:ev.appointment];
[ev removeFromSuperview];
[self saveEvent:nil];
}
- (void)dealloc {
[managedObjectContext release];
[hoursArrayController release];
[hours release];
[dayMatrix release];
[super dealloc];
}
@end