-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFGViewGroup.m
424 lines (347 loc) · 13.7 KB
/
FGViewGroup.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
//
// FGViewGroup.m
// train-helper
//
// Created by 易元 白 on 15/3/10.
// Copyright (c) 2015年 eldereal. All rights reserved.
//
#import "FGViewGroup.h"
#import "FGReuseItemPool.h"
#import "FGTypes.h"
#import "FGInternal.h"
#import "FGBasicViews.h"
#import "UIView+FGStylable.h"
#import <objc/runtime.h>
#import <REKit/REKit.h>
static void * EndGroupMethodKey = &EndGroupMethodKey;
@interface UIView (FGViewGroup)
@property (nonatomic, strong) FGReuseItemPool *pool;
@property (nonatomic, assign) CGFloat styleBottomOrRight;
@end
@interface FGScrollGroupView : UIView
@property (nonatomic, strong) UIScrollView *layoutView;
@end
typedef NS_ENUM(NSUInteger, FGViewGroupLayoutMode)
{
FGViewGroupLayoutModeFree,
FGViewGroupLayoutModeVertical,
FGViewGroupLayoutModeHorizontal
};
@interface FGViewGroup : NSObject<FGContext>
@property (nonatomic, strong) UIView *groupView;
@property (nonatomic, assign) FGViewGroupLayoutMode mode;
@property (nonatomic, strong) UIView *previousView;
@property (nonatomic) NSUInteger subviewIndex;
@end
@implementation FastGui (FGViewGroup)
+ (void) beginGroup
{
[self beginGroupWithReuseId:[FGInternal callerPositionAsReuseId] styleClass:nil isCustom:NO layoutMode:FGViewGroupLayoutModeFree];
}
+ (void)beginGroupWithStyleClass:(NSString *)styleClass
{
[self beginGroupWithReuseId:[FGInternal callerPositionAsReuseId] styleClass:styleClass isCustom:NO layoutMode:FGViewGroupLayoutModeFree];
}
+ (void)beginGroupWithNextView
{
[self beginGroupWithReuseId:nil styleClass:nil isCustom:YES layoutMode:FGViewGroupLayoutModeFree];
}
+ (void)beginVerticalGroup
{
[self beginGroupWithReuseId:[FGInternal callerPositionAsReuseId] styleClass:nil isCustom:NO layoutMode:FGViewGroupLayoutModeVertical];
}
+ (void)beginVerticalGroupWithStyleClass:(NSString *)styleClass
{
[self beginGroupWithReuseId:[FGInternal callerPositionAsReuseId] styleClass:styleClass isCustom:NO layoutMode:FGViewGroupLayoutModeVertical];
}
+ (void)beginVerticalGroupWithNextView
{
[self beginGroupWithReuseId:[FGInternal callerPositionAsReuseId] styleClass:nil isCustom:YES layoutMode:FGViewGroupLayoutModeVertical];
}
+ (void)beginHorizontalGroup
{
[self beginGroupWithReuseId:[FGInternal callerPositionAsReuseId] styleClass:nil isCustom:NO layoutMode:FGViewGroupLayoutModeHorizontal];
}
+ (void)beginHorizontalGroupWithStyleClass:(NSString *)styleClass
{
[self beginGroupWithReuseId:[FGInternal callerPositionAsReuseId] styleClass:styleClass isCustom:NO layoutMode:FGViewGroupLayoutModeHorizontal];
}
+ (void)beginHorizontalGroupWithNextView
{
[self beginGroupWithReuseId:[FGInternal callerPositionAsReuseId] styleClass:nil isCustom:YES layoutMode:FGViewGroupLayoutModeHorizontal];
}
+ (void)beginGroupWithReuseId: (NSString *) reuseId styleClass:(NSString *)styleClass isCustom: (BOOL) isCustom layoutMode: (FGViewGroupLayoutMode) mode
{
FGViewGroup *group = [[FGViewGroup alloc] init];
group.subviewIndex = 0;
group.groupView = nil;
group.mode = mode;
[self pushContext: group];
if(!isCustom){
[self customViewWithClass:styleClass reuseId:reuseId initBlock:^UIView *(UIView *reuseView) {
if (reuseView == nil) {
reuseView = [[UIView alloc] init];
}
return reuseView;
} resultBlock:nil];
}
}
+ (void) endGroup
{
[FastGui customData:EndGroupMethodKey data:nil];
}
@end
@implementation FGViewGroup
@synthesize parentContext;
static void * IndexPropertyKey = &IndexPropertyKey;
- (void)reloadGui
{
[parentContext reloadGui];
}
- (void) customViewControllerWithReuseId:(NSString *)reuseId initBlock:(FGInitCustomViewControllerBlock)initBlock
{
[parentContext customViewControllerWithReuseId:reuseId initBlock:initBlock];
}
- (void)dismissViewController
{
[parentContext dismissViewController];
}
- (id) customData:(void *)key data:(NSDictionary *)data
{
if (key == EndGroupMethodKey){
[self endGroup];
}else{
return [self.parentContext customData:key data:data];
}
return nil;
}
-(void) setAndOverrideVerticalLayouts:(UIView *)view
{
view.bottomConstraint = nil;
if (self.previousView == nil) {
view.topConstraint = [self.groupView updateConstraint:view.topConstraint view1:view attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.groupView attribute:NSLayoutAttributeTop multiplier:1 constant:0];
}else{
view.topConstraint = [self.groupView updateConstraint:view.topConstraint view1:view attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.previousView attribute:NSLayoutAttributeBottom multiplier:1 constant:self.previousView.styleBottomOrRight];
__weak UIView *weakView = view;
[self.previousView respondsToSelector:@selector(styleWithBottom:) withKey:nil usingBlock:^(UIView *self, CGFloat bottom){
weakView.topConstraint.constant = bottom;
}];
}
[view respondsToSelector:@selector(styleWithTop:) withKey:nil usingBlock:^(UIView * self, CGFloat top){
self.topConstraint.constant = top;
}];
[view respondsToSelector:@selector(styleWithBottom:) withKey:nil usingBlock:^(UIView * self, CGFloat bottom){
self.styleBottomOrRight = bottom;
}];
[view respondsToSelector:@selector(styleWithTopPercentage:) withKey:nil usingBlock:^(id self){
printf("'topPercentage' style of this view is disabled because it is in a vertical layout group.");
}];
[view respondsToSelector:@selector(styleWithBottomPercentage:) withKey:nil usingBlock:^(id self){
printf("'bottomPercentage' style of this view is disabled because it is in a vertical layout group.");
}];
}
- (void) setAndOverrideHorizontalLayouts:(UIView *)view
{
if (self.previousView == nil) {
view.leftConstraint = [self.groupView updateConstraint:view.leftConstraint view1:view attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.groupView attribute:NSLayoutAttributeLeft multiplier:1 constant:0];
}else{
view.leftConstraint = [self.groupView updateConstraint:view.leftConstraint view1:view attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.previousView attribute:NSLayoutAttributeRight multiplier:1 constant:self.previousView.styleBottomOrRight];
__weak UIView *weakView = view;
[self.previousView respondsToSelector:@selector(styleWithRight:) withKey:nil usingBlock:^(UIView *self, CGFloat right){
weakView.leftConstraint.constant = right;
}];
}
[view respondsToSelector:@selector(styleWithLeft:) withKey:nil usingBlock:^(UIView * self, CGFloat left){
self.leftConstraint.constant = left;
}];
[view respondsToSelector:@selector(styleWithRight:) withKey:nil usingBlock:^(UIView * self, CGFloat right){
self.styleBottomOrRight = right;
}];
[view respondsToSelector:@selector(styleWithLeftPercentage:) withKey:nil usingBlock:^(id self){
printf("'leftPercentage' style of this view is disabled because it is in a horizontal layout group.");
}];
[view respondsToSelector:@selector(styleWithRightPercentage:) withKey:nil usingBlock:^(id self){
printf("'rightPercentage' style of this view is disabled because it is in a horizontal layout group.");
}];
}
- (id) customViewWithReuseId:(NSString *)reuseId initBlock:(FGInitCustomViewBlock)initBlock resultBlock:(FGGetCustomViewResultBlock)resultBlock applyStyleBlock:(FGStyleBlock)applyStyleBlock
{
if(self.groupView == nil){
UIView * view = [self.parentContext customViewWithReuseId:reuseId initBlock:^UIView *(UIView *reuseView) {
UIView *view = initBlock(reuseView);
if(view.pool == nil){
view.pool = [[FGReuseItemPool alloc] init];
}
[view.pool prepareUpdateItems];
return view;
} resultBlock: ^(UIView *view){
return view;
} applyStyleBlock:applyStyleBlock];
self.groupView = view;
if (resultBlock == nil) {
return nil;
}else{
return resultBlock(view);
}
}else{
BOOL isNew;
UIView *view = (UIView *)[self.groupView.pool updateItem:reuseId initBlock:initBlock outputIsNewView: &isNew];
objc_setAssociatedObject(view, IndexPropertyKey, [NSNumber numberWithUnsignedInteger:self.subviewIndex ++], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
if (isNew) {
[self.groupView addSubview:view];
}
if (self.mode == FGViewGroupLayoutModeVertical) {
[self setAndOverrideVerticalLayouts: view];
}else if(self.mode == FGViewGroupLayoutModeHorizontal){
[self setAndOverrideHorizontalLayouts: view];
}
applyStyleBlock(view);
self.previousView = view;
if (resultBlock == nil) {
return nil;
}else{
return resultBlock(view);
}
}
}
- (void) endGroup
{
if (self.mode == FGViewGroupLayoutModeVertical) {
if (self.previousView != nil) {
self.previousView.bottomConstraint = [self.groupView updateConstraint:self.previousView.bottomConstraint view1:self.groupView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.previousView attribute:NSLayoutAttributeBottom multiplier:1 constant:self.previousView.styleBottomOrRight];
[self.previousView respondsToSelector:@selector(styleWithBottom:) withKey:nil usingBlock:^(UIView *self, CGFloat bottom){
self.bottomConstraint.constant = bottom;
}];
}
}
if (self.mode == FGViewGroupLayoutModeHorizontal) {
if (self.previousView != nil) {
self.previousView.rightConstraint = [self.groupView updateConstraint:self.previousView.rightConstraint view1:self.groupView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.previousView attribute:NSLayoutAttributeRight multiplier:1 constant:self.previousView.styleBottomOrRight];
[self.previousView respondsToSelector:@selector(styleWithRight:) withKey:nil usingBlock:^(UIView *self, CGFloat right){
self.rightConstraint.constant = right;
}];
}
}
self.previousView = nil;
if (self.groupView != nil){
[self.groupView.pool finishUpdateItems:nil needRemove:^(UIView *view) {
[view removeFromSuperview];
}];
}
NSArray *subviews = self.groupView.subviews;
for (int i=0; i<subviews.count; i++) {
NSNumber *index = objc_getAssociatedObject(subviews[i], IndexPropertyKey);
if (index == nil) {
continue;
}
NSUInteger minIndex = index.unsignedIntegerValue;
int minJ = i;
for (int j=i+1; j<subviews.count; j++) {
NSNumber *jindex = objc_getAssociatedObject(subviews[j], IndexPropertyKey);
if (jindex == nil) {
continue;
}
NSUInteger index = jindex.unsignedIntegerValue;
if (index < minIndex) {
minIndex = index;
minJ = j;
}
}
if (minJ != i) {
[self.groupView exchangeSubviewAtIndex:i withSubviewAtIndex:minJ];
subviews = self.groupView.subviews;
}
}
[FastGui popContext];
}
- (void)styleSheet
{
[self.parentContext styleSheet];
}
@end
@implementation UIView (FGViewGroup)
static void * PoolKey = &PoolKey;
static void * StyleBottomOrRight = &StyleBottomOrRight;
- (FGReuseItemPool *)pool
{
return objc_getAssociatedObject(self, PoolKey);
}
- (void)setPool:(FGReuseItemPool *)pool
{
objc_setAssociatedObject(self, PoolKey, pool, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (CGFloat)styleBottomOrRight
{
return [(NSNumber *)objc_getAssociatedObject(self, StyleBottomOrRight) floatValue];
}
- (void)setStyleBottomOrRight:(CGFloat)styleBottomOrRight
{
objc_setAssociatedObject(self, StyleBottomOrRight, [NSNumber numberWithFloat:styleBottomOrRight], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end
@implementation FGScrollGroupView
- (UIView *)layoutView
{
if (_layoutView == nil) {
_layoutView = [[UIScrollView alloc] init];
[super addSubview:_layoutView];
[FGStyle updateStyleOfView:_layoutView withBlock:^(UIView *view) {
[FGStyle topBottom:0 leftRight:0];
}];
}
return _layoutView;
}
- (void)addSubview:(UIView *)view
{
[self.layoutView addSubview:view];
}
@end
@interface FGView()<FGContext>
@end
@implementation FGView
@synthesize parentContext;
- (NSString *) viewStyleClass
{
return nil;
}
- (void) reloadGui
{
[FastGui beginGroupWithNextView];
[FastGui customViewWithClass:[self viewStyleClass] reuseId:[FGInternal memoryPositionAsReuseIdOfObject:self] initBlock:^UIView *(UIView *reuseView) {
return self;
} resultBlock:nil];
[self onGui];
[FastGui endGroup];
}
- (void) layoutSubviews
{
[FastGui reloadGuiSyncWithContext:self];
}
- (void) styleSheet
{
}
-(void) onGui
{
}
- (void) customViewControllerWithReuseId:(NSString *)reuseId initBlock:(FGInitCustomViewControllerBlock)initBlock
{
NSLog(@"View context doesn't support custom view controllers.");
}
- (void)dismissViewController
{
NSLog(@"View context doesn't support custom view controllers.");
}
- (id) customData:(void *)key data:(NSDictionary *)data
{
return nil;
}
- (id)customViewWithReuseId:(NSString *)reuseId initBlock:(FGInitCustomViewBlock)initBlock resultBlock:(FGGetCustomViewResultBlock)resultBlock applyStyleBlock:(FGStyleBlock)applyStyleBlock
{
if ([reuseId isEqualToString:[FGInternal memoryPositionAsReuseIdOfObject:self]]) {
applyStyleBlock(initBlock(nil));
}else{
NSLog(@"Unmatched context, Do you have called to 'EndGroup' without matching 'BeginGroup'?\n");
}
return nil;
}
@end