-
Notifications
You must be signed in to change notification settings - Fork 338
/
PDTSimpleCalendarViewController.m
641 lines (492 loc) · 23 KB
/
PDTSimpleCalendarViewController.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
//
// PDTSimpleCalendarViewController.m
// PDTSimpleCalendar
//
// Created by Jerome Miglino on 10/7/13.
// Copyright (c) 2013 Producteev. All rights reserved.
//
#import "PDTSimpleCalendarViewController.h"
#import "PDTSimpleCalendarViewFlowLayout.h"
#import "PDTSimpleCalendarViewCell.h"
#import "PDTSimpleCalendarViewHeader.h"
const CGFloat PDTSimpleCalendarOverlaySize = 14.0f;
static NSString *const PDTSimpleCalendarViewCellIdentifier = @"com.producteev.collection.cell.identifier";
static NSString *const PDTSimpleCalendarViewHeaderIdentifier = @"com.producteev.collection.header.identifier";
static const NSCalendarUnit kCalendarUnitYMD = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
@interface PDTSimpleCalendarViewController () <PDTSimpleCalendarViewCellDelegate>
@property (nonatomic, strong) UILabel *overlayView;
@property (nonatomic, strong) NSDateFormatter *headerDateFormatter; //Will be used to format date in header view and on scroll.
@property (nonatomic, strong) PDTSimpleCalendarViewWeekdayHeader *weekdayHeader;
// First and last date of the months based on the public properties first & lastDate
@property (nonatomic) NSDate *firstDateMonth;
@property (nonatomic) NSDate *lastDateMonth;
//Number of days per week
@property (nonatomic, assign) NSUInteger daysPerWeek;
@end
@implementation PDTSimpleCalendarViewController
//Explicitly @synthesize the var (it will create the iVar for us automatically as we redefine both getter and setter)
@synthesize firstDate = _firstDate;
@synthesize lastDate = _lastDate;
@synthesize calendar = _calendar;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
//Force the creation of the view with the pre-defined Flow Layout.
//Still possible to define a custom Flow Layout, if needed by using initWithCollectionViewLayout:
self = [super initWithCollectionViewLayout:[[PDTSimpleCalendarViewFlowLayout alloc] init]];
if (self) {
// Custom initialization
[self simpleCalendarCommonInit];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)coder
{
//Force the creation of the view with the pre-defined Flow Layout.
//Still possible to define a custom Flow Layout, if needed by using initWithCollectionViewLayout:
self = [super initWithCollectionViewLayout:[[PDTSimpleCalendarViewFlowLayout alloc] init]];
if (self) {
// Custom initialization
[self simpleCalendarCommonInit];
}
return self;
}
- (id)initWithCollectionViewLayout:(UICollectionViewLayout *)layout
{
self = [super initWithCollectionViewLayout:layout];
if (self) {
[self simpleCalendarCommonInit];
}
return self;
}
- (void)simpleCalendarCommonInit
{
self.overlayView = [[UILabel alloc] init];
self.backgroundColor = [UIColor whiteColor];
self.overlayTextColor = [UIColor blackColor];
self.daysPerWeek = 7;
self.weekdayHeaderEnabled = NO;
self.weekdayTextType = PDTSimpleCalendarViewWeekdayTextTypeShort;
}
#pragma mark - View Lifecycle
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if (self.selectedDate) {
[self.collectionViewLayout invalidateLayout];
}
}
#pragma mark - Accessors
- (NSDateFormatter *)headerDateFormatter;
{
if (!_headerDateFormatter) {
_headerDateFormatter = [[NSDateFormatter alloc] init];
_headerDateFormatter.calendar = self.calendar;
_headerDateFormatter.dateFormat = [NSDateFormatter dateFormatFromTemplate:@"yyyy LLLL" options:0 locale:self.calendar.locale];
}
return _headerDateFormatter;
}
- (NSCalendar *)calendar
{
if (!_calendar) {
[self setCalendar:[NSCalendar currentCalendar]];
}
return _calendar;
}
-(void)setCalendar:(NSCalendar*)calendar
{
_calendar = calendar;
self.headerDateFormatter.calendar = calendar;
self.daysPerWeek = [_calendar maximumRangeOfUnit:NSCalendarUnitWeekday].length;
}
- (NSDate *)firstDate
{
if (!_firstDate) {
NSDateComponents *components = [self.calendar components:kCalendarUnitYMD
fromDate:[NSDate date]];
components.day = 1;
_firstDate = [self.calendar dateFromComponents:components];
}
return _firstDate;
}
- (void)setFirstDate:(NSDate *)firstDate
{
_firstDate = [self clampDate:firstDate toComponents:kCalendarUnitYMD];
}
- (NSDate *)firstDateMonth
{
if (_firstDateMonth) { return _firstDateMonth; }
NSDateComponents *components = [self.calendar components:kCalendarUnitYMD
fromDate:self.firstDate];
components.day = 1;
_firstDateMonth = [self.calendar dateFromComponents:components];
return _firstDateMonth;
}
- (NSDate *)lastDate
{
if (!_lastDate) {
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
offsetComponents.year = 1;
offsetComponents.day = -1;
[self setLastDate:[self.calendar dateByAddingComponents:offsetComponents toDate:self.firstDateMonth options:0]];
}
return _lastDate;
}
- (void)setLastDate:(NSDate *)lastDate
{
_lastDate = [self clampDate:lastDate toComponents:kCalendarUnitYMD];
}
- (NSDate *)lastDateMonth
{
if (_lastDateMonth) { return _lastDateMonth; }
NSDateComponents *components = [self.calendar components:kCalendarUnitYMD
fromDate:self.lastDate];
components.month++;
components.day = 0;
_lastDateMonth = [self.calendar dateFromComponents:components];
return _lastDateMonth;
}
- (void)setSelectedDate:(NSDate *)newSelectedDate
{
//if newSelectedDate is nil, unselect the current selected cell
if (!newSelectedDate) {
[[self cellForItemAtDate:_selectedDate] setSelected:NO];
_selectedDate = newSelectedDate;
return;
}
//Test if selectedDate between first & last date
NSDate *startOfDay = [self clampDate:newSelectedDate toComponents:kCalendarUnitYMD];
if (([startOfDay compare:self.firstDateMonth] == NSOrderedAscending) || ([startOfDay compare:self.lastDateMonth] == NSOrderedDescending)) {
//the newSelectedDate is not between first & last date of the calendar, do nothing.
return;
}
[[self cellForItemAtDate:_selectedDate] setSelected:NO];
[[self cellForItemAtDate:startOfDay] setSelected:YES];
_selectedDate = startOfDay;
NSIndexPath *indexPath = [self indexPathForCellAtDate:_selectedDate];
[self.collectionView reloadItemsAtIndexPaths:@[ indexPath ]];
//Notify the delegate
if ([self.delegate respondsToSelector:@selector(simpleCalendarViewController:didSelectDate:)]) {
[self.delegate simpleCalendarViewController:self didSelectDate:self.selectedDate];
}
}
#pragma mark - Scroll to a specific date
- (void)scrollToSelectedDate:(BOOL)animated
{
if (_selectedDate) {
[self scrollToDate:_selectedDate animated:animated];
}
}
- (void)scrollToDate:(NSDate *)date animated:(BOOL)animated
{
@try {
NSIndexPath *selectedDateIndexPath = [self indexPathForCellAtDate:date];
if (![[self.collectionView indexPathsForVisibleItems] containsObject:selectedDateIndexPath]) {
//First, tried to use [self.collectionView layoutAttributesForSupplementaryElementOfKind:UICollectionElementKindSectionHeader atIndexPath:selectedDateIndexPath]; but it causes the header to be redraw multiple times (X each time you use scrollToDate:)
//TODO: Investigate & eventually file a radar.
NSIndexPath *sectionIndexPath = [NSIndexPath indexPathForItem:0 inSection:selectedDateIndexPath.section];
UICollectionViewLayoutAttributes *sectionLayoutAttributes = [self.collectionView layoutAttributesForItemAtIndexPath:sectionIndexPath];
CGPoint origin = sectionLayoutAttributes.frame.origin;
origin.x = 0;
origin.y -= (PDTSimpleCalendarFlowLayoutHeaderHeight + PDTSimpleCalendarFlowLayoutInsetTop + self.collectionView.contentInset.top);
[self.collectionView setContentOffset:origin animated:animated];
}
}
@catch (NSException *exception) {
//Exception occured (it should not according to the documentation, but in reality...) let's scroll to the IndexPath then
NSInteger section = [self sectionForDate:date];
NSIndexPath *sectionIndexPath = [NSIndexPath indexPathForItem:0 inSection:section];
[self.collectionView scrollToItemAtIndexPath:sectionIndexPath atScrollPosition:UICollectionViewScrollPositionTop animated:animated];
}
}
- (void)setOverlayTextColor:(UIColor *)overlayTextColor
{
_overlayTextColor = overlayTextColor;
if (self.overlayView) {
[self.overlayView setTextColor:self.overlayTextColor];
}
}
#pragma mark - View LifeCycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
//Configure the Collection View
[self.collectionView registerClass:[PDTSimpleCalendarViewCell class] forCellWithReuseIdentifier:PDTSimpleCalendarViewCellIdentifier];
[self.collectionView registerClass:[PDTSimpleCalendarViewHeader class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:PDTSimpleCalendarViewHeaderIdentifier];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
[self.collectionView setBackgroundColor:self.backgroundColor];
//Configure the Overlay View
[self.overlayView setBackgroundColor:[self.backgroundColor colorWithAlphaComponent:0.90]];
[self.overlayView setFont:[UIFont boldSystemFontOfSize:PDTSimpleCalendarOverlaySize]];
[self.overlayView setTextColor:self.overlayTextColor];
[self.overlayView setAlpha:0.0];
[self.overlayView setTextAlignment:NSTextAlignmentCenter];
[self.view addSubview:self.overlayView];
[self.overlayView setTranslatesAutoresizingMaskIntoConstraints:NO];
//Configure the Weekday Header
self.weekdayHeader = [[PDTSimpleCalendarViewWeekdayHeader alloc] initWithCalendar:self.calendar weekdayTextType:self.weekdayTextType];
[self.view addSubview:self.weekdayHeader];
[self.weekdayHeader setTranslatesAutoresizingMaskIntoConstraints:NO];
NSInteger weekdayHeaderHeight = self.weekdayHeaderEnabled ? PDTSimpleCalendarWeekdayHeaderHeight : 0;
NSDictionary *viewsDictionary = @{@"overlayView": self.overlayView, @"weekdayHeader": self.weekdayHeader};
NSDictionary *metricsDictionary = @{@"overlayViewHeight": @(PDTSimpleCalendarFlowLayoutHeaderHeight), @"weekdayHeaderHeight": @(weekdayHeaderHeight)};
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[overlayView]|" options:NSLayoutFormatAlignAllTop metrics:nil views:viewsDictionary]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[weekdayHeader]|" options:NSLayoutFormatAlignAllTop metrics:nil views:viewsDictionary]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[weekdayHeader(weekdayHeaderHeight)][overlayView(overlayViewHeight)]" options:0 metrics:metricsDictionary views:viewsDictionary]];
[self.collectionView setContentInset:UIEdgeInsetsMake(weekdayHeaderHeight, 0, 0, 0)];
}
#pragma mark - Rotation Handling
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[self.collectionView.collectionViewLayout invalidateLayout];
}
#pragma mark - Collection View Data Source
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
//Each Section is a Month
return [self.calendar components:NSCalendarUnitMonth fromDate:self.firstDateMonth toDate:self.lastDateMonth options:0].month + 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
NSDate *firstOfMonth = [self firstOfMonthForSection:section];
NSCalendarUnit weekCalendarUnit = [self weekCalendarUnitDependingOniOSVersion];
NSRange rangeOfWeeks = [self.calendar rangeOfUnit:weekCalendarUnit inUnit:NSCalendarUnitMonth forDate:firstOfMonth];
//We need the number of calendar weeks for the full months (it will maybe include previous month and next months cells)
return (rangeOfWeeks.length * self.daysPerWeek);
}
/**
* https://github.com/jivesoftware/PDTSimpleCalendar/issues/69
* On iOS7, using NSCalendarUnitWeekOfMonth (or WeekOfYear) in rangeOfUnit:inUnit is returning NSNotFound, NSNotFound
* Fun stuff, definition of NSNotFound is enum {NSNotFound = NSIntegerMax};
* So on iOS7, we're trying to allocate NSIntegerMax * 7 cells per Section
*
* //TODO: Remove when removing iOS7 Support
*
* @return the proper NSCalendarUnit to use in rangeOfUnit:inUnit
*/
- (NSCalendarUnit)weekCalendarUnitDependingOniOSVersion {
//isDateInToday is a new (awesome) method available on iOS8 only.
if ([self.calendar respondsToSelector:@selector(isDateInToday:)]) {
return NSCalendarUnitWeekOfMonth;
} else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
return NSWeekCalendarUnit;
#pragma clang diagnostic pop
}
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
PDTSimpleCalendarViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:PDTSimpleCalendarViewCellIdentifier
forIndexPath:indexPath];
cell.delegate = self;
NSDate *firstOfMonth = [self firstOfMonthForSection:indexPath.section];
NSDate *cellDate = [self dateForCellAtIndexPath:indexPath];
NSDateComponents *cellDateComponents = [self.calendar components:kCalendarUnitYMD fromDate:cellDate];
NSDateComponents *firstOfMonthsComponents = [self.calendar components:kCalendarUnitYMD fromDate:firstOfMonth];
BOOL isToday = NO;
BOOL isSelected = NO;
BOOL isCustomDate = NO;
if (cellDateComponents.month == firstOfMonthsComponents.month) {
isSelected = ([self isSelectedDate:cellDate] && (indexPath.section == [self sectionForDate:cellDate]));
isToday = [self isTodayDate:cellDate];
[cell setDate:cellDate calendar:self.calendar];
//Ask the delegate if this date should have specific colors.
if ([self.delegate respondsToSelector:@selector(simpleCalendarViewController:shouldUseCustomColorsForDate:)]) {
isCustomDate = [self.delegate simpleCalendarViewController:self shouldUseCustomColorsForDate:cellDate];
}
} else {
[cell setDate:nil calendar:nil];
}
if (isToday) {
[cell setIsToday:isToday];
}
if (isSelected) {
[cell setSelected:isSelected];
}
//If the current Date is not enabled, or if the delegate explicitely specify custom colors
if (![self isEnabledDate:cellDate] || isCustomDate) {
[cell refreshCellColors];
}
//We rasterize the cell for performances purposes.
//The circle background is made using roundedCorner which is a super expensive operation, specially with a lot of items on the screen to display (like we do)
cell.layer.shouldRasterize = YES;
cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
return cell;
}
#pragma mark - UICollectionViewDelegate
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSDate *firstOfMonth = [self firstOfMonthForSection:indexPath.section];
NSDate *cellDate = [self dateForCellAtIndexPath:indexPath];
//We don't want to select Dates that are "disabled"
if (![self isEnabledDate:cellDate]) {
return NO;
}
NSDateComponents *cellDateComponents = [self.calendar components:NSCalendarUnitDay|NSCalendarUnitMonth fromDate:cellDate];
NSDateComponents *firstOfMonthsComponents = [self.calendar components:NSCalendarUnitMonth fromDate:firstOfMonth];
return (cellDateComponents.month == firstOfMonthsComponents.month);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
self.selectedDate = [self dateForCellAtIndexPath:indexPath];
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
if (kind == UICollectionElementKindSectionHeader) {
PDTSimpleCalendarViewHeader *headerView = [self.collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:PDTSimpleCalendarViewHeaderIdentifier forIndexPath:indexPath];
headerView.titleLabel.text = [self.headerDateFormatter stringFromDate:[self firstOfMonthForSection:indexPath.section]].uppercaseString;
headerView.layer.shouldRasterize = YES;
headerView.layer.rasterizationScale = [UIScreen mainScreen].scale;
return headerView;
}
return nil;
}
#pragma mark - UICollectionViewFlowLayoutDelegate
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat itemWidth = floorf(CGRectGetWidth(self.collectionView.bounds) / self.daysPerWeek);
return CGSizeMake(itemWidth, itemWidth);
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
//We only display the overlay view if there is a vertical velocity
if (fabs(velocity.y) > 0.0f) {
if (self.overlayView.alpha < 1.0) {
[UIView animateWithDuration:0.25 animations:^{
[self.overlayView setAlpha:1.0];
}];
}
}
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
NSTimeInterval delay = (decelerate) ? 1.5 : 0.0;
[self performSelector:@selector(hideOverlayView) withObject:nil afterDelay:delay];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//Update Content of the Overlay View
NSArray *indexPaths = [self.collectionView indexPathsForVisibleItems];
//indexPaths is not sorted
NSArray *sortedIndexPaths = [indexPaths sortedArrayUsingSelector:@selector(compare:)];
NSIndexPath *firstIndexPath = [sortedIndexPaths firstObject];
self.overlayView.text = [self.headerDateFormatter stringFromDate:[self firstOfMonthForSection:firstIndexPath.section]];
}
- (void)hideOverlayView
{
[UIView animateWithDuration:0.25 animations:^{
[self.overlayView setAlpha:0.0];
}];
}
#pragma mark -
#pragma mark - Calendar calculations
- (NSDate *)clampDate:(NSDate *)date toComponents:(NSUInteger)unitFlags
{
NSDateComponents *components = [self.calendar components:unitFlags fromDate:date];
return [self.calendar dateFromComponents:components];
}
- (BOOL)isTodayDate:(NSDate *)date
{
return [self clampAndCompareDate:date withReferenceDate:[NSDate date]];
}
- (BOOL)isSelectedDate:(NSDate *)date
{
if (!self.selectedDate) {
return NO;
}
return [self clampAndCompareDate:date withReferenceDate:self.selectedDate];
}
- (BOOL)isEnabledDate:(NSDate *)date
{
NSDate *clampedDate = [self clampDate:date toComponents:kCalendarUnitYMD];
if (([clampedDate compare:self.firstDate] == NSOrderedAscending) || ([clampedDate compare:self.lastDate] == NSOrderedDescending)) {
return NO;
}
if ([self.delegate respondsToSelector:@selector(simpleCalendarViewController:isEnabledDate:)]) {
return [self.delegate simpleCalendarViewController:self isEnabledDate:date];
}
return YES;
}
- (BOOL)clampAndCompareDate:(NSDate *)date withReferenceDate:(NSDate *)referenceDate
{
NSDate *refDate = [self clampDate:referenceDate toComponents:kCalendarUnitYMD];
NSDate *clampedDate = [self clampDate:date toComponents:kCalendarUnitYMD];
return [refDate isEqualToDate:clampedDate];
}
#pragma mark - Collection View / Calendar Methods
- (NSDate *)firstOfMonthForSection:(NSInteger)section
{
NSDateComponents *offset = [NSDateComponents new];
offset.month = section;
return [self.calendar dateByAddingComponents:offset toDate:self.firstDateMonth options:0];
}
- (NSInteger)sectionForDate:(NSDate *)date
{
return [self.calendar components:NSCalendarUnitMonth fromDate:self.firstDateMonth toDate:date options:0].month;
}
- (NSDate *)dateForCellAtIndexPath:(NSIndexPath *)indexPath
{
NSDate *firstOfMonth = [self firstOfMonthForSection:indexPath.section];
NSUInteger weekday = [[self.calendar components: NSCalendarUnitWeekday fromDate: firstOfMonth] weekday];
NSInteger startOffset = weekday - self.calendar.firstWeekday;
startOffset += startOffset >= 0 ? 0 : self.daysPerWeek;
NSDateComponents *dateComponents = [NSDateComponents new];
dateComponents.day = indexPath.item - startOffset;
return [self.calendar dateByAddingComponents:dateComponents toDate:firstOfMonth options:0];
}
static const NSInteger kFirstDay = 1;
- (NSIndexPath *)indexPathForCellAtDate:(NSDate *)date
{
if (!date) {
return nil;
}
NSInteger section = [self sectionForDate:date];
NSDate *firstOfMonth = [self firstOfMonthForSection:section];
NSInteger weekday = [[self.calendar components: NSCalendarUnitWeekday fromDate: firstOfMonth] weekday];
NSInteger startOffset = weekday - self.calendar.firstWeekday;
startOffset += startOffset >= 0 ? 0 : self.daysPerWeek;
NSInteger day = [[self.calendar components:kCalendarUnitYMD fromDate:date] day];
NSInteger item = (day - kFirstDay + startOffset);
return [NSIndexPath indexPathForItem:item inSection:section];
}
- (PDTSimpleCalendarViewCell *)cellForItemAtDate:(NSDate *)date
{
return (PDTSimpleCalendarViewCell *)[self.collectionView cellForItemAtIndexPath:[self indexPathForCellAtDate:date]];
}
#pragma mark - PDTSimpleCalendarViewCellDelegate
- (BOOL)simpleCalendarViewCell:(PDTSimpleCalendarViewCell *)cell shouldUseCustomColorsForDate:(NSDate *)date
{
//If the date is not enabled (aka outside the first/lastDate) return YES
if (![self isEnabledDate:date]) {
return YES;
}
//Otherwise we ask the delegate
if ([self.delegate respondsToSelector:@selector(simpleCalendarViewController:shouldUseCustomColorsForDate:)]) {
return [self.delegate simpleCalendarViewController:self shouldUseCustomColorsForDate:date];
}
return NO;
}
- (UIColor *)simpleCalendarViewCell:(PDTSimpleCalendarViewCell *)cell circleColorForDate:(NSDate *)date
{
if (![self isEnabledDate:date]) {
return cell.circleDefaultColor;
}
if ([self.delegate respondsToSelector:@selector(simpleCalendarViewController:circleColorForDate:)]) {
return [self.delegate simpleCalendarViewController:self circleColorForDate:date];
}
return nil;
}
- (UIColor *)simpleCalendarViewCell:(PDTSimpleCalendarViewCell *)cell textColorForDate:(NSDate *)date
{
if (![self isEnabledDate:date]) {
return cell.textDisabledColor;
}
if ([self.delegate respondsToSelector:@selector(simpleCalendarViewController:textColorForDate:)]) {
return [self.delegate simpleCalendarViewController:self textColorForDate:date];
}
return nil;
}
@end