mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-12-22 14:20:20 +00:00
Add 'submodules/AsyncDisplayKit/' from commit '02bedc12816e251ad71777f9d2578329b6d2bef6'
git-subtree-dir: submodules/AsyncDisplayKit git-subtree-mainline:d06f423e0egit-subtree-split:02bedc1281
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
//
|
||||
// AppDelegate.h
|
||||
// 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 <UIKit/UIKit.h>
|
||||
|
||||
#define UseAutomaticLayout 1
|
||||
|
||||
@interface AppDelegate : UIResponder <UIApplicationDelegate>
|
||||
|
||||
@property (strong, nonatomic) UIWindow *window;
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,25 @@
|
||||
//
|
||||
// AppDelegate.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 "AppDelegate.h"
|
||||
|
||||
#import "ViewController.h"
|
||||
|
||||
@implementation AppDelegate
|
||||
|
||||
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
|
||||
{
|
||||
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
|
||||
self.window.backgroundColor = [UIColor whiteColor];
|
||||
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[[ViewController alloc] init]];
|
||||
[self.window makeKeyAndVisible];
|
||||
return YES;
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,17 @@
|
||||
//
|
||||
// BlurbNode.h
|
||||
// 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 <AsyncDisplayKit/AsyncDisplayKit.h>
|
||||
|
||||
/**
|
||||
* Simple node that displays a placekitten.com attribution.
|
||||
*/
|
||||
@interface BlurbNode : ASCellNode
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,120 @@
|
||||
//
|
||||
// BlurbNode.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 "BlurbNode.h"
|
||||
#import "AppDelegate.h"
|
||||
|
||||
#import <AsyncDisplayKit/ASDisplayNode+Subclasses.h>
|
||||
#import <AsyncDisplayKit/ASHighlightOverlayLayer.h>
|
||||
|
||||
#import <AsyncDisplayKit/ASInsetLayoutSpec.h>
|
||||
#import <AsyncDisplayKit/ASCenterLayoutSpec.h>
|
||||
|
||||
static CGFloat kTextPadding = 10.0f;
|
||||
static NSString *kLinkAttributeName = @"PlaceKittenNodeLinkAttributeName";
|
||||
|
||||
@interface BlurbNode () <ASTextNodeDelegate>
|
||||
{
|
||||
ASTextNode *_textNode;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
|
||||
@implementation BlurbNode
|
||||
|
||||
#pragma mark -
|
||||
#pragma mark ASCellNode.
|
||||
|
||||
- (instancetype)init
|
||||
{
|
||||
if (!(self = [super init]))
|
||||
return nil;
|
||||
|
||||
// create a text node
|
||||
_textNode = [[ASTextNode alloc] init];
|
||||
|
||||
// configure the node to support tappable links
|
||||
_textNode.delegate = self;
|
||||
_textNode.userInteractionEnabled = YES;
|
||||
_textNode.linkAttributeNames = @[ kLinkAttributeName ];
|
||||
|
||||
// generate an attributed string using the custom link attribute specified above
|
||||
NSString *blurb = @"kittens courtesy placekitten.com \U0001F638";
|
||||
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:blurb];
|
||||
[string addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Light" size:16.0f] range:NSMakeRange(0, blurb.length)];
|
||||
[string addAttributes:@{
|
||||
kLinkAttributeName: [NSURL URLWithString:@"http://placekitten.com/"],
|
||||
NSForegroundColorAttributeName: [UIColor grayColor],
|
||||
NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle | NSUnderlinePatternDot),
|
||||
}
|
||||
range:[blurb rangeOfString:@"placekitten.com"]];
|
||||
_textNode.attributedText = string;
|
||||
|
||||
// add it as a subnode, and we're done
|
||||
[self addSubnode:_textNode];
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)didLoad
|
||||
{
|
||||
// enable highlighting now that self.layer has loaded -- see ASHighlightOverlayLayer.h
|
||||
self.layer.as_allowsHighlightDrawing = YES;
|
||||
|
||||
[super didLoad];
|
||||
}
|
||||
|
||||
#if UseAutomaticLayout
|
||||
- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
|
||||
{
|
||||
ASCenterLayoutSpec *centerSpec = [[ASCenterLayoutSpec alloc] init];
|
||||
centerSpec.centeringOptions = ASCenterLayoutSpecCenteringX;
|
||||
centerSpec.sizingOptions = ASCenterLayoutSpecSizingOptionMinimumY;
|
||||
centerSpec.child = _textNode;
|
||||
|
||||
UIEdgeInsets padding =UIEdgeInsetsMake(kTextPadding, kTextPadding, kTextPadding, kTextPadding);
|
||||
return [ASInsetLayoutSpec insetLayoutSpecWithInsets:padding child:centerSpec];
|
||||
}
|
||||
#else
|
||||
- (CGSize)calculateSizeThatFits:(CGSize)constrainedSize
|
||||
{
|
||||
// called on a background thread. custom nodes must call -measure: on their subnodes in -calculateSizeThatFits:
|
||||
CGSize measuredSize = [_textNode measure:CGSizeMake(constrainedSize.width - 2 * kTextPadding,
|
||||
constrainedSize.height - 2 * kTextPadding)];
|
||||
return CGSizeMake(constrainedSize.width, measuredSize.height + 2 * kTextPadding);
|
||||
}
|
||||
|
||||
- (void)layout
|
||||
{
|
||||
// called on the main thread. we'll use the stashed size from above, instead of blocking on text sizing
|
||||
CGSize textNodeSize = _textNode.calculatedSize;
|
||||
_textNode.frame = CGRectMake(roundf((self.calculatedSize.width - textNodeSize.width) / 2.0f),
|
||||
kTextPadding,
|
||||
textNodeSize.width,
|
||||
textNodeSize.height);
|
||||
}
|
||||
#endif
|
||||
|
||||
#pragma mark -
|
||||
#pragma mark ASTextNodeDelegate methods.
|
||||
|
||||
- (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];
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,36 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>CFBundleDevelopmentRegion</key>
|
||||
<string>en</string>
|
||||
<key>CFBundleExecutable</key>
|
||||
<string>$(EXECUTABLE_NAME)</string>
|
||||
<key>CFBundleIdentifier</key>
|
||||
<string>com.facebook.AsyncDisplayKit.$(PRODUCT_NAME:rfc1034identifier)</string>
|
||||
<key>CFBundleInfoDictionaryVersion</key>
|
||||
<string>6.0</string>
|
||||
<key>CFBundleName</key>
|
||||
<string>$(PRODUCT_NAME)</string>
|
||||
<key>CFBundlePackageType</key>
|
||||
<string>APPL</string>
|
||||
<key>CFBundleShortVersionString</key>
|
||||
<string>1.0</string>
|
||||
<key>CFBundleSignature</key>
|
||||
<string>????</string>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>1</string>
|
||||
<key>LSRequiresIPhoneOS</key>
|
||||
<true/>
|
||||
<key>UIRequiredDeviceCapabilities</key>
|
||||
<array>
|
||||
<string>armv7</string>
|
||||
</array>
|
||||
<key>UISupportedInterfaceOrientations</key>
|
||||
<array>
|
||||
<string>UIInterfaceOrientationPortrait</string>
|
||||
<string>UIInterfaceOrientationLandscapeLeft</string>
|
||||
<string>UIInterfaceOrientationLandscapeRight</string>
|
||||
</array>
|
||||
</dict>
|
||||
</plist>
|
||||
@@ -0,0 +1,22 @@
|
||||
//
|
||||
// KittenNode.h
|
||||
// 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 <AsyncDisplayKit/AsyncDisplayKit.h>
|
||||
|
||||
/**
|
||||
* Social media-style node that displays a kitten picture and a random length
|
||||
* of lorem ipsum text. Uses a placekitten.com kitten of the specified size.
|
||||
*/
|
||||
@interface KittenNode : ASCellNode
|
||||
|
||||
- (instancetype)initWithKittenOfSize:(CGSize)size;
|
||||
|
||||
- (void)toggleImageEnlargement;
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,197 @@
|
||||
//
|
||||
// KittenNode.mm
|
||||
// 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 "KittenNode.h"
|
||||
#import "AppDelegate.h"
|
||||
|
||||
#import <AsyncDisplayKit/ASDisplayNode+Subclasses.h>
|
||||
|
||||
#import <AsyncDisplayKit/ASStackLayoutSpec.h>
|
||||
#import <AsyncDisplayKit/ASInsetLayoutSpec.h>
|
||||
|
||||
static const CGFloat kImageSize = 80.0f;
|
||||
static const CGFloat kOuterPadding = 16.0f;
|
||||
static const CGFloat kInnerPadding = 10.0f;
|
||||
|
||||
|
||||
@interface KittenNode ()
|
||||
{
|
||||
CGSize _kittenSize;
|
||||
|
||||
ASNetworkImageNode *_imageNode;
|
||||
ASTextNode *_textNode;
|
||||
ASDisplayNode *_divider;
|
||||
BOOL _isImageEnlarged;
|
||||
BOOL _swappedTextAndImage;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
|
||||
@implementation KittenNode
|
||||
|
||||
// lorem ipsum text courtesy https://kittyipsum.com/ <3
|
||||
+ (NSArray *)placeholders
|
||||
{
|
||||
static NSArray *placeholders = nil;
|
||||
|
||||
static dispatch_once_t once;
|
||||
dispatch_once(&once, ^{
|
||||
placeholders = @[
|
||||
@"Kitty ipsum dolor sit amet, purr sleep on your face lay down in your way biting, sniff tincidunt a etiam fluffy fur judging you stuck in a tree kittens.",
|
||||
@"Lick tincidunt a biting eat the grass, egestas enim ut lick leap puking climb the curtains lick.",
|
||||
@"Lick quis nunc toss the mousie vel, tortor pellentesque sunbathe orci turpis non tail flick suscipit sleep in the sink.",
|
||||
@"Orci turpis litter box et stuck in a tree, egestas ac tempus et aliquam elit.",
|
||||
@"Hairball iaculis dolor dolor neque, nibh adipiscing vehicula egestas dolor aliquam.",
|
||||
@"Sunbathe fluffy fur tortor faucibus pharetra jump, enim jump on the table I don't like that food catnip toss the mousie scratched.",
|
||||
@"Quis nunc nam sleep in the sink quis nunc purr faucibus, chase the red dot consectetur bat sagittis.",
|
||||
@"Lick tail flick jump on the table stretching purr amet, rhoncus scratched jump on the table run.",
|
||||
@"Suspendisse aliquam vulputate feed me sleep on your keyboard, rip the couch faucibus sleep on your keyboard tristique give me fish dolor.",
|
||||
@"Rip the couch hiss attack your ankles biting pellentesque puking, enim suspendisse enim mauris a.",
|
||||
@"Sollicitudin iaculis vestibulum toss the mousie biting attack your ankles, puking nunc jump adipiscing in viverra.",
|
||||
@"Nam zzz amet neque, bat tincidunt a iaculis sniff hiss bibendum leap nibh.",
|
||||
@"Chase the red dot enim puking chuf, tristique et egestas sniff sollicitudin pharetra enim ut mauris a.",
|
||||
@"Sagittis scratched et lick, hairball leap attack adipiscing catnip tail flick iaculis lick.",
|
||||
@"Neque neque sleep in the sink neque sleep on your face, climb the curtains chuf tail flick sniff tortor non.",
|
||||
@"Ac etiam kittens claw toss the mousie jump, pellentesque rhoncus litter box give me fish adipiscing mauris a.",
|
||||
@"Pharetra egestas sunbathe faucibus ac fluffy fur, hiss feed me give me fish accumsan.",
|
||||
@"Tortor leap tristique accumsan rutrum sleep in the sink, amet sollicitudin adipiscing dolor chase the red dot.",
|
||||
@"Knock over the lamp pharetra vehicula sleep on your face rhoncus, jump elit cras nec quis quis nunc nam.",
|
||||
@"Sollicitudin feed me et ac in viverra catnip, nunc eat I don't like that food iaculis give me fish.",
|
||||
];
|
||||
});
|
||||
|
||||
return placeholders;
|
||||
}
|
||||
|
||||
- (instancetype)initWithKittenOfSize:(CGSize)size
|
||||
{
|
||||
if (!(self = [super init]))
|
||||
return nil;
|
||||
|
||||
_kittenSize = size;
|
||||
|
||||
// kitten image, with a solid background colour serving as placeholder
|
||||
_imageNode = [[ASNetworkImageNode alloc] init];
|
||||
_imageNode.backgroundColor = ASDisplayNodeDefaultPlaceholderColor();
|
||||
_imageNode.URL = [NSURL URLWithString:[NSString stringWithFormat:@"https://placekitten.com/%zd/%zd",
|
||||
(NSInteger)roundl(_kittenSize.width),
|
||||
(NSInteger)roundl(_kittenSize.height)]];
|
||||
// _imageNode.contentMode = UIViewContentModeCenter;
|
||||
[_imageNode addTarget:self action:@selector(toggleNodesSwap) forControlEvents:ASControlNodeEventTouchUpInside];
|
||||
[self addSubnode:_imageNode];
|
||||
|
||||
// lorem ipsum text, plus some nice styling
|
||||
_textNode = [[ASTextNode alloc] init];
|
||||
_textNode.attributedText = [[NSAttributedString alloc] initWithString:[self kittyIpsum]
|
||||
attributes:[self textStyle]];
|
||||
[self addSubnode:_textNode];
|
||||
|
||||
// hairline cell separator
|
||||
_divider = [[ASDisplayNode alloc] init];
|
||||
_divider.backgroundColor = [UIColor lightGrayColor];
|
||||
[self addSubnode:_divider];
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (NSString *)kittyIpsum
|
||||
{
|
||||
NSArray *placeholders = [KittenNode placeholders];
|
||||
u_int32_t ipsumCount = (u_int32_t)[placeholders count];
|
||||
u_int32_t location = arc4random_uniform(ipsumCount);
|
||||
u_int32_t length = arc4random_uniform(ipsumCount - location);
|
||||
|
||||
NSMutableString *string = [placeholders[location] mutableCopy];
|
||||
for (u_int32_t i = location + 1; i < location + length; i++) {
|
||||
[string appendString:(i % 2 == 0) ? @"\n" : @" "];
|
||||
[string appendString:placeholders[i]];
|
||||
}
|
||||
|
||||
return string;
|
||||
}
|
||||
|
||||
- (NSDictionary *)textStyle
|
||||
{
|
||||
UIFont *font = [UIFont fontWithName:@"HelveticaNeue" size:12.0f];
|
||||
|
||||
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
|
||||
style.paragraphSpacing = 0.5 * font.lineHeight;
|
||||
style.hyphenationFactor = 1.0;
|
||||
|
||||
return @{ NSFontAttributeName: font,
|
||||
NSParagraphStyleAttributeName: style };
|
||||
}
|
||||
|
||||
#if UseAutomaticLayout
|
||||
- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
|
||||
{
|
||||
CGSize imageSize = _isImageEnlarged ? CGSizeMake(2.0 * kImageSize, 2.0 * kImageSize)
|
||||
: CGSizeMake(kImageSize, kImageSize);
|
||||
_imageNode.size = ASRelativeSizeRangeMakeWithExactCGSize(imageSize);
|
||||
_textNode.flexShrink = 1.0;
|
||||
|
||||
ASStackLayoutSpec *stackSpec = [[ASStackLayoutSpec alloc] init];
|
||||
stackSpec.direction = ASStackLayoutDirectionHorizontal;
|
||||
stackSpec.spacing = kInnerPadding;
|
||||
[stackSpec setChildren:!_swappedTextAndImage ? @[_imageNode, _textNode] : @[_textNode, _imageNode]];
|
||||
|
||||
ASInsetLayoutSpec *insetSpec = [[ASInsetLayoutSpec alloc] init];
|
||||
insetSpec.insets = UIEdgeInsetsMake(kOuterPadding, kOuterPadding, kOuterPadding, kOuterPadding);
|
||||
insetSpec.child = stackSpec;
|
||||
|
||||
return insetSpec;
|
||||
}
|
||||
|
||||
// With box model, you don't need to override this method, unless you want to add custom logic.
|
||||
- (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);
|
||||
}
|
||||
#else
|
||||
- (CGSize)calculateSizeThatFits:(CGSize)constrainedSize
|
||||
{
|
||||
CGSize imageSize = CGSizeMake(kImageSize, kImageSize);
|
||||
CGSize textSize = [_textNode measure:CGSizeMake(constrainedSize.width - kImageSize - 2 * kOuterPadding - kInnerPadding,
|
||||
constrainedSize.height)];
|
||||
|
||||
// ensure there's room for the text
|
||||
CGFloat requiredHeight = MAX(textSize.height, imageSize.height);
|
||||
return CGSizeMake(constrainedSize.width, requiredHeight + 2 * kOuterPadding);
|
||||
}
|
||||
|
||||
- (void)layout
|
||||
{
|
||||
CGFloat pixelHeight = 1.0f / [[UIScreen mainScreen] scale];
|
||||
_divider.frame = CGRectMake(0.0f, 0.0f, self.calculatedSize.width, pixelHeight);
|
||||
|
||||
_imageNode.frame = CGRectMake(kOuterPadding, kOuterPadding, kImageSize, kImageSize);
|
||||
|
||||
CGSize textSize = _textNode.calculatedSize;
|
||||
_textNode.frame = CGRectMake(kOuterPadding + kImageSize + kInnerPadding, kOuterPadding, textSize.width, textSize.height);
|
||||
}
|
||||
#endif
|
||||
|
||||
- (void)toggleImageEnlargement
|
||||
{
|
||||
_isImageEnlarged = !_isImageEnlarged;
|
||||
[self setNeedsLayout];
|
||||
}
|
||||
|
||||
- (void)toggleNodesSwap
|
||||
{
|
||||
_swappedTextAndImage = !_swappedTextAndImage;
|
||||
[self setNeedsLayout];
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,14 @@
|
||||
//
|
||||
// ViewController.h
|
||||
// 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 <UIKit/UIKit.h>
|
||||
|
||||
@interface ViewController : UIViewController
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,209 @@
|
||||
//
|
||||
// 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 "KittenNode.h"
|
||||
|
||||
|
||||
static const NSInteger kLitterSize = 20; // intial number of kitten cells in ASTableView
|
||||
static const NSInteger kLitterBatchSize = 10; // number of kitten cells to add to ASTableView
|
||||
static const NSInteger kMaxLitterSize = 100; // max number of kitten 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:kLitterSize];
|
||||
|
||||
_blurbNodeIndexPath = [NSIndexPath indexPathForItem:0 inSection:0];
|
||||
|
||||
self.title = @"Kittens";
|
||||
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit
|
||||
target:self
|
||||
action:@selector(toggleEditingMode)];
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (NSMutableArray *)createLitterWithSize:(NSInteger)litterSize
|
||||
{
|
||||
NSMutableArray *kittens = [NSMutableArray arrayWithCapacity:litterSize];
|
||||
for (NSInteger i = 0; i < litterSize; i++) {
|
||||
|
||||
// placekitten.com will return the same kitten picture if the same pixel height & width are requested,
|
||||
// so generate kittens with different width & height values.
|
||||
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);
|
||||
|
||||
[kittens addObject:[NSValue valueWithCGSize:size]];
|
||||
}
|
||||
return kittens;
|
||||
}
|
||||
|
||||
- (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];
|
||||
|
||||
[_tableView reloadDataImmediately];
|
||||
}
|
||||
|
||||
- (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];
|
||||
[_tableView beginUpdates];
|
||||
// Assume only kitten nodes are selectable (see -tableView:shouldHighlightRowAtIndexPath:).
|
||||
KittenNode *node = (KittenNode *)[_tableView nodeForRowAtIndexPath:indexPath];
|
||||
[node toggleImageEnlargement];
|
||||
[_tableView endUpdates];
|
||||
}
|
||||
|
||||
- (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];
|
||||
KittenNode *node = [[KittenNode 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 < kMaxLitterSize;
|
||||
}
|
||||
|
||||
- (void)tableView:(UITableView *)tableView willBeginBatchFetchWithContext:(ASBatchContext *)context
|
||||
{
|
||||
NSLog(@"adding kitties");
|
||||
|
||||
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:kLitterBatchSize];
|
||||
|
||||
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];
|
||||
|
||||
NSLog(@"kittens added");
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
- (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
|
||||
@@ -0,0 +1,18 @@
|
||||
//
|
||||
// main.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 <UIKit/UIKit.h>
|
||||
|
||||
#import "AppDelegate.h"
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
@autoreleasepool {
|
||||
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user