-
Notifications
You must be signed in to change notification settings - Fork 144
/
IBPCollectionCompositionalLayoutSolver.m
323 lines (272 loc) · 15.8 KB
/
IBPCollectionCompositionalLayoutSolver.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#import "IBPCollectionCompositionalLayoutSolver.h"
#import "IBPCollectionCompositionalLayoutSolverResult.h"
#import "IBPNSCollectionLayoutContainer.h"
#import "IBPNSCollectionLayoutDimension.h"
#import "IBPNSCollectionLayoutEnvironment.h"
#import "IBPNSCollectionLayoutGroup_Private.h"
#import "IBPNSCollectionLayoutItem_Private.h"
#import "IBPNSCollectionLayoutSection_Private.h"
#import "IBPNSCollectionLayoutSize_Private.h"
#import "IBPNSCollectionLayoutSpacing.h"
@interface IBPCollectionCompositionalLayoutSolver()
@property (nonatomic, readwrite, copy) IBPNSCollectionLayoutSection *layoutSection;
@property (nonatomic) UICollectionViewScrollDirection scrollDirection;
@property (nonatomic) CGRect layoutFrame;
@property (nonatomic) CGRect contentFrame;
@property (nonatomic) NSMutableArray<IBPCollectionCompositionalLayoutSolverResult *> *results;
@end
@implementation IBPCollectionCompositionalLayoutSolver
+ (instancetype)solverWithLayoutSection:(IBPNSCollectionLayoutSection *)section
scrollDirection:(UICollectionViewScrollDirection)scrollDirection {
return [[self alloc] initWithLayoutSection:section scrollDirection:scrollDirection];
}
- (instancetype)initWithLayoutSection:(IBPNSCollectionLayoutSection *)section
scrollDirection:(UICollectionViewScrollDirection)scrollDirection {
self = [super init];
if (self) {
self.layoutSection = section;
self.scrollDirection = scrollDirection;
self.layoutFrame = CGRectZero;
self.contentFrame = CGRectZero;
self.results = [[NSMutableArray alloc] init];
}
return self;
}
- (void)solveForContainer:(IBPNSCollectionLayoutContainer *)container traitCollection:(UITraitCollection *)traitCollection {
CGSize collectionContentSize = container.effectiveContentSize;
IBPNSDirectionalEdgeInsets sectionContentInsets = self.layoutSection.contentInsets;
IBPNSCollectionLayoutContainer *sectionContainer = [[IBPNSCollectionLayoutContainer alloc] initWithContentSize:collectionContentSize
contentInsets:sectionContentInsets];
IBPNSCollectionLayoutSection *section = self.layoutSection;
IBPNSCollectionLayoutGroup *group = section.group;
CGSize groupContentSize = [group.layoutSize effectiveSizeForContainer:sectionContainer];
IBPNSCollectionLayoutContainer *groupContainer = [[IBPNSCollectionLayoutContainer alloc] initWithContentSize:groupContentSize
contentInsets:group.contentInsets];
CGRect layoutFrame = self.layoutFrame;
layoutFrame.origin.x += sectionContentInsets.leading;
layoutFrame.origin.y += sectionContentInsets.top;
layoutFrame.size = groupContentSize;
self.layoutFrame = layoutFrame;
CGRect contentFrame = self.contentFrame;
contentFrame.origin = layoutFrame.origin;
self.contentFrame = contentFrame;
[self solveGroup:group forContainer:groupContainer containerFrame:layoutFrame traitCollection:traitCollection];
}
- (void)solveGroup:(IBPNSCollectionLayoutGroup *)group forContainer:(IBPNSCollectionLayoutContainer *)container
containerFrame:(CGRect)containerFrame traitCollection:(UITraitCollection *)traitCollection {
__block CGRect contentFrame = self.contentFrame;
CGFloat interItemFixedSpacing = 0;
if (group.interItemSpacing.isFixedSpacing) {
interItemFixedSpacing = group.interItemSpacing.spacing;
}
CGFloat interItemFlexibleSpacing = 0;
if (group.interItemSpacing.isFlexibleSpacing) {
interItemFlexibleSpacing = group.interItemSpacing.spacing;
}
if (group.isCustomGroup) {
IBPNSCollectionLayoutEnvironment *environment = [[IBPNSCollectionLayoutEnvironment alloc] init];
environment.container = container;
environment.traitCollection = traitCollection;
NSArray<IBPNSCollectionLayoutGroupCustomItem *> *items = group.customGroupItemProvider(environment);
for (IBPNSCollectionLayoutGroupCustomItem *item in items) {
CGRect customFrame = item.frame;
customFrame.origin.x += contentFrame.origin.x;
customFrame.origin.y += contentFrame.origin.y;
[self.results addObject:[IBPCollectionCompositionalLayoutSolverResult resultWithLayoutItem:nil frame:customFrame]];
}
return;
}
if (group.count > 0) {
IBPNSCollectionLayoutItem *item = group.subitems[0];
IBPNSDirectionalEdgeInsets contentInsets = item.contentInsets;
CGSize itemSize = [item.layoutSize effectiveSizeForContainer:container];
IBPNSCollectionLayoutContainer *itemContainer = [[IBPNSCollectionLayoutContainer alloc] initWithContentSize:itemSize contentInsets:contentInsets];
if (item.isGroup) {
IBPNSCollectionLayoutGroup *nestedGroup = (IBPNSCollectionLayoutGroup *)item;
if (group.isHorizontalGroup) {
if (floor(CGRectGetMaxX(contentFrame)) > floor(CGRectGetMaxX(containerFrame))) {
return;
}
itemSize.width = (CGRectGetWidth(containerFrame) - interItemFixedSpacing * (group.count - 1)) / group.count;
}
if (group.isVerticalGroup) {
if (floor(CGRectGetMaxX(contentFrame)) > floor(CGRectGetMaxX(containerFrame))) {
return;
}
itemSize.height = (CGRectGetHeight(containerFrame) - interItemFixedSpacing * (group.count - 1)) / group.count;
}
CGRect nestedContainerFrame = containerFrame;
nestedContainerFrame.origin = contentFrame.origin;
nestedContainerFrame.size = itemSize;
for (NSInteger i = 0; i < group.count; i++) {
self.contentFrame = contentFrame;
[self solveGroup:nestedGroup forContainer:itemContainer containerFrame:nestedContainerFrame traitCollection:traitCollection];
if (group.isHorizontalGroup) {
contentFrame.origin.x += interItemFixedSpacing + itemSize.width;
}
if (group.isVerticalGroup) {
contentFrame.origin.y += interItemFixedSpacing + itemSize.height;
}
}
return;
}
if (group.isHorizontalGroup) {
if (floor(CGRectGetMaxX(contentFrame)) > floor(CGRectGetMaxX(containerFrame))) {
return;
}
itemSize.width = (CGRectGetWidth(containerFrame) - interItemFixedSpacing * (group.count - 1)) / group.count;
contentFrame.size = itemSize;
for (NSInteger i = 0; i < group.count; i++) {
CGRect cellFrame = UIEdgeInsetsInsetRect(contentFrame, UIEdgeInsetsMake(contentInsets.top, contentInsets.leading, contentInsets.bottom, contentInsets.trailing));
[self.results addObject:[IBPCollectionCompositionalLayoutSolverResult resultWithLayoutItem:item frame:cellFrame]];
contentFrame.origin.x += interItemFixedSpacing + CGRectGetWidth(contentFrame);
}
}
if (group.isVerticalGroup) {
if (floor(CGRectGetMaxY(contentFrame)) > floor(CGRectGetMaxY(containerFrame))) {
return;
}
itemSize.height = (CGRectGetHeight(containerFrame) - interItemFixedSpacing * (group.count - 1)) / group.count;
contentFrame.size = itemSize;
for (NSInteger i = 0; i < group.count; i++) {
CGRect cellFrame = UIEdgeInsetsInsetRect(contentFrame, UIEdgeInsetsMake(contentInsets.top, contentInsets.leading, contentInsets.bottom, contentInsets.trailing));
[self.results addObject:[IBPCollectionCompositionalLayoutSolverResult resultWithLayoutItem:item frame:cellFrame]];
contentFrame.origin.y += interItemFixedSpacing + CGRectGetHeight(contentFrame);
}
}
} else {
NSMutableArray *groupedItemResults = [[NSMutableArray alloc] init];
[group enumerateItemsWithHandler:^(IBPNSCollectionLayoutItem * _Nonnull item, BOOL * _Nonnull stop) {
IBPNSDirectionalEdgeInsets contentInsets = item.contentInsets;
CGSize itemSize = [item.layoutSize effectiveSizeForContainer:container];
IBPNSCollectionLayoutContainer *itemContainer = [[IBPNSCollectionLayoutContainer alloc] initWithContentSize:itemSize contentInsets:item.contentInsets];
if (item.isGroup) {
IBPNSCollectionLayoutGroup *nestedGroup = (IBPNSCollectionLayoutGroup *)item;
CGRect nestedContainerFrame = containerFrame;
nestedContainerFrame.origin = contentFrame.origin;
nestedContainerFrame.size = [nestedGroup.layoutSize effectiveSizeForContainer:container];
if (group.isHorizontalGroup) {
if (floor(CGRectGetMaxX(nestedContainerFrame)) > floor(CGRectGetMaxX(containerFrame))) {
*stop = YES;
return;
}
}
if (group.isVerticalGroup) {
if (floor(CGRectGetMaxY(nestedContainerFrame)) > floor(CGRectGetMaxY(containerFrame))) {
*stop = YES;
return;
}
}
self.contentFrame = contentFrame;
[self solveGroup:nestedGroup forContainer:itemContainer containerFrame:nestedContainerFrame traitCollection:traitCollection];
if (group.isHorizontalGroup) {
if (floor(CGRectGetMaxX(contentFrame)) > floor(CGRectGetMaxX(containerFrame))) {
*stop = YES;
return;
}
contentFrame.origin.x += itemSize.width;
}
if (group.isVerticalGroup) {
if (floor(CGRectGetMaxY(contentFrame)) > floor(CGRectGetMaxY(containerFrame))) {
*stop = YES;
return;
}
contentFrame.origin.y += itemSize.height;
}
if (group == self.layoutSection.group) {
if (self.scrollDirection == UICollectionViewScrollDirectionVertical && group.layoutSize.heightDimension.isEstimated) {
CGRect layoutFrame = self.layoutFrame;
layoutFrame.size.height = CGRectGetMaxY(contentFrame);
self.layoutFrame = layoutFrame;
}
if (self.scrollDirection == UICollectionViewScrollDirectionHorizontal && group.layoutSize.widthDimension.isEstimated) {
CGRect layoutFrame = self.layoutFrame;
layoutFrame.size.width = CGRectGetMaxY(contentFrame);
self.layoutFrame = layoutFrame;
}
}
return;
}
contentFrame.size = itemSize;
if (group.isHorizontalGroup) {
if (floor(CGRectGetMaxX(contentFrame)) > floor(CGRectGetMaxX(containerFrame))) {
*stop = YES;
return;
}
CGRect cellFrame = UIEdgeInsetsInsetRect(contentFrame, UIEdgeInsetsMake(contentInsets.top, contentInsets.leading, contentInsets.bottom, contentInsets.trailing));
IBPCollectionCompositionalLayoutSolverResult *result = [IBPCollectionCompositionalLayoutSolverResult resultWithLayoutItem:item frame:cellFrame];
[self.results addObject:result];
[groupedItemResults addObject:result];
contentFrame.origin.x += CGRectGetWidth(contentFrame) + interItemFixedSpacing + interItemFlexibleSpacing;
}
if (group.isVerticalGroup) {
if (floor(CGRectGetMaxY(contentFrame)) > floor(CGRectGetMaxY(containerFrame))) {
*stop = YES;
return;
}
CGRect cellFrame = UIEdgeInsetsInsetRect(contentFrame, UIEdgeInsetsMake(contentInsets.top, contentInsets.leading, contentInsets.bottom, contentInsets.trailing));
IBPCollectionCompositionalLayoutSolverResult *result = [IBPCollectionCompositionalLayoutSolverResult resultWithLayoutItem:item frame:cellFrame];
[self.results addObject:result];
[groupedItemResults addObject:result];
contentFrame.origin.y += CGRectGetHeight(contentFrame) + interItemFixedSpacing + interItemFlexibleSpacing;
}
}];
if (interItemFlexibleSpacing > 0 && groupedItemResults.count > 1) {
CGSize totalSize = CGSizeZero;
for (IBPCollectionCompositionalLayoutSolverResult *result in groupedItemResults) {
totalSize.width += CGRectGetWidth(result.frame);
totalSize.height += CGRectGetHeight(result.frame);
}
if (group.isHorizontalGroup) {
CGFloat actualSpacing = (CGRectGetWidth(containerFrame) - totalSize.width) / (groupedItemResults.count - 1);
CGFloat delta = floor(actualSpacing - interItemFlexibleSpacing);
for (NSInteger i = 1; i < groupedItemResults.count; i++) {
IBPCollectionCompositionalLayoutSolverResult *result = groupedItemResults[i];
CGRect frame = result.frame;
frame.origin.x += delta * i;
result.frame = frame;
}
}
if (group.isVerticalGroup) {
CGFloat actualSpacing = (CGRectGetHeight(containerFrame) - totalSize.height) / (groupedItemResults.count - 1);
CGFloat delta = floor(actualSpacing - interItemFlexibleSpacing);
for (NSInteger i = 1; i < groupedItemResults.count; i++) {
IBPCollectionCompositionalLayoutSolverResult *result = groupedItemResults[i];
CGRect frame = result.frame;
frame.origin.y += delta * i;
result.frame = frame;
}
}
}
}
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
NSArray<IBPCollectionCompositionalLayoutSolverResult *> *results = self.results;
NSInteger count = results.count;
CGFloat interGroupSpacing = self.layoutSection.interGroupSpacing;
CGPoint offset = CGPointZero;
BOOL scrollsOrthogonally = self.layoutSection.scrollsOrthogonally;
UICollectionViewScrollDirection scrollDirection = self.scrollDirection;
if (scrollsOrthogonally) {
scrollDirection = scrollDirection == UICollectionViewScrollDirectionVertical ? UICollectionViewScrollDirectionHorizontal : UICollectionViewScrollDirectionVertical;
}
if (scrollDirection == UICollectionViewScrollDirectionVertical) {
offset.y += CGRectGetHeight(self.layoutFrame) * (indexPath.item / count) + interGroupSpacing * (indexPath.item / count);
}
if (scrollDirection == UICollectionViewScrollDirectionHorizontal) {
offset.x += CGRectGetWidth(self.layoutFrame) * (indexPath.item / count) + interGroupSpacing * (indexPath.item / count);
}
UICollectionViewLayoutAttributes *layoutAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
IBPCollectionCompositionalLayoutSolverResult *result = [results objectAtIndex:indexPath.item % count];
CGRect frame = result.frame;
frame.origin.x += offset.x;
frame.origin.y += offset.y;
layoutAttributes.frame = frame;
return layoutAttributes;
}
- (IBPNSCollectionLayoutItem *)layoutItemAtIndexPath:(NSIndexPath *)indexPath {
NSArray<IBPCollectionCompositionalLayoutSolverResult *> *results = self.results;
IBPCollectionCompositionalLayoutSolverResult *result = [results objectAtIndex:indexPath.item % results.count];
return result.layoutItem;
}
@end