Swiftgram/examples/ASDKgram/Sample/PhotoCellNode.m
2016-10-27 16:28:47 -07:00

232 lines
8.6 KiB
Objective-C

//
// PhotoCellNode.m
// Sample
//
// Created by Hannah Troisi on 2/17/16.
//
// 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.
//
// 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.
//
#import "PhotoCellNode.h"
#import "Utilities.h"
#import "AsyncDisplayKit.h"
#import "ASDisplayNode+Beta.h"
#import "CommentsNode.h"
#import "PINImageView+PINRemoteImage.h"
#import "PINButton+PINRemoteImage.h"
#define DEBUG_PHOTOCELL_LAYOUT 0
#define HEADER_HEIGHT 50
#define USER_IMAGE_HEIGHT 30
#define HORIZONTAL_BUFFER 10
#define VERTICAL_BUFFER 5
#define FONT_SIZE 14
@implementation PhotoCellNode
{
PhotoModel *_photoModel;
CommentsNode *_photoCommentsView;
ASNetworkImageNode *_userAvatarImageView;
ASNetworkImageNode *_photoImageView;
ASTextNode *_userNameLabel;
ASTextNode *_photoLocationLabel;
ASTextNode *_photoTimeIntervalSincePostLabel;
ASTextNode *_photoLikesLabel;
ASTextNode *_photoDescriptionLabel;
}
#pragma mark - Lifecycle
- (instancetype)initWithPhotoObject:(PhotoModel *)photo;
{
self = [super init];
if (self) {
_photoModel = photo;
_userAvatarImageView = [[ASNetworkImageNode alloc] init];
_userAvatarImageView.URL = photo.ownerUserProfile.userPicURL; // FIXME: make round
// FIXME: autocomplete for this line seems broken
[_userAvatarImageView setImageModificationBlock:^UIImage *(UIImage *image) {
CGSize profileImageSize = CGSizeMake(USER_IMAGE_HEIGHT, USER_IMAGE_HEIGHT);
return [image makeCircularImageWithSize:profileImageSize];
}];
_photoImageView = [[ASNetworkImageNode alloc] init];
_photoImageView.URL = photo.URL;
_photoImageView.layerBacked = YES;
_userNameLabel = [[ASTextNode alloc] init];
_userNameLabel.attributedText = [photo.ownerUserProfile usernameAttributedStringWithFontSize:FONT_SIZE];
_photoLocationLabel = [[ASTextNode alloc] init];
_photoLocationLabel.maximumNumberOfLines = 1;
[photo.location reverseGeocodedLocationWithCompletionBlock:^(LocationModel *locationModel) {
// check and make sure this is still relevant for this cell (and not an old cell)
// make sure to use _photoModel instance variable as photo may change when cell is reused,
// where as local variable will never change
if (locationModel == _photoModel.location) {
_photoLocationLabel.attributedText = [photo locationAttributedStringWithFontSize:FONT_SIZE];
[self setNeedsLayout];
}
}];
_photoTimeIntervalSincePostLabel = [self createLayerBackedTextNodeWithString:[photo uploadDateAttributedStringWithFontSize:FONT_SIZE]];
_photoLikesLabel = [self createLayerBackedTextNodeWithString:[photo likesAttributedStringWithFontSize:FONT_SIZE]];
_photoDescriptionLabel = [self createLayerBackedTextNodeWithString:[photo descriptionAttributedStringWithFontSize:FONT_SIZE]];
_photoDescriptionLabel.maximumNumberOfLines = 3;
_photoCommentsView = [[CommentsNode alloc] init];
// For now disable shouldRasterizeDescendants as it will throw an assertion: 'Node should always be marked invisible before deallocating. ...'
//_photoCommentsView.shouldRasterizeDescendants = YES;
// instead of adding everything addSubnode:
self.automaticallyManagesSubnodes = YES;
#if DEBUG_PHOTOCELL_LAYOUT
_userAvatarImageView.backgroundColor = [UIColor greenColor];
_userNameLabel.backgroundColor = [UIColor greenColor];
_photoLocationLabel.backgroundColor = [UIColor greenColor];
_photoTimeIntervalSincePostLabel.backgroundColor = [UIColor greenColor];
_photoCommentsView.backgroundColor = [UIColor greenColor];
_photoDescriptionLabel.backgroundColor = [UIColor greenColor];
_photoLikesLabel.backgroundColor = [UIColor greenColor];
#endif
}
return self;
}
- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
{
return
// Main stack
[ASStackLayoutSpec
stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical
spacing:0
justifyContent:ASStackLayoutJustifyContentStart
alignItems:ASStackLayoutAlignItemsStretch
children:@[
// Header stack with inset
[ASInsetLayoutSpec
insetLayoutSpecWithInsets:UIEdgeInsetsMake(0, HORIZONTAL_BUFFER, 0, HORIZONTAL_BUFFER)
child:
// Header stack
[ASStackLayoutSpec
stackLayoutSpecWithDirection:ASStackLayoutDirectionHorizontal
spacing:0.0
justifyContent:ASStackLayoutJustifyContentStart
alignItems:ASStackLayoutAlignItemsCenter
children:@[
// Avatar image with inset
[ASInsetLayoutSpec
insetLayoutSpecWithInsets:UIEdgeInsetsMake(HORIZONTAL_BUFFER, 0, HORIZONTAL_BUFFER, HORIZONTAL_BUFFER)
child:
[_userAvatarImageView styledWithBlock:^(ASLayoutElementStyle *style) {
style.preferredSize = CGSizeMake(USER_IMAGE_HEIGHT, USER_IMAGE_HEIGHT);
}]
],
// User and photo location stack
[[ASStackLayoutSpec
stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical
spacing:0.0
justifyContent:ASStackLayoutJustifyContentStart
alignItems:ASStackLayoutAlignItemsStretch
children:_photoLocationLabel.attributedText ? @[
[_userNameLabel styledWithBlock:^(ASLayoutElementStyle *style) {
style.flexShrink = 1.0;
}],
[_photoLocationLabel styledWithBlock:^(ASLayoutElementStyle *style) {
style.flexShrink = 1.0;
}]
] :
@[
[_userNameLabel styledWithBlock:^(ASLayoutElementStyle *style) {
style.flexShrink = 1.0;
}]
]]
styledWithBlock:^(ASLayoutElementStyle *style) {
style.flexShrink = 1.0;
}],
// Spacer between user / photo location and photo time inverval
[[ASLayoutSpec new] styledWithBlock:^(ASLayoutElementStyle *style) {
style.flexGrow = 1.0;
}],
// Photo and time interval node
[_photoTimeIntervalSincePostLabel styledWithBlock:^(ASLayoutElementStyle *style) {
// to remove double spaces around spacer
style.spacingBefore = HORIZONTAL_BUFFER;
}]
]]
],
// Center photo with ratio
[ASRatioLayoutSpec
ratioLayoutSpecWithRatio:1.0
child:_photoImageView],
// Footer stack with inset
[ASInsetLayoutSpec
insetLayoutSpecWithInsets:UIEdgeInsetsMake(VERTICAL_BUFFER, HORIZONTAL_BUFFER, VERTICAL_BUFFER, HORIZONTAL_BUFFER)
child:
[ASStackLayoutSpec
stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical
spacing:VERTICAL_BUFFER
justifyContent:ASStackLayoutJustifyContentStart
alignItems:ASStackLayoutAlignItemsStretch
children:@[
_photoLikesLabel,
_photoDescriptionLabel,
_photoCommentsView
]]
]
]];
}
#pragma mark - Instance Methods
- (void)fetchData
{
[super fetchData];
[_photoModel.commentFeed refreshFeedWithCompletionBlock:^(NSArray *newComments) {
[self loadCommentsForPhoto:_photoModel];
}];
}
#pragma mark - Helper Methods
- (ASTextNode *)createLayerBackedTextNodeWithString:(NSAttributedString *)attributedString
{
ASTextNode *textNode = [[ASTextNode alloc] init];
textNode.layerBacked = YES;
textNode.attributedText = attributedString;
return textNode;
}
- (void)loadCommentsForPhoto:(PhotoModel *)photo
{
if (photo.commentFeed.numberOfItemsInFeed > 0) {
[_photoCommentsView updateWithCommentFeedModel:photo.commentFeed];
[self setNeedsLayout];
}
}
@end