Files
Swiftgram/examples/ASDKgram/Sample/PhotoTableViewController.m

274 lines
8.9 KiB
Objective-C

//
// PhotoTableViewController.m
// Flickrgram
//
// Created by Hannah Troisi on 2/17/16.
// Copyright © 2016 Hannah Troisi. All rights reserved.
//
#import "PhotoTableViewController.h"
#import "PhotoTableViewCell.h"
#import "UserProfileViewController.h"
#import "LikersViewController.h"
#import "LocationCollectionViewController.h"
#import "PhotoFeedModel.h"
#import "Utilities.h"
#define AUTO_TAIL_LOADING_NUM_SCREENFULS 2.5
@interface PhotoTableViewController () <PhotoTableViewCellProtocol>
@end
@implementation PhotoTableViewController
{
PhotoFeedModel *_photoFeed;
UIView *_statusBarOpaqueUnderlayView;
}
#pragma mark - Lifecycle
- (instancetype)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
self.navigationItem.title = @"500pixgram";
[self.navigationController setNavigationBarHidden:YES];
self.refreshControl = [[UIRefreshControl alloc] init];
[self.refreshControl addTarget:self action:@selector(refreshFeed) forControlEvents:UIControlEventValueChanged];
_photoFeed = [[PhotoFeedModel alloc] initWithPhotoFeedModelType:PhotoFeedModelTypePopular imageSize:[self imageSizeForScreenWidth]];
[self refreshFeed];
_statusBarOpaqueUnderlayView = [[UIView alloc] init];
_statusBarOpaqueUnderlayView.backgroundColor = [UIColor darkBlueColor];
[[[UIApplication sharedApplication] keyWindow] addSubview:_statusBarOpaqueUnderlayView];
}
return self;
}
- (void)viewDidLoad // anything involving the view should go here, not init
{
[super viewDidLoad];
self.tableView.allowsSelection = NO;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.tableView registerClass:[PhotoTableViewCell class] forCellReuseIdentifier:@"photoCell"];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.navigationController.hidesBarsOnSwipe = YES;
}
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
_statusBarOpaqueUnderlayView.frame = [[UIApplication sharedApplication] statusBarFrame];
}
#pragma mark - Instance Methods
- (void)refreshFeed
{
// small first batch
[_photoFeed refreshFeedWithCompletionBlock:^(NSArray *newPhotos){
[self.tableView reloadData]; // overwrite tableView instead of inserting new rows
[self.refreshControl endRefreshing];
[self requestCommentsForPhotos:newPhotos];
// immediately start second larger fetch
[self loadPage];
} numResultsToReturn:4];
}
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
#pragma mark - Helper Methods
- (void)loadPage
{
[_photoFeed requestPageWithCompletionBlock:^(NSArray *newPhotos){
[self insertNewRowsInTableView:newPhotos];
[self requestCommentsForPhotos:newPhotos];
} numResultsToReturn:20];
}
- (void)requestCommentsForPhotos:(NSArray *)newPhotos
{
for (PhotoModel *photo in newPhotos) {
[photo.commentFeed refreshFeedWithCompletionBlock:^(NSArray *newComments) {
NSInteger rowNum = [_photoFeed indexOfPhotoModel:photo];
PhotoTableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:rowNum inSection:0]];
if (cell) {
[cell loadCommentsForPhoto:photo];
[self.tableView beginUpdates];
[self.tableView endUpdates];
// FIXME: adjust content offset - iterate over cells above to get heights...
}
}];
}
}
- (CGSize)imageSizeForScreenWidth
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenScale = [[UIScreen mainScreen] scale];
return CGSizeMake(screenRect.size.width * screenScale, screenRect.size.width * screenScale);
}
- (void)insertNewRowsInTableView:(NSArray *)newPhotos
{
// NSLog(@"_photoFeed number of items = %lu (%lu total)", (unsigned long)[_photoFeed numberOfItemsInFeed], (long)[_photoFeed totalNumberOfPhotos]);
// instead of doing tableView reloadData, use table editing commands
NSMutableArray *indexPaths = [NSMutableArray array];
NSInteger section = 0;
NSUInteger newTotalNumberOfPhotos = [_photoFeed numberOfItemsInFeed];
for (NSUInteger row = newTotalNumberOfPhotos - newPhotos.count; row < newTotalNumberOfPhotos; row++) {
NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:section];
[indexPaths addObject:path];
}
[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
}
//- (void)logPhotoIDsInPhotoFeed
//{
// NSLog(@"_photoFeed number of items = %lu", (unsigned long)[_photoFeed numberOfItemsInFeed]);
//
// for (int i = 0; i < [_photoFeed numberOfItemsInFeed]; i++) {
// if (i % 4 == 0 && i > 0) {
// NSLog(@"\t-----");
// }
//
//// [_photoFeed return]
//// NSString *duplicate = ? @"(DUPLICATE)" : @"";
// NSLog(@"\t%@ %@", [[_photoFeed objectAtIndex:i] photoID], @"");
// }
//}
#pragma mark - UITableViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat currentOffSetY = scrollView.contentOffset.y;
CGFloat contentHeight = scrollView.contentSize.height;
CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
// automatic tail loading
CGFloat screenfullsBeforeBottom = (contentHeight - currentOffSetY) / screenHeight;
if (screenfullsBeforeBottom < AUTO_TAIL_LOADING_NUM_SCREENFULS) {
// NSLog(@"AUTOMATIC TAIL LOADING BEGIN");
[self loadPage];
}
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_photoFeed numberOfItemsInFeed];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
PhotoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"photoCell" forIndexPath:indexPath];
cell.delegate = self;
[cell updateCellWithPhotoObject:[_photoFeed objectAtIndex:indexPath.row]];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
PhotoModel *photo = [_photoFeed objectAtIndex:indexPath.row];
return [PhotoTableViewCell heightForPhotoModel:photo withWidth:self.view.bounds.size.width];
}
#pragma mark - PhotoTableViewCellProtocol
- (void)userProfileWasTouchedWithUser:(UserModel *)user;
{
UserProfileViewController *userProfileView = [[UserProfileViewController alloc] initWithUser:user];
[self.navigationController pushViewController:userProfileView animated:YES];
// force navigationController visible
[self.navigationController setNavigationBarHidden:NO];
}
- (void)photoLocationWasTouchedWithCoordinate:(CLLocationCoordinate2D)coordiantes name:(NSAttributedString *)name
{
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.minimumInteritemSpacing = 1;
layout.minimumLineSpacing = 1;
CGFloat boundsWidth = self.view.bounds.size.width;
layout.headerReferenceSize = CGSizeMake(boundsWidth, 200);
CGFloat photoColumnCount = 3;
CGFloat photoSize = (boundsWidth - (photoColumnCount - 1)) / photoColumnCount;
layout.itemSize = CGSizeMake(photoSize, photoSize);
LocationCollectionViewController *locationCVC = [[LocationCollectionViewController alloc] initWithCollectionViewLayout:layout coordinates:coordiantes];
locationCVC.navigationItem.title = name.string;
[self.navigationController pushViewController:locationCVC animated:YES];
}
- (void)cellWasLongPressedWithPhoto:(PhotoModel *)photo
{
UIAlertAction *savePhotoAction = [UIAlertAction actionWithTitle:@"Save Photo"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"hi");
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * _Nonnull action) {
}];
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
[alert addAction:savePhotoAction];
[alert addAction:cancelAction];
[self presentViewController:alert animated:YES completion:^{}];
}
- (void)photoLikesWasTouchedWithPhoto:(PhotoModel *)photo
{
LikersViewController *vc = [[LikersViewController alloc] initWithPhoto:photo];
[self.navigationController pushViewController:vc animated:YES];
// force navigationController visible
}
@end