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
197 lines (152 loc) · 6.18 KB
/
Copy pathViewController.m
File metadata and controls
197 lines (152 loc) · 6.18 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
//
// ViewController.m
// Texture
//
// Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
// Changes after 4/13/2017 are: Copyright (c) Pinterest, Inc. All rights reserved.
// Licensed under Apache 2.0: http://www.apache.org/licenses/LICENSE-2.0
//
#import "ViewController.h"
#import <AsyncDisplayKit/AsyncDisplayKit.h>
#import <AsyncDisplayKit/ASAssert.h>
#import "BlurbNode.h"
#import "NicCageNode.h"
#import <AsyncDisplayKit/ASDisplayNode+Beta.h>
static const NSInteger kCageSize = 20; // intial number of Cage cells in ASTableView
static const NSInteger kCageBatchSize = 10; // number of Cage cells to add to ASTableView
static const NSInteger kMaxCageSize = 100; // max number of Cage cells allowed in ASTableView
@interface ViewController () <ASTableViewDataSource, ASTableViewDelegate>
{
ASTableView *_tableView;
// array of boxed CGSizes corresponding to placekitten.com kittens
NSMutableArray *_kittenDataSource;
BOOL _dataSourceLocked;
NSIndexPath *_blurbNodeIndexPath;
}
@property (nonatomic, strong) NSMutableArray *kittenDataSource;
@property (atomic, assign) BOOL dataSourceLocked;
@end
@implementation ViewController
#pragma mark -
#pragma mark UIViewController.
- (instancetype)init
{
if (!(self = [super init]))
return nil;
_tableView = [[ASTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone; // KittenNode has its own separator
_tableView.asyncDataSource = self;
_tableView.asyncDelegate = self;
// populate our "data source" with some random kittens
_kittenDataSource = [self createLitterWithSize:kCageSize];
_blurbNodeIndexPath = [NSIndexPath indexPathForItem:0 inSection:0];
self.title = @"Nic Cage";
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit
target:self
action:@selector(toggleEditingMode)];
return self;
}
- (NSMutableArray *)createLitterWithSize:(NSInteger)litterSize
{
NSMutableArray *cages = [NSMutableArray arrayWithCapacity:litterSize];
for (NSInteger i = 0; i < litterSize; i++) {
u_int32_t deltaX = arc4random_uniform(10) - 5;
u_int32_t deltaY = arc4random_uniform(10) - 5;
CGSize size = CGSizeMake(350 + 2 * deltaX, 350 + 4 * deltaY);
[cages addObject:[NSValue valueWithCGSize:size]];
}
return cages;
}
- (void)setKittenDataSource:(NSMutableArray *)kittenDataSource {
ASDisplayNodeAssert(!self.dataSourceLocked, @"Could not update data source when it is locked !");
_kittenDataSource = kittenDataSource;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view addSubview:_tableView];
}
- (void)viewWillLayoutSubviews
{
_tableView.frame = self.view.bounds;
}
- (void)toggleEditingMode
{
[_tableView setEditing:!_tableView.editing animated:YES];
}
#pragma mark -
#pragma mark ASTableView.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[_tableView deselectRowAtIndexPath:indexPath animated:YES];
// Assume only kitten nodes are selectable (see -tableView:shouldHighlightRowAtIndexPath:).
NicCageNode *node = (NicCageNode *)[_tableView nodeForRowAtIndexPath:indexPath];
[node toggleImageEnlargement];
}
- (ASCellNode *)tableView:(ASTableView *)tableView nodeForRowAtIndexPath:(NSIndexPath *)indexPath
{
// special-case the first row
if ([_blurbNodeIndexPath compare:indexPath] == NSOrderedSame) {
BlurbNode *node = [[BlurbNode alloc] init];
return node;
}
NSValue *size = _kittenDataSource[indexPath.row - 1];
NicCageNode *node = [[NicCageNode alloc] initWithKittenOfSize:size.CGSizeValue];
return node;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// blurb node + kLitterSize kitties
return 1 + _kittenDataSource.count;
}
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
// Enable selection for kitten nodes
return [_blurbNodeIndexPath compare:indexPath] != NSOrderedSame;
}
- (void)tableViewLockDataSource:(ASTableView *)tableView
{
self.dataSourceLocked = YES;
}
- (void)tableViewUnlockDataSource:(ASTableView *)tableView
{
self.dataSourceLocked = NO;
}
- (BOOL)shouldBatchFetchForTableView:(UITableView *)tableView
{
return _kittenDataSource.count < kMaxCageSize;
}
- (void)tableView:(UITableView *)tableView willBeginBatchFetchWithContext:(ASBatchContext *)context
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
sleep(1);
dispatch_async(dispatch_get_main_queue(), ^{
// populate a new array of random-sized kittens
NSArray *moarKittens = [self createLitterWithSize:kCageBatchSize];
NSMutableArray *indexPaths = [[NSMutableArray alloc] init];
// find number of kittens in the data source and create their indexPaths
NSInteger existingRows = _kittenDataSource.count + 1;
for (NSInteger i = 0; i < moarKittens.count; i++) {
[indexPaths addObject:[NSIndexPath indexPathForRow:existingRows + i inSection:0]];
}
// add new kittens to the data source & notify table of new indexpaths
[_kittenDataSource addObjectsFromArray:moarKittens];
[tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
[context completeBatchFetching:YES];
});
});
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Enable editing for Kitten nodes
return [_blurbNodeIndexPath compare:indexPath] != NSOrderedSame;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Assume only kitten nodes are editable (see -tableView:canEditRowAtIndexPath:).
[_kittenDataSource removeObjectAtIndex:indexPath.row - 1];
[_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
@end