Commit a228fb37 authored by edchin's avatar edchin Committed by Commit Bot

[ios] Update tab grid selection behavior

The selection was previously done simulataneously with
insertions/deletions, which resulted in one-off selection error.
Selection should be done after any insert/delete operation.

Cq-Include-Trybots: master.tryserver.chromium.mac:ios-simulator-cronet;master.tryserver.chromium.mac:ios-simulator-full-configs
Change-Id: Iea75f1233a587fe0970fd01219a96de10255968f
Reviewed-on: https://chromium-review.googlesource.com/959723Reviewed-by: default avataredchin <edchin@chromium.org>
Reviewed-by: default avatarMark Cogan <marq@chromium.org>
Commit-Queue: edchin <edchin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#542804}
parent 2e2541e8
...@@ -4,10 +4,8 @@ ...@@ -4,10 +4,8 @@
#import "ios/chrome/browser/ui/tab_grid/grid_view_controller.h" #import "ios/chrome/browser/ui/tab_grid/grid_view_controller.h"
#import "base/ios/block_types.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_cell.h" #import "ios/chrome/browser/ui/tab_grid/grid_cell.h"
#import "ios/chrome/browser/ui/tab_grid/grid_constants.h" #import "ios/chrome/browser/ui/tab_grid/grid_constants.h"
#import "ios/chrome/browser/ui/tab_grid/grid_image_data_source.h" #import "ios/chrome/browser/ui/tab_grid/grid_image_data_source.h"
...@@ -208,7 +206,7 @@ NSIndexPath* CreateIndexPath(NSInteger index) { ...@@ -208,7 +206,7 @@ NSIndexPath* CreateIndexPath(NSInteger index) {
- (void)insertItem:(GridItem*)item - (void)insertItem:(GridItem*)item
atIndex:(NSUInteger)index atIndex:(NSUInteger)index
selectedIndex:(NSUInteger)selectedIndex { selectedIndex:(NSUInteger)selectedIndex {
ProceduralBlock performDataSourceUpdates = ^{ auto performDataSourceUpdates = ^{
[self.items insertObject:item atIndex:index]; [self.items insertObject:item atIndex:index];
self.selectedIndex = selectedIndex; self.selectedIndex = selectedIndex;
}; };
...@@ -216,16 +214,16 @@ NSIndexPath* CreateIndexPath(NSInteger index) { ...@@ -216,16 +214,16 @@ NSIndexPath* CreateIndexPath(NSInteger index) {
performDataSourceUpdates(); performDataSourceUpdates();
return; return;
} }
ProceduralBlock performAllUpdates = ^{ auto performAllUpdates = ^{
performDataSourceUpdates(); performDataSourceUpdates();
self.collectionView.backgroundView.hidden = YES; self.collectionView.backgroundView.hidden = YES;
[self.collectionView insertItemsAtIndexPaths:@[ CreateIndexPath(index) ]]; [self.collectionView insertItemsAtIndexPaths:@[ CreateIndexPath(index) ]];
};
auto completion = ^(BOOL finished) {
[self.collectionView [self.collectionView
selectItemAtIndexPath:CreateIndexPath(selectedIndex) selectItemAtIndexPath:CreateIndexPath(selectedIndex)
animated:YES animated:YES
scrollPosition:UICollectionViewScrollPositionNone]; scrollPosition:UICollectionViewScrollPositionNone];
};
ProceduralBlockWithBool completion = ^(BOOL finished) {
if (self.items.count == 1) { if (self.items.count == 1) {
[self.delegate firstItemWasAddedInGridViewController:self]; [self.delegate firstItemWasAddedInGridViewController:self];
} }
...@@ -236,7 +234,7 @@ NSIndexPath* CreateIndexPath(NSInteger index) { ...@@ -236,7 +234,7 @@ NSIndexPath* CreateIndexPath(NSInteger index) {
- (void)removeItemAtIndex:(NSUInteger)index - (void)removeItemAtIndex:(NSUInteger)index
selectedIndex:(NSUInteger)selectedIndex { selectedIndex:(NSUInteger)selectedIndex {
ProceduralBlock performDataSourceUpdates = ^{ auto performDataSourceUpdates = ^{
[self.items removeObjectAtIndex:index]; [self.items removeObjectAtIndex:index];
self.selectedIndex = selectedIndex; self.selectedIndex = selectedIndex;
}; };
...@@ -244,18 +242,17 @@ NSIndexPath* CreateIndexPath(NSInteger index) { ...@@ -244,18 +242,17 @@ NSIndexPath* CreateIndexPath(NSInteger index) {
performDataSourceUpdates(); performDataSourceUpdates();
return; return;
} }
ProceduralBlock performAllUpdates = ^{ auto performAllUpdates = ^{
performDataSourceUpdates(); performDataSourceUpdates();
[self.collectionView deleteItemsAtIndexPaths:@[ CreateIndexPath(index) ]]; [self.collectionView deleteItemsAtIndexPaths:@[ CreateIndexPath(index) ]];
};
auto completion = ^(BOOL finished) {
if (self.items.count > 0) { if (self.items.count > 0) {
[self.collectionView [self.collectionView
selectItemAtIndexPath:CreateIndexPath(selectedIndex) selectItemAtIndexPath:CreateIndexPath(selectedIndex)
animated:YES animated:YES
scrollPosition:UICollectionViewScrollPositionNone]; scrollPosition:UICollectionViewScrollPositionNone];
} } else {
};
ProceduralBlockWithBool completion = ^(BOOL finished) {
if (self.items.count == 0) {
self.collectionView.backgroundView.hidden = NO; self.collectionView.backgroundView.hidden = NO;
[self.delegate lastItemWasClosedInGridViewController:self]; [self.delegate lastItemWasClosedInGridViewController:self];
} }
...@@ -284,7 +281,7 @@ NSIndexPath* CreateIndexPath(NSInteger index) { ...@@ -284,7 +281,7 @@ NSIndexPath* CreateIndexPath(NSInteger index) {
- (void)moveItemFromIndex:(NSUInteger)fromIndex - (void)moveItemFromIndex:(NSUInteger)fromIndex
toIndex:(NSUInteger)toIndex toIndex:(NSUInteger)toIndex
selectedIndex:(NSUInteger)selectedIndex { selectedIndex:(NSUInteger)selectedIndex {
ProceduralBlock performDataSourceUpdates = ^{ 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];
...@@ -294,16 +291,19 @@ NSIndexPath* CreateIndexPath(NSInteger index) { ...@@ -294,16 +291,19 @@ NSIndexPath* CreateIndexPath(NSInteger index) {
performDataSourceUpdates(); performDataSourceUpdates();
return; return;
} }
ProceduralBlock performAllUpdates = ^{ auto performAllUpdates = ^{
performDataSourceUpdates(); performDataSourceUpdates();
[self.collectionView moveItemAtIndexPath:CreateIndexPath(fromIndex) [self.collectionView moveItemAtIndexPath:CreateIndexPath(fromIndex)
toIndexPath:CreateIndexPath(toIndex)]; toIndexPath:CreateIndexPath(toIndex)];
};
auto completion = ^(BOOL finished) {
[self.collectionView [self.collectionView
selectItemAtIndexPath:CreateIndexPath(selectedIndex) selectItemAtIndexPath:CreateIndexPath(selectedIndex)
animated:YES animated:YES
scrollPosition:UICollectionViewScrollPositionNone]; scrollPosition:UICollectionViewScrollPositionNone];
}; };
[self.collectionView performBatchUpdates:performAllUpdates completion:nil]; [self.collectionView performBatchUpdates:performAllUpdates
completion:completion];
} }
#pragma mark - Private #pragma mark - Private
......
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