From cb9ae838b8752a75e64803f9508c94aa2ce655be Mon Sep 17 00:00:00 2001 From: Simon Maddox Date: Wed, 26 Feb 2014 17:41:03 +0000 Subject: [PATCH 1/3] Activity dots can now be shown on each cell --- DayFlow/DFDatePickerDayCell.h | 1 + DayFlow/DFDatePickerDayCell.m | 42 ++++++++++++++++++++++++++++ DayFlow/DFDatePickerView.h | 10 +++++++ DayFlow/DFDatePickerView.m | 4 ++- DayFlow/DFDatePickerViewController.h | 1 + DayFlow/DFDatePickerViewController.m | 10 +++++++ 6 files changed, 67 insertions(+), 1 deletion(-) 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..cafb8b5 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,26 @@ - (void) layoutSubviews { }]; + self.activityDot.image = [[self class] fetchObjectForKey:[[self class] cacheKeyForActivity:self.hasActivity] withCreator:^id{ + UIGraphicsBeginImageContextWithOptions(self.activityDot.bounds.size, YES, self.window.screen.scale); + CGContextRef context = UIGraphicsGetCurrentContext(); + + if (self.hasActivity){ + + CGContextSetFillColorWithColor(context, [UIColor colorWithRed:53.0f/256.0f green:145.0f/256.0f blue:195.0f/256.0f alpha:1.0f].CGColor); + CGContextFillRect(context, self.activityDot.bounds); + + 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 +146,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 +170,10 @@ + (id) cacheKeyForPickerDate:(DFDatePickerDate)date { return @(date.day); } ++ (id) cacheKeyForActivity:(BOOL)hasActivity { + return @(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 From 283d246ad19bd63bd68d2d59292f4a1d5b24a204 Mon Sep 17 00:00:00 2001 From: Simon Maddox Date: Wed, 26 Feb 2014 17:43:38 +0000 Subject: [PATCH 2/3] Dots do not have the correct background color when shown in another month --- DayFlow/DFDatePickerDayCell.m | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/DayFlow/DFDatePickerDayCell.m b/DayFlow/DFDatePickerDayCell.m index cafb8b5..15da033 100644 --- a/DayFlow/DFDatePickerDayCell.m +++ b/DayFlow/DFDatePickerDayCell.m @@ -103,14 +103,10 @@ - (void) layoutSubviews { }]; self.activityDot.image = [[self class] fetchObjectForKey:[[self class] cacheKeyForActivity:self.hasActivity] withCreator:^id{ - UIGraphicsBeginImageContextWithOptions(self.activityDot.bounds.size, YES, self.window.screen.scale); + UIGraphicsBeginImageContextWithOptions(self.activityDot.bounds.size, NO, self.window.screen.scale); CGContextRef context = UIGraphicsGetCurrentContext(); if (self.hasActivity){ - - CGContextSetFillColorWithColor(context, [UIColor colorWithRed:53.0f/256.0f green:145.0f/256.0f blue:195.0f/256.0f alpha:1.0f].CGColor); - CGContextFillRect(context, self.activityDot.bounds); - CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor); CGContextFillEllipseInRect(context, self.activityDot.bounds); } From 42c034be43aeb7460894474395b66a6cde46fefe Mon Sep 17 00:00:00 2001 From: Simon Maddox Date: Thu, 27 Feb 2014 10:26:29 +0000 Subject: [PATCH 3/3] I was reusing cache keys for a different purpose, and wondered why it was producing odd results. Woops. --- DayFlow/DFDatePickerDayCell.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DayFlow/DFDatePickerDayCell.m b/DayFlow/DFDatePickerDayCell.m index 15da033..35bf112 100644 --- a/DayFlow/DFDatePickerDayCell.m +++ b/DayFlow/DFDatePickerDayCell.m @@ -167,7 +167,7 @@ + (id) cacheKeyForPickerDate:(DFDatePickerDate)date { } + (id) cacheKeyForActivity:(BOOL)hasActivity { - return @(hasActivity); + return [NSString stringWithFormat:@"HasActivity-%d", hasActivity]; } + (id) fetchObjectForKey:(id)key withCreator:(id(^)(void))block {