Swiftgram/examples/Kittens/Sample/ViewController.m
Adlai Holler 27fac9f586
Create a centralized configuration API (#747)
* Update the dangerfile

* Make a trivial change to test new dangerfile

* Try out the new value with another trivial change

* Add a configuration API to make a unified place for pulling config from clients safely

* Specify properties for delegate

* Finish removing text experiment global enable

* Generate the config file

* Clean up configuration to fix tests

* Work on making it serializable

* Finish it up

* Fix example code

* Update sample project

* Clean up a few things

* Align with new project order

* Make it faster and update license header

* Add an option to specify your config at compile time

* Update another license header

* Add a version field, and bring interface state coalescing into configuration

* Update CA queue code

* Update CATransactionQueue tests

* Turn transaction queue on by default (for now, see comment)

* Update the tests

* Update the tests AGAIN

* Remove unused ordered set
2018-03-25 12:52:57 -07:00

185 lines
6.2 KiB
Objective-C

//
// ViewController.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) 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 "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 ASTableNode
static const NSInteger kLitterBatchSize = 10; // number of kitten cells to add to ASTableNode
static const NSInteger kMaxLitterSize = 100; // max number of kitten cells allowed in ASTableNode
@interface ViewController () <ASTableDataSource, ASTableDelegate>
{
ASTableNode *_tableNode;
// array of boxed CGSizes corresponding to placekitten.com kittens
NSMutableArray *_kittenDataSource;
NSIndexPath *_blurbNodeIndexPath;
}
@property (nonatomic, strong) NSMutableArray *kittenDataSource;
@end
@implementation ViewController
#pragma mark - Lifecycle
- (instancetype)init
{
_tableNode = [[ASTableNode alloc] initWithStyle:UITableViewStylePlain];
_tableNode.dataSource = self;
_tableNode.delegate = self;
if (!(self = [super initWithNode:_tableNode]))
return nil;
// 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;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_tableNode.view.separatorStyle = UITableViewCellSeparatorStyleNone; // KittenNode has its own separator
[self.node addSubnode:_tableNode];
}
#pragma mark - Data Model
- (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)toggleEditingMode
{
[_tableNode.view setEditing:!_tableNode.view.editing animated:YES];
}
#pragma mark - ASTableNode
- (NSInteger)tableNode:(ASTableNode *)tableNode numberOfRowsInSection:(NSInteger)section
{
// blurb node + kLitterSize kitties
return 1 + _kittenDataSource.count;
}
- (ASCellNode *)tableNode:(ASTableNode *)tableNode 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;
}
- (void)tableNode:(ASTableNode *)tableNode didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[_tableNode deselectRowAtIndexPath:indexPath animated:YES];
// Assume only kitten nodes are selectable (see -tableNode:shouldHighlightRowAtIndexPath:).
KittenNode *node = (KittenNode *)[_tableNode nodeForRowAtIndexPath:indexPath];
[node toggleImageEnlargement];
}
- (BOOL)tableNode:(ASTableNode *)tableNode shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
// Enable selection for kitten nodes
return [_blurbNodeIndexPath compare:indexPath] != NSOrderedSame;
}
- (void)tableNode:(ASTableNode *)tableNode willBeginBatchFetchWithContext:(nonnull ASBatchContext *)context
{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), 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];
[tableNode insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
[context completeBatchFetching:YES];
});
}
- (BOOL)shouldBatchFetchForTableNode:(ASTableNode *)tableNode
{
return _kittenDataSource.count < kMaxLitterSize;
}
- (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];
[_tableNode deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
@end