diff --git a/DayFlow/DFDatePickerDayCell.h b/DayFlow/DFDatePickerDayCell.h index d14fc77..347feb4 100644 --- a/DayFlow/DFDatePickerDayCell.h +++ b/DayFlow/DFDatePickerDayCell.h @@ -5,5 +5,6 @@ @property (nonatomic, readwrite, assign) DFDatePickerDate date; @property (nonatomic, getter=isEnabled) BOOL enabled; +@property (nonatomic, assign) BOOL hasActivity; @end diff --git a/DayFlow/DFDatePickerDayCell.m b/DayFlow/DFDatePickerDayCell.m index e565732..35bf112 100644 --- a/DayFlow/DFDatePickerDayCell.m +++ b/DayFlow/DFDatePickerDayCell.m @@ -6,6 +6,7 @@ + (id) cacheKeyForPickerDate:(DFDatePickerDate)date; + (id) fetchObjectForKey:(id)key withCreator:(id(^)(void))block; @property (nonatomic, readonly, strong) UIImageView *imageView; @property (nonatomic, readonly, strong) UIView *overlayView; +@property (nonatomic, strong) UIImageView *activityDot; @end @implementation DFDatePickerDayCell @@ -39,6 +40,12 @@ - (void) setSelected:(BOOL)selected { [self setNeedsLayout]; } +- (void) setHasActivity:(BOOL)hasActivity +{ + _hasActivity = hasActivity; + [self setNeedsLayout]; +} + - (void) layoutSubviews { [super layoutSubviews]; @@ -95,6 +102,22 @@ - (void) layoutSubviews { }]; + self.activityDot.image = [[self class] fetchObjectForKey:[[self class] cacheKeyForActivity:self.hasActivity] withCreator:^id{ + UIGraphicsBeginImageContextWithOptions(self.activityDot.bounds.size, NO, self.window.screen.scale); + CGContextRef context = UIGraphicsGetCurrentContext(); + + if (self.hasActivity){ + CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor); + CGContextFillEllipseInRect(context, self.activityDot.bounds); + } + + UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); + UIGraphicsEndImageContext(); + return image; + }]; + + self.activityDot.hidden = !self.hasActivity; + self.overlayView.hidden = !(self.selected || self.highlighted); } @@ -119,6 +142,17 @@ - (UIImageView *) imageView { return _imageView; } +- (UIImageView *) activityDot { + if (!_activityDot) { + CGFloat circleWidth = 5.0; + CGRect circleRect = (CGRectMake((self.bounds.size.width / 2) - (circleWidth / 2), self.bounds.size.height - (circleWidth * 2), circleWidth, circleWidth)); + + _activityDot = [[UIImageView alloc] initWithFrame:circleRect]; + [self.contentView addSubview:_activityDot]; + } + return _activityDot; +} + + (NSCache *) imageCache { static NSCache *cache; static dispatch_once_t onceToken; @@ -132,6 +166,10 @@ + (id) cacheKeyForPickerDate:(DFDatePickerDate)date { return @(date.day); } ++ (id) cacheKeyForActivity:(BOOL)hasActivity { + return [NSString stringWithFormat:@"HasActivity-%d", hasActivity]; +} + + (id) fetchObjectForKey:(id)key withCreator:(id(^)(void))block { id answer = [[self imageCache] objectForKey:key]; if (!answer) { diff --git a/DayFlow/DFDatePickerView.h b/DayFlow/DFDatePickerView.h index 8700f38..cf01010 100644 --- a/DayFlow/DFDatePickerView.h +++ b/DayFlow/DFDatePickerView.h @@ -1,9 +1,19 @@ #import +@class DFDatePickerView; + +@protocol DFDatePickerViewDataSource + +- (BOOL) datePickerView:(DFDatePickerView *)datePickerView shouldShowActivityOnDate:(NSDate *)date; + +@end + @interface DFDatePickerView : UIView - (instancetype) initWithCalendar:(NSCalendar *)calendar; @property (nonatomic, readwrite, strong) NSDate *selectedDate; +@property (nonatomic, weak) id dataSource; + @end diff --git a/DayFlow/DFDatePickerView.m b/DayFlow/DFDatePickerView.m index 0bc2f42..f2ce4e3 100644 --- a/DayFlow/DFDatePickerView.m +++ b/DayFlow/DFDatePickerView.m @@ -183,7 +183,8 @@ - (void) shiftDatesByComponents:(NSDateComponents *)components { NSIndexPath *fromIndexPath = [cv indexPathForCell:((UICollectionViewCell *)visibleCells[0]) ]; NSInteger fromSection = fromIndexPath.section; NSDate *fromSectionOfDate = [self dateForFirstDayInSection:fromSection]; - CGPoint fromSectionOrigin = [self convertPoint:[cvLayout layoutAttributesForItemAtIndexPath:fromIndexPath].frame.origin fromView:cv]; + UICollectionViewLayoutAttributes *fromAttrs = [cvLayout layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:fromSection]]; + CGPoint fromSectionOrigin = [self convertPoint:fromAttrs.frame.origin fromView:cv]; _fromDate = [self pickerDateFromDate:[self.calendar dateByAddingComponents:components toDate:[self dateFromPickerDate:self.fromDate] options:0]]; _toDate = [self pickerDateFromDate:[self.calendar dateByAddingComponents:components toDate:[self dateFromPickerDate:self.toDate] options:0]]; @@ -318,6 +319,7 @@ - (DFDatePickerDayCell *)collectionView:(UICollectionView *)collectionView cellF cell.date = cellPickerDate; cell.enabled = ((firstDayPickerDate.year == cellPickerDate.year) && (firstDayPickerDate.month == cellPickerDate.month)); cell.selected = [self.selectedDate isEqualToDate:cellDate]; + cell.hasActivity = [self.dataSource datePickerView:self shouldShowActivityOnDate:cellDate]; return cell; diff --git a/DayFlow/DFDatePickerViewController.h b/DayFlow/DFDatePickerViewController.h index 5afac2f..bb5ae9e 100644 --- a/DayFlow/DFDatePickerViewController.h +++ b/DayFlow/DFDatePickerViewController.h @@ -5,6 +5,7 @@ @protocol DFDatePickerViewControllerDelegate - (void) datePickerViewController:(DFDatePickerViewController *)controller didSelectDate:(NSDate *)date; +- (BOOL) datePickerViewController:(DFDatePickerViewController *)controller shouldShowActivityOnDate:(NSDate *)date; @end diff --git a/DayFlow/DFDatePickerViewController.m b/DayFlow/DFDatePickerViewController.m index 1e934c6..7bd3b2c 100644 --- a/DayFlow/DFDatePickerViewController.m +++ b/DayFlow/DFDatePickerViewController.m @@ -1,5 +1,9 @@ #import "DFDatePickerViewController.h" +@interface DFDatePickerViewController () + +@end + @implementation DFDatePickerViewController @synthesize datePickerView = _datePickerView; @@ -13,6 +17,7 @@ - (DFDatePickerView *) datePickerView { _datePickerView = [DFDatePickerView new]; _datePickerView.frame = self.view.bounds; _datePickerView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight; + _datePickerView.dataSource = self; } return _datePickerView; } @@ -37,4 +42,9 @@ - (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:( } } +- (BOOL) datePickerView:(DFDatePickerView *)datePickerView shouldShowActivityOnDate:(NSDate *)date +{ + return [self.delegate datePickerViewController:self shouldShowActivityOnDate:date]; +} + @end