Skip to content

Commit

Permalink
Merge pull request #3 from grigaci/features/infiniteTableView
Browse files Browse the repository at this point in the history
Add infinite table view
  • Loading branch information
grigaci committed Jul 24, 2015
2 parents 94e1496 + c250fd1 commit b184f22
Show file tree
Hide file tree
Showing 181 changed files with 5,349 additions and 8,059 deletions.
5 changes: 2 additions & 3 deletions BIObjCHelpers.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "BIObjCHelpers"
s.version = "0.2.11"
s.version = "0.2.10"
s.summary = "My collection of Objective-C helpers"
s.description = <<-DESC
Simple collection of classes commonly used in Objective-C projects.
Expand All @@ -13,7 +13,6 @@ Pod::Spec.new do |s|

s.platform = :ios, '7.0'
s.requires_arc = true
s.dependency 'SVPullToRefresh'
s.source_files = 'BIObjCHelpers/**/*.{h,m}', 'BIObjCHelpers/**/**/*.{h,m}'

end
122 changes: 120 additions & 2 deletions BIObjCHelpers.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions BIObjCHelpers/Categories/UIView/UIView+BILoadXib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// UIView+BILoadXib.h
// Pods
//
// Created by Mihai Chifor on 7/16/15.
//
// Copyright © 2015 iGama Apps. All rights reserved.

#import <UIKit/UIKit.h>

@interface UIView (BILoadXib)

+ (nonnull instancetype)viewFromXib;

@end
22 changes: 22 additions & 0 deletions BIObjCHelpers/Categories/UIView/UIView+BILoadXib.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// UIView+BILoadXib.m
// Pods
//
// Created by Mihai Chifor on 7/16/15.
//
// Copyright © 2015 iGama Apps. All rights reserved.

#import "UIView+BILoadXib.h"

@implementation UIView (BILoadXib)

+ (nonnull instancetype)viewFromXib {
NSString *className = NSStringFromClass(self);
UIView *view = [[NSBundle mainBundle] loadNibNamed:className owner:nil options:nil][0];

NSAssert([view isKindOfClass:self], @"View class should match");

return view;
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@
//

#import "BIDatasourceTableView.h"
#import "BITableView.h"

@class BITableViewBatch;

@interface BIDatasourceFeedTableView : BIDatasourceTableView

@property (nonatomic, strong, nullable, readonly) BITableViewBatch *currentBatch;
@property (nonatomic, strong, readonly, nonnull) BITableView *tableView;

+ (nonnull instancetype)datasourceWithBITableView:(nonnull BITableView *)tableView;

- (nonnull BITableViewBatch *)createNextBatch;
- (void)fetchBatch:(nonnull BITableViewBatch *)batch;
Expand Down
22 changes: 18 additions & 4 deletions BIObjCHelpers/Datasource/TableView/BIDatasourceFeedTableView.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@

#import "BIDatasourceFeedTableView.h"
#import "BITableViewBatch.h"

#import <SVPullToRefresh/SVPullToRefresh.h>
#import "BITableViewCell.h"

@interface BIDatasourceFeedTableView ()

Expand All @@ -20,17 +19,32 @@ @interface BIDatasourceFeedTableView ()

@implementation BIDatasourceFeedTableView

@dynamic tableView;
@synthesize cellClass = _cellClass;

+ (nonnull instancetype)datasourceWithBITableView:(nonnull BITableView *)tableView {
return [self datasourceWithTableView:tableView];
}

#pragma mark - BIDatasourceBase methods

- (void)load {
[super load];
__weak typeof(self) weakself = self;
[self.tableView addInfiniteScrollingWithActionHandler:^{
[self.tableView setInfiniteScrollingCallback:^{
BITableViewBatch *batch = [weakself createNextBatch];
[weakself fetchBatch:batch];
}];
}

// Overriden getter
- (Class)cellClass {
if (!_cellClass) {
_cellClass = [BITableViewCell class];
}
return _cellClass;
}

#pragma mark - Public methods

- (nonnull BITableViewBatch *)createNextBatch {
Expand Down Expand Up @@ -73,7 +87,7 @@ - (void)fetchBatchCompletedWithSuccess:(nonnull NSArray *)newIndexPaths {

- (void)fetchBatchCompletedCommon {
self.currentBatch = nil;
[self.tableView.infiniteScrollingView stopAnimating];
self.tableView.infiniteScrollingState = BIInfiniteScrollingStateStopped;
}

@end
2 changes: 2 additions & 0 deletions BIObjCHelpers/Datasource/TableView/BIDatasourceTableView.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

#import "BIDatasourceBase.h"
#import "BITableView.h"

#import <UIKit/UIKit.h>

Expand All @@ -21,6 +22,7 @@ typedef void(^BIDatasourceTableViewConfigureCell)(id __nonnull cell, NSIndexPath
@property (nonatomic, copy, nullable) NSString *cellIdentifier;
@property (nonatomic, strong, nullable) Class cellClass;
@property (nonatomic, copy, nullable) BIDatasourceTableViewConfigureCell configureCellBlock;
@property (nonatomic, copy, nullable) NSInteger(^numberOfRowsInSectionCallback)(NSInteger section);

- (void)configureCell:(nonnull UITableViewCell *)cell atIndexPath:(nonnull NSIndexPath *)indexPath;

Expand Down
5 changes: 4 additions & 1 deletion BIObjCHelpers/Datasource/TableView/BIDatasourceTableView.m
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ - (Class)cellClass {
#pragma mark - UITableViewDataSource Methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (self.numberOfRowsInSectionCallback) {
return self.numberOfRowsInSectionCallback(section);
}

return 0;
}

Expand All @@ -72,7 +76,6 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
cell = [[self.cellClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:self.cellIdentifier];
}
[self configureCell: cell atIndexPath: indexPath];

return cell;
}

Expand Down
17 changes: 17 additions & 0 deletions BIObjCHelpers/Details/BIScrollDirection.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// BIBaseDefines.h
// BIObjCHelpers
//
// Created by Mihai Chifor on 7/16/15.
// Copyright © 2015 iGama Apps. All rights reserved.
//

#import <Foundation/Foundation.h>

typedef NS_OPTIONS(NSInteger, BIScrollDirection) {
BIScrollDirectionNone = 0,
BIScrollDirectionRight = 1 << 0,
BIScrollDirectionLeft = 1 << 1,
BIScrollDirectionUp = 1 << 2,
BIScrollDirectionDown = 1 << 3
};
1 change: 1 addition & 0 deletions BIObjCHelpers/Handlers/TableView/BIHandlerTableView.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ - (void)load {
self.tableView.delegate = self;
}


#pragma mark - UITableViewDelegate methods

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// BIActivityIndicatorContainerView.h
// BIObjCHelpers
//
// Created by Bogdan Iusco on 20/07/15.
// Copyright (c) 2015 iGama Apps. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface BIActivityIndicatorContainerView : UIView

- (nonnull instancetype)initWithFrame:(CGRect)frame NS_DESIGNATED_INITIALIZER;

@property (nonatomic, strong, nonnull, readonly) UIActivityIndicatorView *activityIndicatorView;

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//
// BIActivityIndicatorContainerView.m
// BIObjCHelpers
//
// Created by Bogdan Iusco on 20/07/15.
// Copyright (c) 2015 iGama Apps. All rights reserved.
//

#import "BIActivityIndicatorContainerView.h"

@interface BIActivityIndicatorContainerView ()

@property (nonatomic, strong, nonnull, readwrite) UIActivityIndicatorView *activityIndicatorView;

@end


@implementation BIActivityIndicatorContainerView

#pragma mark - Init methods

- (nonnull instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self addSubview:self.activityIndicatorView];
}
return self;
}

- (void)layoutSubviews {
[super layoutSubviews];
CGPoint center = CGPointMake(CGRectGetWidth(self.bounds) / 2.f, CGRectGetHeight(self.bounds) / 2.f);
self.activityIndicatorView.center = center;
}

#pragma mark - UIView methods

- (void)setHidden:(BOOL)hidden {
hidden? [self.activityIndicatorView stopAnimating] : [self.activityIndicatorView startAnimating];
[super setHidden:hidden];
}

#pragma mark - Property methods

- (UIActivityIndicatorView *)activityIndicatorView {
if (!_activityIndicatorView) {
_activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[_activityIndicatorView startAnimating];
}
return _activityIndicatorView;
}

@end
81 changes: 81 additions & 0 deletions BIObjCHelpers/Views/TableView/BITableView.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//
// BITableView.h
// BIObjCHelpers
//
// Created by Mihai Chifor on 7/16/15.
// Copyright © 2015 iGama Apps. All rights reserved.
//

#import "BIScrollDirection.h"
#import "BITableViewCell.h"
#import <UIKit/UIKit.h>

/*!
@typedef BIInfiniteScrollingState
@abstract Represents the state of the tableView infinite scroll
@field BIInfiniteScrollingStateStopped The tableView is currently not fetching any batches
@field BIInfiniteScrollingStateLoading The tableView is currently fetching batches
*/
typedef NS_OPTIONS(NSInteger, BIInfiniteScrollingState) {
BIInfiniteScrollingStateStopped = 0,
BIInfiniteScrollingStateLoading = 1
};

/*!
@abstract Determine if batch fetching should begin based on the state of the parameters.
@param scrollDirection The current scrolling direction of the scroll view.
@param bounds The bounds of the scrollview.
@param contentSize The content size of the scrollview.
@param targetOffset The offset that the scrollview will scroll to.
@param leadingScreens How many screens in the remaining distance will trigger batch fetching.
@return Whether or not the current state should proceed with batch fetching.
@discussion This method is broken into a category for unit testing purposes and should be used with the BITableView and
* BICollectionView batch fetching API.
*/
extern BOOL BIDisplayShouldFetchBatch(BIScrollDirection scrollDirection,
CGRect bounds,
CGSize contentSize,
CGPoint targetOffset,
CGFloat leadingScreens);

extern const CGFloat kBILeadingScreens;
extern const CGFloat kBITableFooterViewAnimationDuration;

@class BIActivityIndicatorContainerView;

/*!
@brief Table view with infinite scrolling support.
@note There is one limitation to this table view. Setting its delegate to nil causes the infinite scrolling
to not work.
*/
@interface BITableView : UITableView

/*!
@callback infiniteScrollingCallback Used to notify dataSource to fetch the next batch
*/
@property (nonatomic, copy, nullable) void (^infiniteScrollingCallback)();

/*!
@field enableInfiniteScrolling specifies whether the scrolling of the tableView is infinite or not
@discussion If it is set to NO, no other batches are fetched. Default is YES
*/

@property (nonatomic, assign) BOOL enableInfiniteScrolling;

/*!
@field leadingScreens Represents the number of screens left to scroll before triggering the fetch of the next batch
@discussion Default is 0.5f (half of screen)
*/
@property (nonatomic, assign) CGFloat leadingScreens;

/*!
@field activityIndicatorContainer Activity indicator that is displayed on the tableView footer while a new batch is fetched
@discussion Used as table footer view. Override it for further customization
*/
@property (nonatomic, strong, nonnull, readonly) BIActivityIndicatorContainerView *activityIndicatorContainer;

@property (nonatomic, assign) BIInfiniteScrollingState infiniteScrollingState;

- (void)triggerInfiniteScrolling;

@end
Loading

0 comments on commit b184f22

Please sign in to comment.