forked from TextureGroup/Texture
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathItemNode.m
More file actions
398 lines (321 loc) · 14.6 KB
/
Copy pathItemNode.m
File metadata and controls
398 lines (321 loc) · 14.6 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
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
//
// ItemNode.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 "ItemNode.h"
#import "ItemStyles.h"
#import "PlaceholderNetworkImageNode.h"
#import <AsyncDisplayKit/ASDisplayNodeExtras.h>
static CGFloat ASIsRTL()
{
static BOOL __isRTL = NO;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
__isRTL = [UIApplication sharedApplication].userInterfaceLayoutDirection == UIUserInterfaceLayoutDirectionRightToLeft;
});
return __isRTL;
}
const CGFloat kFixedLabelsAreaHeight = 96.0;
const CGFloat kDesignWidth = 320.0;
const CGFloat kDesignHeight = 299.0;
const CGFloat kBadgeHeight = 34.0;
const CGFloat kSoldOutGBHeight = 50.0;
@interface ItemNode() <ASNetworkImageNodeDelegate>
@property (nonatomic, strong) ItemViewModel *nodeModel;
@property (nonatomic, strong) PlaceholderNetworkImageNode *dealImageView;
@property (nonatomic, strong) ASTextNode *titleLabel;
@property (nonatomic, strong) ASTextNode *firstInfoLabel;
@property (nonatomic, strong) ASTextNode *distanceLabel;
@property (nonatomic, strong) ASTextNode *secondInfoLabel;
@property (nonatomic, strong) ASTextNode *originalPriceLabel;
@property (nonatomic, strong) ASTextNode *finalPriceLabel;
@property (nonatomic, strong) ASTextNode *soldOutLabelFlat;
@property (nonatomic, strong) ASDisplayNode *soldOutLabelBackground;
@property (nonatomic, strong) ASDisplayNode *soldOutOverlay;
@property (nonatomic, strong) ASTextNode *badge;
@end
@implementation ItemNode
// Defined on ASCellNode
@dynamic nodeModel;
+ (void)load
{
// Need to happen on main thread.
ASIsRTL();
}
- (instancetype)init
{
self = [super init];
if (self != nil) {
[self setupNodes];
[self updateBackgroundColor];
}
return self;
}
- (void)setNodeModel:(ItemViewModel *)nodeModel
{
[super setNodeModel:nodeModel];
[self updateLabels];
[self updateAccessibilityIdentifier];
}
- (void)setupNodes
{
self.dealImageView = [[PlaceholderNetworkImageNode alloc] init];
self.dealImageView.delegate = self;
self.dealImageView.placeholderEnabled = YES;
self.dealImageView.placeholderImageOverride = [ItemStyles placeholderImage];
self.dealImageView.defaultImage = [ItemStyles placeholderImage];
self.dealImageView.contentMode = UIViewContentModeScaleToFill;
self.dealImageView.placeholderFadeDuration = 0.0;
self.dealImageView.layerBacked = YES;
self.titleLabel = [[ASTextNode alloc] init];
self.titleLabel.maximumNumberOfLines = 2;
self.titleLabel.style.alignSelf = ASStackLayoutAlignSelfStart;
self.titleLabel.style.flexGrow = 1.0;
self.titleLabel.layerBacked = YES;
self.firstInfoLabel = [[ASTextNode alloc] init];
self.firstInfoLabel.maximumNumberOfLines = 1;
self.firstInfoLabel.layerBacked = YES;
self.secondInfoLabel = [[ASTextNode alloc] init];
self.secondInfoLabel.maximumNumberOfLines = 1;
self.secondInfoLabel.layerBacked = YES;
self.distanceLabel = [[ASTextNode alloc] init];
self.distanceLabel.maximumNumberOfLines = 1;
self.distanceLabel.layerBacked = YES;
self.originalPriceLabel = [[ASTextNode alloc] init];
self.originalPriceLabel.maximumNumberOfLines = 1;
self.originalPriceLabel.layerBacked = YES;
self.finalPriceLabel = [[ASTextNode alloc] init];
self.finalPriceLabel.maximumNumberOfLines = 1;
self.finalPriceLabel.layerBacked = YES;
self.badge = [[ASTextNode alloc] init];
self.badge.hidden = YES;
self.badge.layerBacked = YES;
self.soldOutLabelFlat = [[ASTextNode alloc] init];
self.soldOutLabelFlat.layerBacked = YES;
self.soldOutLabelBackground = [[ASDisplayNode alloc] init];
self.soldOutLabelBackground.style.width = ASDimensionMakeWithFraction(1.0);
self.soldOutLabelBackground.style.height = ASDimensionMakeWithPoints(kSoldOutGBHeight);
self.soldOutLabelBackground.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.9];
self.soldOutLabelBackground.style.flexGrow = 1.0;
self.soldOutLabelBackground.layerBacked = YES;
self.soldOutOverlay = [[ASDisplayNode alloc] init];
self.soldOutOverlay.style.flexGrow = 1.0;
self.soldOutOverlay.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.5];
self.soldOutOverlay.layerBacked = YES;
[self addSubnode:self.dealImageView];
[self addSubnode:self.titleLabel];
[self addSubnode:self.firstInfoLabel];
[self addSubnode:self.secondInfoLabel];
[self addSubnode:self.originalPriceLabel];
[self addSubnode:self.finalPriceLabel];
[self addSubnode:self.distanceLabel];
[self addSubnode:self.badge];
[self addSubnode:self.soldOutLabelBackground];
[self addSubnode:self.soldOutLabelFlat];
[self addSubnode:self.soldOutOverlay];
self.soldOutOverlay.hidden = YES;
self.soldOutLabelBackground.hidden = YES;
self.soldOutLabelFlat.hidden = YES;
if (ASIsRTL()) {
self.titleLabel.style.alignSelf = ASStackLayoutAlignSelfEnd;
self.firstInfoLabel.style.alignSelf = ASStackLayoutAlignSelfEnd;
self.distanceLabel.style.alignSelf = ASStackLayoutAlignSelfEnd;
self.secondInfoLabel.style.alignSelf = ASStackLayoutAlignSelfEnd;
self.originalPriceLabel.style.alignSelf = ASStackLayoutAlignSelfStart;
self.finalPriceLabel.style.alignSelf = ASStackLayoutAlignSelfStart;
} else {
self.firstInfoLabel.style.alignSelf = ASStackLayoutAlignSelfStart;
self.distanceLabel.style.alignSelf = ASStackLayoutAlignSelfStart;
self.secondInfoLabel.style.alignSelf = ASStackLayoutAlignSelfStart;
self.originalPriceLabel.style.alignSelf = ASStackLayoutAlignSelfEnd;
self.finalPriceLabel.style.alignSelf = ASStackLayoutAlignSelfEnd;
}
}
- (void)updateLabels
{
// Set Title text
if (self.nodeModel.titleText) {
self.titleLabel.attributedText = [[NSAttributedString alloc] initWithString:self.nodeModel.titleText attributes:[ItemStyles titleStyle]];
}
if (self.nodeModel.firstInfoText) {
self.firstInfoLabel.attributedText = [[NSAttributedString alloc] initWithString:self.nodeModel.firstInfoText attributes:[ItemStyles subtitleStyle]];
}
if (self.nodeModel.secondInfoText) {
self.secondInfoLabel.attributedText = [[NSAttributedString alloc] initWithString:self.nodeModel.secondInfoText attributes:[ItemStyles secondInfoStyle]];
}
if (self.nodeModel.originalPriceText) {
self.originalPriceLabel.attributedText = [[NSAttributedString alloc] initWithString:self.nodeModel.originalPriceText attributes:[ItemStyles originalPriceStyle]];
}
if (self.nodeModel.finalPriceText) {
self.finalPriceLabel.attributedText = [[NSAttributedString alloc] initWithString:self.nodeModel.finalPriceText attributes:[ItemStyles finalPriceStyle]];
}
if (self.nodeModel.distanceLabelText) {
NSString *format = ASIsRTL() ? @"%@ •" : @"• %@";
NSString *distanceText = [NSString stringWithFormat:format, self.nodeModel.distanceLabelText];
self.distanceLabel.attributedText = [[NSAttributedString alloc] initWithString:distanceText attributes:[ItemStyles distanceStyle]];
}
BOOL isSoldOut = self.nodeModel.soldOutText != nil;
if (isSoldOut) {
NSString *soldOutText = self.nodeModel.soldOutText;
self.soldOutLabelFlat.attributedText = [[NSAttributedString alloc] initWithString:soldOutText attributes:[ItemStyles soldOutStyle]];
}
self.soldOutOverlay.hidden = !isSoldOut;
self.soldOutLabelFlat.hidden = !isSoldOut;
self.soldOutLabelBackground.hidden = !isSoldOut;
BOOL hasBadge = self.nodeModel.badgeText != nil;
if (hasBadge) {
self.badge.attributedText = [[NSAttributedString alloc] initWithString:self.nodeModel.badgeText attributes:[ItemStyles badgeStyle]];
self.badge.backgroundColor = [ItemStyles badgeColor];
}
self.badge.hidden = !hasBadge;
}
- (void)updateAccessibilityIdentifier
{
ASSetDebugName(self, @"Item #%zd", self.nodeModel.identifier);
self.accessibilityIdentifier = self.nodeModel.titleText;
}
- (void)updateBackgroundColor
{
if (self.highlighted) {
self.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.3];
} else if (self.selected) {
self.backgroundColor = [UIColor lightGrayColor];
} else {
self.backgroundColor = [UIColor whiteColor];
}
}
- (void)setSelected:(BOOL)selected
{
[super setSelected:selected];
[self updateBackgroundColor];
}
- (void)setHighlighted:(BOOL)highlighted
{
[super setHighlighted:highlighted];
[self updateBackgroundColor];
}
#pragma mark - ASDisplayNode
- (void)displayWillStart
{
[super displayWillStart];
[self didEnterPreloadState];
}
- (void)didEnterPreloadState
{
[super didEnterPreloadState];
if (self.nodeModel) {
[self loadImage];
}
}
- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize {
ASLayoutSpec *textSpec = [self textSpec];
ASLayoutSpec *imageSpec = [self imageSpecWithSize:constrainedSize];
ASOverlayLayoutSpec *soldOutOverImage = [ASOverlayLayoutSpec overlayLayoutSpecWithChild:imageSpec overlay:[self soldOutLabelSpec]];
NSArray *stackChildren = @[soldOutOverImage, textSpec];
ASStackLayoutSpec *mainStack = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical spacing:0.0 justifyContent:ASStackLayoutJustifyContentStart alignItems:ASStackLayoutAlignItemsStretch children:stackChildren];
ASOverlayLayoutSpec *soldOutOverlay = [ASOverlayLayoutSpec overlayLayoutSpecWithChild:mainStack overlay:self.soldOutOverlay];
return soldOutOverlay;
}
- (ASLayoutSpec *)textSpec
{
CGFloat kInsetHorizontal = 16.0;
CGFloat kInsetTop = 6.0;
CGFloat kInsetBottom = 0.0;
UIEdgeInsets textInsets = UIEdgeInsetsMake(kInsetTop, kInsetHorizontal, kInsetBottom, kInsetHorizontal);
ASLayoutSpec *verticalSpacer = [[ASLayoutSpec alloc] init];
verticalSpacer.style.flexGrow = 1.0;
ASLayoutSpec *horizontalSpacer1 = [[ASLayoutSpec alloc] init];
horizontalSpacer1.style.flexGrow = 1.0;
ASLayoutSpec *horizontalSpacer2 = [[ASLayoutSpec alloc] init];
horizontalSpacer2.style.flexGrow = 1.0;
NSArray *info1Children = @[self.firstInfoLabel, self.distanceLabel, horizontalSpacer1, self.originalPriceLabel];
NSArray *info2Children = @[self.secondInfoLabel, horizontalSpacer2, self.finalPriceLabel];
if (ASIsRTL()) {
info1Children = [[info1Children reverseObjectEnumerator] allObjects];
info2Children = [[info2Children reverseObjectEnumerator] allObjects];
}
ASStackLayoutSpec *info1Stack = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionHorizontal spacing:1.0 justifyContent:ASStackLayoutJustifyContentStart alignItems:ASStackLayoutAlignItemsBaselineLast children:info1Children];
ASStackLayoutSpec *info2Stack = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionHorizontal spacing:0.0 justifyContent:ASStackLayoutJustifyContentCenter alignItems:ASStackLayoutAlignItemsBaselineLast children:info2Children];
ASStackLayoutSpec *textStack = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical spacing:0.0 justifyContent:ASStackLayoutJustifyContentEnd alignItems:ASStackLayoutAlignItemsStretch children:@[self.titleLabel, verticalSpacer, info1Stack, info2Stack]];
ASInsetLayoutSpec *textWrapper = [ASInsetLayoutSpec insetLayoutSpecWithInsets:textInsets child:textStack];
textWrapper.style.flexGrow = 1.0;
return textWrapper;
}
- (ASLayoutSpec *)imageSpecWithSize:(ASSizeRange)constrainedSize
{
CGFloat imageRatio = [self imageRatioFromSize:constrainedSize.max];
ASRatioLayoutSpec *imagePlace = [ASRatioLayoutSpec ratioLayoutSpecWithRatio:imageRatio child:self.dealImageView];
self.badge.style.layoutPosition = CGPointMake(0, constrainedSize.max.height - kFixedLabelsAreaHeight - kBadgeHeight);
self.badge.style.height = ASDimensionMakeWithPoints(kBadgeHeight);
ASAbsoluteLayoutSpec *badgePosition = [ASAbsoluteLayoutSpec absoluteLayoutSpecWithChildren:@[self.badge]];
ASOverlayLayoutSpec *badgeOverImage = [ASOverlayLayoutSpec overlayLayoutSpecWithChild:imagePlace overlay:badgePosition];
badgeOverImage.style.flexGrow = 1.0;
return badgeOverImage;
}
- (ASLayoutSpec *)soldOutLabelSpec {
ASCenterLayoutSpec *centerSoldOutLabel = [ASCenterLayoutSpec centerLayoutSpecWithCenteringOptions:ASCenterLayoutSpecCenteringXY sizingOptions:ASCenterLayoutSpecSizingOptionMinimumXY child:self.soldOutLabelFlat];
ASCenterLayoutSpec *centerSoldOut = [ASCenterLayoutSpec centerLayoutSpecWithCenteringOptions:ASCenterLayoutSpecCenteringXY sizingOptions:ASCenterLayoutSpecSizingOptionDefault child:self.soldOutLabelBackground];
ASBackgroundLayoutSpec *soldOutLabelOverBackground = [ASBackgroundLayoutSpec backgroundLayoutSpecWithChild:centerSoldOutLabel background:centerSoldOut];
return soldOutLabelOverBackground;
}
+ (CGSize)sizeForWidth:(CGFloat)width
{
CGFloat height = [self scaledHeightForPreferredSize:[self preferredViewSize] scaledWidth:width];
return CGSizeMake(width, height);
}
+ (CGSize)preferredViewSize
{
return CGSizeMake(kDesignWidth, kDesignHeight);
}
+ (CGFloat)scaledHeightForPreferredSize:(CGSize)preferredSize scaledWidth:(CGFloat)scaledWidth
{
CGFloat scale = scaledWidth / kDesignWidth;
CGFloat scaledHeight = ceilf(scale * (kDesignHeight - kFixedLabelsAreaHeight)) + kFixedLabelsAreaHeight;
return scaledHeight;
}
#pragma mark - Image
- (CGFloat)imageRatioFromSize:(CGSize)size
{
CGFloat imageHeight = size.height - kFixedLabelsAreaHeight;
CGFloat imageRatio = imageHeight / size.width;
return imageRatio;
}
- (CGSize)imageSize
{
if (!CGSizeEqualToSize(self.dealImageView.frame.size, CGSizeZero)) {
return self.dealImageView.frame.size;
} else if (!CGSizeEqualToSize(self.calculatedSize, CGSizeZero)) {
CGFloat imageRatio = [self imageRatioFromSize:self.calculatedSize];
CGFloat imageWidth = self.calculatedSize.width;
return CGSizeMake(imageWidth, imageRatio * imageWidth);
} else {
return CGSizeZero;
}
}
- (void)loadImage
{
CGSize imageSize = [self imageSize];
if (CGSizeEqualToSize(CGSizeZero, imageSize)) {
return;
}
NSURL *url = [self.nodeModel imageURLWithSize:imageSize];
// if we're trying to set the deal image to what it already was, skip the work
if ([[url absoluteString] isEqualToString:[self.dealImageView.URL absoluteString]]) {
return;
}
// Clear the flag that says we've loaded our image
[self.dealImageView setURL:url];
}
@end