Skip to content

Commit

Permalink
Merge pull request jivesoftware#59 from adonoho/iOS8-deprecated-symbols
Browse files Browse the repository at this point in the history
Remove deprecation warnings in Xcode v6.3.1 by updating calendar symbols...
  • Loading branch information
jmig committed Jul 1, 2015
2 parents 9903b50 + 37ffeaf commit 4a88478
Showing 1 changed file with 48 additions and 36 deletions.
84 changes: 48 additions & 36 deletions PDTSimpleCalendar/PDTSimpleCalendarViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@

const CGFloat PDTSimpleCalendarOverlaySize = 14.0f;

static NSString *PDTSimpleCalendarViewCellIdentifier = @"com.producteev.collection.cell.identifier";
static NSString *PDTSimpleCalendarViewHeaderIdentifier = @"com.producteev.collection.header.identifier";

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.

// First and last date of the months based on the public properties first & lastDate
@property (nonatomic, readonly) NSDate *firstDateMonth;
@property (nonatomic, readonly) NSDate *lastDateMonth;
@property (nonatomic) NSDate *firstDateMonth;
@property (nonatomic) NSDate *lastDateMonth;

//Number of days per week
@property (nonatomic, assign) NSUInteger daysPerWeek;
Expand Down Expand Up @@ -121,13 +121,13 @@ -(void)setCalendar:(NSCalendar*)calendar
{
_calendar = calendar;
self.headerDateFormatter.calendar = calendar;
self.daysPerWeek = [_calendar maximumRangeOfUnit:NSWeekdayCalendarUnit].length;
self.daysPerWeek = [_calendar maximumRangeOfUnit:NSCalendarUnitWeekday].length;
}

- (NSDate *)firstDate
{
if (!_firstDate) {
NSDateComponents *components = [self.calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay
NSDateComponents *components = [self.calendar components:kCalendarUnitYMD
fromDate:[NSDate date]];
components.day = 1;
_firstDate = [self.calendar dateFromComponents:components];
Expand All @@ -138,17 +138,20 @@ - (NSDate *)firstDate

- (void)setFirstDate:(NSDate *)firstDate
{
_firstDate = [self clampDate:firstDate toComponents:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay];
_firstDate = [self clampDate:firstDate toComponents:kCalendarUnitYMD];
}

//TODO: Store the value in the variable to avoid calculation everytime.
- (NSDate *)firstDateMonth
{
NSDateComponents *components = [self.calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay
if (_firstDateMonth) { return _firstDateMonth; }

NSDateComponents *components = [self.calendar components:kCalendarUnitYMD
fromDate:self.firstDate];
components.day = 1;

return [self.calendar dateFromComponents:components];
_firstDateMonth = [self.calendar dateFromComponents:components];

return _firstDateMonth;
}

- (NSDate *)lastDate
Expand All @@ -165,17 +168,21 @@ - (NSDate *)lastDate

- (void)setLastDate:(NSDate *)lastDate
{
_lastDate = [self clampDate:lastDate toComponents:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay];
_lastDate = [self clampDate:lastDate toComponents:kCalendarUnitYMD];
}

//TODO: Store the value in the variable to avoid calculation everytime.
- (NSDate *)lastDateMonth
{
NSDateComponents *components = [self.calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:self.lastDate];
if (_lastDateMonth) { return _lastDateMonth; }

NSDateComponents *components = [self.calendar components:kCalendarUnitYMD
fromDate:self.lastDate];
components.month++;
components.day = 0;

return [self.calendar dateFromComponents:components];
_lastDateMonth = [self.calendar dateFromComponents:components];

return _lastDateMonth;
}

- (void)setSelectedDate:(NSDate *)newSelectedDate
Expand All @@ -189,7 +196,7 @@ - (void)setSelectedDate:(NSDate *)newSelectedDate
}

//Test if selectedDate between first & last date
NSDate *startOfDay = [self clampDate:newSelectedDate toComponents:NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit];
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;
Expand Down Expand Up @@ -304,13 +311,13 @@ - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrie
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
//Each Section is a Month
return [self.calendar components:NSMonthCalendarUnit fromDate:self.firstDateMonth toDate:self.lastDateMonth options:0].month + 1;
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];
NSRange rangeOfWeeks = [self.calendar rangeOfUnit:NSWeekCalendarUnit inUnit:NSMonthCalendarUnit forDate:firstOfMonth];
NSRange rangeOfWeeks = [self.calendar rangeOfUnit:NSCalendarUnitWeekOfMonth 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);
Expand All @@ -327,8 +334,8 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cell
NSDate *firstOfMonth = [self firstOfMonthForSection:indexPath.section];
NSDate *cellDate = [self dateForCellAtIndexPath:indexPath];

NSDateComponents *cellDateComponents = [self.calendar components:NSDayCalendarUnit|NSMonthCalendarUnit fromDate:cellDate];
NSDateComponents *firstOfMonthsComponents = [self.calendar components:NSMonthCalendarUnit fromDate:firstOfMonth];
NSDateComponents *cellDateComponents = [self.calendar components:kCalendarUnitYMD fromDate:cellDate];
NSDateComponents *firstOfMonthsComponents = [self.calendar components:kCalendarUnitYMD fromDate:firstOfMonth];

BOOL isToday = NO;
BOOL isSelected = NO;
Expand Down Expand Up @@ -382,8 +389,8 @@ - (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtInde
return NO;
}

NSDateComponents *cellDateComponents = [self.calendar components:NSDayCalendarUnit|NSMonthCalendarUnit fromDate:cellDate];
NSDateComponents *firstOfMonthsComponents = [self.calendar components:NSMonthCalendarUnit fromDate:firstOfMonth];
NSDateComponents *cellDateComponents = [self.calendar components:NSCalendarUnitDay|NSCalendarUnitMonth fromDate:cellDate];
NSDateComponents *firstOfMonthsComponents = [self.calendar components:NSCalendarUnitMonth fromDate:firstOfMonth];

return (cellDateComponents.month == firstOfMonthsComponents.month);
}
Expand Down Expand Up @@ -424,7 +431,7 @@ - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollection
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
//We only display the overlay view if there is a vertical velocity
if ( fabsf(velocity.y) > 0.0f) {
if (fabs(velocity.y) > 0.0f) {
if (self.overlayView.alpha < 1.0) {
[UIView animateWithDuration:0.25 animations:^{
[self.overlayView setAlpha:1.0];
Expand Down Expand Up @@ -481,7 +488,7 @@ - (BOOL)isSelectedDate:(NSDate *)date

- (BOOL)isEnabledDate:(NSDate *)date
{
NSDate *clampedDate = [self clampDate:date toComponents:(NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit)];
NSDate *clampedDate = [self clampDate:date toComponents:kCalendarUnitYMD];
if (([clampedDate compare:self.firstDate] == NSOrderedAscending) || ([clampedDate compare:self.lastDate] == NSOrderedDescending)) {
return NO;
}
Expand All @@ -495,8 +502,8 @@ - (BOOL)isEnabledDate:(NSDate *)date

- (BOOL)clampAndCompareDate:(NSDate *)date withReferenceDate:(NSDate *)referenceDate
{
NSDate *refDate = [self clampDate:referenceDate toComponents:(NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit)];
NSDate *clampedDate = [self clampDate:date toComponents:(NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit)];
NSDate *refDate = [self clampDate:referenceDate toComponents:kCalendarUnitYMD];
NSDate *clampedDate = [self clampDate:date toComponents:kCalendarUnitYMD];

return [refDate isEqualToDate:clampedDate];
}
Expand All @@ -513,36 +520,41 @@ - (NSDate *)firstOfMonthForSection:(NSInteger)section

- (NSInteger)sectionForDate:(NSDate *)date
{
return [self.calendar components:NSMonthCalendarUnit fromDate:self.firstDateMonth toDate:date options:0].month;
return [self.calendar components:NSCalendarUnitMonth fromDate:self.firstDateMonth toDate:date options:0].month;
}


- (NSDate *)dateForCellAtIndexPath:(NSIndexPath *)indexPath
{
NSDate *firstOfMonth = [self firstOfMonthForSection:indexPath.section];
NSInteger ordinalityOfFirstDay = [self.calendar ordinalityOfUnit:NSDayCalendarUnit inUnit:NSWeekCalendarUnit forDate:firstOfMonth];

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 = (1 - ordinalityOfFirstDay) + indexPath.item;
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 ordinalityOfFirstDay = [self.calendar ordinalityOfUnit:NSDayCalendarUnit inUnit:NSWeekCalendarUnit forDate:firstOfMonth];

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];

NSDateComponents *dateComponents = [self.calendar components:NSDayCalendarUnit fromDate:date];
NSDateComponents *firstOfMonthComponents = [self.calendar components:NSDayCalendarUnit fromDate:firstOfMonth];
NSInteger item = (dateComponents.day - firstOfMonthComponents.day) - (1 - ordinalityOfFirstDay);
NSInteger item = (day - kFirstDay + startOffset);

return [NSIndexPath indexPathForItem:item inSection:section];
}
Expand All @@ -552,7 +564,7 @@ - (PDTSimpleCalendarViewCell *)cellForItemAtDate:(NSDate *)date
return (PDTSimpleCalendarViewCell *)[self.collectionView cellForItemAtIndexPath:[self indexPathForCellAtDate:date]];
}

#pragma mark PDTSimpleCalendarViewCellDelegate
#pragma mark - PDTSimpleCalendarViewCellDelegate

- (BOOL)simpleCalendarViewCell:(PDTSimpleCalendarViewCell *)cell shouldUseCustomColorsForDate:(NSDate *)date
{
Expand Down

0 comments on commit 4a88478

Please sign in to comment.