Skip to content
This repository was archived by the owner on Feb 16, 2019. It is now read-only.

Commit b329156

Browse files
committed
Initial version of begin/updates working
1 parent b04e685 commit b329156

File tree

1 file changed

+75
-79
lines changed

1 file changed

+75
-79
lines changed

SSToolkit/SSCollectionView.m

Lines changed: 75 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -323,85 +323,81 @@ - (void)beginUpdates {
323323

324324

325325
- (void)endUpdates {
326-
// NSLog(@"updates: %@", [_updates objectAtIndex:_updatesDepth]);
327-
//
328-
// // Aggregate all item internal updates
329-
// NSMutableDictionary *sections = [[NSMutableDictionary alloc] init];
330-
// for (NSDictionary *update in [_updates objectAtIndex:_updatesDepth]) {
331-
// NSIndexPath *indexPath = [update objectForKey:@"indexPath"];
332-
// NSNumber *key = [NSNumber numberWithInteger:indexPath.section];
333-
//
334-
// NSMutableDictionary *section = [sections objectForKey:key];
335-
// if (!section) {
336-
// section = [NSMutableDictionary dictionary];
337-
// [sections setObject:section forKey:key];
338-
// }
339-
//
340-
// // Update row closest to the top
341-
// NSNumber *row = [section objectForKey:@"row"];
342-
// if (!row || indexPath.row < [row integerValue]) {
343-
// [section setObject:[NSNumber numberWithInteger:indexPath.row] forKey:@"row"];
344-
// [section setObject:[update objectForKey:@"animation"] forKey:@"animation"];
345-
// }
346-
//
347-
// // Update delta
348-
// NSInteger delta = [[section objectForKey:@"delta"] integerValue];
349-
// if ([[update objectForKey:@"type"] isEqualToString:@"insert"]) {
350-
// delta++;
351-
// } else if ([[update objectForKey:@"type"] isEqualToString:@"delete"]) {
352-
// delta--;
353-
// }
354-
//
355-
// [section setObject:[NSNumber numberWithInteger:delta] forKey:@"delta"];
356-
// }
357-
//
358-
// NSLog(@"sections: %@", sections);
359-
//
360-
// // Process each section and apply table view updates
361-
// for (NSNumber *key in sections) {
362-
// NSDictionary *section = [sections objectForKey:key];
363-
// NSInteger sectionIndex = [key integerValue];
364-
//
365-
// // Add or delete cells
366-
// NSInteger delta = [[section objectForKey:@"delta"] integerValue];
367-
// NSInteger rows = [_tableView numberOfRowsInSection:sectionIndex];
368-
// UITableViewRowAnimation animation = (UITableViewRowAnimation)[[section objectForKey:@"animation"] integerValue];
369-
// if (delta != 0) {
370-
// // Add rows
371-
// if (delta > 0) {
372-
// for (NSInteger i = 1; i <= delta; i++) {
373-
// NSLog(@"Add row: %@", [NSIndexPath indexPathForRow:rows + i inSection:sectionIndex]);
374-
// NSArray *indexPaths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:rows + i inSection:sectionIndex]];
375-
// [_tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:animation];
376-
// }
377-
// }
378-
//
379-
// // Delete rows
380-
// else {
381-
// for (NSInteger i = delta; i > 0; i--) {
382-
// NSLog(@"Delete row: %@", [NSIndexPath indexPathForRow:rows - i inSection:sectionIndex]);
383-
// NSArray *indexPaths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:rows - i inSection:sectionIndex]];
384-
// [_tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:animation];
385-
// }
386-
// }
387-
// }
388-
//
389-
// // Reload changed cells
390-
// NSNumber *top = [section objectForKey:@"row"];
391-
// if (top) {
392-
// NSInteger topIndex = [top integerValue];
393-
// NSInteger topRow = [self _cellIndexPathFromItemIndexPath:[NSIndexPath indexPathForRow:topIndex inSection:sectionIndex]].row;
394-
// for (NSInteger i = topRow; i < rows + delta; i++) {
395-
// NSLog(@"Reload row: %@", [NSIndexPath indexPathForRow:i inSection:sectionIndex]);
396-
// NSArray *indexPaths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:i inSection:sectionIndex]];
397-
// [_tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:animation];
398-
// }
399-
// }
400-
// }
401-
402-
// TODO:
403-
// * Remove extremities from row calculations
404-
// * Calculate row delta using number of items per row
326+
// Aggregate all item internal updates
327+
NSMutableDictionary *sections = [[NSMutableDictionary alloc] init];
328+
for (NSDictionary *update in [_updates objectAtIndex:_updatesDepth]) {
329+
NSIndexPath *indexPath = [update objectForKey:@"indexPath"];
330+
NSNumber *key = [NSNumber numberWithInteger:indexPath.section];
331+
332+
NSMutableDictionary *section = [sections objectForKey:key];
333+
if (!section) {
334+
section = [NSMutableDictionary dictionary];
335+
[sections setObject:section forKey:key];
336+
}
337+
338+
// Update item closest to the top
339+
NSNumber *item = [section objectForKey:@"item"];
340+
if (!item || indexPath.row < [item integerValue]) {
341+
[section setObject:[NSNumber numberWithInteger:indexPath.row] forKey:@"item"];
342+
[section setObject:[update objectForKey:@"animation"] forKey:@"animation"];
343+
}
344+
345+
// Update delta
346+
NSInteger delta = [[section objectForKey:@"delta"] integerValue];
347+
if ([[update objectForKey:@"type"] isEqualToString:@"insert"]) {
348+
delta++;
349+
} else if ([[update objectForKey:@"type"] isEqualToString:@"delete"]) {
350+
delta--;
351+
}
352+
353+
[section setObject:[NSNumber numberWithInteger:delta] forKey:@"delta"];
354+
}
355+
356+
// Process each section that has changes and apply table view updates
357+
for (NSNumber *key in sections) {
358+
NSDictionary *section = [sections objectForKey:key];
359+
NSInteger sectionIndex = [key integerValue];
360+
361+
// Add or delete cells
362+
NSInteger itemsDelta = [[section objectForKey:@"delta"] integerValue];
363+
NSInteger totalItems = [self numberOfItemsInSection:sectionIndex];
364+
NSInteger itemsPerRow = [self _numberOfItemsPerRowForSection:sectionIndex];
365+
366+
NSInteger rows = itemsPerRow == 0 ? 0 : (NSInteger)ceilf((CGFloat)totalItems / (CGFloat)itemsPerRow);
367+
368+
totalItems += itemsDelta;
369+
NSInteger rowsDelta = rows - (itemsPerRow == 0 ? 0 : (NSInteger)ceilf((CGFloat)totalItems / (CGFloat)itemsPerRow));
370+
371+
UITableViewRowAnimation animation = (UITableViewRowAnimation)[[section objectForKey:@"animation"] integerValue];
372+
if (rowsDelta != 0) {
373+
// Add rows
374+
if (rowsDelta > 0) {
375+
for (NSInteger i = 1; i <= rowsDelta; i++) {
376+
NSArray *indexPaths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:rows + i inSection:sectionIndex]];
377+
[_tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:animation];
378+
}
379+
}
380+
381+
// Delete rows
382+
else {
383+
for (NSInteger i = rowsDelta; i > 0; i--) {
384+
NSArray *indexPaths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:rows - i inSection:sectionIndex]];
385+
[_tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:animation];
386+
}
387+
}
388+
}
389+
390+
// Reload changed cells
391+
NSNumber *top = [section objectForKey:@"item"];
392+
if (top) {
393+
NSInteger topIndex = [top integerValue];
394+
NSInteger topRow = [self _cellIndexPathFromItemIndexPath:[NSIndexPath indexPathForRow:topIndex inSection:sectionIndex]].row;
395+
for (NSInteger i = topRow; i < rows + rowsDelta; i++) {
396+
NSArray *indexPaths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:i inSection:sectionIndex]];
397+
[_tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:animation];
398+
}
399+
}
400+
}
405401

406402
// Apply updates
407403
[_tableView endUpdates];

0 commit comments

Comments
 (0)