Skip to content

Commit

Permalink
修正圖片重新下載完成時, 沒有當下被重新填入的問題
Browse files Browse the repository at this point in the history
  • Loading branch information
DaidoujiChen committed Nov 3, 2018
1 parent 7939f30 commit 4ae3204
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
@protocol HentaiImagesManagerDelegate <NSObject>

@required
- (void)imageDownloaded;
- (void)imageHeightChangedAtPageIndex:(NSInteger)pageIndex;

@end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ - (void)displayImage:(NSString *)imagePage data:(NSData *)data {

__strong HentaiImagesManager *strongSelf = weakSelf;
strongSelf.heights[@(pageIndex)] = @{ @"width": @(imageWidth), @"height": @(imageHeight) };
[strongSelf.delegate imageDownloaded];
[strongSelf.delegate imageHeightChangedAtPageIndex:pageIndex];

if (strongSelf.aliveForDownload) {
NSInteger totalImages = strongSelf.heights.count;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ - (void)userCurrentIndex:(NSInteger)index {

#pragma mark - HentaiImagesManager

- (void)imageDownloaded {
[self refreshMaxIndexAndReload];
- (void)imageHeightChangedAtPageIndex:(NSInteger)pageIndex {
[self refreshMaxIndexAndReload:pageIndex];
[self refreshTitle];
}

Expand Down Expand Up @@ -156,27 +156,37 @@ - (void)scrollToIndex:(NSInteger)index {
}

// 刷新新 load 好的頁面
- (void)refreshMaxIndexAndReload {
- (void)refreshMaxIndexAndReload:(NSInteger)pageIndex {
NSInteger preMaxAllowScrollIndex = self.maxAllowScrollIndex;
NSArray<NSNumber *> *sortKeys = [self.manager.heights.allKeys sortedArrayUsingComparator: ^NSComparisonResult(id obj1, id obj2) {
return [obj1 compare:obj2];
}];

NSMutableArray *reloadIndexPaths = [NSMutableArray array];
NSMutableArray *insertIndexPaths = [NSMutableArray array];
NSInteger index;
for (index = preMaxAllowScrollIndex; index < sortKeys.count; index++) {
NSNumber *sortKey = sortKeys[index];
if ([@(index) compare:sortKey] == NSOrderedSame) {
[reloadIndexPaths addObject:[NSIndexPath indexPathForRow:index inSection:0]];
[insertIndexPaths addObject:[NSIndexPath indexPathForRow:index inSection:0]];
}
else {
break;
}
}
self.maxAllowScrollIndex = index;

NSMutableArray *reloadIndexPaths = [NSMutableArray array];
for (UICollectionViewCell *cell in self.collectionView.visibleCells) {
NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];
if (indexPath && indexPath.row == pageIndex) {
[reloadIndexPaths addObject:indexPath];
break;
}
}

[self.collectionView performBatchUpdates: ^{
[self.collectionView insertItemsAtIndexPaths:reloadIndexPaths];
[self.collectionView reloadItemsAtIndexPaths:reloadIndexPaths];
[self.collectionView insertItemsAtIndexPaths:insertIndexPaths];
} completion:nil];
}

Expand Down

0 comments on commit 4ae3204

Please sign in to comment.