Skip to content

Commit

Permalink
Implement stub methods, call delegate methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
robbdimitrov committed Aug 30, 2013
1 parent ced73ae commit 8c5d2d3
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 26 deletions.
10 changes: 8 additions & 2 deletions RDVCalendarView/RDVCalendarMonthView.m
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,21 @@ - (NSInteger)indexForCell:(RDVCalendarDayCell *)cell {
}

- (NSInteger)indexForRowAtPoint:(CGPoint)point {
RDVCalendarDayCell *cell = [self viewAtLocation:point];

if (cell) {
return [self indexForCell:cell];
}

return 0;
}

- (RDVCalendarDayCell *)cellForRowAtIndex:(NSInteger)index {
return nil;
return [[self visibleCells] objectAtIndex:index];
}

- (NSInteger)indexForSelectedCell {
return 0;
return [self indexForCell:[self selectedDayCell]];
}

- (RDVCalendarDayCell *)viewAtLocation:(CGPoint)location {
Expand Down
4 changes: 1 addition & 3 deletions RDVCalendarView/RDVCalendarView.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,13 @@
@property (nonatomic) UIColor *dayTextColor;
@property (nonatomic) UIColor *highlightedDayTextColor;

@property NSDate *selectedDate;
@property (nonatomic) NSDate *selectedDate;

- (void)reloadData;

@end

@protocol RDVCalendarViewDelegate <NSObject>
@optional
- (BOOL)calendarView:(RDVCalendarView *)calendarView shouldSelectDate:(NSDate *)date;
- (void)calendarView:(RDVCalendarView *)calendarView willSelectDate:(NSDate *)date;
- (void)calendarView:(RDVCalendarView *)calendarView didSelectDate:(NSDate *)date;
@end
50 changes: 46 additions & 4 deletions RDVCalendarView/RDVCalendarView.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ - (id)initWithFrame:(CGRect)frame

_currentDay = [calendar components:NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit fromDate:[NSDate date]];

_selectedDate = [NSDate date];
NSDate *currentDate = [NSDate date];

[self setupViews];

Expand All @@ -51,7 +51,7 @@ - (id)initWithFrame:(CGRect)frame
NSDayCalendarUnit|
NSWeekdayCalendarUnit|
NSCalendarCalendarUnit
fromDate:_selectedDate];
fromDate:currentDate];
_month.day = 1;
[self updateMonthLabelMonth:_month];

Expand Down Expand Up @@ -83,8 +83,10 @@ - (void)layoutSubviews {

- (void)setupViews {
_headerView = [[RDVCalendarHeaderView alloc] init];
[[_headerView backButton] addTarget:self action:@selector(previousMonthButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[[_headerView forwardButton] addTarget:self action:@selector(nextMonthButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[[_headerView backButton] addTarget:self action:@selector(previousMonthButtonTapped:)
forControlEvents:UIControlEventTouchUpInside];
[[_headerView forwardButton] addTarget:self action:@selector(nextMonthButtonTapped:)
forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_headerView];

_weekDaysView = [[RDVCalendarWeekDaysHeader alloc] init];
Expand Down Expand Up @@ -130,6 +132,42 @@ - (void)updateMonthViewMonth:(NSDateComponents *)month {
[[self monthView] reloadData];
}

- (void)setSelectedDate:(NSDate *)selectedDate {
NSDate *oldDate = [self selectedDate];

if (![oldDate isEqualToDate:selectedDate]) {
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];

if (![self selectedDay]) {
[self setSelectedDay:[[NSDateComponents alloc] init]];
}

NSDateComponents *selectedDateComponents = [calendar components:NSYearCalendarUnit|
NSMonthCalendarUnit|
NSDayCalendarUnit
fromDate:selectedDate];

[[self selectedDay] setMonth:[selectedDateComponents month]];
[[self selectedDay] setYear:[selectedDateComponents year]];
[[self selectedDay] setDay:[selectedDateComponents day]];

self.month = [calendar components:NSYearCalendarUnit|
NSMonthCalendarUnit|
NSDayCalendarUnit|
NSWeekdayCalendarUnit|
NSCalendarCalendarUnit
fromDate:selectedDate];
self.month.day = 1;
[self updateMonthLabelMonth:self.month];

[self updateMonthViewMonth:self.month];
}
}

- (NSDate *)selectedDate {
return [[NSCalendar autoupdatingCurrentCalendar] dateFromComponents:[self selectedDay]];
}

#pragma mark - RDVCalendarMonthViewDelegate

- (RDVCalendarDayCell *)calendarMonthView:(RDVCalendarMonthView *)calendarMonthView dayCellForIndex:(NSInteger)index {
Expand Down Expand Up @@ -173,6 +211,10 @@ - (void)calendarMonthView:(RDVCalendarMonthView *)calendarMonthView didSelectDay
[[self selectedDay] setMonth:[[self month] month]];
[[self selectedDay] setYear:[[self month] year]];
[[self selectedDay] setDay:index + 1];

if ([[self delegate] respondsToSelector:@selector(calendarView:didSelectDate:)]) {
[[self delegate] calendarView:self didSelectDate:[self selectedDate]];
}
}

- (NSInteger)numberOfWeeksInCalendarMonthView:(RDVCalendarMonthView *)calendarMonthView {
Expand Down
1 change: 1 addition & 0 deletions RDVCalendarView/RDVCalendarViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
@interface RDVCalendarViewController : UIViewController <RDVCalendarViewDelegate>

@property (nonatomic) RDVCalendarView *calendarView;
@property (nonatomic) NSDate *selectedDate;

@end
16 changes: 1 addition & 15 deletions RDVCalendarView/RDVCalendarViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@ - (void)loadView {
self.view = _calendarView;
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
return YES;
Expand All @@ -46,16 +40,8 @@ - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfa

#pragma mark - RDVCalendarViewDelegate

- (BOOL)calendarView:(RDVCalendarView *)calendarView shouldSelectDate:(NSDate *)date {
return YES;
}

- (void)calendarView:(RDVCalendarView *)calendarView willSelectDate:(NSDate *)date {

}

- (void)calendarView:(RDVCalendarView *)calendarView didSelectDate:(NSDate *)date {

[self setSelectedDate:date];
}

@end
3 changes: 1 addition & 2 deletions RDVCalendarView/RDVCalendarWeekDaysHeader.m
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ - (void)setupWeekDays {

NSArray *weekSymbols = [formatter shortWeekdaySymbols];

// weekdaySymbols returns and array of strings. Keep in mind that they start from Sunday
// 0 - Sunday, 1 - Monday, 2 - Tuesday ...
// weekdaySymbols returns and array of strings
NSMutableArray *weekDays = [[NSMutableArray alloc] initWithCapacity:[weekSymbols count]];
for (NSInteger day = firstWeekDay; day < [weekSymbols count]; day++) {
[weekDays addObject:[weekSymbols objectAtIndex:day]];
Expand Down

0 comments on commit 8c5d2d3

Please sign in to comment.