diff --git a/TimesSquare/Resources/CalendarPreviousMonth.png b/TimesSquare/Resources/CalendarPreviousMonth.png new file mode 100644 index 0000000..35edb79 Binary files /dev/null and b/TimesSquare/Resources/CalendarPreviousMonth.png differ diff --git a/TimesSquare/Resources/CalendarPreviousMonth@2x.png b/TimesSquare/Resources/CalendarPreviousMonth@2x.png new file mode 100644 index 0000000..daf0db3 Binary files /dev/null and b/TimesSquare/Resources/CalendarPreviousMonth@2x.png differ diff --git a/TimesSquare/Resources/CalendarRow.png b/TimesSquare/Resources/CalendarRow.png new file mode 100644 index 0000000..9905881 Binary files /dev/null and b/TimesSquare/Resources/CalendarRow.png differ diff --git a/TimesSquare/Resources/CalendarRow@2x.png b/TimesSquare/Resources/CalendarRow@2x.png new file mode 100644 index 0000000..5446039 Binary files /dev/null and b/TimesSquare/Resources/CalendarRow@2x.png differ diff --git a/TimesSquare/Resources/CalendarRowBottom.png b/TimesSquare/Resources/CalendarRowBottom.png new file mode 100644 index 0000000..c5a4abc Binary files /dev/null and b/TimesSquare/Resources/CalendarRowBottom.png differ diff --git a/TimesSquare/Resources/CalendarRowBottom@2x.png b/TimesSquare/Resources/CalendarRowBottom@2x.png new file mode 100644 index 0000000..3496670 Binary files /dev/null and b/TimesSquare/Resources/CalendarRowBottom@2x.png differ diff --git a/TimesSquare/Resources/CalendarSelectedDate.png b/TimesSquare/Resources/CalendarSelectedDate.png new file mode 100644 index 0000000..ab2f830 Binary files /dev/null and b/TimesSquare/Resources/CalendarSelectedDate.png differ diff --git a/TimesSquare/Resources/CalendarSelectedDate@2x.png b/TimesSquare/Resources/CalendarSelectedDate@2x.png new file mode 100644 index 0000000..ffa18cc Binary files /dev/null and b/TimesSquare/Resources/CalendarSelectedDate@2x.png differ diff --git a/TimesSquare/Resources/CalendarTodaysDate.png b/TimesSquare/Resources/CalendarTodaysDate.png new file mode 100644 index 0000000..3551826 Binary files /dev/null and b/TimesSquare/Resources/CalendarTodaysDate.png differ diff --git a/TimesSquare/Resources/CalendarTodaysDate@2x.png b/TimesSquare/Resources/CalendarTodaysDate@2x.png new file mode 100644 index 0000000..e54c1a8 Binary files /dev/null and b/TimesSquare/Resources/CalendarTodaysDate@2x.png differ diff --git a/TimesSquare/Resources/Default-568h@2x.png b/TimesSquare/Resources/Default-568h@2x.png new file mode 100644 index 0000000..0891b7a Binary files /dev/null and b/TimesSquare/Resources/Default-568h@2x.png differ diff --git a/TimesSquare/Resources/Default.png b/TimesSquare/Resources/Default.png new file mode 100644 index 0000000..4c8ca6f Binary files /dev/null and b/TimesSquare/Resources/Default.png differ diff --git a/TimesSquare/Resources/Default@2x.png b/TimesSquare/Resources/Default@2x.png new file mode 100644 index 0000000..35b84cf Binary files /dev/null and b/TimesSquare/Resources/Default@2x.png differ diff --git a/TimesSquare/TSQCalendarCell.m b/TimesSquare/TSQCalendarCell.m index 99276c9..011e94c 100644 --- a/TimesSquare/TSQCalendarCell.m +++ b/TimesSquare/TSQCalendarCell.m @@ -55,7 +55,7 @@ - (NSUInteger)daysInWeek; { static NSUInteger daysInWeek = 0; if (daysInWeek == 0) { - daysInWeek = [self.calendar maximumRangeOfUnit:NSWeekdayCalendarUnit].length; + daysInWeek = [self.calendar maximumRangeOfUnit:NSCalendarUnitWeekday].length; } return daysInWeek; } diff --git a/TimesSquare/TSQCalendarRowCell.m b/TimesSquare/TSQCalendarRowCell.m index 0cf087b..1952ab4 100644 --- a/TimesSquare/TSQCalendarRowCell.m +++ b/TimesSquare/TSQCalendarRowCell.m @@ -141,7 +141,7 @@ - (void)setBeginningDate:(NSDate *)date; [self.notThisMonthButtons[index] setTitle:title forState:UIControlStateDisabled]; [self.notThisMonthButtons[index] setAccessibilityLabel:accessibilityLabel]; - NSDateComponents *thisDateComponents = [self.calendar components:NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit fromDate:date]; + NSDateComponents *thisDateComponents = [self.calendar components:NSCalendarUnitDay|NSCalendarUnitMonth|NSCalendarUnitYear fromDate:date]; [self.dayButtons[index] setHidden:YES]; [self.notThisMonthButtons[index] setHidden:YES]; @@ -232,9 +232,9 @@ - (void)selectColumnForDate:(NSDate *)date; NSInteger newIndexOfSelectedButton = -1; if (date) { - NSInteger thisDayMonth = [self.calendar components:NSMonthCalendarUnit fromDate:date].month; + NSInteger thisDayMonth = [self.calendar components:NSCalendarUnitMonth fromDate:date].month; if (self.monthOfBeginningDate == thisDayMonth) { - newIndexOfSelectedButton = [self.calendar components:NSDayCalendarUnit fromDate:self.beginningDate toDate:date options:0].day; + newIndexOfSelectedButton = [self.calendar components:NSCalendarUnitDay fromDate:self.beginningDate toDate:date options:0].day; if (newIndexOfSelectedButton >= (NSInteger)self.daysInWeek) { newIndexOfSelectedButton = -1; } @@ -279,7 +279,7 @@ - (NSDateFormatter *)accessibilityFormatter; - (NSInteger)monthOfBeginningDate; { if (!_monthOfBeginningDate) { - _monthOfBeginningDate = [self.calendar components:NSMonthCalendarUnit fromDate:self.firstOfMonth].month; + _monthOfBeginningDate = [self.calendar components:NSCalendarUnitMonth fromDate:self.firstOfMonth].month; } return _monthOfBeginningDate; } @@ -293,7 +293,7 @@ - (void)setFirstOfMonth:(NSDate *)firstOfMonth; - (NSDateComponents *)todayDateComponents; { if (!_todayDateComponents) { - self.todayDateComponents = [self.calendar components:NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit fromDate:[NSDate date]]; + self.todayDateComponents = [self.calendar components:NSCalendarUnitDay|NSCalendarUnitMonth|NSCalendarUnitYear fromDate:[NSDate date]]; } return _todayDateComponents; } diff --git a/TimesSquare/TSQCalendarView.m b/TimesSquare/TSQCalendarView.m index 4b73585..f922f07 100644 --- a/TimesSquare/TSQCalendarView.m +++ b/TimesSquare/TSQCalendarView.m @@ -109,13 +109,13 @@ - (void)setPinsHeaderToTop:(BOOL)pinsHeaderToTop; - (void)setFirstDate:(NSDate *)firstDate; { // clamp to the beginning of its month - _firstDate = [self clampDate:firstDate toComponents:NSMonthCalendarUnit|NSYearCalendarUnit]; + _firstDate = [self clampDate:firstDate toComponents:NSCalendarUnitMonth|NSCalendarUnitYear]; } - (void)setLastDate:(NSDate *)lastDate; { // clamp to the end of its month - NSDate *firstOfMonth = [self clampDate:lastDate toComponents:NSMonthCalendarUnit|NSYearCalendarUnit]; + NSDate *firstOfMonth = [self clampDate:lastDate toComponents:NSCalendarUnitMonth|NSCalendarUnitYear]; NSDateComponents *offsetComponents = [[NSDateComponents alloc] init]; offsetComponents.month = 1; @@ -126,7 +126,7 @@ - (void)setLastDate:(NSDate *)lastDate; - (void)setSelectedDate:(NSDate *)newSelectedDate; { // clamp to beginning of its day - NSDate *startOfDay = [self clampDate:newSelectedDate toComponents:NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit]; + NSDate *startOfDay = [self clampDate:newSelectedDate toComponents:NSCalendarUnitDay|NSCalendarUnitMonth|NSCalendarUnitYear]; if ([self.delegate respondsToSelector:@selector(calendarView:shouldSelectDate:)] && ![self.delegate calendarView:self shouldSelectDate:startOfDay]) { return; @@ -186,7 +186,7 @@ - (TSQCalendarRowCell *)cellForRowAtDate:(NSDate *)date; - (NSInteger)sectionForDate:(NSDate *)date; { - return [self.calendar components:NSMonthCalendarUnit fromDate:self.firstDate toDate:date options:0].month; + return [self.calendar components:NSCalendarUnitMonth fromDate:self.firstDate toDate:date options:0].month; } - (NSIndexPath *)indexPathForRowAtDate:(NSDate *)date; @@ -198,8 +198,8 @@ - (NSIndexPath *)indexPathForRowAtDate:(NSDate *)date; NSInteger section = [self sectionForDate:date]; NSDate *firstOfMonth = [self firstOfMonthForSection:section]; - NSInteger firstWeek = [self.calendar components:NSWeekOfMonthCalendarUnit fromDate:firstOfMonth].weekOfMonth; - NSInteger targetWeek = [self.calendar components:NSWeekOfMonthCalendarUnit fromDate:date].weekOfMonth; + NSInteger firstWeek = [self.calendar components:NSCalendarUnitWeekOfMonth fromDate:firstOfMonth].weekOfMonth; + NSInteger targetWeek = [self.calendar components:NSCalendarUnitWeekOfMonth fromDate:date].weekOfMonth; return [NSIndexPath indexPathForRow:(self.pinsHeaderToTop ? 0 : 1) + targetWeek - firstWeek inSection:section]; } @@ -237,13 +237,13 @@ - (void)layoutSubviews; - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; { - return 1 + [self.calendar components:NSMonthCalendarUnit fromDate:self.firstDate toDate:self.lastDate options:0].month; + return 1 + [self.calendar components:NSCalendarUnitMonth fromDate:self.firstDate toDate:self.lastDate options:0].month; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(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]; return (self.pinsHeaderToTop ? 0 : 1) + rangeOfWeeks.length; } @@ -276,10 +276,10 @@ - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)ce NSDate *firstOfMonth = [self firstOfMonthForSection:indexPath.section]; [(TSQCalendarCell *)cell setFirstOfMonth:firstOfMonth]; if (indexPath.row > 0 || self.pinsHeaderToTop) { - NSInteger ordinalityOfFirstDay = [self.calendar ordinalityOfUnit:NSDayCalendarUnit inUnit:NSWeekCalendarUnit forDate:firstOfMonth]; + NSInteger ordinalityOfFirstDay = [self.calendar ordinalityOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitWeekOfMonth forDate:firstOfMonth]; NSDateComponents *dateComponents = [NSDateComponents new]; dateComponents.day = 1 - ordinalityOfFirstDay; - dateComponents.week = indexPath.row - (self.pinsHeaderToTop ? 0 : 1); + dateComponents.weekOfMonth = indexPath.row - (self.pinsHeaderToTop ? 0 : 1); [(TSQCalendarRowCell *)cell setBeginningDate:[self.calendar dateByAddingComponents:dateComponents toDate:firstOfMonth options:0]]; [(TSQCalendarRowCell *)cell selectColumnForDate:self.selectedDate]; diff --git a/TimesSquare/TSQTACalendarRowCell.h b/TimesSquare/TSQTACalendarRowCell.h new file mode 100644 index 0000000..f8b6799 --- /dev/null +++ b/TimesSquare/TSQTACalendarRowCell.h @@ -0,0 +1,14 @@ +// +// TSQTACalendarRowCell.h +// TimesSquare +// +// Created by Jim Puls on 12/5/12. +// Licensed to Square, Inc. under one or more contributor license agreements. +// See the LICENSE file distributed with this work for the terms under +// which Square, Inc. licenses this file to you. + +#import "TimesSquare.h" + +@interface TSQTACalendarRowCell : TSQCalendarRowCell + +@end diff --git a/TimesSquare/TSQTACalendarRowCell.m b/TimesSquare/TSQTACalendarRowCell.m new file mode 100644 index 0000000..2a44bf3 --- /dev/null +++ b/TimesSquare/TSQTACalendarRowCell.m @@ -0,0 +1,42 @@ +// +// TSQTACalendarRowCell.m +// TimesSquare +// +// Created by Jim Puls on 12/5/12. +// Licensed to Square, Inc. under one or more contributor license agreements. +// See the LICENSE file distributed with this work for the terms under +// which Square, Inc. licenses this file to you. + +#import "TSQTACalendarRowCell.h" + +@implementation TSQTACalendarRowCell + +- (void)layoutViewsForColumnAtIndex:(NSUInteger)index inRect:(CGRect)rect; +{ + // Move down for the row at the top + rect.origin.y += self.columnSpacing; + rect.size.height -= (self.bottomRow ? 2.0f : 1.0f) * self.columnSpacing; + [super layoutViewsForColumnAtIndex:index inRect:rect]; +} + +- (UIImage *)todayBackgroundImage; +{ + return [[UIImage imageNamed:@"CalendarTodaysDate.png"] stretchableImageWithLeftCapWidth:4 topCapHeight:4]; +} + +- (UIImage *)selectedBackgroundImage; +{ + return [[UIImage imageNamed:@"CalendarSelectedDate.png"] stretchableImageWithLeftCapWidth:4 topCapHeight:4]; +} + +- (UIImage *)notThisMonthBackgroundImage; +{ + return [[UIImage imageNamed:@"CalendarPreviousMonth.png"] stretchableImageWithLeftCapWidth:0 topCapHeight:0]; +} + +- (UIImage *)backgroundImage; +{ + return [UIImage imageNamed:[NSString stringWithFormat:@"CalendarRow%@.png", self.bottomRow ? @"Bottom" : @""]]; +} + +@end diff --git a/TimesSquare/TSQTAViewController.h b/TimesSquare/TSQTAViewController.h new file mode 100644 index 0000000..be8d30e --- /dev/null +++ b/TimesSquare/TSQTAViewController.h @@ -0,0 +1,16 @@ +// +// TSQTAViewController.h +// TimesSquare +// +// Created by Jim Puls on 12/5/12. +// Licensed to Square, Inc. under one or more contributor license agreements. +// See the LICENSE file distributed with this work for the terms under +// which Square, Inc. licenses this file to you. + +#import + +@interface TSQTAViewController : UIViewController + +@property (nonatomic, strong) NSCalendar *calendar; + +@end diff --git a/TimesSquare/TSQTAViewController.m b/TimesSquare/TSQTAViewController.m new file mode 100644 index 0000000..485e7fa --- /dev/null +++ b/TimesSquare/TSQTAViewController.m @@ -0,0 +1,86 @@ +// +// TSQTAViewController.m +// TimesSquare +// +// Created by Jim Puls on 12/5/12. +// Licensed to Square, Inc. under one or more contributor license agreements. +// See the LICENSE file distributed with this work for the terms under +// which Square, Inc. licenses this file to you. + +#import "TSQTAViewController.h" +#import "TSQTACalendarRowCell.h" +#import "TimesSquare.h" + + +@interface TSQTAViewController () + +@property (nonatomic, retain) NSTimer *timer; + +@end + + +@interface TSQCalendarView (AccessingPrivateStuff) + +@property (nonatomic, readonly) UITableView *tableView; + +@end + + +@implementation TSQTAViewController + +- (void)loadView; +{ + TSQCalendarView *calendarView = [[TSQCalendarView alloc] init]; + calendarView.calendar = self.calendar; + calendarView.rowCellClass = [TSQTACalendarRowCell class]; + //calendarView.firstDate = [NSDate dateWithTimeIntervalSinceNow:-60 * 60 * 24 * 365 * 1]; + calendarView.firstDate = [[NSDate alloc]init]; + calendarView.lastDate = [NSDate dateWithTimeIntervalSinceNow:60 * 60 * 24 * 365 * 1]; + //calendarView.lastDate = [NSDate dateWithTimeIntervalSinceNow:60 * 60 * 24 * 365 * 5]; + calendarView.backgroundColor = [UIColor colorWithRed:0.84f green:0.85f blue:0.86f alpha:1.0f]; + calendarView.pagingEnabled = YES; + CGFloat onePixel = 1.0f / [UIScreen mainScreen].scale; + calendarView.contentInset = UIEdgeInsetsMake(0.0f, onePixel, 0.0f, onePixel); + + self.view = calendarView; +} + +- (void)setCalendar:(NSCalendar *)calendar; +{ + _calendar = calendar; + + self.navigationItem.title = calendar.calendarIdentifier; + self.tabBarItem.title = calendar.calendarIdentifier; +} + +- (void)viewDidLayoutSubviews; +{ + // Set the calendar view to show today date on start + [(TSQCalendarView *)self.view scrollToDate:[NSDate date] animated:NO]; +} + +- (void)viewDidAppear:(BOOL)animated; +{ + [super viewDidAppear:animated]; + + // Uncomment this to test scrolling performance of your custom drawing +// self.timer = [NSTimer scheduledTimerWithTimeInterval:.1 target:self selector:@selector(scroll) userInfo:nil repeats:YES]; +} + +- (void)viewWillDisappear:(BOOL)animated; +{ + [self.timer invalidate]; + self.timer = nil; +} + +- (void)scroll; +{ + static BOOL atTop = YES; + TSQCalendarView *calendarView = (TSQCalendarView *)self.view; + UITableView *tableView = calendarView.tableView; + + [tableView setContentOffset:CGPointMake(0.f, atTop ? 10000.f : 0.f) animated:YES]; + atTop = !atTop; +} + +@end diff --git a/TimesSquare/TimesSquare.h b/TimesSquare/TimesSquare.h index 7b3e07e..addfc8a 100644 --- a/TimesSquare/TimesSquare.h +++ b/TimesSquare/TimesSquare.h @@ -7,6 +7,6 @@ // See the LICENSE file distributed with this work for the terms under // which Square, Inc. licenses this file to you. -#import -#import -#import +#import "TSQCalendarMonthHeaderCell.h" +#import "TSQCalendarRowCell.h" +#import "TSQCalendarView.h"