Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DSLCalendarView/DSLCalendarDayView.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ enum {
@property (nonatomic, assign) DSLCalendarDayViewPositionInWeek positionInWeek;
@property (nonatomic, assign) DSLCalendarDayViewSelectionState selectionState;
@property (nonatomic, assign, getter = isInCurrentMonth) BOOL inCurrentMonth;
@property (nonatomic, assign) BOOL isDisabled;

@property (nonatomic, strong, readonly) NSDate *dayAsDate;

Expand Down
4 changes: 4 additions & 0 deletions DSLCalendarView/DSLCalendarDayView.m
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ - (void)drawDayNumber {
[[UIColor whiteColor] set];
}

if (self.isDisabled) {
[[UIColor colorWithWhite:0.702 alpha:1.000] setFill];
}

UIFont *textFont = [UIFont boldSystemFontOfSize:17.0];
CGSize textSize = [_labelText sizeWithFont:textFont];

Expand Down
2 changes: 2 additions & 0 deletions DSLCalendarView/DSLCalendarMonthView.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@

@property (nonatomic, copy, readonly) NSDateComponents *month;
@property (nonatomic, strong, readonly) NSSet *dayViews;
@property (nonatomic, strong) NSDate *minimumDate;
@property (nonatomic, strong) NSDate *maximumDate;

// Designated initialiser
- (id)initWithMonth:(NSDateComponents*)month width:(CGFloat)width dayViewClass:(Class)dayViewClass dayViewHeight:(CGFloat)dayViewHeight;
Expand Down
32 changes: 32 additions & 0 deletions DSLCalendarView/DSLCalendarMonthView.m
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ - (void)createDayViews {
break;
}

BOOL isDisabled = NO;
if ( (self.minimumDate && [day.date isBeforeDay:self.minimumDate]) || (self.maximumDate && [self.maximumDate isBeforeDay:day.date]) ) {
isDisabled = YES;
}
dayView.isDisabled = isDisabled;

[self.dayViewsDictionary setObject:dayView forKey:[self dayViewKeyForDay:day]];
[self addSubview:dayView];
}
Expand Down Expand Up @@ -227,4 +233,30 @@ - (DSLCalendarDayView*)dayViewForDay:(NSDateComponents*)day {
return [self.dayViewsDictionary objectForKey:[self dayViewKeyForDay:day]];
}

- (void)setMinimumDate:(NSDate *)minimumDate {
_minimumDate = minimumDate;
//redraw dayViews
[self reloadDayViewsMinMaxDate];
}

- (void)setMaximumDate:(NSDate *)maximumDate {
_maximumDate = maximumDate;
//redraw dayViews
[self reloadDayViewsMinMaxDate];
}

- (void)reloadDayViewsMinMaxDate {
[self.dayViews enumerateObjectsUsingBlock:^(DSLCalendarDayView *dayView, BOOL *stop) {
NSDateComponents *day = dayView.day;
BOOL isDisabled = NO;
if ( (self.minimumDate && [day.date isBeforeDay:self.minimumDate]) || (self.maximumDate && [self.maximumDate isBeforeDay:day.date]) ) {
isDisabled = YES;
}
if (isDisabled != dayView.isDisabled) {
dayView.isDisabled = isDisabled;
[dayView setNeedsDisplay];
}
}];
}

@end
2 changes: 2 additions & 0 deletions DSLCalendarView/DSLCalendarRange.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,6 @@
- (BOOL)containsDay:(NSDateComponents*)day;
- (BOOL)containsDate:(NSDate*)date;

- (BOOL)isEqualToRange:(DSLCalendarRange *)calendarRange;

@end
7 changes: 7 additions & 0 deletions DSLCalendarView/DSLCalendarRange.m
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,11 @@ - (BOOL)containsDate:(NSDate*)date {
return YES;
}

- (BOOL)isEqualToRange:(DSLCalendarRange *)calendarRange {
if ([_startDate isEqualToDate:[calendarRange.startDay date]] && [_endDate isEqualToDate:[calendarRange.endDay date]]) {
return YES;
}
return NO;
}

@end
2 changes: 2 additions & 0 deletions DSLCalendarView/DSLCalendarView.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
@property (nonatomic, copy) NSDateComponents *visibleMonth;
@property (nonatomic, strong) DSLCalendarRange *selectedRange;
@property (nonatomic, assign) BOOL showDayCalloutView;
@property (nonatomic, strong) NSDate *minimumDate;
@property (nonatomic, strong) NSDate *maximumDate;

+ (Class)monthSelectorViewClass;
+ (Class)monthViewClass;
Expand Down
40 changes: 39 additions & 1 deletion DSLCalendarView/DSLCalendarView.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ @interface DSLCalendarView ()
@property (nonatomic, strong) UIView *monthContainerView;
@property (nonatomic, strong) UIView *monthContainerViewContentView;
@property (nonatomic, strong) DSLCalendarMonthSelectorView *monthSelectorView;
@property (nonatomic, strong) NSDateComponents *lastTouchedDay;

@end

Expand Down Expand Up @@ -165,6 +166,20 @@ - (void)setVisibleMonth:(NSDateComponents *)visibleMonth animated:(BOOL)animated
[self positionViewsForMonth:_visibleMonth fromMonth:fromMonth animated:animated];
}

- (void)setMinimumDate:(NSDate *)minimumDate {
_minimumDate = minimumDate;
[self.monthViews enumerateKeysAndObjectsUsingBlock:^(id key, DSLCalendarMonthView *monthView, BOOL *stop) {
monthView.minimumDate = minimumDate;
}];
}

- (void)setMaximumDate:(NSDate *)maximumDate {
_maximumDate = maximumDate;
[self.monthViews enumerateKeysAndObjectsUsingBlock:^(id key, DSLCalendarMonthView *monthView, BOOL *stop) {
monthView.maximumDate = maximumDate;
}];
}


#pragma mark - Events

Expand Down Expand Up @@ -210,6 +225,8 @@ - (DSLCalendarMonthView*)cachedOrCreatedMonthViewForMonth:(NSDateComponents*)mon

[monthView updateDaySelectionStatesForRange:self.selectedRange];
}
monthView.minimumDate = self.minimumDate;
monthView.maximumDate = self.maximumDate;

return monthView;
}
Expand Down Expand Up @@ -361,11 +378,19 @@ - (BOOL)monthStartsOnFirstDayOfWeek:(NSDateComponents*)month {

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
DSLCalendarDayView *touchedView = [self dayViewForTouches:touches];
self.lastTouchedDay = touchedView.day;
if (touchedView == nil) {
self.draggingStartDay = nil;
return;
}

if (self.minimumDate && [touchedView.day.date isBeforeDay:self.minimumDate]) {
return;
}
if (self.maximumDate && [self.maximumDate isBeforeDay:touchedView.day.date]) {
return;
}

self.draggingStartDay = touchedView.day;
self.draggingFixedDay = touchedView.day;
self.draggedOffStartDay = NO;
Expand Down Expand Up @@ -403,6 +428,19 @@ - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
return;
}

BOOL isTheSameDay = [self.lastTouchedDay.date isEqualToDate:touchedView.day.date];
self.lastTouchedDay = touchedView.day;
if (isTheSameDay) {
return;
}

if (self.minimumDate && [touchedView.day.date isBeforeDay:self.minimumDate]) {
return;
}
if (self.maximumDate && [self.maximumDate isBeforeDay:touchedView.day.date]) {
return;
}

DSLCalendarRange *newRange;
if ([touchedView.day.date compare:self.draggingFixedDay.date] == NSOrderedAscending) {
newRange = [[DSLCalendarRange alloc] initWithStartDay:touchedView.day endDay:self.draggingFixedDay];
Expand Down Expand Up @@ -431,6 +469,7 @@ - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
}

DSLCalendarDayView *touchedView = [self dayViewForTouches:touches];
self.lastTouchedDay = touchedView.day;
if (touchedView == nil) {
self.draggingStartDay = nil;
return;
Expand Down Expand Up @@ -498,7 +537,6 @@ - (DSLCalendarDayView*)dayViewForTouches:(NSSet*)touches {
return nil;
}


#pragma mark - Day callout view methods

- (void)positionCalloutViewForDayView:(DSLCalendarDayView*)dayView {
Expand Down
6 changes: 6 additions & 0 deletions DSLCalendarView/NSDate+DSLCalendarView.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,10 @@
- (NSDateComponents*)dslCalendarView_dayWithCalendar:(NSCalendar*)calendar;
- (NSDateComponents*)dslCalendarView_monthWithCalendar:(NSCalendar*)calendar;


- (BOOL)isBeforeDay:(NSDate *)day2;
// NSDateAdditions.m
// SymbioCalendar
- (NSDate *)cc_dateByMovingToBeginningOfDay;

@end
13 changes: 13 additions & 0 deletions DSLCalendarView/NSDate+DSLCalendarView.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,17 @@ - (NSDateComponents*)dslCalendarView_monthWithCalendar:(NSCalendar*)calendar {
return [calendar components:NSCalendarCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit fromDate:self];
}

- (BOOL)isBeforeDay:(NSDate *)day2 {
return ([[self cc_dateByMovingToBeginningOfDay] compare:[day2 cc_dateByMovingToBeginningOfDay]] == NSOrderedAscending);
}

- (NSDate *)cc_dateByMovingToBeginningOfDay {
unsigned int flags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents* parts = [[NSCalendar currentCalendar] components:flags fromDate:self];
[parts setHour:0];
[parts setMinute:0];
[parts setSecond:0];
return [[NSCalendar currentCalendar] dateFromComponents:parts];
}

@end
2 changes: 2 additions & 0 deletions Example/DSLCalendarViewExample/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ - (void)viewDidLoad
// Do any additional setup after loading the view, typically from a nib.

self.calendarView.delegate = self;
self.calendarView.minimumDate = [NSDate date];
self.calendarView.maximumDate = [NSDate dateWithTimeIntervalSinceNow:86400*7];
}

- (void)viewDidUnload
Expand Down