Permalink
Browse files

changes the way you load collectionview up. Adds an additional parame…

…ter to specify whether you want to empty the reusable queue and the visible viewcontrollers before reloading
  • Loading branch information...
1 parent 595c254 commit 7ec72e6d6584e9b370a73cc371ec7490156e8d6e @pieteromvlee pieteromvlee committed Dec 2, 2010
Showing with 63 additions and 27 deletions.
  1. +9 −7 BCCollectionView.h
  2. +48 −19 BCCollectionView.m
  3. +6 −1 BCCollectionViewDelegate.h
View
@@ -21,23 +21,23 @@
NSMutableIndexSet *selectionIndexes;
NSIndexSet *originalSelectionIndexes;
- NSPoint mouseDownLocation;
- NSPoint mouseDraggedLocation;
-
NSColor *backgroundColor;
NSUInteger lastSelectionIndex;
+
+@private
+ NSPoint mouseDownLocation;
+ NSPoint mouseDraggedLocation;
+ NSRect previousFrameBounds;
}
@property (nonatomic, assign) id<BCCollectionViewDelegate> delegate;
@property (nonatomic, retain) NSColor *backgroundColor;
-//by setting the contentArray, the view will load itself up.
-@property (nonatomic, copy) NSArray *contentArray;
-
//private
@property (nonatomic, copy) NSIndexSet *originalSelectionIndexes;
+@property (nonatomic, copy) NSArray *contentArray;
-- (void)reloadData;
+- (void)reloadDataWithItems:(NSArray *)newContent emptyCaches:(BOOL)shouldEmptyCaches;
//Managing Selections
- (void)selectItemAtIndex:(NSUInteger)index;
@@ -57,7 +57,9 @@
- (NSUInteger)indexOfItemAtPoint:(NSPoint)p;
- (NSUInteger)indexOfItemAtPointOrClosestGuess:(NSPoint)p;
- (NSViewController *)viewControllerForItemAtIndex:(NSUInteger)index;
+
- (NSIndexSet *)indexesOfInvisibleViewControllers;
+- (NSRange)rangeOfVisibleItems;
//Querying ViewControllers
- (NSIndexSet *)indexesOfViewControllers;
View
@@ -23,7 +23,6 @@ - (id)initWithCoder:(NSCoder *)aDecoder
contentArray = [[NSArray alloc] init];
selectionIndexes = [[NSMutableIndexSet alloc] init];
- [self addObserver:self forKeyPath:@"contentArray" options:0 context:NULL];
[self addObserver:self forKeyPath:@"backgroundColor" options:0 context:NULL];
NSClipView *enclosingClipView = [[self enclosingScrollView] contentView];
@@ -40,17 +39,14 @@ - (id)initWithCoder:(NSCoder *)aDecoder
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change
context:(void *)context
{
- if ([keyPath isEqualToString:@"contentArray"])
- [self reloadData];
- else if ([keyPath isEqualToString:@"backgroundColor"])
+ if ([keyPath isEqualToString:@"backgroundColor"])
[self setNeedsDisplay:YES];
else
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
- (void)dealloc
{
- [self removeObserver:self forKeyPath:@"contentArray"];
[self removeObserver:self forKeyPath:@"backgroundColor"];
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
@@ -291,15 +287,6 @@ - (void)addMissingViewControllers
}];
}
-- (void)removeAllViewControllers
-{
- for (NSViewController *viewController in [visibleViewControllers allValues])
- [[viewController view] removeFromSuperview];
-
- [reusableViewControllers removeAllObjects];
- [visibleViewControllers removeAllObjects];
-}
-
- (void)moveViewControllersToProperPosition
{
for (NSNumber *number in visibleViewControllers)
@@ -388,31 +375,73 @@ - (BOOL)canBecomeKeyView
#pragma mark Reloading and Updating the Icon View
-- (void)reloadData
+- (void)softReloadVisibleViewControllers
+{
+ NSMutableArray *removeKeys = [NSMutableArray array];
+
+ for (NSString *number in visibleViewControllers) {
+ NSUInteger index = [number integerValue];
+ NSViewController *controller = [visibleViewControllers objectForKey:number];
+
+ if (index < [contentArray count]) {
+ if ([selectionIndexes containsIndex:index])
+ [self delegateUpdateDeselectionForItemAtIndex:index];
+ [delegate collectionView:self willShowViewController:controller forItem:[contentArray objectAtIndex:index]];
+ } else {
+ if ([selectionIndexes containsIndex:index])
+ [self delegateUpdateDeselectionForItemAtIndex:index];
+
+ [self delegateViewControllerBecameInvisibleAtIndex:index];
+ [[controller view] removeFromSuperview];
+ [reusableViewControllers addObject:controller];
+ [removeKeys addObject:number];
+ }
+ }
+ [visibleViewControllers removeObjectsForKeys:removeKeys];
+}
+
+- (void)reloadDataWithItems:(NSArray *)newContent emptyCaches:(BOOL)shouldEmptyCaches
{
if (!delegate)
return;
+ self.contentArray = newContent;
+
NSRect frame = [self frame];
frame.size.height = [self visibleRect].size.height;
frame.size.height = MAX(frame.size.height, [self numberOfRows] * [self cellSize].height);
[self setFrame:frame];
- [self removeAllViewControllers];
+ if (shouldEmptyCaches) {
+ for (NSViewController *viewController in [visibleViewControllers allValues])
+ [[viewController view] removeFromSuperview];
+
+ [reusableViewControllers removeAllObjects];
+ [visibleViewControllers removeAllObjects];
+ } else
+ [self softReloadVisibleViewControllers];
+
+ [selectionIndexes removeAllIndexes];
+
dispatch_async(dispatch_get_main_queue(), ^{
[self addMissingViewControllers];
});
}
-- (void)scrollViewDidScroll:(NSScrollView *)scrollView
+- (void)scrollViewDidScroll:(NSNotification *)note
{
dispatch_async(dispatch_get_main_queue(), ^{
[self removeInvisibleViewControllers];
[self addMissingViewControllers];
});
- if ([delegate respondsToSelector:@selector(collectionViewDidScroll:)])
- [delegate collectionViewDidScroll:self];
+ if ([delegate respondsToSelector:@selector(collectionViewDidScroll:inDirection:)]) {
+ if ([self visibleRect].origin.y > previousFrameBounds.origin.y)
+ [delegate collectionViewDidScroll:self inDirection:BCCollectionViewScrollDirectionDown];
+ else
+ [delegate collectionViewDidScroll:self inDirection:BCCollectionViewScrollDirectionUp];
+ previousFrameBounds = [self visibleRect];
+ }
}
- (void)viewDidResize
@@ -10,6 +10,11 @@
@class BCCollectionView;
+enum {
+ BCCollectionViewScrollDirectionUp = 0,
+ BCCollectionViewScrollDirectionDown = 1
+};
+
@protocol BCCollectionViewDelegate <NSObject>
@required
//CollectionView assumes all cells aer the same size and will resize its subviews to this size.
@@ -33,7 +38,7 @@
- (void)collectionView:(BCCollectionView *)collectionView didSelectItem:(id)anItem withViewController:(NSViewController *)viewController;
- (void)collectionView:(BCCollectionView *)collectionView didDeselectItem:(id)anItem withViewController:(NSViewController *)viewController;
-- (void)collectionViewDidScroll:(BCCollectionView *)collectionView;
+- (void)collectionViewDidScroll:(BCCollectionView *)collectionView inDirection:(NSUInteger)scrollDirection;
- (void)collectionView:(BCCollectionView *)collectionView didDoubleClickViewControllerAtIndex:(NSViewController *)viewController;
//defaults to YES

0 comments on commit 7ec72e6

Please sign in to comment.