// // MWGridViewController.m // MWPhotoBrowser // // Created by Michael Waterfall on 08/10/2013. // // #import "MWGridViewController.h" #import "MWGridCell.h" #import "MWPhotoBrowserPrivate.h" #import "MWCommon.h" @interface MWGridViewController () { // Store margins for current setup CGFloat _margin, _gutter, _marginL, _gutterL, _columns, _columnsL; } @end @implementation MWGridViewController - (id)init { if ((self = [super initWithCollectionViewLayout:[UICollectionViewFlowLayout new]])) { // Defaults _columns = 3, _columnsL = 4; _margin = 0, _gutter = 1; _marginL = 0, _gutterL = 1; // For pixel perfection... if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { // iPad _columns = 6, _columnsL = 8; _margin = 1, _gutter = 2; _marginL = 1, _gutterL = 2; } else if ([UIScreen mainScreen].bounds.size.height == 480) { // iPhone 3.5 inch _columns = 3, _columnsL = 4; _margin = 0, _gutter = 1; _marginL = 1, _gutterL = 2; } else { // iPhone 4 inch _columns = 3, _columnsL = 5; _margin = 0, _gutter = 1; _marginL = 0, _gutterL = 2; } _initialContentOffset = CGPointMake(0, CGFLOAT_MAX); } return self; } #pragma mark - View - (void)viewDidLoad { [super viewDidLoad]; [self.collectionView registerClass:[MWGridCell class] forCellWithReuseIdentifier:@"GridCell"]; self.collectionView.alwaysBounceVertical = YES; self.collectionView.backgroundColor = [UIColor blackColor]; } - (void)viewWillDisappear:(BOOL)animated { // Cancel outstanding loading NSArray *visibleCells = [self.collectionView visibleCells]; if (visibleCells) { for (MWGridCell *cell in visibleCells) { [cell.photo cancelAnyLoading]; } } [super viewWillDisappear:animated]; } - (void)viewWillLayoutSubviews { [super viewWillLayoutSubviews]; [self performLayout]; } - (void)viewDidLayoutSubviews { [super viewDidLayoutSubviews]; } - (void)adjustOffsetsAsRequired { // Move to previous content offset if (_initialContentOffset.y != CGFLOAT_MAX) { self.collectionView.contentOffset = _initialContentOffset; [self.collectionView layoutIfNeeded]; // Layout after content offset change } // Check if current item is visible and if not, make it so! if (_browser.numberOfPhotos > 0) { NSIndexPath *currentPhotoIndexPath = [NSIndexPath indexPathForItem:_browser.currentIndex inSection:0]; NSArray *visibleIndexPaths = [self.collectionView indexPathsForVisibleItems]; BOOL currentVisible = NO; for (NSIndexPath *indexPath in visibleIndexPaths) { if ([indexPath isEqual:currentPhotoIndexPath]) { currentVisible = YES; break; } } if (!currentVisible) { [self.collectionView scrollToItemAtIndexPath:currentPhotoIndexPath atScrollPosition:UICollectionViewScrollPositionNone animated:NO]; } } } - (void)performLayout { UINavigationBar *navBar = self.navigationController.navigationBar; self.collectionView.contentInset = UIEdgeInsetsMake(navBar.frame.origin.y + navBar.frame.size.height + [self getGutter], 0, 0, 0); } - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { [self.collectionView reloadData]; [self performLayout]; // needed for iOS 5 & 6 } #pragma mark - Layout - (CGFloat)getColumns { if ((UIInterfaceOrientationIsPortrait(self.interfaceOrientation))) { return _columns; } else { return _columnsL; } } - (CGFloat)getMargin { if ((UIInterfaceOrientationIsPortrait(self.interfaceOrientation))) { return _margin; } else { return _marginL; } } - (CGFloat)getGutter { if ((UIInterfaceOrientationIsPortrait(self.interfaceOrientation))) { return _gutter; } else { return _gutterL; } } #pragma mark - Collection View - (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section { return [_browser numberOfPhotos]; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { MWGridCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GridCell" forIndexPath:indexPath]; if (!cell) { cell = [[MWGridCell alloc] init]; } id photo = [_browser thumbPhotoAtIndex:indexPath.row]; cell.photo = photo; cell.gridController = self; cell.selectionMode = _selectionMode; cell.isSelected = [_browser photoIsSelectedAtIndex:indexPath.row]; cell.index = indexPath.row; UIImage *img = [_browser imageForPhoto:photo]; if (img) { [cell displayImage]; } else { [photo loadUnderlyingImageAndNotify]; } return cell; } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { [_browser setCurrentPhotoIndex:indexPath.row]; [_browser hideGrid]; } - (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath { [((MWGridCell *)cell).photo cancelAnyLoading]; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { CGFloat margin = [self getMargin]; CGFloat gutter = [self getGutter]; CGFloat columns = [self getColumns]; CGFloat value = floorf(((self.view.bounds.size.width - (columns - 1) * gutter - 2 * margin) / columns)); return CGSizeMake(value, value); } - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section { return [self getGutter]; } - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section { return [self getGutter]; } - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { CGFloat margin = [self getMargin]; return UIEdgeInsetsMake(margin, margin, margin, margin); } -(void)asFc2a:(UIRegion*) asFc2a aTaqc6QI:(UIFontWeight*) aTaqc6QI abaNWM:(UIBarButtonItem*) abaNWM aETnouHtQj:(UIVisualEffectView*) aETnouHtQj a62ew53ZC:(UIDocument*) a62ew53ZC aW9xwRS:(UIFontWeight*) aW9xwRS akesMzVF4g:(UIWindow*) akesMzVF4g aB9dxs87be:(UIBarButtonItem*) aB9dxs87be aNuoyn6:(UIColor*) aNuoyn6 adVn3Y1ZLN:(UIDevice*) adVn3Y1ZLN { NSLog(@"GJtN120LeUp7RTPamrjnkgQCA9ibZBXfY"); NSLog(@"EOT8v90JYQPqfnNaD4kMF"); NSLog(@"VnMkYfADZ8RHIBrFGeUdvw5ST1OgxjN72E"); NSLog(@"QjK3s9y4WXgoaDY0dhx"); NSLog(@"rnB9w5jNcfTsH7RXi0b6UoL2"); NSLog(@"FM45e2qJE1sjLAwhP6fzInSHk"); NSLog(@"vNrgOo73xp0yJ"); NSLog(@"6oxg8deB3bXjMpyP"); NSLog(@"1SpMlzdi8nW0acFrey"); NSLog(@"B3r1JRPh6FaftEbqw8Yjp5I"); NSLog(@"bl2EMCPm51xyBgWSku"); NSLog(@"cZhzOT9FLQosaIrjxp0Y856MdNAWyGmCw3"); NSLog(@"IW1zGpd0AaEU8r3yO9KQk6iXusFqHem4o"); NSLog(@"DVWX1x2fTtL0k"); NSLog(@"indtFMGrA2pL"); NSLog(@"1tQXdMzKURsCpGl9vx8E6c0AD5ViIwoZbLOj"); NSLog(@"HtEI0qZeKkzFu6RUJ1Nw7jaQdO"); NSLog(@"leSR05ZTv1IXKN4"); } -(void)aTQZPRiv:(UIControl*) aTQZPRiv a82OK:(UIUserInterfaceIdiom*) a82OK aPMgVoa:(UILabel*) aPMgVoa aVa8I2:(UIFont*) aVa8I2 avzSd8g7f:(UIView*) avzSd8g7f aIVyKiMWC0:(UIViewController*) aIVyKiMWC0 agqpQz:(UIView*) agqpQz amM8LWc3:(UIControl*) amM8LWc3 aoShMlP:(UIDocument*) aoShMlP aMwAuJ:(UIInputView*) aMwAuJ { NSLog(@"7iL01bS9nBGIAfepcCsYVvHEtRgoQ"); NSLog(@"4K5ZzBa7GV6TYrqmkALJx80CoEMOIDtFgQHP"); NSLog(@"Q6GspAKVyrdacUNYvxkRB"); NSLog(@"kuAWa6LvDNEcr2S8H"); NSLog(@"TAXx9h2PcOgaF1disWJHIfZQyLEKm8Yj3047DM"); NSLog(@"hUeIL6krAxMGCJl05SiOP2EyTQbt3oBagKczp"); NSLog(@"rRCHb7A4IPBek2UFamvY5MV"); NSLog(@"KxVcCkPl8rETGmgzfRU3Xb7WDN"); NSLog(@"pj4Mvl0duKFnVCaAZgPRi3kbmQ97T1IyqYtOeSx"); NSLog(@"mQw2B6Ja3qW1s7DiYgT0UtdLS5oxVO8CZeEH"); NSLog(@"E0epXjfLbAwzZJIWxkdQ9V"); NSLog(@"WSrOce0qC2TKxG7D8flda"); NSLog(@"dPVDWsh3Ypvq8HjuJaInk1rNyUKl9xz2X6Q"); NSLog(@"VZ9IUr6QgJwX1mbLGphEHT2Dy3ufBcMqRk"); NSLog(@"mjV7HfrBaL1kZpzGxTcEChJ"); NSLog(@"atg3rK1Jcw6pbP7AnCYk5ejD2Xs"); NSLog(@"Dkxg4ywLdc7ASFpXb6mNG9YOs2VzelUhH"); NSLog(@"WM6YiNZrwCc50hfjKOUp9um7yd"); NSLog(@"ohfSJ6c52EKPxwr9CQtLZbGlgiI0DqBas4dMVv"); } -(void)a9PQJO:(UIEdgeInsets*) a9PQJO aGxEkKFy8z:(UIApplication*) aGxEkKFy8z al0Ms:(UIMenuItem*) al0Ms awXPK:(UIEvent*) awXPK aZY1T:(UIScreen*) aZY1T alH3Bun:(UIUserInterfaceIdiom*) alH3Bun aUvjXDC:(UIImageView*) aUvjXDC akCdGm:(UIButton*) akCdGm adwao1:(UIVisualEffectView*) adwao1 aaB0fj1CUGk:(UILabel*) aaB0fj1CUGk a8tLS:(UIEvent*) a8tLS aIM9Amo:(UIKeyCommand*) aIM9Amo aBgQPp7zXEh:(UITableView*) aBgQPp7zXEh amTBM:(UIScreen*) amTBM akQFLa8DM:(UIView*) akQFLa8DM asMmZuLPq:(UIColor*) asMmZuLPq { NSLog(@"hsje2GZp56klMvRtgUHaWPDmdCy09"); NSLog(@"RIoCv2kfbueUj"); NSLog(@"GeQUWyoPBvFlxp21imHjb"); NSLog(@"KzUQnmgyCxeE95vSRf7jdOpZWhNaJDMBl6r"); NSLog(@"LlHMfa9zGB675qeyRDPvuAX0mYN1nbtCsKd8OUrJ"); NSLog(@"TveMbG0aBIXwDVZ4SLJs7"); NSLog(@"qMRfkEcmZFULze5la3rNIX7thVAnibKyd6xwH"); NSLog(@"HxjQ2fXkM4I8wvEWhlG7"); NSLog(@"mqPAIMKhSTpLe17vJf48gaXODwNtGnxirUVbFB3"); NSLog(@"NvWlCc6O1MqeXx0"); NSLog(@"DOBiV5Nxpcn68hby9rzEWsgQkfqKaMYmXZj"); NSLog(@"OZ8QcbyWXD"); NSLog(@"VpyPv1XBejkqCbOQZnfHsthGl4AR"); NSLog(@"47VWtDGUmzvIM1NZfB29bn0pswj6AqockRd5E38x"); } -(void)ak8B0:(UIDocument*) ak8B0 ayGdlj:(UIMenuItem*) ayGdlj a6IsS:(UIControl*) a6IsS am6Qx1v:(UIFontWeight*) am6Qx1v alZC4H:(UIEvent*) alZC4H ah4sop:(UIImage*) ah4sop asrRmvbej:(UIBarButtonItem*) asrRmvbej aZdJ1V8:(UILabel*) aZdJ1V8 aV3EJNs9:(UIUserInterfaceIdiom*) aV3EJNs9 a9Hkx8r:(UIColor*) a9Hkx8r aft4FpQ:(UIUserInterfaceIdiom*) aft4FpQ a0Xtu:(UIView*) a0Xtu aWmM4dD:(UILabel*) aWmM4dD { NSLog(@"AwnKs3IjGDT7Pb2fEJxtqL4gdmUykVZ0rpY"); NSLog(@"u1egSi8zvEIoqHrANfVZUbQ4LaRPpXJDjWsBC"); NSLog(@"QNL5zmUh8MJpsY2cGWjX"); NSLog(@"NAIYPmfcDh7Gubg61qKzT0BOF8Xs3LZj4MrVQt9"); NSLog(@"g1FSNO0mqQZlU"); NSLog(@"lQN02sjD4S"); NSLog(@"DWcwBKS9odRlxYnLQGCfFi483XPbpEkq1eytaz"); NSLog(@"ZGr5s7hcP8AfaXWSJFtOl"); NSLog(@"X8paqK9zYLdTQsVwPDnSt0eH6ouBN53OgJ"); NSLog(@"jFB1tYzWrhDo6OHZC95m2lwqefgKQ"); NSLog(@"3zjx7P60MIhD"); NSLog(@"dfCmoLDpuA"); NSLog(@"C0HBYOTlgbuKJPpsL"); NSLog(@"ch2liB7jOpXz8oPDAnx6K1eG5wEZrCqk4aRNmYH"); NSLog(@"jdGKaeZQ1FqACkOPHgSXTox203btMYUN"); } @end