forked from smontgomerie/Appcelerator-Calendar-Module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TiCalendarView.m
304 lines (239 loc) · 6.88 KB
/
TiCalendarView.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2010 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
#import "TiCalendarView.h"
#import "KLGridView.h"
#import "KLTile.h"
#import "KLDateSort.h"
#import "MroBinarySearch.h"
#import "TiUtils.h"
@implementation TiCalendarView
@synthesize dates, month;
- (id) init
{
// NSLog(@"Init view");
self = [super init];
if (self != nil)
{
calendarView = [[[KLCalendarView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 360) delegate:self] autorelease];
//myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0,260,320,160) style:UITableViewStylePlain];
id c = [self.proxy valueForUndefinedKey:@"calendarColor"];
if ( c != nil )
{
NSLog(@"Setting color: %@", c);
TiColor *color = [TiUtils colorValue:c];
[calendarView setBackgroundColor:[color _color]];
}
NSLog(@"Init");
//myTableView.dataSource = self;
//myTableView.delegate = self;
UIView *myHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0,0,myTableView.frame.size.width , 20)];
c = [self.proxy valueForUndefinedKey:@"headerColor"];
if ( c != nil )
{
TiColor *color = [TiUtils colorValue:c];
myHeaderView.backgroundColor = [color _color];
}
else
{
myHeaderView.backgroundColor = [UIColor grayColor];
}
//[myTableView setTableHeaderView:myHeaderView];
// [self addSubview:myTableView];
[self addSubview:calendarView];
//[self bringSubviewToFront:myTableView];
}
return self;
}
/*
-(UIView*) calendar
{
NSLog(@"Build view");
if (map==nil)
{
map = [[MKMapView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 360)];
map.delegate = self;
map.userInteractionEnabled = YES;
map.showsUserLocation = YES; // defaults
map.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
[self addSubview:map];
}
return map;
}
*/
#pragma mark tableViewDelegate Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// [cell setText:@"No Data For Now"];
return cell;
}
/*----- Calendar Delegates -----> */
- (void)calendarView:(KLCalendarView *)calView tappedTile:(KLTile *)aTile{
NSLog(@"Date Selected is %@",[aTile date]);
[aTile flash];
if(tile == nil)
{
tile = aTile;
}
else
{
[tile restoreBackgroundColor];
// tile.backgroundColor = calendarView.backgroundColor;
tile = aTile;
}
NSMutableDictionary* data = [NSMutableDictionary dictionaryWithObjectsAndKeys:
[[aTile date] toNSDate],@"date",
nil];
[self.proxy fireEvent:@"dateSelected" withObject: data propagate:NO];
// [self _fireEventToListener:@"dateSelected" withObject:[aTile date] listener:callback thisObject:nil];
}
- (KLTile *)calendarView:(KLCalendarView *)calView createTileForDate:(KLDate *)date{
//NSLog(@"createTileForDate: %@", date);
CheckmarkTile *t = [[CheckmarkTile alloc] init];
t.checkmarked = NO;//based on any condition you can checkMark a tile
bool checked = [self dateChecked: date];
/*
int index = [dates indexOfObjectPassingTest:^(id obj, NSUInteger idx, BOOL *stop) {
if ( [date compareWithNSDate: ((NSDate*) obj)] == NSOrderedSame)
{
// NSLog(@"Same %@ with %@", date, obj);
*stop =YES;
return YES;
}
return NO;
}];
*/
// NSLog(@"index: %d", index);
if ( checked )
{
((CheckmarkTile*) t).checkmarked = YES;
[t setNeedsDisplay];
}
// t.backgroundColor = calendarView.backgroundColor;
return t;
}
- (void)didChangeMonths{
UIView *clip = calendarView.superview;
if (!clip)
return;
CGRect f = clip.frame;
NSInteger weeks = [calendarView selectedMonthNumberOfWeeks];
CGFloat adjustment = 0.f;
switch (weeks) {
case 4:
adjustment = (92/321)*360+30;
break;
case 5:
adjustment = (46/321)*360;
break;
case 6:
adjustment = 0.f;
break;
default:
break;
}
f.size.height = 360 - adjustment;
clip.frame = f;
CGRect f2 = CGRectMake(0,260-adjustment,320,160+adjustment);
myTableView.frame = f2;
[self bringSubviewToFront:myTableView];
tile = nil;
}
-(bool) dateChecked: (KLDate*) date
{
// NSLog(@"datechecked %@", date);
/*
bool found=false;
for ( int i=0; i< dates.count; i++)
{
NSDate* obj = [dates objectAtIndex:i];
// NSLog(@"Comparing %@ with %@", date, obj);
if ( [date compareWithNSDate: ((NSDate*) obj)] == NSOrderedSame)
{
found = TRUE;
break;
}
if (found) break;
}
NSLog(@"index: %d", found);
*/
MroBinarySearch* binarySearch = [[MroBinarySearch alloc] initWithArray:dates];
NSInteger index= [binarySearch binarySearch:date usingFunction: KLDateSort context:NULL];
// NSLog(@"bs1 index: %d", index);
/* NSInteger bs1 = [dates binarySearch:[date toNSDate]];
NSLog(@"bs1 index: %d", bs1);
NSInteger bs = [dates binarySearch:date usingFunction:KLDateSort context:NULL];
NSLog(@"bs index: %d", bs);
*/
return ( index >=0 && dates.count > 0 );
}
- (void) setDatesArray: (NSArray*) datesArray
{
ENSURE_TYPE(datesArray,NSArray);
NSLog(@"setdatesArray %d objects", datesArray.count);
dates = [NSMutableArray arrayWithCapacity:datesArray.count];
if ( datesArray.count > 0 )
{
for (int i=0; i<datesArray.count; i++) {
NSDate* date = [datesArray objectAtIndex:i];
ENSURE_TYPE(date,NSDate);
[dates addObject: date];
}
}
[dates retain];
// NSLog(@"Dates: %@", dates);
// NSLog(@"Retain Count: %d, %d", dates.retainCount, [[dates objectAtIndex:0] retainCount]);
KLGridView* grid = calendarView.grid ;
for ( KLTile* t in grid.tiles )
{
((CheckmarkTile*) t).checkmarked = [self dateChecked: t.date];
}
}
- (void) setMonth: (NSNumber*) value
{
self.month = value;
NSLog(@"%@", value);
}
- (void) setCalendarColor_: (id) color
{
TiColor* c = [TiUtils colorValue:color];
UIColor * newColor = [c _color];
NSLog(@"Set color %@", c);
if ( newColor != nil)
{
[calendarView setBackgroundColor:newColor];
}
KLGridView* grid = calendarView.grid ;
for ( KLTile* t in grid.tiles )
{
t.backgroundColor = newColor;
}
}
- (void) setHeaderColor_: (id) color
{
UIColor * newColor = [[TiUtils colorValue:color] _color];
if ( newColor != nil)
{
// [myHeaderView setBackgroundColor:newColor];
}
}
-(void)_destroy
{
NSLog(@"destroy");
RELEASE_TO_NIL(dates);
}
@end