Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a method to customise the marker colours via the data source delegate and to detect long press in the calendar #192

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
23 changes: 23 additions & 0 deletions src/TapkuLibrary/TKCalendarMonthView.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@
*/
- (void) calendarMonthView:(TKCalendarMonthView*)monthView didSelectDate:(NSDate*)date;

/** The highlighed date changed and was long pressed
@param monthView The calendar month view.
@param date The highlighted date.
*/
- (void) calendarMonthView:(TKCalendarMonthView*)monthView didLongPressDate:(NSDate*)date;


/** The calendar should change the current month to grid shown.
@param monthView The calendar month view.
Expand Down Expand Up @@ -128,4 +134,21 @@

*/
- (NSArray*) calendarMonthView:(TKCalendarMonthView*)monthView marksFromDate:(NSDate*)startDate toDate:(NSDate*)lastDate;



/** A data source for gathering formatting information about the non Selected Tile for a particular day


@param monthView The calendar month grid.
@param startDate The first date shown by the calendar month grid.
@param lastDate The last date shown by the calendar month grid.
@return Returns an array of UIColor objects corresponding the number of days specified in the start and last day parameters. Each UIColor variable will give a Color value that will be used to display a dot under the day.

*/

- (NSArray *) calendarMonthView:(TKCalendarMonthView*)monthView colorsForMarksFromDate:(NSDate *)startDate toDate:(NSDate*)lastDate;



@end
149 changes: 130 additions & 19 deletions src/TapkuLibrary/TKCalendarMonthView.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ @interface TKCalendarMonthTiles : UIView {
SEL action;

int firstOfPrev,lastOfPrev;

NSArray *marks;
NSArray *marksColors;


int today;
BOOL markWasOnToday;

Expand All @@ -58,7 +62,7 @@ @interface TKCalendarMonthTiles : UIView {
@property (strong,nonatomic) NSDate *monthDate;
@property (nonatomic, strong) NSMutableArray *accessibleElements;

- (id) initWithMonth:(NSDate*)date marks:(NSArray*)marks startDayOnSunday:(BOOL)sunday;
- (id) initWithMonth:(NSDate*)date marks:(NSArray*)marks marksColors:(NSArray *)colors startDayOnSunday:(BOOL)sunday;
- (void) setTarget:(id)target action:(SEL)action;

- (void) selectDay:(int)day;
Expand Down Expand Up @@ -214,16 +218,20 @@ + (NSArray*) rangeOfDatesInMonthGrid:(NSDate*)date startOnSunday:(BOOL)sunday{

return [NSArray arrayWithObjects:firstDate,lastDate,nil];
}
- (id) initWithMonth:(NSDate*)date marks:(NSArray*)markArray startDayOnSunday:(BOOL)sunday{
- (id) initWithMonth:(NSDate*)date marks:(NSArray*)markArray marksColors:(NSArray*)colors startDayOnSunday:(BOOL)sunday{
if(!(self=[super initWithFrame:CGRectZero])) return nil;

firstOfPrev = -1;
marks = markArray;
marksColors = colors;

_monthDate = date;
startOnSunday = sunday;

TKDateInformation dateInfo = [_monthDate dateInformationWithTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
firstWeekday = dateInfo.weekday;




NSDate *prev = [_monthDate previousMonth];
Expand Down Expand Up @@ -277,7 +285,7 @@ - (CGRect) rectForCellAtIndex:(int)index{

return CGRectMake(col*46, row*44+6, 47, 45);
}
- (void) drawTileInRect:(CGRect)r day:(int)day mark:(BOOL)mark font:(UIFont*)f1 font2:(UIFont*)f2{
- (void) drawTileInRect:(CGRect)r day:(int)day mark:(BOOL)mark markColor:(UIColor *)markColor font:(UIFont*)f1 font2:(UIFont*)f2{

NSString *str = [NSString stringWithFormat:@"%d",day];

Expand All @@ -289,13 +297,47 @@ - (void) drawTileInRect:(CGRect)r day:(int)day mark:(BOOL)mark font:(UIFont*)f1
alignment: UITextAlignmentCenter];

if(mark){
r.size.height = 10;
r.size.height = 13;
r.origin.y += 18;

[@"•" drawInRect: r
withFont: f2
lineBreakMode: UILineBreakModeWordWrap
alignment: UITextAlignmentCenter];
//draws the . in Tiles when not selected


NSMutableParagraphStyle *paraAttr = [[NSMutableParagraphStyle defaultParagraphStyle ] mutableCopy];
[paraAttr setAlignment:UITextAlignmentCenter];
[paraAttr setLineBreakMode:UILineBreakModeWordWrap];

NSDictionary *attrsDictionary;

if(markColor)
{
attrsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:f2,NSFontAttributeName,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Attributes for NSAttributedString such as NSFontAttributeName are available only for iOS 6 or newer. If one needs to use NSAttributedString for iOS 5 or older, it must be done by using CoreText (kCTFontAttributeName and such).

markColor,NSForegroundColorAttributeName,

paraAttr,NSParagraphStyleAttributeName,
nil];
}
else
{
attrsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:f2,NSFontAttributeName,
[UIColor grayColor],NSForegroundColorAttributeName,

paraAttr,NSParagraphStyleAttributeName,
nil];
}


NSAttributedString *drawingString = [[NSAttributedString alloc] initWithString:@"•" attributes:attrsDictionary];


[drawingString drawInRect:r];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drawInRect for NSAttributedString is available only for iOS 6 or newer.




// [@"-" drawInRect: r
// withFont: f2
// lineBreakMode: UILineBreakModeWordWrap
// alignment: UITextAlignmentCenter];
}


Expand Down Expand Up @@ -327,9 +369,9 @@ - (void) drawRect:(CGRect)rect {
for(int i = firstOfPrev;i<= lastOfPrev;i++){
r = [self rectForCellAtIndex:index];
if ([marks count] > 0)
[self drawTileInRect:r day:i mark:[[marks objectAtIndex:index] boolValue] font:font font2:font2];
[self drawTileInRect:r day:i mark:[[marks objectAtIndex:index] boolValue] markColor:[marksColors objectAtIndex:index] font:font font2:font2];
else
[self drawTileInRect:r day:i mark:NO font:font font2:font2];
[self drawTileInRect:r day:i mark:NO markColor:nil font:font font2:font2];
index++;
}
}
Expand All @@ -343,9 +385,9 @@ - (void) drawRect:(CGRect)rect {
if(today == i) [[UIColor whiteColor] set];

if ([marks count] > 0)
[self drawTileInRect:r day:i mark:[[marks objectAtIndex:index] boolValue] font:font font2:font2];
[self drawTileInRect:r day:i mark:[[marks objectAtIndex:index] boolValue] markColor:[marksColors objectAtIndex:index] font:font font2:font2];
else
[self drawTileInRect:r day:i mark:NO font:font font2:font2];
[self drawTileInRect:r day:i mark:NO markColor:nil font:font font2:font2];
if(today == i) [color set];
index++;
}
Expand All @@ -355,9 +397,9 @@ - (void) drawRect:(CGRect)rect {
while(index % 7 != 0){
r = [self rectForCellAtIndex:index] ;
if ([marks count] > 0)
[self drawTileInRect:r day:i mark:[[marks objectAtIndex:index] boolValue] font:font font2:font2];
[self drawTileInRect:r day:i mark:[[marks objectAtIndex:index] boolValue] markColor:[marksColors objectAtIndex:index] font:font font2:font2];
else
[self drawTileInRect:r day:i mark:NO font:font font2:font2];
[self drawTileInRect:r day:i mark:NO markColor:nil font:font font2:font2];
i++;
index++;
}
Expand Down Expand Up @@ -441,7 +483,7 @@ - (NSDate*) dateSelected{

}


#define LONG_PRESS_THRESHOLD 3.0
- (void) reactToTouch:(UITouch*)touch down:(BOOL)down{

CGPoint p = [touch locationInView:self];
Expand Down Expand Up @@ -519,6 +561,13 @@ - (void) reactToTouch:(UITouch*)touch down:(BOOL)down{
selectedDay = day;
selectedPortion = portion;
[target performSelector:action withObject:[NSArray arrayWithObject:[NSNumber numberWithInt:day]]];



// [NSObject cancelPreviousPerformRequestsWithTarget:target selector:@selector(tileDelay:)];
[NSObject cancelPreviousPerformRequestsWithTarget:target];
[target performSelector:@selector(tileDelay:) withObject:[NSArray arrayWithObject:[NSNumber numberWithInt:day]] afterDelay:LONG_PRESS_THRESHOLD];


}else if(down){
[target performSelector:action withObject:[NSArray arrayWithObjects:[NSNumber numberWithInt:day],[NSNumber numberWithInt:portion],nil]];
Expand All @@ -527,6 +576,7 @@ - (void) reactToTouch:(UITouch*)touch down:(BOOL)down{
}

}

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//[super touchesBegan:touches withEvent:event];
[self reactToTouch:[touches anyObject] down:NO];
Expand All @@ -536,6 +586,9 @@ - (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
[self reactToTouch:[touches anyObject] down:YES];

[NSObject cancelPreviousPerformRequestsWithTarget:target];
// [NSObject cancelPreviousPerformRequestsWithTarget:target selector:@selector(tileDelay:)];
}

- (UILabel *) currentDay{
Expand All @@ -554,6 +607,9 @@ - (UILabel *) currentDay{
return _currentDay;
}
- (UILabel *) dot{

//draws the . in Tiles when it is selected

if(_dot==nil){
CGRect r = self.selectedImageView.bounds;
r.origin.y += 29;
Expand Down Expand Up @@ -607,7 +663,7 @@ - (id) initWithSundayAsFirst:(BOOL)s{
self.backgroundColor = [UIColor grayColor];

sunday = s;
currentTile = [[TKCalendarMonthTiles alloc] initWithMonth:[[NSDate date] firstOfMonth] marks:nil startDayOnSunday:sunday];
currentTile = [[TKCalendarMonthTiles alloc] initWithMonth:[[NSDate date] firstOfMonth] marks:nil marksColors:nil startDayOnSunday:sunday];
[currentTile setTarget:self action:@selector(tile:)];

CGRect r = CGRectMake(0, 0, self.tileBox.bounds.size.width, self.tileBox.bounds.size.height + self.tileBox.frame.origin.y);
Expand Down Expand Up @@ -726,7 +782,12 @@ - (void) changeMonthAnimation:(UIView*)sender{

NSArray *dates = [TKCalendarMonthTiles rangeOfDatesInMonthGrid:nextMonth startOnSunday:sunday];
NSArray *ar = [self.dataSource calendarMonthView:self marksFromDate:[dates objectAtIndex:0] toDate:[dates lastObject]];
TKCalendarMonthTiles *newTile = [[TKCalendarMonthTiles alloc] initWithMonth:nextMonth marks:ar startDayOnSunday:sunday];
NSArray *colors = [self.dataSource calendarMonthView:self colorsForMarksFromDate:[dates objectAtIndex:0] toDate:[dates lastObject]];




TKCalendarMonthTiles *newTile = [[TKCalendarMonthTiles alloc] initWithMonth:nextMonth marks:ar marksColors:colors startDayOnSunday:sunday];
[newTile setTarget:self action:@selector(tile:)];


Expand Down Expand Up @@ -847,8 +908,11 @@ - (void) selectDate:(NSDate*)date{

NSArray *dates = [TKCalendarMonthTiles rangeOfDatesInMonthGrid:month startOnSunday:sunday];
NSArray *data = [self.dataSource calendarMonthView:self marksFromDate:[dates objectAtIndex:0] toDate:[dates lastObject]];
NSArray *colors = [self.dataSource calendarMonthView:self colorsForMarksFromDate:[dates objectAtIndex:0] toDate:[dates lastObject]];

TKCalendarMonthTiles *newTile = [[TKCalendarMonthTiles alloc] initWithMonth:month
marks:data
marks:data
marksColors:colors
startDayOnSunday:sunday];
[newTile setTarget:self action:@selector(tile:)];
[currentTile removeFromSuperview];
Expand All @@ -870,8 +934,10 @@ - (void) selectDate:(NSDate*)date{
- (void) reload{
NSArray *dates = [TKCalendarMonthTiles rangeOfDatesInMonthGrid:[currentTile monthDate] startOnSunday:sunday];
NSArray *ar = [self.dataSource calendarMonthView:self marksFromDate:[dates objectAtIndex:0] toDate:[dates lastObject]];
NSArray *colors = [self.dataSource calendarMonthView:self colorsForMarksFromDate:[dates objectAtIndex:0] toDate:[dates lastObject]];


TKCalendarMonthTiles *refresh = [[TKCalendarMonthTiles alloc] initWithMonth:[currentTile monthDate] marks:ar startDayOnSunday:sunday];
TKCalendarMonthTiles *refresh = [[TKCalendarMonthTiles alloc] initWithMonth:[currentTile monthDate] marks:ar marksColors:colors startDayOnSunday:sunday];
[refresh setTarget:self action:@selector(tile:)];

[self.tileBox addSubview:refresh];
Expand Down Expand Up @@ -925,6 +991,51 @@ - (void) tile:(NSArray*)ar{

}

- (void) tileDelay:(NSArray*)ar{

if([ar count] < 2){

if([self.delegate respondsToSelector:@selector(calendarMonthView:didLongPressDate:)])
[self.delegate calendarMonthView:self didLongPressDate:[self dateSelected]];

}else{

int direction = [[ar lastObject] intValue];
UIButton *b = direction > 1 ? self.rightArrow : self.leftArrow;

NSDate* newMonth = [self dateForMonthChange:b];
if ([self.delegate respondsToSelector:@selector(calendarMonthView:monthShouldChange:animated:)] && ![delegate calendarMonthView:self monthShouldChange:newMonth animated:YES])
return;

if ([self.delegate respondsToSelector:@selector(calendarMonthView:monthWillChange:animated:)])
[self.delegate calendarMonthView:self monthWillChange:newMonth animated:YES];



[self changeMonthAnimation:b];

int day = [[ar objectAtIndex:0] intValue];


// thanks rafael
TKDateInformation info = [[currentTile monthDate] dateInformationWithTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
info.day = day;

NSDate *dateForMonth = [NSDate dateFromDateInformation:info timeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
[currentTile selectDay:day];


if([self.delegate respondsToSelector:@selector(calendarMonthView:didLongPressDate:)])
[self.delegate calendarMonthView:self didLongPressDate:dateForMonth];

if([self.delegate respondsToSelector:@selector(calendarMonthView:monthDidChange:animated:)])
[self.delegate calendarMonthView:self monthDidChange:dateForMonth animated:YES];


}

}

#pragma mark Properties
- (UIImageView *) topBackground{
if(_topBackground==nil){
Expand Down
3 changes: 3 additions & 0 deletions src/TapkuLibrary/TKCalendarMonthViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,8 @@ - (NSArray*) calendarMonthView:(TKCalendarMonthView*)monthView marksFromDate:(NS
return nil;
}

- (NSArray *) calendarMonthView:(TKCalendarMonthView*)monthView colorsForMarksFromDate:(NSDate *)startDate toDate:(NSDate*)lastDate{
return nil;
}

@end