Commit 197f4993 authored by edchin's avatar edchin Committed by Commit Bot

[ios] Light refactor on grid view controller

This CL refactors duplicate logic into a helper method.

Cq-Include-Trybots: luci.chromium.try:ios-simulator-full-configs;master.tryserver.chromium.mac:ios-simulator-cronet
Change-Id: I83dcd77667a25821d8bac524be16647517bb83c4
Reviewed-on: https://chromium-review.googlesource.com/1094476
Commit-Queue: edchin <edchin@chromium.org>
Reviewed-by: default avataredchin <edchin@chromium.org>
Reviewed-by: default avatarSergio Collazos <sczs@chromium.org>
Cr-Commit-Position: refs/heads/master@{#566736}
parent b09a5cbc
...@@ -4,9 +4,11 @@ ...@@ -4,9 +4,11 @@
#import "ios/chrome/browser/ui/tab_grid/grid/grid_view_controller.h" #import "ios/chrome/browser/ui/tab_grid/grid/grid_view_controller.h"
#include "base/ios/block_types.h"
#import "base/logging.h" #import "base/logging.h"
#import "base/mac/foundation_util.h" #import "base/mac/foundation_util.h"
#import "base/numerics/safe_conversions.h" #import "base/numerics/safe_conversions.h"
#include "ios/chrome/browser/procedural_block_types.h"
#import "ios/chrome/browser/ui/tab_grid/grid/grid_cell.h" #import "ios/chrome/browser/ui/tab_grid/grid/grid_cell.h"
#import "ios/chrome/browser/ui/tab_grid/grid/grid_constants.h" #import "ios/chrome/browser/ui/tab_grid/grid/grid_constants.h"
#import "ios/chrome/browser/ui/tab_grid/grid/grid_image_data_source.h" #import "ios/chrome/browser/ui/tab_grid/grid/grid_image_data_source.h"
...@@ -284,17 +286,12 @@ NSIndexPath* CreateIndexPath(NSInteger index) { ...@@ -284,17 +286,12 @@ NSIndexPath* CreateIndexPath(NSInteger index) {
// Consistency check: |item|'s ID is not in |items|. // Consistency check: |item|'s ID is not in |items|.
// (using DCHECK rather than DCHECK_EQ to avoid a checked_cast on NSNotFound). // (using DCHECK rather than DCHECK_EQ to avoid a checked_cast on NSNotFound).
DCHECK([self indexOfItemWithID:item.identifier] == NSNotFound); DCHECK([self indexOfItemWithID:item.identifier] == NSNotFound);
auto performDataSourceUpdates = ^{ auto modelUpdates = ^{
[self.items insertObject:item atIndex:index]; [self.items insertObject:item atIndex:index];
self.selectedItemID = selectedItemID; self.selectedItemID = selectedItemID;
};
if (![self isViewAppeared]) {
performDataSourceUpdates();
[self.delegate gridViewController:self didChangeItemCount:self.items.count]; [self.delegate gridViewController:self didChangeItemCount:self.items.count];
return; };
} auto collectionViewUpdates = ^{
auto performAllUpdates = ^{
performDataSourceUpdates();
[self animateEmptyStateOut]; [self animateEmptyStateOut];
[self.collectionView insertItemsAtIndexPaths:@[ CreateIndexPath(index) ]]; [self.collectionView insertItemsAtIndexPaths:@[ CreateIndexPath(index) ]];
}; };
...@@ -305,24 +302,20 @@ NSIndexPath* CreateIndexPath(NSInteger index) { ...@@ -305,24 +302,20 @@ NSIndexPath* CreateIndexPath(NSInteger index) {
scrollPosition:UICollectionViewScrollPositionNone]; scrollPosition:UICollectionViewScrollPositionNone];
[self.delegate gridViewController:self didChangeItemCount:self.items.count]; [self.delegate gridViewController:self didChangeItemCount:self.items.count];
}; };
[self.collectionView performBatchUpdates:performAllUpdates [self performModelUpdates:modelUpdates
completion:completion]; collectionViewUpdates:collectionViewUpdates
collectionViewUpdatesCompletion:completion];
} }
- (void)removeItemWithID:(NSString*)removedItemID - (void)removeItemWithID:(NSString*)removedItemID
selectedItemID:(NSString*)selectedItemID { selectedItemID:(NSString*)selectedItemID {
NSUInteger index = [self indexOfItemWithID:removedItemID]; NSUInteger index = [self indexOfItemWithID:removedItemID];
auto performDataSourceUpdates = ^{ auto modelUpdates = ^{
[self.items removeObjectAtIndex:index]; [self.items removeObjectAtIndex:index];
self.selectedItemID = selectedItemID; self.selectedItemID = selectedItemID;
};
if (![self isViewAppeared]) {
performDataSourceUpdates();
[self.delegate gridViewController:self didChangeItemCount:self.items.count]; [self.delegate gridViewController:self didChangeItemCount:self.items.count];
return; };
} auto collectionViewUpdates = ^{
auto performAllUpdates = ^{
performDataSourceUpdates();
[self.collectionView deleteItemsAtIndexPaths:@[ CreateIndexPath(index) ]]; [self.collectionView deleteItemsAtIndexPaths:@[ CreateIndexPath(index) ]];
if (self.items.count == 0) { if (self.items.count == 0) {
[self animateEmptyStateIn]; [self animateEmptyStateIn];
...@@ -337,8 +330,9 @@ NSIndexPath* CreateIndexPath(NSInteger index) { ...@@ -337,8 +330,9 @@ NSIndexPath* CreateIndexPath(NSInteger index) {
} }
[self.delegate gridViewController:self didChangeItemCount:self.items.count]; [self.delegate gridViewController:self didChangeItemCount:self.items.count];
}; };
[self.collectionView performBatchUpdates:performAllUpdates [self performModelUpdates:modelUpdates
completion:completion]; collectionViewUpdates:collectionViewUpdates
collectionViewUpdatesCompletion:completion];
} }
- (void)selectItemWithID:(NSString*)selectedItemID { - (void)selectItemWithID:(NSString*)selectedItemID {
...@@ -371,20 +365,12 @@ NSIndexPath* CreateIndexPath(NSInteger index) { ...@@ -371,20 +365,12 @@ NSIndexPath* CreateIndexPath(NSInteger index) {
// If this move would be a no-op, early return and avoid spurious UI updates. // If this move would be a no-op, early return and avoid spurious UI updates.
if (fromIndex == toIndex) if (fromIndex == toIndex)
return; return;
auto modelUpdates = ^{
auto performDataSourceUpdates = ^{
GridItem* item = self.items[fromIndex]; GridItem* item = self.items[fromIndex];
[self.items removeObjectAtIndex:fromIndex]; [self.items removeObjectAtIndex:fromIndex];
[self.items insertObject:item atIndex:toIndex]; [self.items insertObject:item atIndex:toIndex];
}; };
// If the view isn't visible, there's no need for the collection view to auto collectionViewUpdates = ^{
// update.
if (![self isViewAppeared]) {
performDataSourceUpdates();
return;
}
auto performAllUpdates = ^{
performDataSourceUpdates();
[self.collectionView moveItemAtIndexPath:CreateIndexPath(fromIndex) [self.collectionView moveItemAtIndexPath:CreateIndexPath(fromIndex)
toIndexPath:CreateIndexPath(toIndex)]; toIndexPath:CreateIndexPath(toIndex)];
}; };
...@@ -394,8 +380,9 @@ NSIndexPath* CreateIndexPath(NSInteger index) { ...@@ -394,8 +380,9 @@ NSIndexPath* CreateIndexPath(NSInteger index) {
animated:YES animated:YES
scrollPosition:UICollectionViewScrollPositionNone]; scrollPosition:UICollectionViewScrollPositionNone];
}; };
[self.collectionView performBatchUpdates:performAllUpdates [self performModelUpdates:modelUpdates
completion:completion]; collectionViewUpdates:collectionViewUpdates
collectionViewUpdatesCompletion:completion];
} }
#pragma mark - Private properties #pragma mark - Private properties
...@@ -406,6 +393,27 @@ NSIndexPath* CreateIndexPath(NSInteger index) { ...@@ -406,6 +393,27 @@ NSIndexPath* CreateIndexPath(NSInteger index) {
#pragma mark - Private #pragma mark - Private
// Performs model updates and view updates together if the view is appeared, or
// only the model updates if the view is not appeared. |completion| is only run
// if view is appeared.
- (void)performModelUpdates:(ProceduralBlock)modelUpdates
collectionViewUpdates:(ProceduralBlock)collectionViewUpdates
collectionViewUpdatesCompletion:
(ProceduralBlockWithBool)collectionViewUpdatesCompletion {
// If the view isn't visible, there's no need for the collection view to
// update.
if (![self isViewAppeared]) {
modelUpdates();
return;
}
[self.collectionView performBatchUpdates:^{
// Synchronize model and view updates.
modelUpdates();
collectionViewUpdates();
}
completion:collectionViewUpdatesCompletion];
}
// Returns the index in |self.items| of the first item whose identifier is // Returns the index in |self.items| of the first item whose identifier is
// |identifier|. // |identifier|.
- (NSUInteger)indexOfItemWithID:(NSString*)identifier { - (NSUInteger)indexOfItemWithID:(NSString*)identifier {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment