Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get CatDealsCollectionView example running again #trivial #783

Merged
merged 4 commits into from
Feb 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions examples/CatDealsCollectionView/Sample/BlurbNode.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
//
// BlurbNode.h
// Sample
// 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 root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
// 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.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// Modifications to this file made after 4/13/2017 are: Copyright (c) through the 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 <AsyncDisplayKit/AsyncDisplayKit.h>
Expand Down
37 changes: 18 additions & 19 deletions examples/CatDealsCollectionView/Sample/BlurbNode.m
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
//
// BlurbNode.m
// Sample
// 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 root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
// 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.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// Modifications to this file made after 4/13/2017 are: Copyright (c) through the 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 "BlurbNode.h"
Expand Down Expand Up @@ -57,16 +57,15 @@ - (instancetype)init
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:blurb];
[string addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Light" size:16.0f] range:NSMakeRange(0, blurb.length)];
[string addAttributes:@{
NSLinkAttributeName: [NSURL URLWithString:@"http://lorempixel.com/"],
NSForegroundColorAttributeName: [UIColor blueColor],
NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle | NSUnderlinePatternDot),
}
range:[blurb rangeOfString:@"lorempixel.com"]];
NSLinkAttributeName: [NSURL URLWithString:@"http://lorempixel.com/"],
NSForegroundColorAttributeName: [UIColor blueColor],
NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle | NSUnderlinePatternDot),
} range:[blurb rangeOfString:@"lorempixel.com"]];
[string addAttributes:@{
NSLinkAttributeName: [NSURL URLWithString:@"http://www.catipsum.com/"],
NSForegroundColorAttributeName: [UIColor blueColor],
NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle | NSUnderlinePatternDot),
} range:[blurb rangeOfString:@"catipsum.com"]];
NSLinkAttributeName: [NSURL URLWithString:@"http://www.catipsum.com/"],
NSForegroundColorAttributeName: [UIColor blueColor],
NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle | NSUnderlinePatternDot),
} range:[blurb rangeOfString:@"catipsum.com"]];
_textNode.attributedText = string;

// add it as a subnode, and we're done
Expand All @@ -90,7 +89,7 @@ - (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
centerSpec.sizingOptions = ASCenterLayoutSpecSizingOptionMinimumY;
centerSpec.child = _textNode;

UIEdgeInsets padding =UIEdgeInsetsMake(kTextPadding, kTextPadding, kTextPadding, kTextPadding);
UIEdgeInsets padding = UIEdgeInsetsMake(kTextPadding, kTextPadding, kTextPadding, kTextPadding);
return [ASInsetLayoutSpec insetLayoutSpecWithInsets:padding child:centerSpec];
}

Expand Down
1 change: 0 additions & 1 deletion examples/CatDealsCollectionView/Sample/ItemNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

@interface ItemNode : ASCellNode

- initWithViewModel:(ItemViewModel *)viewModel;
+ (CGSize)sizeForWidth:(CGFloat)width;
+ (CGSize)preferredViewSize;

Expand Down
131 changes: 80 additions & 51 deletions examples/CatDealsCollectionView/Sample/ItemNode.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@
#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;
Expand All @@ -28,7 +38,7 @@

@interface ItemNode() <ASNetworkImageNodeDelegate>

@property (nonatomic, strong) ItemViewModel *viewModel;
@property (nonatomic, strong) ItemViewModel *nodeModel;

@property (nonatomic, strong) PlaceholderNetworkImageNode *dealImageView;

Expand All @@ -46,28 +56,35 @@ @interface ItemNode() <ASNetworkImageNodeDelegate>
@end

@implementation ItemNode
@dynamic viewModel;
// Defined on ASCellNode
@dynamic nodeModel;

- (instancetype)initWithViewModel:(ItemViewModel *)viewModel
+ (void)load
{
// Need to happen on main thread.
ASIsRTL();
}

- (instancetype)init
{
self = [super init];
if (self != nil) {
self.viewModel = viewModel;
[self setup];
[self updateLabels];
[self setupNodes];
[self updateBackgroundColor];

ASSetDebugName(self, @"Item #%zd", viewModel.identifier);
self.accessibilityIdentifier = viewModel.titleText;
}
return self;
}

+ (BOOL)isRTL {
return [UIApplication sharedApplication].userInterfaceLayoutDirection == UIUserInterfaceLayoutDirectionRightToLeft;
- (void)setNodeModel:(ItemViewModel *)nodeModel
{
[super setNodeModel:nodeModel];

[self updateLabels];
[self updateAccessibilityIdentifier];
}

- (void)setup {
- (void)setupNodes
{
self.dealImageView = [[PlaceholderNetworkImageNode alloc] init];
self.dealImageView.delegate = self;
self.dealImageView.placeholderEnabled = YES;
Expand Down Expand Up @@ -138,7 +155,7 @@ - (void)setup {
self.soldOutLabelBackground.hidden = YES;
self.soldOutLabelFlat.hidden = YES;

if ([ItemNode isRTL]) {
if (ASIsRTL()) {
self.titleLabel.style.alignSelf = ASStackLayoutAlignSelfEnd;
self.firstInfoLabel.style.alignSelf = ASStackLayoutAlignSelfEnd;
self.distanceLabel.style.alignSelf = ASStackLayoutAlignSelfEnd;
Expand All @@ -154,49 +171,56 @@ - (void)setup {
}
}

- (void)updateLabels {
- (void)updateLabels
{
// Set Title text
if (self.viewModel.titleText) {
self.titleLabel.attributedText = [[NSAttributedString alloc] initWithString:self.viewModel.titleText attributes:[ItemStyles titleStyle]];
if (self.nodeModel.titleText) {
self.titleLabel.attributedText = [[NSAttributedString alloc] initWithString:self.nodeModel.titleText attributes:[ItemStyles titleStyle]];
}
if (self.viewModel.firstInfoText) {
self.firstInfoLabel.attributedText = [[NSAttributedString alloc] initWithString:self.viewModel.firstInfoText attributes:[ItemStyles subtitleStyle]];
if (self.nodeModel.firstInfoText) {
self.firstInfoLabel.attributedText = [[NSAttributedString alloc] initWithString:self.nodeModel.firstInfoText attributes:[ItemStyles subtitleStyle]];
}

if (self.viewModel.secondInfoText) {
self.secondInfoLabel.attributedText = [[NSAttributedString alloc] initWithString:self.viewModel.secondInfoText attributes:[ItemStyles secondInfoStyle]];
if (self.nodeModel.secondInfoText) {
self.secondInfoLabel.attributedText = [[NSAttributedString alloc] initWithString:self.nodeModel.secondInfoText attributes:[ItemStyles secondInfoStyle]];
}
if (self.viewModel.originalPriceText) {
self.originalPriceLabel.attributedText = [[NSAttributedString alloc] initWithString:self.viewModel.originalPriceText attributes:[ItemStyles originalPriceStyle]];
if (self.nodeModel.originalPriceText) {
self.originalPriceLabel.attributedText = [[NSAttributedString alloc] initWithString:self.nodeModel.originalPriceText attributes:[ItemStyles originalPriceStyle]];
}
if (self.viewModel.finalPriceText) {
self.finalPriceLabel.attributedText = [[NSAttributedString alloc] initWithString:self.viewModel.finalPriceText attributes:[ItemStyles finalPriceStyle]];
if (self.nodeModel.finalPriceText) {
self.finalPriceLabel.attributedText = [[NSAttributedString alloc] initWithString:self.nodeModel.finalPriceText attributes:[ItemStyles finalPriceStyle]];
}
if (self.viewModel.distanceLabelText) {
NSString *format = [ItemNode isRTL] ? @"%@ •" : @"• %@";
NSString *distanceText = [NSString stringWithFormat:format, self.viewModel.distanceLabelText];
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.viewModel.soldOutText != nil;
BOOL isSoldOut = self.nodeModel.soldOutText != nil;

if (isSoldOut) {
NSString *soldOutText = self.viewModel.soldOutText;
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.viewModel.badgeText != nil;
BOOL hasBadge = self.nodeModel.badgeText != nil;
if (hasBadge) {
self.badge.attributedText = [[NSAttributedString alloc] initWithString:self.viewModel.badgeText attributes:[ItemStyles badgeStyle]];
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) {
Expand All @@ -208,9 +232,6 @@ - (void)updateBackgroundColor
}
}

- (void)imageNode:(ASNetworkImageNode *)imageNode didLoadImage:(UIImage *)image {
}

- (void)setSelected:(BOOL)selected
{
[super setSelected:selected];
Expand All @@ -223,21 +244,22 @@ - (void)setHighlighted:(BOOL)highlighted
[self updateBackgroundColor];
}

#pragma mark - superclass
#pragma mark - ASDisplayNode

- (void)displayWillStart {
- (void)displayWillStart
{
[super displayWillStart];
[self didEnterPreloadState];
}

- (void)didEnterPreloadState {
- (void)didEnterPreloadState
{
[super didEnterPreloadState];
if (self.viewModel) {
if (self.nodeModel) {
[self loadImage];
}
}


- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize {

ASLayoutSpec *textSpec = [self textSpec];
Expand All @@ -253,7 +275,8 @@ - (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize {
return soldOutOverlay;
}

- (ASLayoutSpec *)textSpec {
- (ASLayoutSpec *)textSpec
{
CGFloat kInsetHorizontal = 16.0;
CGFloat kInsetTop = 6.0;
CGFloat kInsetBottom = 0.0;
Expand All @@ -271,7 +294,7 @@ - (ASLayoutSpec *)textSpec {

NSArray *info1Children = @[self.firstInfoLabel, self.distanceLabel, horizontalSpacer1, self.originalPriceLabel];
NSArray *info2Children = @[self.secondInfoLabel, horizontalSpacer2, self.finalPriceLabel];
if ([ItemNode isRTL]) {
if (ASIsRTL()) {
info1Children = [[info1Children reverseObjectEnumerator] allObjects];
info2Children = [[info2Children reverseObjectEnumerator] allObjects];
}
Expand All @@ -288,7 +311,8 @@ - (ASLayoutSpec *)textSpec {
return textWrapper;
}

- (ASLayoutSpec *)imageSpecWithSize:(ASSizeRange)constrainedSize {
- (ASLayoutSpec *)imageSpecWithSize:(ASSizeRange)constrainedSize
{
CGFloat imageRatio = [self imageRatioFromSize:constrainedSize.max];

ASRatioLayoutSpec *imagePlace = [ASRatioLayoutSpec ratioLayoutSpecWithRatio:imageRatio child:self.dealImageView];
Expand All @@ -310,34 +334,38 @@ - (ASLayoutSpec *)soldOutLabelSpec {
return soldOutLabelOverBackground;
}


+ (CGSize)sizeForWidth:(CGFloat)width {
+ (CGSize)sizeForWidth:(CGFloat)width
{
CGFloat height = [self scaledHeightForPreferredSize:[self preferredViewSize] scaledWidth:width];
return CGSizeMake(width, height);
}


+ (CGSize)preferredViewSize {
+ (CGSize)preferredViewSize
{
return CGSizeMake(kDesignWidth, kDesignHeight);
}

+ (CGFloat)scaledHeightForPreferredSize:(CGSize)preferredSize scaledWidth:(CGFloat)scaledWidth {
+ (CGFloat)scaledHeightForPreferredSize:(CGSize)preferredSize scaledWidth:(CGFloat)scaledWidth
{
CGFloat scale = scaledWidth / kDesignWidth;
CGFloat scaledHeight = ceilf(scale * (kDesignHeight - kFixedLabelsAreaHeight)) + kFixedLabelsAreaHeight;

return scaledHeight;
}

#pragma mark - view operations
#pragma mark - Image

- (CGFloat)imageRatioFromSize:(CGSize)size {
- (CGFloat)imageRatioFromSize:(CGSize)size
{
CGFloat imageHeight = size.height - kFixedLabelsAreaHeight;
CGFloat imageRatio = imageHeight / size.width;

return imageRatio;
}

- (CGSize)imageSize {
- (CGSize)imageSize
{
if (!CGSizeEqualToSize(self.dealImageView.frame.size, CGSizeZero)) {
return self.dealImageView.frame.size;
} else if (!CGSizeEqualToSize(self.calculatedSize, CGSizeZero)) {
Expand All @@ -349,13 +377,14 @@ - (CGSize)imageSize {
}
}

- (void)loadImage {
- (void)loadImage
{
CGSize imageSize = [self imageSize];
if (CGSizeEqualToSize(CGSizeZero, imageSize)) {
return;
}

NSURL *url = [self.viewModel imageURLWithSize:imageSize];
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]]) {
Expand Down
Loading