forked from TextureGroup/Texture
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostNode.m
More file actions
337 lines (258 loc) · 12.2 KB
/
Copy pathPostNode.m
File metadata and controls
337 lines (258 loc) · 12.2 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
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
//
// PostNode.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 "PostNode.h"
#import "Post.h"
#import "TextStyles.h"
#import "LikesNode.h"
#import "CommentsNode.h"
#define PostNodeDividerColor [UIColor lightGrayColor]
@interface PostNode() <ASNetworkImageNodeDelegate, ASTextNodeDelegate>
@property (strong, nonatomic) Post *post;
@property (strong, nonatomic) ASDisplayNode *divider;
@property (strong, nonatomic) ASTextNode *nameNode;
@property (strong, nonatomic) ASTextNode *usernameNode;
@property (strong, nonatomic) ASTextNode *timeNode;
@property (strong, nonatomic) ASTextNode *postNode;
@property (strong, nonatomic) ASImageNode *viaNode;
@property (strong, nonatomic) ASNetworkImageNode *avatarNode;
@property (strong, nonatomic) ASNetworkImageNode *mediaNode;
@property (strong, nonatomic) LikesNode *likesNode;
@property (strong, nonatomic) CommentsNode *commentsNode;
@property (strong, nonatomic) ASImageNode *optionsNode;
@end
@implementation PostNode
#pragma mark - Lifecycle
- (instancetype)initWithPost:(Post *)post
{
self = [super init];
if (self) {
_post = post;
self.selectionStyle = UITableViewCellSelectionStyleNone;
// Name node
_nameNode = [[ASTextNode alloc] init];
_nameNode.attributedText = [[NSAttributedString alloc] initWithString:_post.name attributes:[TextStyles nameStyle]];
_nameNode.maximumNumberOfLines = 1;
[self addSubnode:_nameNode];
// Username node
_usernameNode = [[ASTextNode alloc] init];
_usernameNode.attributedText = [[NSAttributedString alloc] initWithString:_post.username attributes:[TextStyles usernameStyle]];
_usernameNode.style.flexShrink = 1.0; //if name and username don't fit to cell width, allow username shrink
_usernameNode.truncationMode = NSLineBreakByTruncatingTail;
_usernameNode.maximumNumberOfLines = 1;
[self addSubnode:_usernameNode];
// Time node
_timeNode = [[ASTextNode alloc] init];
_timeNode.attributedText = [[NSAttributedString alloc] initWithString:_post.time attributes:[TextStyles timeStyle]];
[self addSubnode:_timeNode];
// Post node
_postNode = [[ASTextNode alloc] init];
// Processing URLs in post
NSString *kLinkAttributeName = @"TextLinkAttributeName";
if (![_post.post isEqualToString:@""]) {
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:_post.post attributes:[TextStyles postStyle]];
NSDataDetector *urlDetector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
[urlDetector enumerateMatchesInString:attrString.string options:kNilOptions range:NSMakeRange(0, attrString.string.length) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop){
if (result.resultType == NSTextCheckingTypeLink) {
NSMutableDictionary *linkAttributes = [[NSMutableDictionary alloc] initWithDictionary:[TextStyles postLinkStyle]];
linkAttributes[kLinkAttributeName] = [NSURL URLWithString:result.URL.absoluteString];
[attrString addAttributes:linkAttributes range:result.range];
}
}];
// Configure node to support tappable links
_postNode.delegate = self;
_postNode.userInteractionEnabled = YES;
_postNode.linkAttributeNames = @[ kLinkAttributeName ];
_postNode.attributedText = attrString;
_postNode.passthroughNonlinkTouches = YES; // passes touches through when they aren't on a link
}
[self addSubnode:_postNode];
// Media
if (![_post.media isEqualToString:@""]) {
_mediaNode = [[ASNetworkImageNode alloc] init];
_mediaNode.backgroundColor = ASDisplayNodeDefaultPlaceholderColor();
_mediaNode.cornerRadius = 4.0;
_mediaNode.URL = [NSURL URLWithString:_post.media];
_mediaNode.delegate = self;
_mediaNode.imageModificationBlock = ^UIImage *(UIImage *image, ASPrimitiveTraitCollection traitCollection) {
UIImage *modifiedImage;
CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
UIGraphicsBeginImageContextWithOptions(image.size, false, [[UIScreen mainScreen] scale]);
[[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:8.0] addClip];
[image drawInRect:rect];
modifiedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return modifiedImage;
};
[self addSubnode:_mediaNode];
}
// User pic
_avatarNode = [[ASNetworkImageNode alloc] init];
_avatarNode.backgroundColor = ASDisplayNodeDefaultPlaceholderColor();
_avatarNode.style.width = ASDimensionMakeWithPoints(44);
_avatarNode.style.height = ASDimensionMakeWithPoints(44);
_avatarNode.cornerRadius = 22.0;
_avatarNode.URL = [NSURL URLWithString:_post.photo];
_avatarNode.imageModificationBlock = ^UIImage *(UIImage *image, ASPrimitiveTraitCollection traitCollection) {
UIImage *modifiedImage;
CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
UIGraphicsBeginImageContextWithOptions(image.size, false, [[UIScreen mainScreen] scale]);
[[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:44.0] addClip];
[image drawInRect:rect];
modifiedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return modifiedImage;
};
[self addSubnode:_avatarNode];
// Hairline cell separator
_divider = [[ASDisplayNode alloc] init];
[self updateDividerColor];
[self addSubnode:_divider];
// Via
if (_post.via != 0) {
_viaNode = [[ASImageNode alloc] init];
_viaNode.image = (_post.via == 1) ? [UIImage imageNamed:@"icon_ios.png"] : [UIImage imageNamed:@"icon_android.png"];
[self addSubnode:_viaNode];
}
// Bottom controls
_likesNode = [[LikesNode alloc] initWithLikesCount:_post.likes];
[self addSubnode:_likesNode];
_commentsNode = [[CommentsNode alloc] initWithCommentsCount:_post.comments];
[self addSubnode:_commentsNode];
_optionsNode = [[ASImageNode alloc] init];
_optionsNode.image = [UIImage imageNamed:@"icon_more"];
[self addSubnode:_optionsNode];
for (ASDisplayNode *node in self.subnodes) {
// ASTextNode with embedded links doesn't support layer backing
if (node.supportsLayerBacking) {
node.layerBacked = YES;
}
}
}
return self;
}
- (void)updateDividerColor
{
/*
* UITableViewCell traverses through all its descendant views and adjusts their background color accordingly
* either to [UIColor clearColor], although potentially it could use the same color as the selection highlight itself.
* After selection, the same trick is performed again in reverse, putting all the backgrounds back as they used to be.
* But in our case, we don't want to have the background color disappearing so we reset it after highlighting or
* selection is done.
*/
_divider.backgroundColor = PostNodeDividerColor;
}
#pragma mark - ASDisplayNode
- (void)didLoad
{
// enable highlighting now that self.layer has loaded -- see ASHighlightOverlayLayer.h
self.layer.as_allowsHighlightDrawing = YES;
[super didLoad];
}
- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
{
// Flexible spacer between username and time
ASLayoutSpec *spacer = [[ASLayoutSpec alloc] init];
spacer.style.flexGrow = 1.0;
// Horizontal stack for name, username, via icon and time
NSMutableArray *layoutSpecChildren = [@[_nameNode, _usernameNode, spacer] mutableCopy];
if (_post.via != 0) {
[layoutSpecChildren addObject:_viaNode];
}
[layoutSpecChildren addObject:_timeNode];
ASStackLayoutSpec *nameStack =
[ASStackLayoutSpec
stackLayoutSpecWithDirection:ASStackLayoutDirectionHorizontal
spacing:5.0
justifyContent:ASStackLayoutJustifyContentStart
alignItems:ASStackLayoutAlignItemsCenter
children:layoutSpecChildren];
nameStack.style.alignSelf = ASStackLayoutAlignSelfStretch;
// bottom controls horizontal stack
ASStackLayoutSpec *controlsStack =
[ASStackLayoutSpec
stackLayoutSpecWithDirection:ASStackLayoutDirectionHorizontal
spacing:10
justifyContent:ASStackLayoutJustifyContentStart
alignItems:ASStackLayoutAlignItemsCenter
children:@[_likesNode, _commentsNode, _optionsNode]];
// Add more gaps for control line
controlsStack.style.spacingAfter = 3.0;
controlsStack.style.spacingBefore = 3.0;
NSMutableArray *mainStackContent = [[NSMutableArray alloc] init];
[mainStackContent addObject:nameStack];
[mainStackContent addObject:_postNode];
if (![_post.media isEqualToString:@""]){
// Only add the media node if an image is present
if (_mediaNode.image != nil) {
ASRatioLayoutSpec *imagePlace =
[ASRatioLayoutSpec
ratioLayoutSpecWithRatio:0.5
child:_mediaNode];
imagePlace.style.spacingAfter = 3.0;
imagePlace.style.spacingBefore = 3.0;
[mainStackContent addObject:imagePlace];
}
}
[mainStackContent addObject:controlsStack];
// Vertical spec of cell main content
ASStackLayoutSpec *contentSpec =
[ASStackLayoutSpec
stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical
spacing:8.0
justifyContent:ASStackLayoutJustifyContentStart
alignItems:ASStackLayoutAlignItemsStretch
children:mainStackContent];
contentSpec.style.flexShrink = 1.0;
// Horizontal spec for avatar
ASStackLayoutSpec *avatarContentSpec =
[ASStackLayoutSpec
stackLayoutSpecWithDirection:ASStackLayoutDirectionHorizontal
spacing:8.0
justifyContent:ASStackLayoutJustifyContentStart
alignItems:ASStackLayoutAlignItemsStart
children:@[_avatarNode, contentSpec]];
return [ASInsetLayoutSpec
insetLayoutSpecWithInsets:UIEdgeInsetsMake(10, 10, 10, 10)
child:avatarContentSpec];
}
- (void)layout
{
[super layout];
// Manually layout the divider.
CGFloat pixelHeight = 1.0f / [[UIScreen mainScreen] scale];
_divider.frame = CGRectMake(0.0f, 0.0f, self.calculatedSize.width, pixelHeight);
}
#pragma mark - ASCellNode
- (void)setHighlighted:(BOOL)highlighted
{
[super setHighlighted:highlighted];
[self updateDividerColor];
}
- (void)setSelected:(BOOL)selected
{
[super setSelected:selected];
[self updateDividerColor];
}
#pragma mark - <ASTextNodeDelegate>
- (BOOL)textNode:(ASTextNode *)richTextNode shouldHighlightLinkAttribute:(NSString *)attribute value:(id)value atPoint:(CGPoint)point
{
// Opt into link highlighting -- tap and hold the link to try it! must enable highlighting on a layer, see -didLoad
return YES;
}
- (void)textNode:(ASTextNode *)richTextNode tappedLinkAttribute:(NSString *)attribute value:(NSURL *)URL atPoint:(CGPoint)point textRange:(NSRange)textRange
{
// The node tapped a link, open it
[[UIApplication sharedApplication] openURL:URL];
}
#pragma mark - ASNetworkImageNodeDelegate methods.
- (void)imageNode:(ASNetworkImageNode *)imageNode didLoadImage:(UIImage *)image
{
[self setNeedsLayout];
}
@end