Swiftgram/examples/ASDKgram/Sample/PhotoFeedNodeController.m
Hannah Troisi 4e8d835280 [ASDKgram example update] addressed PR comments
- overrode the -(void)fetchData method in PhotoCellNode.m to download the photo’s comments. This method gets called with the PhotoCellNode enters ASInterfaceStateFetchData, which is set by the rangeController.
    - UIKIT COMPARISON: I left the comment bulk download (for all photos in a page load) in the PhotoFeedViewController side for UIKit because when implemented in the PhotoTableViewCell, each cell jumped around as it changed size when it came on screen.
- minor appearance updates
    - updated color scheme
    - fixed status bar style to darkBackgroundColor
- cleaned up layoutSpecThatFits: in PhotoCellNode
2016-04-14 00:52:05 -07:00

185 lines
5.4 KiB
Objective-C

//
// PhotoFeedNodeController.m
// ASDKgram
//
// Created by Hannah Troisi on 2/17/16.
// Copyright © 2016 Hannah Troisi. All rights reserved.
//
#import "PhotoFeedNodeController.h"
#import <AsyncDisplayKit/AsyncDisplayKit.h>
#import "Utilities.h"
#import "PhotoModel.h"
#import "PhotoCellNode.h"
#import "PhotoFeedModel.h"
#define AUTO_TAIL_LOADING_NUM_SCREENFULS 2.5
@interface PhotoFeedNodeController () <ASTableDelegate, ASTableDataSource>
@end
@implementation PhotoFeedNodeController
{
PhotoFeedModel *_photoFeed;
ASTableNode *_tableNode;
UIActivityIndicatorView *_activityIndicatorView;
}
#pragma mark - Lifecycle
- (instancetype)init
{
_tableNode = [[ASTableNode alloc] init];
self = [super initWithNode:_tableNode];
if (self) {
self.navigationItem.title = @"ASDK";
[self.navigationController setNavigationBarHidden:YES];
_tableNode.dataSource = self;
_tableNode.delegate = self;
_activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
}
return self;
}
// do any ASDK view stuff in loadView
- (void)loadView
{
[super loadView];
_photoFeed = [[PhotoFeedModel alloc] initWithPhotoFeedModelType:PhotoFeedModelTypePopular imageSize:[self imageSizeForScreenWidth]];
[self refreshFeed];
CGSize boundSize = self.view.bounds.size;
[_activityIndicatorView sizeToFit];
CGRect refreshRect = _activityIndicatorView.frame;
refreshRect.origin = CGPointMake((boundSize.width - _activityIndicatorView.frame.size.width) / 2.0,
(boundSize.height - _activityIndicatorView.frame.size.height) / 2.0);
_activityIndicatorView.frame = refreshRect;
[self.view addSubview:_activityIndicatorView];
self.view.backgroundColor = [UIColor whiteColor];
_tableNode.view.allowsSelection = NO;
_tableNode.view.separatorStyle = UITableViewCellSeparatorStyleNone;
_tableNode.view.leadingScreensForBatching = AUTO_TAIL_LOADING_NUM_SCREENFULS; // overriding default of 2.0
}
#pragma mark - helper methods
- (void)refreshFeed
{
[_activityIndicatorView startAnimating];
// small first batch
[_photoFeed refreshFeedWithCompletionBlock:^(NSArray *newPhotos){
[_activityIndicatorView stopAnimating];
[self insertNewRowsInTableView:newPhotos];
// [self requestCommentsForPhotos:newPhotos];
// immediately start second larger fetch
[self loadPageWithContext:nil];
} numResultsToReturn:4];
}
- (void)loadPageWithContext:(ASBatchContext *)context
{
[_photoFeed requestPageWithCompletionBlock:^(NSArray *newPhotos){
[self insertNewRowsInTableView:newPhotos];
// [self requestCommentsForPhotos:newPhotos];
if (context) {
[context completeBatchFetching:YES];
}
} numResultsToReturn:20];
}
//- (void)requestCommentsForPhotos:(NSArray *)newPhotos
//{
// for (PhotoModel *photo in newPhotos) {
// [photo.commentFeed refreshFeedWithCompletionBlock:^(NSArray *newComments) {
//
// NSInteger rowNum = [_photoFeed indexOfPhotoModel:photo];
// NSIndexPath *cellPath = [NSIndexPath indexPathForRow:rowNum inSection:0];
// PhotoCellNode *cell = (PhotoCellNode *)[_tableNode.view nodeForRowAtIndexPath:cellPath];
//
// if (cell) {
// [cell loadCommentsForPhoto:photo];
// [_tableNode.view beginUpdates];
// [_tableNode.view endUpdates];
// }
// }];
// }
//}
- (void)insertNewRowsInTableView:(NSArray *)newPhotos
{
NSInteger section = 0;
NSMutableArray *indexPaths = [NSMutableArray array];
NSUInteger newTotalNumberOfPhotos = [_photoFeed numberOfItemsInFeed];
for (NSUInteger row = newTotalNumberOfPhotos - newPhotos.count; row < newTotalNumberOfPhotos; row++) {
NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:section];
[indexPaths addObject:path];
}
[_tableNode.view insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
}
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
- (CGSize)imageSizeForScreenWidth
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenScale = [[UIScreen mainScreen] scale];
return CGSizeMake(screenRect.size.width * screenScale, screenRect.size.width * screenScale);
}
#pragma mark - ASTableDataSource methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_photoFeed numberOfItemsInFeed];
}
- (ASCellNodeBlock)tableView:(ASTableView *)tableView nodeBlockForRowAtIndexPath:(NSIndexPath *)indexPath
{
PhotoModel *photoModel = [_photoFeed objectAtIndex:indexPath.row];
// this will be executed on a background thread - important to make sure it's thread safe
ASCellNode *(^ASCellNodeBlock)() = ^ASCellNode *() {
PhotoCellNode *cellNode = [[PhotoCellNode alloc] initWithPhotoObject:photoModel];
return cellNode;
};
return ASCellNodeBlock;
}
#pragma mark - ASTableDelegate methods
// Receive a message that the tableView is near the end of its data set and more data should be fetched if necessary.
- (void)tableView:(ASTableView *)tableView willBeginBatchFetchWithContext:(ASBatchContext *)context
{
[context beginBatchFetching];
[self loadPageWithContext:context];
}
#pragma mark - PhotoFeedViewControllerProtocol
- (void)resetAllData
{
[_photoFeed clearFeed];
[_tableNode.view reloadData];
[self refreshFeed];
}
@end