Skip to content

Commit

Permalink
New method, improved layout
Browse files Browse the repository at this point in the history
New method to limit the days shown. Good for limiting the view to a
select number of days such as that of an event.

Improvements to the layout of events so that a single event that may
have more than 1 other overlapping it is layed out better.
  • Loading branch information
AaronBratcher committed Dec 16, 2013
1 parent 5342f5b commit 6efcd74
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 21 deletions.
@@ -0,0 +1,86 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0500"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "1D6058900D05DD3D006BFB54"
BuildableName = "CalendarUI.app"
BlueprintName = "CalendarUI"
ReferencedContainer = "container:CalendarUI.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
buildConfiguration = "Debug">
<Testables>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "1D6058900D05DD3D006BFB54"
BuildableName = "CalendarUI.app"
BlueprintName = "CalendarUI"
ReferencedContainer = "container:CalendarUI.xcodeproj">
</BuildableReference>
</MacroExpansion>
</TestAction>
<LaunchAction
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
buildConfiguration = "Debug"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
allowLocationSimulation = "YES">
<BuildableProductRunnable>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "1D6058900D05DD3D006BFB54"
BuildableName = "CalendarUI.app"
BlueprintName = "CalendarUI"
ReferencedContainer = "container:CalendarUI.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
buildConfiguration = "Release"
debugDocumentVersioning = "YES">
<BuildableProductRunnable>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "1D6058900D05DD3D006BFB54"
BuildableName = "CalendarUI.app"
BlueprintName = "CalendarUI"
ReferencedContainer = "container:CalendarUI.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>CalendarUI.xcscheme</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
<key>SuppressBuildableAutocreation</key>
<dict>
<key>1D6058900D05DD3D006BFB54</key>
<dict>
<key>primary</key>
<true/>
</dict>
</dict>
</dict>
</plist>
1 change: 1 addition & 0 deletions Classes/Views/MADayView.h
Expand Up @@ -64,6 +64,7 @@
@property (nonatomic,unsafe_unretained) IBOutlet id<MADayViewDelegate> delegate;

- (void)reloadData;
- (void)limitDaysToFirstDate:(NSDate *)firstDate lastDate:(NSDate*)lastDate;

@end

Expand Down
116 changes: 95 additions & 21 deletions Classes/Views/MADayView.m
Expand Up @@ -106,6 +106,13 @@ - (void)addEvent:(MAEvent *)event;

@end

@interface MADayView()

@property (strong) NSDate *firstDate;
@property (strong) NSDate *lastDate;

@end

@interface MADayView (PrivateMethods)
- (void)setupCustomInitialisation;
- (void)changeDay:(UIButton *)sender;
Expand All @@ -132,6 +139,9 @@ @implementation MADayView
@synthesize autoScrollToFirstEvent=_autoScrollToFirstEvent;
@synthesize labelFontSize=_labelFontSize;
@synthesize delegate=_delegate;
@synthesize firstDate=_firstDate;
@synthesize lastDate=_lastDate;


- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
Expand Down Expand Up @@ -170,13 +180,13 @@ - (void)layoutSubviews {
CGRectGetMinY(self.bounds),
CGRectGetWidth(self.bounds), TOP_BACKGROUND_HEIGHT + 10);
self.leftArrow.frame = CGRectMake(CGRectGetMinX(self.topBackground.bounds),
(int) (CGRectGetHeight(self.topBackground.bounds) - ARROW_HEIGHT) / 2,
(int) ((CGRectGetHeight(self.topBackground.bounds) - ARROW_HEIGHT) / 2) + 6,
ARROW_WIDTH, ARROW_HEIGHT);
self.rightArrow.frame = CGRectMake(CGRectGetWidth(self.topBackground.bounds) - ARROW_WIDTH,
(int) (CGRectGetHeight(self.topBackground.bounds) - ARROW_HEIGHT) / 2,
(int) ((CGRectGetHeight(self.topBackground.bounds) - ARROW_HEIGHT) / 2) + 6,
ARROW_WIDTH, ARROW_HEIGHT);
self.dateLabel.frame = CGRectMake(CGRectGetMaxX(self.leftArrow.bounds),
(int) (CGRectGetHeight(self.topBackground.bounds) - ARROW_HEIGHT) / 2,
(int) ((CGRectGetHeight(self.topBackground.bounds) - ARROW_HEIGHT) / 2) + 6,
CGRectGetWidth(self.topBackground.bounds) - CGRectGetWidth(self.leftArrow.bounds) - CGRectGetWidth(self.rightArrow.bounds),
ARROW_HEIGHT);

Expand Down Expand Up @@ -308,6 +318,19 @@ - (void)setDataSource:(id <MADayViewDataSource>)dataSource {
}

- (void)setDay:(NSDate *)date {
if (self.firstDate) {
self.leftArrow.hidden = [[self.firstDate earlierDate: date] isEqualToDate:date];
if ([[self.firstDate earlierDate:date] isEqualToDate:date]) {
date = [self.firstDate copy];
}
}
if (self.lastDate) {
self.rightArrow.hidden = [[self.lastDate laterDate:date] isEqualToDate:date];
if ([[self.lastDate laterDate:date] isEqualToDate:date]) {
date = [self.lastDate copy];
}
}

NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:date];
[components setHour:0];
[components setMinute:0];
Expand Down Expand Up @@ -359,6 +382,22 @@ - (void)reloadData {
}
}

- (void)limitDaysToFirstDate:(NSDate *)firstDate lastDate:(NSDate*)lastDate {
NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:firstDate];
[components setHour:0];
[components setMinute:0];
[components setSecond:0];
self.firstDate = [CURRENT_CALENDAR dateFromComponents:components];

components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:lastDate];
[components setHour:0];
[components setMinute:0];
[components setSecond:0];
self.lastDate = [CURRENT_CALENDAR dateFromComponents:components];

self.day = self.firstDate;
}

- (void)changeDay:(UIButton *)sender {
if (ARROW_LEFT == sender.tag) {
self.day = [self previousDayFromDate:_day];
Expand Down Expand Up @@ -636,45 +675,80 @@ - (void)layoutSubviews {

NSArray *subviews = self.subviews;
int max = [subviews count];
MADayEventView *curEv = nil, *prevEv = nil, *firstEvent = nil;
MADayEventView *curEv = nil, *prevEv = nil, *nextEv = nil, *firstEvent = nil;
const CGFloat spacePerMinute = (_lineY[1] - _lineY[0]) / 60.f;

// set initial view positions
for (i=0; i < max; i++) {
if (![NSStringFromClass([[subviews objectAtIndex:i] class])isEqualToString:@"MADayEventView"]) {
continue;
}

prevEv = curEv;
curEv = [subviews objectAtIndex:i];

curEv.frame = CGRectMake((int) _lineX,
(int) (spacePerMinute * [curEv.event minutesSinceMidnight] + _lineY[0]),
(int) (self.bounds.size.width - _lineX),
(int) (spacePerMinute * [curEv.event durationInMinutes]));

/*
* Layout intersecting events to two columns.
*/
if (CGRectIntersectsRect(curEv.frame, prevEv.frame))
{
prevEv.frame = CGRectMake((int) (prevEv.frame.origin.x),
(int) (prevEv.frame.origin.y),
(int) (prevEv.frame.size.width / 2.f),
(int) (prevEv.frame.size.height));

curEv.frame = CGRectMake((int) (curEv.frame.origin.x + (curEv.frame.size.width / 2.f)),
(int) (curEv.frame.origin.y),
(int) (curEv.frame.size.width / 2.f),
(int) (curEv.frame.size.height));
}

[curEv setNeedsDisplay];

if (!firstEvent || curEv.frame.origin.y < firstEvent.frame.origin.y) {
firstEvent = curEv;
}

}

curEv = nil;
BOOL shrinkCurEv = NO;
// check for overlaps and compensate
for (i=0; i < max; i++) {
if (![NSStringFromClass([[subviews objectAtIndex:i] class])isEqualToString:@"MADayEventView"]) {
continue;
}
prevEv = curEv;
curEv = [subviews objectAtIndex:i];

for (int j=i+1; j < max; j++) {
if (![NSStringFromClass([[subviews objectAtIndex:j] class])isEqualToString:@"MADayEventView"]) {
continue;
}
nextEv = [subviews objectAtIndex:j];

/*
* Layout intersecting events to two columns.
*/

if (CGRectIntersectsRect(curEv.frame, nextEv.frame))
{
if (curEv.frame.origin.x == nextEv.frame.origin.x) {
shrinkCurEv = YES;
}


nextEv.frame = CGRectMake((int) (nextEv.frame.origin.x + ((curEv.frame.origin.x == _lineX || curEv.frame.origin.x == nextEv.frame.origin.x) ? (nextEv.frame.size.width / 2.f) : 0)),
(int) (nextEv.frame.origin.y),
(int) (nextEv.frame.size.width / 2.f),
(int) (nextEv.frame.size.height));


[nextEv setNeedsDisplay];
}
}

if (shrinkCurEv) {
shrinkCurEv = NO;
curEv.frame = CGRectMake((int) (curEv.frame.origin.x),
(int) (curEv.frame.origin.y),
(int) (curEv.frame.size.width / 2.f),
(int) (curEv.frame.size.height));

[curEv setNeedsDisplay];
}
}


if (self.dayView.autoScrollToFirstEvent) {
CGPoint autoScrollPoint;

Expand Down

0 comments on commit 6efcd74

Please sign in to comment.