Skip to content

Commit

Permalink
Add utilities for dequeueing views from a collection view.
Browse files Browse the repository at this point in the history
  • Loading branch information
lhunath committed Jun 22, 2014
1 parent 95e5cfa commit e50cad1
Show file tree
Hide file tree
Showing 12 changed files with 295 additions and 49 deletions.
16 changes: 14 additions & 2 deletions Pearl-UIKit/NSLayoutConstraint+PearlUIKit.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
/**
* Copyright Maarten Billemont (http://www.lhunath.com, lhunath@lyndir.com)
*
* See the enclosed file LICENSE for license information (LGPLv3). If you did
* not receive this file, see http://www.gnu.org/licenses/lgpl-3.0.txt
*
* @author Maarten Billemont <lhunath@lyndir.com>
* @license http://www.gnu.org/licenses/lgpl-3.0.txt
*/

//
// NSLayoutConstraint (PearlUIKit)
//
// Created by Maarten Billemont on 2/5/2014.
// Copyright (c) 2014 Tristan Interactive. All rights reserved.
// Created by Maarten Billemont on 2/5/2014.
// Copyright 2014 lhunath (Maarten Billemont). All rights reserved.
//

#import <UIKit/UIKit.h>
Expand Down
16 changes: 14 additions & 2 deletions Pearl-UIKit/NSLayoutConstraint+PearlUIKit.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
/**
* Copyright Maarten Billemont (http://www.lhunath.com, lhunath@lyndir.com)
*
* See the enclosed file LICENSE for license information (LGPLv3). If you did
* not receive this file, see http://www.gnu.org/licenses/lgpl-3.0.txt
*
* @author Maarten Billemont <lhunath@lyndir.com>
* @license http://www.gnu.org/licenses/lgpl-3.0.txt
*/

//
// NSLayoutConstraint (PearlUIKit)
//
// Created by Maarten Billemont on 2/5/2014.
// Copyright (c) 2014 Tristan Interactive. All rights reserved.
// Created by Maarten Billemont on 2/5/2014.
// Copyright 2014 lhunath (Maarten Billemont). All rights reserved.
//

#import "NSLayoutConstraint+PearlUIKit.h"
Expand Down
2 changes: 2 additions & 0 deletions Pearl-UIKit/Pearl-UIKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@
#import "PearlUIView.h"
#import "PearlValidatingTextField.h"
#import "PearlWebViewController.h"
#import "UICollectionReusableView+PearlDequeue.h"
#import "UIControl+PearlBlocks.h"
#import "UIControl+PearlSelect.h"
#import "UIImage+PearlScaling.h"
#import "UIResponder+PearlFirstResponder.h"
#import "UIScrollView+PearlAdjustInsets.h"
#import "UIScrollView+PearlFlashingIndicators.h"
#import "UITableView+PearlReloadFromArray.h"
#import "UITextView+PearlAttributes.h"
18 changes: 9 additions & 9 deletions Pearl-UIKit/PearlUIUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ extern CGRect CGRectWithOrigin(CGRect rect, CGPoint origin);
extern CGRect CGRectWithSize(CGRect rect, CGSize size);

// Calculate the point of a certain part of a CGRect.
extern CGPoint CGPointFromCGRectCenter(CGRect rect);
extern CGPoint CGPointFromCGRectTop(CGRect rect);
extern CGPoint CGPointFromCGRectRight(CGRect rect);
extern CGPoint CGPointFromCGRectBottom(CGRect rect);
extern CGPoint CGPointFromCGRectLeft(CGRect rect);
extern CGPoint CGPointFromCGRectTopLeft(CGRect rect);
extern CGPoint CGPointFromCGRectTopRight(CGRect rect);
extern CGPoint CGPointFromCGRectBottomRight(CGRect rect);
extern CGPoint CGPointFromCGRectBottomLeft(CGRect rect);
extern CGPoint CGRectGetCenter(CGRect rect);
extern CGPoint CGRectGetTop(CGRect rect);
extern CGPoint CGRectGetRight(CGRect rect);
extern CGPoint CGRectGetBottom(CGRect rect);
extern CGPoint CGRectGetLeft(CGRect rect);
extern CGPoint CGRectGetTopLeft(CGRect rect);
extern CGPoint CGRectGetTopRight(CGRect rect);
extern CGPoint CGRectGetBottomRight(CGRect rect);
extern CGPoint CGRectGetBottomLeft(CGRect rect);

/** Get the UIEdgeInsets that insets each edge by the largest edge of either a or b. */
UIEdgeInsets UIEdgeInsetsUnionEdgeInsets(UIEdgeInsets a, UIEdgeInsets b);
Expand Down
28 changes: 15 additions & 13 deletions Pearl-UIKit/PearlUIUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,60 +46,62 @@ CGRect CGRectWithSize(CGRect rect, CGSize size) {
return (CGRect){ rect.origin, size };
}

CGPoint CGPointFromCGRectCenter(CGRect rect) {
CGPoint CGRectGetCenter(CGRect rect) {

return CGPointMake( rect.origin.x + rect.size.width / 2, rect.origin.y + rect.size.height / 2 );
}

CGPoint CGPointFromCGRectTop(CGRect rect) {
CGPoint CGRectGetTop(CGRect rect) {

return CGPointMake( rect.origin.x + rect.size.width / 2, rect.origin.y );
}

CGPoint CGPointFromCGRectRight(CGRect rect) {
CGPoint CGRectGetRight(CGRect rect) {

return CGPointMake( rect.origin.x + rect.size.width, rect.origin.y + rect.size.height / 2 );
}

CGPoint CGPointFromCGRectBottom(CGRect rect) {
CGPoint CGRectGetBottom(CGRect rect) {

return CGPointMake( rect.origin.x + rect.size.width / 2, rect.origin.y + rect.size.height );
}

CGPoint CGPointFromCGRectLeft(CGRect rect) {
CGPoint CGRectGetLeft(CGRect rect) {

return CGPointMake( rect.origin.x, rect.origin.y + rect.size.height / 2 );
}

CGPoint CGPointFromCGRectTopLeft(CGRect rect) {
CGPoint CGRectGetTopLeft(CGRect rect) {

return CGPointMake( rect.origin.x, rect.origin.y );
}

CGPoint CGPointFromCGRectTopRight(CGRect rect) {
CGPoint CGRectGetTopRight(CGRect rect) {

return CGPointMake( rect.origin.x + rect.size.width, rect.origin.y );
}

CGPoint CGPointFromCGRectBottomRight(CGRect rect) {
CGPoint CGRectGetBottomRight(CGRect rect) {

return CGPointMake( rect.origin.x + rect.size.width, rect.origin.y + rect.size.height );
}

CGPoint CGPointFromCGRectBottomLeft(CGRect rect) {
CGPoint CGRectGetBottomLeft(CGRect rect) {

return CGPointMake( rect.origin.x, rect.origin.y + rect.size.height );
}

UIEdgeInsets UIEdgeInsetsUnionEdgeInsets(UIEdgeInsets a, UIEdgeInsets b) {

return UIEdgeInsetsMake( MAX(a.top, b.top), MAX(a.left, b.left), MAX(a.bottom, b.bottom), MAX(a.right, b.right) );
}

UIEdgeInsets UIEdgeInsetsForRectSubtractingRect(CGRect insetRect, CGRect subtractRect) {

CGPoint topLeftBounds = CGPointFromCGRectTopLeft( insetRect );
CGPoint bottomRightBounds = CGPointFromCGRectBottomRight( insetRect );
CGPoint topLeftFrom = CGPointFromCGRectTopLeft( subtractRect );
CGPoint bottomRightFrom = CGPointFromCGRectBottomRight( subtractRect );
CGPoint topLeftBounds = CGRectGetTopLeft( insetRect );
CGPoint bottomRightBounds = CGRectGetBottomRight( insetRect );
CGPoint topLeftFrom = CGRectGetTopLeft( subtractRect );
CGPoint bottomRightFrom = CGRectGetBottomRight( subtractRect );
CGPoint topLeftInset = CGPointMinusCGPoint( bottomRightFrom, topLeftBounds );
CGPoint bottomRightInset = CGPointMinusCGPoint( bottomRightBounds, topLeftFrom );

Expand Down
39 changes: 39 additions & 0 deletions Pearl-UIKit/UICollectionReusableView+PearlDequeue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Copyright Maarten Billemont (http://www.lhunath.com, lhunath@lyndir.com)
*
* See the enclosed file LICENSE for license information (LGPLv3). If you did
* not receive this file, see http://www.gnu.org/licenses/lgpl-3.0.txt
*
* @author Maarten Billemont <lhunath@lyndir.com>
* @license http://www.gnu.org/licenses/lgpl-3.0.txt
*/

//
// UICollectionReusableView(PearlDequeue)
//
// Created by Maarten Billemont on 2014-05-26.
// Copyright 2014 lhunath (Maarten Billemont). All rights reserved.
//

#import <UIKit/UIKit.h>


@interface UICollectionReusableView(PearlDequeue)

+ (instancetype)templateCellFromCollectionView:(UICollectionView *)collectionView;
+ (instancetype)templateSupplementaryFromCollectionView:(UICollectionView *)collectionView kind:(NSString *)kind;

+ (instancetype)dequeueCellFromCollectionView:(UICollectionView *)collectionView indexPath:(NSIndexPath *)indexPath;
+ (instancetype)dequeueSupplementaryFromCollectionView:(UICollectionView *)collectionView kind:(NSString *)kind
indexPath:(NSIndexPath *)indexPath;

+ (void)registerCellWithCollectionView:(UICollectionView *)collectionView;
+ (void)registerCellWithCollectionView:(UICollectionView *)collectionView usingNib:(UINib *)nib;

+ (void)registerSupplementaryWithCollectionView:(UICollectionView *)collectionView kind:(NSString *)kind;
+ (void)registerSupplementaryWithCollectionView:(UICollectionView *)collectionView kind:(NSString *)kind usingNib:(UINib *)nib;

+ (void)registerDecorationWithCollectionView:(UICollectionView *)collectionView kind:(NSString *)kind;
+ (void)registerDecorationWithCollectionView:(UICollectionView *)collectionView kind:(NSString *)kind usingNib:(UINib *)nib;

@end
131 changes: 131 additions & 0 deletions Pearl-UIKit/UICollectionReusableView+PearlDequeue.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/**
* Copyright Maarten Billemont (http://www.lhunath.com, lhunath@lyndir.com)
*
* See the enclosed file LICENSE for license information (LGPLv3). If you did
* not receive this file, see http://www.gnu.org/licenses/lgpl-3.0.txt
*
* @author Maarten Billemont <lhunath@lyndir.com>
* @license http://www.gnu.org/licenses/lgpl-3.0.txt
*/

//
// UICollectionReusableView(PearlDequeue)
//
// Created by Maarten Billemont on 2014-05-26.
// Copyright 2014 lhunath (Maarten Billemont). All rights reserved.
//

#import "UICollectionReusableView+PearlDequeue.h"

@interface PearlTemplateCollectionViewDataSource : NSObject <UICollectionViewDataSource>

@property(nonatomic) NSString *identifier;

+ (instancetype)templateSourceForIdentifier:(NSString *)identifier;

@end

@implementation UICollectionReusableView(PearlDequeue)

+ (instancetype)templateCellFromCollectionView:(UICollectionView *)collectionView {

id<UICollectionViewDelegate> originalDelegate = collectionView.delegate;
id<UICollectionViewDataSource> originalDataSource = collectionView.dataSource;
collectionView.delegate = nil;
collectionView.dataSource = [PearlTemplateCollectionViewDataSource templateSourceForIdentifier:NSStringFromClass( self )];
id template = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass( self )
forIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
collectionView.delegate = originalDelegate;
collectionView.dataSource = originalDataSource;
[collectionView reloadData];

return template;
}

+ (instancetype)templateSupplementaryFromCollectionView:(UICollectionView *)collectionView kind:(NSString *)kind {

id<UICollectionViewDataSource> originalDataSource = collectionView.dataSource;
collectionView.dataSource = [PearlTemplateCollectionViewDataSource templateSourceForIdentifier:NSStringFromClass( self )];
id template = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:NSStringFromClass( self )
forIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
collectionView.dataSource = originalDataSource;
[collectionView reloadData];

return template;
}

+ (instancetype)dequeueCellFromCollectionView:(UICollectionView *)collectionView indexPath:(NSIndexPath *)indexPath {

return [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass( self ) forIndexPath:indexPath];
}

+ (instancetype)dequeueSupplementaryFromCollectionView:(UICollectionView *)collectionView kind:(NSString *)kind
indexPath:(NSIndexPath *)indexPath {

return [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:NSStringFromClass( self ) forIndexPath:indexPath];
}

+ (void)registerCellWithCollectionView:(UICollectionView *)collectionView {

[collectionView registerClass:self forCellWithReuseIdentifier:NSStringFromClass( self )];
}

+ (void)registerCellWithCollectionView:(UICollectionView *)collectionView usingNib:(UINib *)nib {

[collectionView registerNib:nib forCellWithReuseIdentifier:NSStringFromClass( self )];
}

+ (void)registerSupplementaryWithCollectionView:(UICollectionView *)collectionView kind:(NSString *)kind {

[collectionView registerClass:self forSupplementaryViewOfKind:kind withReuseIdentifier:NSStringFromClass( self )];
}

+ (void)registerSupplementaryWithCollectionView:(UICollectionView *)collectionView kind:(NSString *)kind usingNib:(UINib *)nib {

[collectionView registerNib:nib forSupplementaryViewOfKind:kind withReuseIdentifier:NSStringFromClass( self )];
}

+ (void)registerDecorationWithCollectionView:(UICollectionView *)collectionView kind:(NSString *)kind {

[collectionView.collectionViewLayout registerClass:self forDecorationViewOfKind:kind];
}

+ (void)registerDecorationWithCollectionView:(UICollectionView *)collectionView kind:(NSString *)kind usingNib:(UINib *)nib {

[collectionView.collectionViewLayout registerNib:nib forDecorationViewOfKind:kind];
}

@end

@implementation PearlTemplateCollectionViewDataSource

+ (instancetype)templateSourceForIdentifier:(NSString *)identifier {

PearlTemplateCollectionViewDataSource *dataSource = [self new];
dataSource.identifier = identifier;

return dataSource;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

return [collectionView dequeueReusableCellWithReuseIdentifier:self.identifier forIndexPath:indexPath];
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind
atIndexPath:(NSIndexPath *)indexPath {

return [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:self.identifier forIndexPath:indexPath];
}

@end
27 changes: 27 additions & 0 deletions Pearl-UIKit/UITableView+PearlReloadFromArray.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Copyright Maarten Billemont (http://www.lhunath.com, lhunath@lyndir.com)
*
* See the enclosed file LICENSE for license information (LGPLv3). If you did
* not receive this file, see http://www.gnu.org/licenses/lgpl-3.0.txt
*
* @author Maarten Billemont <lhunath@lyndir.com>
* @license http://www.gnu.org/licenses/lgpl-3.0.txt
*/

//
// UITableView(PearlReloadFromArray)
//
// Created by Maarten Billemont on 2014-05-21.
// Copyright 2014 lhunath (Maarten Billemont). All rights reserved.
//

#import <Foundation/Foundation.h>


@interface UITableView(PearlReloadFromArray)

- (void)reloadRowsFromArray:(NSArray *)fromArray toArray:(NSArray *)toArray inSection:(NSInteger)section;
- (void)reloadRowsFromArray:(NSArray *)fromArray toArray:(NSArray *)toArray inSection:(NSInteger)section
withRowAnimation:(UITableViewRowAnimation)animation;

@end
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
/**
* Copyright Maarten Billemont (http://www.lhunath.com, lhunath@lyndir.com)
*
* See the enclosed file LICENSE for license information (LGPLv3). If you did
* not receive this file, see http://www.gnu.org/licenses/lgpl-3.0.txt
*
* @author Maarten Billemont <lhunath@lyndir.com>
* @license http://www.gnu.org/licenses/lgpl-3.0.txt
*/

//
// UITableView(PearlReloadFromArray)
//
// Created by Maarten Billemont on 2014-05-21.
// Copyright (c) 2014 Tristan Interactive. All rights reserved.
// Created by Maarten Billemont on 2014-05-21.
// Copyright 2014 lhunath (Maarten Billemont). All rights reserved.
//

#import "UITableView+ReloadFromArray.h"
#import "UITableView+PearlReloadFromArray.h"


@implementation UITableView(ReloadFromArray)
@implementation UITableView(PearlReloadFromArray)

- (void)reloadRowsFromArray:(NSArray *)fromArray toArray:(NSArray *)toArray inSection:(NSInteger)section {

Expand Down
Loading

0 comments on commit e50cad1

Please sign in to comment.