Skip to content

Commit

Permalink
Fix whitespace, expand tabs
Browse files Browse the repository at this point in the history
  • Loading branch information
andreyvit committed Apr 8, 2011
1 parent d13666f commit 04c7273
Show file tree
Hide file tree
Showing 13 changed files with 453 additions and 453 deletions.
36 changes: 18 additions & 18 deletions ATArrayView/ATArrayView.h
Expand Up @@ -9,27 +9,27 @@
// A container that arranges its items in rows and columns similar to the
// thumbnails screen in Photos.app, the API is modeled after UITableView.
@interface ATArrayView : UIView {
// subviews
UIScrollView *_scrollView;
// subviews
UIScrollView *_scrollView;

// properties
id<ATArrayViewDelegate> _delegate;
// properties
id<ATArrayViewDelegate> _delegate;

UIEdgeInsets _contentInsets;
CGSize _itemSize;
CGFloat _minimumColumnGap;
UIEdgeInsets _contentInsets;
CGSize _itemSize;
CGFloat _minimumColumnGap;
int _preloadBuffer;
// state
NSInteger _itemCount;
NSMutableSet *_recycledItems;
NSMutableSet *_visibleItems;

// geometry
NSInteger _colCount;
NSInteger _rowCount;
CGFloat _rowGap;
CGFloat _colGap;
UIEdgeInsets _effectiveInsets;
// state
NSInteger _itemCount;
NSMutableSet *_recycledItems;
NSMutableSet *_visibleItems;

// geometry
NSInteger _colCount;
NSInteger _rowCount;
CGFloat _rowGap;
CGFloat _colGap;
UIEdgeInsets _effectiveInsets;
}

/* Depending on memory, I you can use the preload buffer to buffer additional rows that
Expand Down
162 changes: 81 additions & 81 deletions ATArrayView/ATArrayView.m
Expand Up @@ -29,10 +29,10 @@ @implementation ATArrayView
#pragma mark init/dealloc

- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
if ((self = [super initWithFrame:frame])) {
[self setup];
}
return self;
}
return self;
}

- (void) awakeFromNib {
Expand All @@ -44,7 +44,7 @@ - (void) awakeFromNib {
-(void) setup {
_visibleItems = [[NSMutableSet alloc] init];
_recycledItems = [[NSMutableSet alloc] init];

_itemSize = CGSizeMake(70, 70);
_minimumColumnGap = 5;
_preloadBuffer = 0;
Expand All @@ -57,24 +57,24 @@ -(void) setup {
}

- (void)dealloc {
[_scrollView release], _scrollView = nil;
[super dealloc];
[_scrollView release], _scrollView = nil;
[super dealloc];
}


#pragma mark -
#pragma mark Data

- (void)reloadData {
_itemCount = [_delegate numberOfItemsInArrayView:self];
_itemCount = [_delegate numberOfItemsInArrayView:self];

// recycle all items
for (UIView *view in _visibleItems) {
[self recycleItem:view];
}
[_visibleItems removeAllObjects];
// recycle all items
for (UIView *view in _visibleItems) {
[self recycleItem:view];
}
[_visibleItems removeAllObjects];

[self configureItems:NO];
[self configureItems:NO];
}


Expand All @@ -89,12 +89,12 @@ - (UIView *)viewForItemAtIndex:(NSUInteger)index {
}

- (void)configureItems:(BOOL)reconfigure {
// update content size if needed
CGSize contentSize = CGSizeMake(self.bounds.size.width,
_itemSize.height * _rowCount + _rowGap * (_rowCount - 1) + _effectiveInsets.top + _effectiveInsets.bottom);
if (_scrollView.contentSize.width != contentSize.width || _scrollView.contentSize.height != contentSize.height) {
_scrollView.contentSize = contentSize;
}
// update content size if needed
CGSize contentSize = CGSizeMake(self.bounds.size.width,
_itemSize.height * _rowCount + _rowGap * (_rowCount - 1) + _effectiveInsets.top + _effectiveInsets.bottom);
if (_scrollView.contentSize.width != contentSize.width || _scrollView.contentSize.height != contentSize.height) {
_scrollView.contentSize = contentSize;
}

// calculate which items are visible
int firstItem = self.firstVisibleItemIndex;
Expand All @@ -103,86 +103,86 @@ - (void)configureItems:(BOOL)reconfigure {
// recycle items that are no longer visible
for (UIView *item in _visibleItems) {
if (item.tag < firstItem || item.tag > lastItem) {
[self recycleItem:item];
[self recycleItem:item];
}
}
[_visibleItems minusSet:_recycledItems];

if (lastItem < 0)
return;
if (lastItem < 0)
return;

// add missing items
for (int index = firstItem; index <= lastItem; index++) {
UIView *item = [self viewForItemAtIndex:index];
if (item == nil) {
item = [_delegate viewForItemInArrayView:self atIndex:index];
UIView *item = [self viewForItemAtIndex:index];
if (item == nil) {
item = [_delegate viewForItemInArrayView:self atIndex:index];
[_scrollView addSubview:item];
[_visibleItems addObject:item];
} else if (!reconfigure) {
continue;
}
[self configureItem:item forIndex:index];
} else if (!reconfigure) {
continue;
}
[self configureItem:item forIndex:index];
}
}

- (void)configureItem:(UIView *)item forIndex:(NSInteger)index {
item.tag = index;
item.frame = [self rectForItemAtIndex:index];
[item setNeedsDisplay]; // just in case
[item setNeedsDisplay]; // just in case
}


#pragma mark -
#pragma mark Layouting

- (void)layoutSubviews {
BOOL boundsChanged = !CGRectEqualToRect(_scrollView.frame, self.bounds);
if (boundsChanged) {
// Strangely enough, if we do this assignment every time without the above
// check, bouncing will behave incorrectly.
_scrollView.frame = self.bounds;
}

_colCount = floorf((self.bounds.size.width - _contentInsets.left - _contentInsets.right) / _itemSize.width);

while (1) {
_colGap = (self.bounds.size.width - _contentInsets.left - _contentInsets.right - _itemSize.width * _colCount) / (_colCount + 1);
if (_colGap >= _minimumColumnGap)
break;
--_colCount;
};

_rowCount = (_itemCount + _colCount - 1) / _colCount;
_rowGap = _colGap;

_effectiveInsets = UIEdgeInsetsMake(_contentInsets.top + _rowGap,
_contentInsets.left + _colGap,
_contentInsets.bottom + _rowGap,
_contentInsets.right + _colGap);

[self configureItems:boundsChanged];
BOOL boundsChanged = !CGRectEqualToRect(_scrollView.frame, self.bounds);
if (boundsChanged) {
// Strangely enough, if we do this assignment every time without the above
// check, bouncing will behave incorrectly.
_scrollView.frame = self.bounds;
}

_colCount = floorf((self.bounds.size.width - _contentInsets.left - _contentInsets.right) / _itemSize.width);

while (1) {
_colGap = (self.bounds.size.width - _contentInsets.left - _contentInsets.right - _itemSize.width * _colCount) / (_colCount + 1);
if (_colGap >= _minimumColumnGap)
break;
--_colCount;
};

_rowCount = (_itemCount + _colCount - 1) / _colCount;
_rowGap = _colGap;

_effectiveInsets = UIEdgeInsetsMake(_contentInsets.top + _rowGap,
_contentInsets.left + _colGap,
_contentInsets.bottom + _rowGap,
_contentInsets.right + _colGap);

[self configureItems:boundsChanged];
}

- (NSInteger)firstVisibleItemIndex {
int firstRow = MAX(floorf((CGRectGetMinY(_scrollView.bounds) - _effectiveInsets.top) / (_itemSize.height + _rowGap)), 0);
//return MIN( firstRow * _colCount, _itemCount - 1);
//Formula changed to incorporate the 'preload' buffer functionality
return MIN( MAX(0,firstRow - (_preloadBuffer)) * _colCount, _itemCount - 1);
//return MIN( firstRow * _colCount, _itemCount - 1);
//Formula changed to incorporate the 'preload' buffer functionality
return MIN( MAX(0,firstRow - (_preloadBuffer)) * _colCount, _itemCount - 1);
}

- (NSInteger)lastVisibleItemIndex {
int lastRow = MIN( ceilf((CGRectGetMaxY(_scrollView.bounds) - _effectiveInsets.top) / (_itemSize.height + _rowGap)), _rowCount - 1);
//return MIN((lastRow + 1) * _colCount - 1, _itemCount - 1);
//return MIN((lastRow + 1) * _colCount - 1, _itemCount - 1);
return MIN((lastRow + (_preloadBuffer + 1)) * _colCount - 1, _itemCount - 1);
}

- (CGRect)rectForItemAtIndex:(NSUInteger)index {
NSInteger row = index / _colCount;
NSInteger col = index % _colCount;
NSInteger row = index / _colCount;
NSInteger col = index % _colCount;

return CGRectMake(_effectiveInsets.left + (_itemSize.width + _colGap) * col,
_effectiveInsets.top + (_itemSize.height + _rowGap) * row,
_itemSize.width, _itemSize.height);
_effectiveInsets.top + (_itemSize.height + _rowGap) * row,
_itemSize.width, _itemSize.height);
}


Expand All @@ -192,24 +192,24 @@ - (CGRect)rectForItemAtIndex:(NSUInteger)index {
// It's the caller's responsibility to remove this item from _visibleItems,
// since this method is often called while traversing _visibleItems array.
- (void)recycleItem:(UIView *)item {
[_recycledItems addObject:item];
[item removeFromSuperview];
[_recycledItems addObject:item];
[item removeFromSuperview];
}

- (UIView *)dequeueReusableItem {
UIView *result = [_recycledItems anyObject];
if (result) {
[_recycledItems removeObject:[[result retain] autorelease]];
}
return result;
UIView *result = [_recycledItems anyObject];
if (result) {
[_recycledItems removeObject:[[result retain] autorelease]];
}
return result;
}


#pragma mark -
#pragma mark UIScrollViewDelegate methods

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
[self configureItems:NO];
[self configureItems:NO];
}

@end
Expand All @@ -225,49 +225,49 @@ @implementation ATArrayViewController
#pragma mark init/dealloc

- (void)dealloc {
[super dealloc];
[super dealloc];
}


#pragma mark -
#pragma mark View Loading

- (void)loadView {
self.view = [[[ATArrayView alloc] init] autorelease];
self.view = [[[ATArrayView alloc] init] autorelease];
}

- (void)viewDidLoad {
[super viewDidLoad];
if (self.arrayView.delegate == nil)
self.arrayView.delegate = self;
[super viewDidLoad];
if (self.arrayView.delegate == nil)
self.arrayView.delegate = self;
}


#pragma mark Lifecycle

- (void)viewWillAppear:(BOOL)animated {
if (self.arrayView.itemCount == 0)
[self.arrayView reloadData];
if (self.arrayView.itemCount == 0)
[self.arrayView reloadData];
}


#pragma mark -
#pragma mark View Access

- (ATArrayView *)arrayView {
return (ATArrayView *)self.view;
return (ATArrayView *)self.view;
}


#pragma mark -
#pragma mark ATArrayViewDelegate methods

- (NSInteger)numberOfItemsInArrayView:(ATArrayView *)arrayView {
return 0;
return 0;
}

- (UIView *)viewForItemInArrayView:(ATArrayView *)arrayView atIndex:(NSInteger)index {
return nil;
return nil;
}

@end
24 changes: 12 additions & 12 deletions ATArrayView/ATArrayViewDemo/Classes/DemoItemView.m
Expand Up @@ -12,26 +12,26 @@ @implementation DemoItemView

- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
self.layer.cornerRadius = 8;
self.layer.borderColor = [[UIColor redColor] CGColor];
self.layer.borderWidth = 1;
self.layer.backgroundColor = [[UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1.0] CGColor];
self.opaque = NO;
self.layer.cornerRadius = 8;
self.layer.borderColor = [[UIColor redColor] CGColor];
self.layer.borderWidth = 1;
self.layer.backgroundColor = [[UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1.0] CGColor];
self.opaque = NO;
}
return self;
}

- (void)drawRect:(CGRect)rect {
CGSize size = self.bounds.size;
CGSize size = self.bounds.size;

NSString *label = [NSString stringWithFormat:@"%02d", self.tag];
UIFont *font = [UIFont boldSystemFontOfSize:17];
NSString *label = [NSString stringWithFormat:@"%02d", self.tag];
UIFont *font = [UIFont boldSystemFontOfSize:17];

CGSize textSize = [label sizeWithFont:font];
CGSize textSize = [label sizeWithFont:font];

[[UIColor redColor] set];
[label drawAtPoint:CGPointMake((size.width - textSize.width) / 2,
(size.height - textSize.height) / 2) withFont:font];
[[UIColor redColor] set];
[label drawAtPoint:CGPointMake((size.width - textSize.width) / 2,
(size.height - textSize.height) / 2) withFont:font];
}


Expand Down

0 comments on commit 04c7273

Please sign in to comment.