forked from TextureGroup/Texture
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewController.m
More file actions
229 lines (188 loc) · 7.31 KB
/
Copy pathViewController.m
File metadata and controls
229 lines (188 loc) · 7.31 KB
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
//
// ViewController.m
// Texture
//
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the /ASDK-Licenses directory of this source tree. An additional
// grant of patent rights can be found in the PATENTS file in the same directory.
//
// Modifications to this file made after 4/13/2017 are: Copyright (c) 2017-present,
// Pinterest, Inc. Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
#import "ViewController.h"
#import <AsyncDisplayKit/AsyncDisplayKit.h>
#import "ItemNode.h"
#import "BlurbNode.h"
#import "LoadingNode.h"
static const NSTimeInterval kWebResponseDelay = 1.0;
static const BOOL kSimulateWebResponse = YES;
static const NSInteger kBatchSize = 20;
static const CGFloat kHorizontalSectionPadding = 10.0f;
@interface ViewController () <ASCollectionDataSource, ASCollectionDelegate, ASCollectionDelegateFlowLayout>
{
ASCollectionNode *_collectionNode;
NSMutableArray *_data;
}
@end
@implementation ViewController
#pragma mark -
#pragma mark UIViewController.
- (instancetype)init
{
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
_collectionNode = [[ASCollectionNode alloc] initWithCollectionViewLayout:layout];
self = [super initWithNode:_collectionNode];
if (self) {
self.title = @"Cat Deals";
_collectionNode.dataSource = self;
_collectionNode.delegate = self;
_collectionNode.backgroundColor = [UIColor grayColor];
_collectionNode.accessibilityIdentifier = @"Cat deals list";
ASRangeTuningParameters preloadTuning;
preloadTuning.leadingBufferScreenfuls = 2;
preloadTuning.trailingBufferScreenfuls = 1;
[_collectionNode setTuningParameters:preloadTuning forRangeType:ASLayoutRangeTypePreload];
ASRangeTuningParameters displayTuning;
displayTuning.leadingBufferScreenfuls = 1;
displayTuning.trailingBufferScreenfuls = 0.5;
[_collectionNode setTuningParameters:displayTuning forRangeType:ASLayoutRangeTypeDisplay];
[_collectionNode registerSupplementaryNodeOfKind:UICollectionElementKindSectionHeader];
[_collectionNode registerSupplementaryNodeOfKind:UICollectionElementKindSectionFooter];
_data = [[NSMutableArray alloc] init];
self.navigationItem.leftItemsSupplementBackButton = YES;
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(reloadTapped)];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// set any collectionView properties here (once the node's backing view is loaded)
_collectionNode.leadingScreensForBatching = 2;
[self fetchMoreCatsWithCompletion:nil];
}
- (void)fetchMoreCatsWithCompletion:(void (^)(BOOL))completion
{
if (kSimulateWebResponse) {
__weak typeof(self) weakSelf = self;
void(^mockWebService)() = ^{
NSLog(@"ViewController \"got data from a web service\"");
ViewController *strongSelf = weakSelf;
if (strongSelf != nil)
{
[strongSelf appendMoreItems:kBatchSize completion:completion];
}
else {
NSLog(@"ViewController is nil - won't update collection");
}
};
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(kWebResponseDelay * NSEC_PER_SEC)), dispatch_get_main_queue(), mockWebService);
} else {
[self appendMoreItems:kBatchSize completion:completion];
}
}
- (void)appendMoreItems:(NSInteger)numberOfNewItems completion:(void (^)(BOOL))completion
{
NSArray *newData = [self getMoreData:numberOfNewItems];
[_collectionNode performBatchAnimated:YES updates:^{
[_data addObjectsFromArray:newData];
NSArray *addedIndexPaths = [self indexPathsForObjects:newData];
[_collectionNode insertItemsAtIndexPaths:addedIndexPaths];
} completion:completion];
}
- (NSArray *)getMoreData:(NSInteger)count
{
NSMutableArray *data = [NSMutableArray array];
for (int i = 0; i < count; i++) {
[data addObject:[ItemViewModel randomItem]];
}
return data;
}
- (NSArray *)indexPathsForObjects:(NSArray *)data
{
NSMutableArray *indexPaths = [NSMutableArray array];
NSInteger section = 0;
for (ItemViewModel *viewModel in data) {
NSInteger item = [_data indexOfObject:viewModel];
NSAssert(item < [_data count] && item != NSNotFound, @"Item should be in _data");
[indexPaths addObject:[NSIndexPath indexPathForItem:item inSection:section]];
}
return indexPaths;
}
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
[_collectionNode.view.collectionViewLayout invalidateLayout];
}
- (void)reloadTapped
{
[_collectionNode reloadData];
}
#pragma mark - ASCollectionNodeDelegate / ASCollectionNodeDataSource
- (ASCellNodeBlock)collectionNode:(ASCollectionNode *)collectionNode nodeBlockForItemAtIndexPath:(NSIndexPath *)indexPath
{
return ^{
return [[ItemNode alloc] init];
};
}
- (id)collectionNode:(ASCollectionNode *)collectionNode nodeModelForItemAtIndexPath:(NSIndexPath *)indexPath
{
return _data[indexPath.item];
}
- (ASCellNode *)collectionNode:(ASCollectionNode *)collectionNode nodeForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
if ([kind isEqualToString:UICollectionElementKindSectionHeader] && indexPath.section == 0) {
return [[BlurbNode alloc] init];
} else if ([kind isEqualToString:UICollectionElementKindSectionFooter] && indexPath.section == 0) {
return [[LoadingNode alloc] init];
}
return nil;
}
- (ASSizeRange)collectionNode:(ASCollectionNode *)collectionNode constrainedSizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat collectionViewWidth = CGRectGetWidth(self.view.frame) - 2 * kHorizontalSectionPadding;
CGFloat oneItemWidth = [ItemNode preferredViewSize].width;
NSInteger numColumns = floor(collectionViewWidth / oneItemWidth);
// Number of columns should be at least 1
numColumns = MAX(1, numColumns);
CGFloat totalSpaceBetweenColumns = (numColumns - 1) * kHorizontalSectionPadding;
CGFloat itemWidth = ((collectionViewWidth - totalSpaceBetweenColumns) / numColumns);
CGSize itemSize = [ItemNode sizeForWidth:itemWidth];
return ASSizeRangeMake(itemSize, itemSize);
}
- (NSInteger)collectionNode:(ASCollectionNode *)collectionNode numberOfItemsInSection:(NSInteger)section
{
return [_data count];
}
- (NSInteger)numberOfSectionsInCollectionNode:(ASCollectionNode *)collectionNode
{
return 1;
}
- (void)collectionNode:(ASCollectionNode *)collectionNode willBeginBatchFetchWithContext:(ASBatchContext *)context
{
[self fetchMoreCatsWithCompletion:^(BOOL finished){
[context completeBatchFetching:YES];
}];
}
#pragma mark - ASCollectionDelegateFlowLayout
- (ASSizeRange)collectionNode:(ASCollectionNode *)collectionNode sizeRangeForHeaderInSection:(NSInteger)section
{
if (section == 0) {
return ASSizeRangeUnconstrained;
} else {
return ASSizeRangeZero;
}
}
- (ASSizeRange)collectionNode:(ASCollectionNode *)collectionNode sizeRangeForFooterInSection:(NSInteger)section
{
if (section == 0) {
return ASSizeRangeUnconstrained;
} else {
return ASSizeRangeZero;
}
}
@end