-
Notifications
You must be signed in to change notification settings - Fork 203
/
Copy pathMGCMonthPlannerViewLayout.m
executable file
·205 lines (167 loc) · 7.5 KB
/
MGCMonthPlannerViewLayout.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//
// MGCMonthPlannerViewLayout.m
// Graphical Calendars Library for iOS
//
// Distributed under the MIT License
// Get the latest version from here:
//
// https://github.com/jumartin/Calendar
//
// Copyright (c) 2014-2015 Julien Martin
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
#import "MGCMonthPlannerViewLayout.h"
#import "MGCMonthPlannerView.h"
const CGFloat kMonthHeaderMargin = 3.;
@interface MGCMonthPlannerViewLayout()
@property (nonatomic) NSDictionary *layoutInfo;
@property (nonatomic) CGFloat contentHeight;
@end
@implementation MGCMonthPlannerViewLayout
- (CGFloat)widthForColumnRange:(NSRange)range
{
CGFloat availableWidth = self.collectionView.bounds.size.width - (self.monthInsets.left + self.monthInsets.right);
CGFloat columnWidth = availableWidth / 7;
if (NSMaxRange(range) == 7) {
return availableWidth - columnWidth * (7 - range.length);
}
return columnWidth * range.length;
}
- (CGFloat)columnWidth:(NSUInteger)colIndex
{
return [self widthForColumnRange:NSMakeRange(colIndex, 1)];
}
#pragma mark - NSObject
- (id)init
{
if (self = [super init]) {
_monthInsets = UIEdgeInsetsMake(20, 0, 20, 0);
_rowHeight = 140;
_dayHeaderHeight = 28;
_showEvents = YES;
}
return self;
}
#pragma mark - UICollectionViewLayout
- (void)prepareLayout
{
NSUInteger numberOfMonths = [self.collectionView numberOfSections];
NSMutableDictionary *layoutInfo = [NSMutableDictionary dictionary];
NSMutableDictionary *dayCellsInfo = [NSMutableDictionary dictionary];
NSMutableDictionary *monthsInfo = [NSMutableDictionary dictionary];
NSMutableDictionary *rowsInfo = [NSMutableDictionary dictionary];
CGFloat y = 0;
CGFloat totalWidth = self.collectionView.bounds.size.width - (self.monthInsets.left + self.monthInsets.right);
for (NSUInteger month = 0; month < numberOfMonths; month++)
{
NSUInteger col = [self.delegate collectionView:self.collectionView layout:self columnForDayAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:month]];
NSUInteger daysInMonth = [self.collectionView numberOfItemsInSection:month];
NSUInteger numRows = ceil((col + daysInMonth) / 7.);
NSUInteger day = 0;
CGRect monthRect = { .origin = CGPointMake(0, y) };
NSIndexPath *path = [NSIndexPath indexPathForItem:1 inSection:month];;
CGRect headerFrame = CGRectMake(self.monthInsets.left, y, totalWidth, self.monthInsets.top);
if (self.alignMonthHeaders) {
CGFloat xOffset = [self widthForColumnRange:NSMakeRange(0, col)];
headerFrame.origin.x += (xOffset + kMonthHeaderMargin);
headerFrame.size.width -= (xOffset + 2*kMonthHeaderMargin);
}
UICollectionViewLayoutAttributes *attribs = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:MonthHeaderViewKind withIndexPath:path];
attribs.frame = headerFrame;
[monthsInfo setObject:attribs forKey:path];
y += self.monthInsets.top;
for (NSUInteger row = 0; row < numRows; row++)
{
NSRange colRange = NSMakeRange(col, MIN(7 - col, daysInMonth - day));
if (self.showEvents) {
NSIndexPath *path = [NSIndexPath indexPathForItem:day inSection:month];
UICollectionViewLayoutAttributes *attribs = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:MonthRowViewKind withIndexPath:path];
CGFloat x = [self widthForColumnRange:NSMakeRange(0, col)] + self.monthInsets.left;
CGFloat width = [self widthForColumnRange:NSMakeRange(col, colRange.length)];
attribs.frame = CGRectMake(x, y + self.dayHeaderHeight, width, self.rowHeight - self.dayHeaderHeight);
attribs.zIndex = 1;
[rowsInfo setObject:attribs forKey:path];
}
for (; col < NSMaxRange(colRange); col++, day++)
{
NSIndexPath *path = [NSIndexPath indexPathForItem:day inSection:month];
UICollectionViewLayoutAttributes *attribs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:path];
CGFloat x = [self widthForColumnRange:NSMakeRange(0, col)] + self.monthInsets.left;
CGFloat width = [self widthForColumnRange:NSMakeRange(col, 1)];
attribs.frame = CGRectMake(x, y, width, self.rowHeight);
[dayCellsInfo setObject:attribs forKey:path];
}
y += self.rowHeight;
col = 0;
}
y += self.monthInsets.bottom;
monthRect.size = CGSizeMake(self.collectionView.bounds.size.width, y - monthRect.origin.y);
path = [NSIndexPath indexPathForItem:0 inSection:month];
attribs = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:MonthBackgroundViewKind withIndexPath:path];
attribs.frame = UIEdgeInsetsInsetRect(monthRect, self.monthInsets);
attribs.zIndex = 2;
[monthsInfo setObject:attribs forKey:path];
}
self.contentHeight = y;
[layoutInfo setObject:dayCellsInfo forKey:@"DayCellInfo"];
[layoutInfo setObject:monthsInfo forKey:@"MonthInfo"];
[layoutInfo setObject:rowsInfo forKey:@"RowsInfo"];
self.layoutInfo = layoutInfo;
}
- (CGSize)collectionViewContentSize
{
return CGSizeMake(self.collectionView.bounds.size.width, self.contentHeight);
}
- (NSArray*)layoutAttributesForElementsInRect:(CGRect)rect
{
NSMutableArray *allAttribs = [NSMutableArray arrayWithCapacity:self.layoutInfo.count];
for (NSString *kind in self.layoutInfo)
{
NSDictionary *attributesDict = [self.layoutInfo objectForKey:kind];
for (NSIndexPath *key in attributesDict)
{
UICollectionViewLayoutAttributes *attributes = [attributesDict objectForKey:key];
if (CGRectIntersectsRect(rect, attributes.frame))
{
[allAttribs addObject:attributes];
}
}
}
return allAttribs;
}
- (UICollectionViewLayoutAttributes*)layoutAttributesForItemAtIndexPath:(NSIndexPath*)indexPath
{
NSDictionary *layout = [self.layoutInfo objectForKey:@"DayCellInfo"];
return [layout objectForKey:indexPath];
}
- (UICollectionViewLayoutAttributes*)layoutAttributesForSupplementaryViewOfKind:(NSString*)kind atIndexPath:(NSIndexPath*)indexPath
{
UICollectionViewLayoutAttributes *attribs = nil;
if ([kind isEqualToString:MonthBackgroundViewKind] || [kind isEqualToString:MonthHeaderViewKind]) {
NSDictionary *layout = [self.layoutInfo objectForKey:@"MonthInfo"];
attribs = [layout objectForKey:indexPath];
}
else if ([kind isEqualToString:MonthRowViewKind]) {
NSDictionary *layout = [self.layoutInfo objectForKey:@"RowsInfo"];
attribs = [layout objectForKey:indexPath];
}
return attribs;
}
@end