Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sypher/cocoapods update #8

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ATV.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Pod::Spec.new do |s|
s.summary = "A pluggable table view with a different data source for each section."
s.homepage = "https://github.com/pnc/ATV"

s.license = 'MIT (example)'
s.license = 'MIT'
s.author = { "Phil Calvin" => "[email protected]" }
s.source = { :git => "https://github.com/pnc/ATV.git", :branch => "master" }

Expand Down
6 changes: 6 additions & 0 deletions ATV/ATVArrayTableSection.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,10 @@
- (void)setObjects:(NSArray*)objects;
- (void)setObjects:(NSArray*)objects animated:(BOOL)animated;

#pragma mark - Overrides

- (UITableViewRowAnimation)animationForInsertingObject:(id)object atIndex:(NSUInteger)index;
- (UITableViewRowAnimation)animationForDeletingObject:(id)object atIndex:(NSUInteger)index;
- (id)uniqueIdentifierForObject:(id)object;

@end
44 changes: 32 additions & 12 deletions ATV/ATVArrayTableSection.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ - (void) setObjects:(NSArray*)objects {
}

- (void) setObjects:(NSArray*)objects animated:(BOOL)animated {
UITableViewRowAnimation insertAnimation = animated ? UITableViewRowAnimationTop : UITableViewRowAnimationNone;
UITableViewRowAnimation deleteAnimation = animated ? UITableViewRowAnimationBottom : UITableViewRowAnimationNone;

if (!_objects) {
_objects = objects;
[self reloadSectionWithRowAnimation:UITableViewRowAnimationNone];
Expand Down Expand Up @@ -79,25 +76,27 @@ - (void) setObjects:(NSArray*)objects animated:(BOOL)animated {
// if it's not a new entry.
for (int i = 0; i < oldObjects.count; i++) {
id item = [oldObjects objectAtIndex:i];
NSMutableArray *positions = [oldIndexes objectForKey:item];
id identifier = [self uniqueIdentifierForObject:item];
NSMutableArray *positions = [oldIndexes objectForKey:identifier];
if (!positions) {
positions = [NSMutableArray array];
}
[positions addObject:@(i)];
[oldIndexes setObject:positions forKey:item];
[oldIndexes setObject:positions forKey:identifier];
}

for (int i = 0; i < objects.count; i++) {
id item = [objects objectAtIndex:i];
NSMutableArray *positions = [oldIndexes objectForKey:item];
id identifier = [self uniqueIdentifierForObject:item];
NSMutableArray *positions = [oldIndexes objectForKey:identifier];
NSNumber *oldIndex = nil;
if (positions.count > 0) {
// Without loss of generality, assume this duplicate was the first. This
// keeps duplicates from swapping places needlessly.
oldIndex = [positions objectAtIndex:0];
[positions removeObjectAtIndex:0];
} else {
[oldIndexes removeObjectForKey:item];
[oldIndexes removeObjectForKey:identifier];
}

NSNumber *newIndex = @(i);
Expand Down Expand Up @@ -127,23 +126,44 @@ - (void) setObjects:(NSArray*)objects animated:(BOOL)animated {
}
}
} else {
UITableViewCell *cell = [self cellAtIndex:[oldIndex unsignedIntegerValue]];
if (cell) {
// Cell is visible, repaint it
[self configureCell:cell atIndex:[newIndex unsignedIntegerValue]];
}
[self moveRowAtIndex:[oldIndex unsignedIntegerValue] toIndex:[newIndex unsignedIntegerValue]];
}
} else {
UITableViewRowAnimation insertAnimation = animated ?
[self animationForInsertingObject:item atIndex:[newIndex unsignedIntegerValue]] : UITableViewRowAnimationNone;
[self insertRowsAtIndices:[NSIndexSet indexSetWithIndex:[newIndex unsignedIntegerValue]] withRowAnimation:insertAnimation];
}
}
NSMutableIndexSet *deleted = [NSMutableIndexSet new];
for (id item in oldIndexes) {
NSArray <NSNumber *> *indexes = [oldIndexes objectForKey:item];
for (id identifier in oldIndexes) {
NSArray <NSNumber *> *indexes = [oldIndexes objectForKey:identifier];
for (NSNumber *index in indexes) {
[deleted addIndex:[index unsignedIntegerValue]];
id item = [oldObjects objectAtIndex:[index unsignedIntegerValue]];
UITableViewRowAnimation deleteAnimation = animated ?
[self animationForDeletingObject:item atIndex:[index unsignedIntegerValue]] : UITableViewRowAnimationNone;
[self deleteRowsAtIndices:[NSIndexSet indexSetWithIndex:[index unsignedIntegerValue]]
withRowAnimation:deleteAnimation];
}
}
[self deleteRowsAtIndices:deleted withRowAnimation:deleteAnimation];
[self endUpdates];
}

- (UITableViewRowAnimation)animationForInsertingObject:(id)object atIndex:(NSUInteger)index {
return UITableViewRowAnimationTop;
}

- (UITableViewRowAnimation)animationForDeletingObject:(id)object atIndex:(NSUInteger)index {
return UITableViewRowAnimationBottom;
}

- (id)uniqueIdentifierForObject:(id)object {
return object;
}

#pragma mark - Cell source

- (UITableViewCell*) cellForRowAtIndex:(NSUInteger)index {
Expand Down
3 changes: 3 additions & 0 deletions ATV/ATVTableSection.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,7 @@
toIndex:(NSUInteger)newIndex;
- (void) reloadSectionWithRowAnimation:(UITableViewRowAnimation)animation;

- (void)setNeedsToPerformUpdates;
- (void)performUpdatesAnimated:(BOOL)animated;

@end
12 changes: 12 additions & 0 deletions ATV/ATVTableSection.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#import "ATVTableSection.h"
#import "ATVTableSection_Private.h"
#import "ATVTableView.h"
#import "ATVTableView_Private.h"

@implementation ATVTableSection

Expand Down Expand Up @@ -202,4 +203,15 @@ - (void) deselectRowAtIndex:(NSUInteger)index animated:(BOOL)animated {
[self._tableView deselectRowAtIndex:index inSection:self animated:animated];
}

#pragma mark - Deferred updates

- (void)setNeedsToPerformUpdates {
self.needsToPerformUpdates = YES;
[self._tableView setWillPerformUpdates];
}

- (void)performUpdatesAnimated:(BOOL)animated {
self.needsToPerformUpdates = NO;
}

@end
1 change: 1 addition & 0 deletions ATV/ATVTableSection_Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
@interface ATVTableSection ()
@property (ATV_WEAK) ATVTableView* _tableView;
@property (strong) NSMutableDictionary* registeredNibs;
@property BOOL needsToPerformUpdates;
@end
4 changes: 4 additions & 0 deletions ATV/ATVTableView.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
@property (nonatomic, strong) UIView* emptyView;
@property (nonatomic) BOOL emptyViewHonorsContentInset;

#pragma mark - Deferred updates

- (void)performUpdatesAnimated:(BOOL)animated;

#pragma mark - Private

- (UITableViewCell*) cellForRowAtIndex:(NSUInteger)index inSection:(ATVTableSection*)section;
Expand Down
37 changes: 37 additions & 0 deletions ATV/ATVTableView.m
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,43 @@ - (void) removeAllSectionsWithRowAnimation:(UITableViewRowAnimation)animation {
[self updateEmptyView];
}

#pragma mark - Deferred updates

- (void)setWillPerformUpdates {
if (self.window) {
if (!self.willPerformUpdates) {
self.willPerformUpdates = YES;
[self performSelector:@selector(performUpdates) withObject:nil afterDelay:0.0];
}
}
}

- (void)didMoveToWindow {
[super didMoveToWindow];
[self performUpdatesAnimated:NO];
}

- (void)performUpdates {
[self performUpdatesAnimated:YES];
}

- (void)performUpdatesAnimated:(BOOL)animated {
NSMutableArray *toUpdate = [NSMutableArray array];
for (ATVTableSection *section in self.sections) {
if ([section needsToPerformUpdates]) {
[toUpdate addObject:section];
}
}
if (toUpdate.count > 0) {
[self beginUpdates];
for (ATVTableSection *section in toUpdate) {
[section performUpdatesAnimated:animated];
NSAssert(![section needsToPerformUpdates], @"Section %@ still needs to perform updates after updating, did you forget to call [super performUpdates]?", self);
}
[self endUpdates];
}
self.willPerformUpdates = NO;
}

#pragma mark - Table view data source

Expand Down
3 changes: 3 additions & 0 deletions ATV/ATVTableView_Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@
@property (strong) NSMutableArray* sections;
@property UITableViewCellSeparatorStyle desiredSeparatorStyle;

@property BOOL willPerformUpdates;
- (void)setWillPerformUpdates;

@end