From e90a43924c86ac9eb160c64a08e758836f20be74 Mon Sep 17 00:00:00 2001 From: Makarov Yuriy Date: Wed, 17 Jun 2015 19:48:23 +0300 Subject: [PATCH 1/4] Added a predicate block, which allows to specify which items should be updated using cellUpdateBlock --- ArrayDiff/UIKit/NNDiffReloadOptions.h | 2 +- ArrayDiff/UIKit/NNDiffReloadOptions.m | 10 ++++++++++ ArrayDiff/UIKit/Private/NNDiffReloader.m | 2 +- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/ArrayDiff/UIKit/NNDiffReloadOptions.h b/ArrayDiff/UIKit/NNDiffReloadOptions.h index 78321d6..1347de0 100644 --- a/ArrayDiff/UIKit/NNDiffReloadOptions.h +++ b/ArrayDiff/UIKit/NNDiffReloadOptions.h @@ -12,7 +12,7 @@ @property (nonatomic, copy) void (^cellUpdateBlock)(id cell, NSIndexPath *indexPath); -@property (nonatomic, assign) BOOL useUpdateBlockForReload; +@property (nonatomic, copy) BOOL (^shouldUseUpdateBlockForReload)(NSIndexPath *indexPath); @property (nonatomic, assign) BOOL useMoveIfPossible; diff --git a/ArrayDiff/UIKit/NNDiffReloadOptions.m b/ArrayDiff/UIKit/NNDiffReloadOptions.m index fd9050c..97cf096 100644 --- a/ArrayDiff/UIKit/NNDiffReloadOptions.m +++ b/ArrayDiff/UIKit/NNDiffReloadOptions.m @@ -10,4 +10,14 @@ @implementation NNDiffReloadOptions +- (instancetype)init { + self = [super init]; + if (self) { + _shouldUseUpdateBlockForReload = ^(NSIndexPath *_) { + return NO; + }; + } + return self; +} + @end diff --git a/ArrayDiff/UIKit/Private/NNDiffReloader.m b/ArrayDiff/UIKit/Private/NNDiffReloader.m index c1bcdb3..257c043 100644 --- a/ArrayDiff/UIKit/Private/NNDiffReloader.m +++ b/ArrayDiff/UIKit/Private/NNDiffReloader.m @@ -42,7 +42,7 @@ - (void)reloadWithSectionsDiff:(NNSectionsDiff *)diff for (NNSectionsDiffChange *change in diff.changed) { if (change.type == NNDiffChangeUpdate) { - if (options.useUpdateBlockForReload) { + if (options.shouldUseUpdateBlockForReload(change.after)) { [indexPathsToUpdateWithBlock addObject:change.after]; } else { if (options.useMoveIfPossible) { From f1058e81a4f8b5c024f11f3f1e413e5d43f863c4 Mon Sep 17 00:00:00 2001 From: Makarov Yuriy Date: Thu, 18 Jun 2015 17:07:38 +0300 Subject: [PATCH 2/4] Updated a block for deferred data source update to avoid a case when collection/table views query their data sources _before_ entering performBatchUpdates block and thus have new data before starting updates --- ArrayDiff/UIKit/NNDiffReloadOptions.h | 2 ++ ArrayDiff/UIKit/Private/NNDiffReloader.m | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/ArrayDiff/UIKit/NNDiffReloadOptions.h b/ArrayDiff/UIKit/NNDiffReloadOptions.h index 1347de0..9f70077 100644 --- a/ArrayDiff/UIKit/NNDiffReloadOptions.h +++ b/ArrayDiff/UIKit/NNDiffReloadOptions.h @@ -14,6 +14,8 @@ @property (nonatomic, copy) BOOL (^shouldUseUpdateBlockForReload)(NSIndexPath *indexPath); +@property (nonatomic, copy) void (^dataSourceUpdateBlock)(); + @property (nonatomic, assign) BOOL useMoveIfPossible; @end diff --git a/ArrayDiff/UIKit/Private/NNDiffReloader.m b/ArrayDiff/UIKit/Private/NNDiffReloader.m index 257c043..52dd957 100644 --- a/ArrayDiff/UIKit/Private/NNDiffReloader.m +++ b/ArrayDiff/UIKit/Private/NNDiffReloader.m @@ -34,6 +34,10 @@ - (void)reloadWithSectionsDiff:(NNSectionsDiff *)diff NSMutableArray *indexPathsToUpdateWithBlock = [NSMutableArray array]; [self performUpdates:^{ + if (options.dataSourceUpdateBlock) { + options.dataSourceUpdateBlock(); + } + [self deleteSections:diff.deletedSections]; [self insertSections:diff.insertedSections]; From d0ae566f0395e3fba3f6748dc207d402b624460c Mon Sep 17 00:00:00 2001 From: Makarov Yuriy Date: Thu, 18 Jun 2015 17:13:24 +0300 Subject: [PATCH 3/4] Added an option for forcing reload data in table/collection views --- ArrayDiff/UIKit/NNDiffReloadOptions.h | 2 ++ ArrayDiff/UIKit/Private/NNCollectionViewReloader.m | 4 ++++ ArrayDiff/UIKit/Private/NNDiffReloader.h | 2 ++ ArrayDiff/UIKit/Private/NNDiffReloader.m | 9 ++++++++- ArrayDiff/UIKit/Private/NNTableViewReloader.m | 4 ++++ 5 files changed, 20 insertions(+), 1 deletion(-) diff --git a/ArrayDiff/UIKit/NNDiffReloadOptions.h b/ArrayDiff/UIKit/NNDiffReloadOptions.h index 9f70077..4b72aba 100644 --- a/ArrayDiff/UIKit/NNDiffReloadOptions.h +++ b/ArrayDiff/UIKit/NNDiffReloadOptions.h @@ -18,4 +18,6 @@ @property (nonatomic, assign) BOOL useMoveIfPossible; +@property (nonatomic, assign) BOOL forceReloadData; + @end diff --git a/ArrayDiff/UIKit/Private/NNCollectionViewReloader.m b/ArrayDiff/UIKit/Private/NNCollectionViewReloader.m index 69bfab7..511f9f9 100644 --- a/ArrayDiff/UIKit/Private/NNCollectionViewReloader.m +++ b/ArrayDiff/UIKit/Private/NNCollectionViewReloader.m @@ -71,4 +71,8 @@ - (id)cellForItemAtIndexPath:(NSIndexPath *)indexPath { return [self.collectionView cellForItemAtIndexPath:indexPath]; } +- (void)reloadData { + [self reloadData]; +} + @end diff --git a/ArrayDiff/UIKit/Private/NNDiffReloader.h b/ArrayDiff/UIKit/Private/NNDiffReloader.h index c8098f3..616b16c 100644 --- a/ArrayDiff/UIKit/Private/NNDiffReloader.h +++ b/ArrayDiff/UIKit/Private/NNDiffReloader.h @@ -33,4 +33,6 @@ - (id)cellForItemAtIndexPath:(NSIndexPath *)indexPath; +- (void)reloadData; + @end \ No newline at end of file diff --git a/ArrayDiff/UIKit/Private/NNDiffReloader.m b/ArrayDiff/UIKit/Private/NNDiffReloader.m index 52dd957..41c0679 100644 --- a/ArrayDiff/UIKit/Private/NNDiffReloader.m +++ b/ArrayDiff/UIKit/Private/NNDiffReloader.m @@ -28,6 +28,11 @@ - (void)reloadWithSectionsDiff:(NNSectionsDiff *)diff return; } + if (options.forceReloadData) { + [self reloadData]; + return; + } + diff = [self sanitizeDiff:diff]; NNSectionsDiffTracker *tracker = [[NNSectionsDiffTracker alloc] initWithSectionsDiff:diff]; @@ -37,7 +42,7 @@ - (void)reloadWithSectionsDiff:(NNSectionsDiff *)diff if (options.dataSourceUpdateBlock) { options.dataSourceUpdateBlock(); } - + [self deleteSections:diff.deletedSections]; [self insertSections:diff.insertedSections]; @@ -149,4 +154,6 @@ - (void)moveItemAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *) - (id)cellForItemAtIndexPath:(NSIndexPath *)indexPath { methodNotImplemented(); } +- (void)reloadData { methodNotImplemented(); } + @end diff --git a/ArrayDiff/UIKit/Private/NNTableViewReloader.m b/ArrayDiff/UIKit/Private/NNTableViewReloader.m index ef5dd12..462e814 100644 --- a/ArrayDiff/UIKit/Private/NNTableViewReloader.m +++ b/ArrayDiff/UIKit/Private/NNTableViewReloader.m @@ -82,4 +82,8 @@ - (id)cellForItemAtIndexPath:(NSIndexPath *)indexPath { return [self.tableView cellForRowAtIndexPath:indexPath]; } +- (void)reloadData { + [self reloadData]; +} + @end From 6be46b7ed572ee03deef0fa557948bfa0fbc59a4 Mon Sep 17 00:00:00 2001 From: Makarov Yuriy Date: Thu, 18 Jun 2015 17:16:35 +0300 Subject: [PATCH 4/4] Revert "Added an option for forcing reload data in table/collection views" This reverts commit d0ae566f0395e3fba3f6748dc207d402b624460c. --- ArrayDiff/UIKit/NNDiffReloadOptions.h | 2 -- ArrayDiff/UIKit/Private/NNCollectionViewReloader.m | 4 ---- ArrayDiff/UIKit/Private/NNDiffReloader.h | 2 -- ArrayDiff/UIKit/Private/NNDiffReloader.m | 9 +-------- ArrayDiff/UIKit/Private/NNTableViewReloader.m | 4 ---- 5 files changed, 1 insertion(+), 20 deletions(-) diff --git a/ArrayDiff/UIKit/NNDiffReloadOptions.h b/ArrayDiff/UIKit/NNDiffReloadOptions.h index 4b72aba..9f70077 100644 --- a/ArrayDiff/UIKit/NNDiffReloadOptions.h +++ b/ArrayDiff/UIKit/NNDiffReloadOptions.h @@ -18,6 +18,4 @@ @property (nonatomic, assign) BOOL useMoveIfPossible; -@property (nonatomic, assign) BOOL forceReloadData; - @end diff --git a/ArrayDiff/UIKit/Private/NNCollectionViewReloader.m b/ArrayDiff/UIKit/Private/NNCollectionViewReloader.m index 511f9f9..69bfab7 100644 --- a/ArrayDiff/UIKit/Private/NNCollectionViewReloader.m +++ b/ArrayDiff/UIKit/Private/NNCollectionViewReloader.m @@ -71,8 +71,4 @@ - (id)cellForItemAtIndexPath:(NSIndexPath *)indexPath { return [self.collectionView cellForItemAtIndexPath:indexPath]; } -- (void)reloadData { - [self reloadData]; -} - @end diff --git a/ArrayDiff/UIKit/Private/NNDiffReloader.h b/ArrayDiff/UIKit/Private/NNDiffReloader.h index 616b16c..c8098f3 100644 --- a/ArrayDiff/UIKit/Private/NNDiffReloader.h +++ b/ArrayDiff/UIKit/Private/NNDiffReloader.h @@ -33,6 +33,4 @@ - (id)cellForItemAtIndexPath:(NSIndexPath *)indexPath; -- (void)reloadData; - @end \ No newline at end of file diff --git a/ArrayDiff/UIKit/Private/NNDiffReloader.m b/ArrayDiff/UIKit/Private/NNDiffReloader.m index 41c0679..52dd957 100644 --- a/ArrayDiff/UIKit/Private/NNDiffReloader.m +++ b/ArrayDiff/UIKit/Private/NNDiffReloader.m @@ -28,11 +28,6 @@ - (void)reloadWithSectionsDiff:(NNSectionsDiff *)diff return; } - if (options.forceReloadData) { - [self reloadData]; - return; - } - diff = [self sanitizeDiff:diff]; NNSectionsDiffTracker *tracker = [[NNSectionsDiffTracker alloc] initWithSectionsDiff:diff]; @@ -42,7 +37,7 @@ - (void)reloadWithSectionsDiff:(NNSectionsDiff *)diff if (options.dataSourceUpdateBlock) { options.dataSourceUpdateBlock(); } - + [self deleteSections:diff.deletedSections]; [self insertSections:diff.insertedSections]; @@ -154,6 +149,4 @@ - (void)moveItemAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *) - (id)cellForItemAtIndexPath:(NSIndexPath *)indexPath { methodNotImplemented(); } -- (void)reloadData { methodNotImplemented(); } - @end diff --git a/ArrayDiff/UIKit/Private/NNTableViewReloader.m b/ArrayDiff/UIKit/Private/NNTableViewReloader.m index 462e814..ef5dd12 100644 --- a/ArrayDiff/UIKit/Private/NNTableViewReloader.m +++ b/ArrayDiff/UIKit/Private/NNTableViewReloader.m @@ -82,8 +82,4 @@ - (id)cellForItemAtIndexPath:(NSIndexPath *)indexPath { return [self.tableView cellForRowAtIndexPath:indexPath]; } -- (void)reloadData { - [self reloadData]; -} - @end