|
| 1 | +#import "PestoCollectionViewController.h" |
| 2 | +#import "PestoCardCollectionViewCell.h" |
| 3 | +#import "PestoData.h" |
| 4 | +#import "PestoRemoteImageService.h" |
| 5 | + |
| 6 | +static CGFloat kPestoCollectionViewControllerAnimationDuration = 0.33f; |
| 7 | +static CGFloat kPestoCollectionViewControllerDefaultHeaderHeight = 240.f; |
| 8 | +static CGFloat kPestoCollectionViewControllerInset = 5.f; |
| 9 | +static CGFloat kPestoCollectionViewControllerSmallHeaderHeight = 120.f; |
| 10 | + |
| 11 | +@interface PestoCollectionViewController () |
| 12 | + |
| 13 | +@property (nonatomic) CGFloat logoScale; |
| 14 | +@property (nonatomic) UIView *logoSmallView; |
| 15 | +@property (nonatomic) UIView *logoView; |
| 16 | +@property (nonatomic) PestoData *pestoData; |
| 17 | +@property (nonatomic) PestoRemoteImageService *imageService; |
| 18 | + |
| 19 | +@end |
| 20 | + |
| 21 | +@implementation PestoCollectionViewController |
| 22 | + |
| 23 | +- (instancetype)initWithCollectionViewLayout:(UICollectionViewLayout *)layout { |
| 24 | + self = [super initWithCollectionViewLayout:layout]; |
| 25 | + if (self) { |
| 26 | + self.collectionView.backgroundColor = [UIColor whiteColor]; |
| 27 | + [self.collectionView registerClass:[PestoCardCollectionViewCell class] |
| 28 | + forCellWithReuseIdentifier:@"PestoCardCollectionViewCell"]; |
| 29 | + self.pestoData = [[PestoData alloc] init]; |
| 30 | + self.imageService = [PestoRemoteImageService sharedService]; |
| 31 | + [self setNeedsStatusBarAppearanceUpdate]; |
| 32 | + } |
| 33 | + return self; |
| 34 | +} |
| 35 | + |
| 36 | +- (void)setFlexHeaderContainerVC:(MDCFlexibleHeaderContainerViewController *)flexHeaderContainerVC { |
| 37 | + _flexHeaderContainerVC = flexHeaderContainerVC; |
| 38 | + MDCFlexibleHeaderView *headerView = _flexHeaderContainerVC.headerViewController.headerView; |
| 39 | + headerView.trackingScrollView = self.collectionView; |
| 40 | + headerView.maximumHeight = kPestoCollectionViewControllerDefaultHeaderHeight; |
| 41 | + headerView.minimumHeight = kPestoCollectionViewControllerSmallHeaderHeight; |
| 42 | + [headerView.contentView addSubview:[self pestoHeaderView]]; |
| 43 | +} |
| 44 | + |
| 45 | +- (void)viewWillAppear:(BOOL)animated { |
| 46 | + [super viewWillAppear:animated]; |
| 47 | +} |
| 48 | + |
| 49 | +- (NSInteger)collectionView:(UICollectionView *)view |
| 50 | + numberOfItemsInSection:(NSInteger)section { |
| 51 | + return [self.pestoData.imageFileNames count]; |
| 52 | +} |
| 53 | + |
| 54 | +- (BOOL)prefersStatusBarHidden { |
| 55 | + return YES; |
| 56 | +} |
| 57 | + |
| 58 | +#pragma mark - UICollectionViewDataSource |
| 59 | + |
| 60 | +- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView |
| 61 | + cellForItemAtIndexPath:(NSIndexPath *)indexPath { |
| 62 | + PestoCardCollectionViewCell *cell = |
| 63 | + [collectionView dequeueReusableCellWithReuseIdentifier:@"PestoCardCollectionViewCell" |
| 64 | + forIndexPath:indexPath]; |
| 65 | + NSInteger itemNum = indexPath.row; |
| 66 | + NSString *baseURL = PestoDataBaseURL; |
| 67 | + NSString *imageURLString = |
| 68 | + [baseURL stringByAppendingString:self.pestoData.imageFileNames[itemNum]]; |
| 69 | + NSURL *imageURL = [NSURL URLWithString:imageURLString]; |
| 70 | + |
| 71 | + cell.title = self.pestoData.titles[itemNum]; |
| 72 | + cell.author = self.pestoData.authors[itemNum]; |
| 73 | + cell.imageURL = imageURLString; |
| 74 | + cell.icon = self.pestoData.iconNames[itemNum]; |
| 75 | + [self updateImageViewWithURL:imageURL cell:cell]; |
| 76 | + [cell setNeedsLayout]; |
| 77 | + |
| 78 | + return cell; |
| 79 | +} |
| 80 | + |
| 81 | +- (void)updateImageViewWithURL:(NSURL *)imageURL |
| 82 | + cell:(PestoCardCollectionViewCell *)cell { |
| 83 | + [_imageService fetchImageDataFromURL:imageURL |
| 84 | + completion:^(NSData *imageData) { |
| 85 | + if (!imageData) { |
| 86 | + return; |
| 87 | + } |
| 88 | + dispatch_async(dispatch_get_main_queue(), ^{ |
| 89 | + UIImage *image = [UIImage imageWithData:imageData]; |
| 90 | + UIImage *thumbnailImage = |
| 91 | + [_imageService.thumbnailCache objectForKey:imageURL]; |
| 92 | + cell.image = image; |
| 93 | + cell.imageView.image = thumbnailImage; |
| 94 | + [cell setNeedsLayout]; |
| 95 | + }); |
| 96 | + }]; |
| 97 | +} |
| 98 | + |
| 99 | +#pragma mark - UICollectionViewDelegate |
| 100 | + |
| 101 | +- (CGSize)collectionView:(UICollectionView *)collectionView |
| 102 | + layout:(UICollectionViewLayout *)collectionViewLayout |
| 103 | + sizeForItemAtIndexPath:(NSIndexPath *)indexPath { |
| 104 | + return self.cellSize; |
| 105 | +} |
| 106 | + |
| 107 | +- (void)collectionView:(UICollectionView *)collectionView |
| 108 | + didSelectItemAtIndexPath:(NSIndexPath *)indexPath { |
| 109 | + |
| 110 | + PestoCardCollectionViewCell *cell = |
| 111 | + (PestoCardCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath]; |
| 112 | + [self.delegate didSelectCell:cell completion:^{ |
| 113 | + }]; |
| 114 | +} |
| 115 | + |
| 116 | +#pragma mark - UIScrollViewDelegate |
| 117 | + |
| 118 | +- (void)scrollViewDidScroll:(UIScrollView *)scrollView { |
| 119 | + _scrollOffsetY = scrollView.contentOffset.y; |
| 120 | + [_flexHeaderContainerVC.headerViewController scrollViewDidScroll:scrollView]; |
| 121 | + CGRect headerFrame = _flexHeaderContainerVC.headerViewController.headerView.bounds; |
| 122 | + _logoView.center = CGPointMake(headerFrame.size.width / 2.f, |
| 123 | + headerFrame.size.height / 2.f); |
| 124 | + _logoSmallView.center = CGPointMake(headerFrame.size.width / 2.f, |
| 125 | + headerFrame.size.height / 2.f); |
| 126 | + |
| 127 | + _logoScale = scrollView.contentOffset.y / -kPestoCollectionViewControllerDefaultHeaderHeight; |
| 128 | + |
| 129 | + if (_logoScale < 0.5f) { |
| 130 | + _logoScale = 0.5f; |
| 131 | + [UIView animateWithDuration:kPestoCollectionViewControllerAnimationDuration |
| 132 | + delay:0 |
| 133 | + options:UIViewAnimationOptionCurveEaseOut |
| 134 | + animations:^{ |
| 135 | + _logoView.layer.opacity = 0; |
| 136 | + _logoSmallView.layer.opacity = 1.f; |
| 137 | + } |
| 138 | + completion:^(BOOL finished){ |
| 139 | + }]; |
| 140 | + } else { |
| 141 | + [UIView animateWithDuration:kPestoCollectionViewControllerAnimationDuration |
| 142 | + delay:0 |
| 143 | + options:UIViewAnimationOptionCurveEaseOut |
| 144 | + animations:^{ |
| 145 | + _logoView.layer.opacity = 1.f; |
| 146 | + _logoSmallView.layer.opacity = 0; |
| 147 | + } |
| 148 | + completion:^(BOOL finished){ |
| 149 | + }]; |
| 150 | + } |
| 151 | + _logoView.transform = CGAffineTransformScale(CGAffineTransformIdentity, _logoScale, _logoScale); |
| 152 | +} |
| 153 | + |
| 154 | +#pragma mark - Private methods |
| 155 | + |
| 156 | +- (UIView *)pestoHeaderView { |
| 157 | + CGRect headerFrame = _flexHeaderContainerVC.headerViewController.headerView.bounds; |
| 158 | + UIView *pestoHeaderView = [[UIView alloc] initWithFrame:headerFrame]; |
| 159 | + UIColor *teal = [UIColor colorWithRed:0 green:0.67f blue:0.55f alpha:1]; |
| 160 | + pestoHeaderView.backgroundColor = teal; |
| 161 | + pestoHeaderView.layer.masksToBounds = YES; |
| 162 | + pestoHeaderView.autoresizingMask = |
| 163 | + (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight); |
| 164 | + |
| 165 | + UIImage *image = [UIImage imageNamed:@"PestoLogoLarge"]; |
| 166 | + _logoView = [[UIImageView alloc] initWithImage:image]; |
| 167 | + _logoView.contentMode = UIViewContentModeScaleAspectFill; |
| 168 | + _logoView.center = CGPointMake(pestoHeaderView.frame.size.width / 2, |
| 169 | + pestoHeaderView.frame.size.height / 2); |
| 170 | + [pestoHeaderView addSubview:_logoView]; |
| 171 | + |
| 172 | + UIImage *logoSmallImage = [UIImage imageNamed:@"PestoLogoSmall"]; |
| 173 | + _logoSmallView = [[UIImageView alloc] initWithImage:logoSmallImage]; |
| 174 | + _logoSmallView.contentMode = UIViewContentModeScaleAspectFill; |
| 175 | + _logoSmallView.layer.opacity = 0; |
| 176 | + [pestoHeaderView addSubview:_logoSmallView]; |
| 177 | + |
| 178 | + return pestoHeaderView; |
| 179 | +} |
| 180 | + |
| 181 | +- (CGSize)cellSize { |
| 182 | + CGFloat cellDim = floor((self.view.frame.size.width - (2.f * kPestoCollectionViewControllerInset)) |
| 183 | + / 2.f) - (2.f * kPestoCollectionViewControllerInset); |
| 184 | + if (cellDim > 320) { |
| 185 | + cellDim = floor((self.view.frame.size.width - |
| 186 | + (3.f * kPestoCollectionViewControllerInset)) / 3.f) - |
| 187 | + (2.f * kPestoCollectionViewControllerInset); |
| 188 | + } |
| 189 | + self.cellSize = CGSizeMake(cellDim, cellDim); |
| 190 | + return CGSizeMake(cellDim, cellDim); |
| 191 | +} |
| 192 | + |
| 193 | +@end |
0 commit comments