Skip to content

Commit

Permalink
Merge pull request jumartin#53 from patrickHelmedica/master
Browse files Browse the repository at this point in the history
Added customizable new timed event duration
  • Loading branch information
jumartin authored Nov 10, 2016
2 parents 6de3682 + 0f228d6 commit c7764c7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 22 deletions.
33 changes: 19 additions & 14 deletions CalendarLib/MGCDayPlannerEKViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -398,20 +398,20 @@ - (BOOL)dayPlannerView:(MGCDayPlannerView*)view canMoveEventOfType:(MGCEventType

- (void)dayPlannerView:(MGCDayPlannerView*)view moveEventOfType:(MGCEventType)type atIndex:(NSUInteger)index date:(NSDate*)date toType:(MGCEventType)targetType date:(NSDate*)targetDate
{
EKEvent *ev = [self eventOfType:type atIndex:index date:date];

if (ev) {
NSDateComponents *duration = [self.calendar components:NSCalendarUnitMinute fromDate:ev.startDate toDate:ev.endDate options:0];
if (ev.allDay && targetType == MGCTimedEventType) {
duration.minute = 60;
}
NSDate *end = [self.calendar dateByAddingComponents:duration toDate:targetDate options:0];

// allDay property has to be set before start and end dates !
ev.allDay = (targetType == MGCAllDayEventType);
ev.startDate = targetDate;
ev.endDate = end;
EKEvent *ev = [self eventOfType:type atIndex:index date:date];

if (ev) {
NSDateComponents *duration = [self.calendar components:NSMinuteCalendarUnit fromDate:ev.startDate toDate:ev.endDate options:0];
if (ev.allDay && targetType == MGCTimedEventType) {
duration.minute = view.durationForNewTimedEvent * 60;
}
NSDate *end = [self.calendar dateByAddingComponents:duration toDate:targetDate options:0];

// allDay property has to be set before start and end dates !
ev.allDay = (targetType == MGCAllDayEventType);
ev.startDate = targetDate;
ev.endDate = end;

[self.eventKitSupport saveEvent:ev completion:^(BOOL completion) {
[self.dayPlannerView endInteraction];
}];
Expand All @@ -435,8 +435,13 @@ - (void)dayPlannerView:(MGCDayPlannerView *)view createNewEventOfType:(MGCEventT

EKEvent *ev = [EKEvent eventWithEventStore:self.eventStore];
ev.startDate = date;

NSDateComponents *comps = [NSDateComponents new];
comps.hour = 1;
comps.day = ((NSInteger) view.durationForNewTimedEvent) / (60 * 60 * 24);
comps.hour = (((NSInteger) view.durationForNewTimedEvent) / (60 * 60)) - (comps.day * 24);
comps.minute = (((NSInteger) view.durationForNewTimedEvent) / 60) - (comps.day * 24 * 60) - (comps.hour * 60);
comps.second = ((NSInteger) round(view.durationForNewTimedEvent)) % 60;

ev.endDate = [self.calendar dateByAddingComponents:comps toDate:date options:0];
ev.allDay = (type == MGCAllDayEventType) ? YES : NO;

Expand Down
6 changes: 6 additions & 0 deletions CalendarLib/MGCDayPlannerView.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,12 @@ typedef NS_ENUM(NSUInteger, MGCDayPlannerTimeMark) {
*/
@property (nonatomic) BOOL canMoveEvents;

/*!
@abstract The duration of newly created timed events
@discussion The default duration is 1 hour.
*/
@property (nonatomic) NSTimeInterval durationForNewTimedEvent;

/*!
@abstract The object that acts as the delegate of the day planner view.
@discussion The delegate must adopt the `MGCDayPlannerViewDelegate` protocol.
Expand Down
19 changes: 11 additions & 8 deletions CalendarLib/MGCDayPlannerView.m
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ - (void)setup
_dimmedTimeRangesCache = [[OSCache alloc]init];
_dimmedTimeRangesCache.countLimit = 200;

_durationForNewTimedEvent = 60 * 60;

self.backgroundColor = [UIColor whiteColor];
self.autoresizesSubviews = NO;

Expand Down Expand Up @@ -1131,9 +1133,8 @@ - (CGRect)rectForNewEventOfType:(MGCEventType)type atDate:(NSDate*)date
CGFloat x = section * self.dayColumnSize.width;

if (type == MGCTimedEventType) {
NSDateComponents *comp = [self.calendar components:NSCalendarUnitHour|NSCalendarUnitMinute fromDate:date];
CGFloat y = [self offsetFromTime:(comp.hour*3600. + comp.minute*60.) rounding:0];
CGRect rect = CGRectMake(x, y, self.dayColumnSize.width, self.hourSlotHeight);
CGFloat y = [self offsetFromTime:self.durationForNewTimedEvent rounding:0];
CGRect rect = CGRectMake(x, y, self.dayColumnSize.width, self.interactiveCellTimedEventHeight);
return [self convertRect:rect fromView:self.timedEventsView];
}
else if (type == MGCAllDayEventType) {
Expand Down Expand Up @@ -1170,7 +1171,8 @@ - (void)handleLongPress:(UILongPressGestureRecognizer*)gesture
}
}
else { // an empty space was touched
NSDate *date = [self dateAtPoint:CGPointMake(ptSelf.x, ptSelf.y - self.hourSlotHeight / 2) rounded:YES];
CGFloat createEventSlotHeight = floor(self.durationForNewTimedEvent * self.hourSlotHeight / 60.0f / 60.0f);
NSDate *date = [self dateAtPoint:CGPointMake(ptSelf.x, ptSelf.y - createEventSlotHeight / 2) rounded:YES];

if (![self beginCreateEventOfType:type atDate:date]) {
gesture.enabled = NO;
Expand Down Expand Up @@ -1225,10 +1227,12 @@ - (BOOL)beginCreateEventOfType:(MGCEventType)type atDate:(NSDate*)date
NSAssert([self.visibleDays containsDate:date], @"beginCreateEventOfType:atDate for non visible date");

if (!self.canCreateEvents) return NO;


self.interactiveCellTimedEventHeight = floor(self.durationForNewTimedEvent * self.hourSlotHeight / 60.0f / 60.0f);

self.isInteractiveCellForNewEvent = YES;
self.interactiveCellType = type;
self.interactiveCellTouchPoint = CGPointMake(0, self.hourSlotHeight / 2);
self.interactiveCellTouchPoint = CGPointMake(0, self.interactiveCellTimedEventHeight / 2);
self.interactiveCellDate = date;

self.interactiveCell = [[MGCInteractiveEventView alloc]initWithFrame:CGRectZero];
Expand All @@ -1251,8 +1255,7 @@ - (BOOL)beginCreateEventOfType:(MGCEventType)type atDate:(NSDate*)date
}
}

CGRect rect = [self rectForNewEventOfType:type atDate:date];
self.interactiveCellTimedEventHeight = self.hourSlotHeight;
CGRect rect = [self rectForNewEventOfType:type atDate:date];
self.interactiveCell.frame = rect;
[self addSubview:self.interactiveCell];
self.interactiveCell.hidden = NO;
Expand Down

0 comments on commit c7764c7

Please sign in to comment.