OLD | NEW |
---|---|
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import "ios/clean/chrome/browser/ui/tab_collection/tab_collection_view_controll er.h" | 5 #import "ios/clean/chrome/browser/ui/tab_collection/tab_collection_view_controll er.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/mac/foundation_util.h" | 8 #include "base/mac/foundation_util.h" |
9 #include "base/strings/sys_string_conversions.h" | 9 #include "base/strings/sys_string_conversions.h" |
10 #import "ios/clean/chrome/browser/ui/tab_collection/tab_collection_item.h" | 10 #import "ios/clean/chrome/browser/ui/tab_collection/tab_collection_item.h" |
11 #import "ios/clean/chrome/browser/ui/tab_collection/tab_collection_tab_cell.h" | 11 #import "ios/clean/chrome/browser/ui/tab_collection/tab_collection_tab_cell.h" |
12 | 12 |
13 #if !defined(__has_feature) || !__has_feature(objc_arc) | 13 #if !defined(__has_feature) || !__has_feature(objc_arc) |
14 #error "This file requires ARC support." | 14 #error "This file requires ARC support." |
15 #endif | 15 #endif |
16 | 16 |
17 namespace { | |
18 const int kInvalidIndex = -1; | |
19 } | |
20 | |
17 @interface TabCollectionViewController ()<UICollectionViewDelegate, | 21 @interface TabCollectionViewController ()<UICollectionViewDelegate, |
18 SessionCellDelegate> | 22 SessionCellDelegate> |
19 @property(nonatomic, readwrite) UICollectionView* tabs; | 23 @property(nonatomic, readwrite) UICollectionView* tabs; |
20 @property(nonatomic, readwrite) NSMutableArray<TabCollectionItem*>* items; | 24 @property(nonatomic, readwrite) NSMutableArray<TabCollectionItem*>* items; |
21 @property(nonatomic, assign) int selectedIndex; | 25 @property(nonatomic, assign) int selectedIndex; |
22 @end | 26 @end |
23 | 27 |
24 @implementation TabCollectionViewController | 28 @implementation TabCollectionViewController |
25 @synthesize tabs = _tabs; | 29 @synthesize tabs = _tabs; |
26 @synthesize items = _items; | 30 @synthesize items = _items; |
(...skipping 15 matching lines...) Expand all Loading... | |
42 self.tabs.delegate = self; | 46 self.tabs.delegate = self; |
43 [self.tabs registerClass:[TabCollectionTabCell class] | 47 [self.tabs registerClass:[TabCollectionTabCell class] |
44 forCellWithReuseIdentifier:[TabCollectionTabCell identifier]]; | 48 forCellWithReuseIdentifier:[TabCollectionTabCell identifier]]; |
45 | 49 |
46 [NSLayoutConstraint activateConstraints:@[ | 50 [NSLayoutConstraint activateConstraints:@[ |
47 [self.tabs.topAnchor constraintEqualToAnchor:self.view.topAnchor], | 51 [self.tabs.topAnchor constraintEqualToAnchor:self.view.topAnchor], |
48 [self.tabs.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor], | 52 [self.tabs.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor], |
49 [self.tabs.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor], | 53 [self.tabs.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor], |
50 [self.tabs.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor], | 54 [self.tabs.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor], |
51 ]]; | 55 ]]; |
52 | |
53 [self selectItemAtIndex:self.selectedIndex]; | |
54 } | 56 } |
55 | 57 |
56 - (UIStatusBarStyle)preferredStatusBarStyle { | 58 - (UIStatusBarStyle)preferredStatusBarStyle { |
57 return UIStatusBarStyleLightContent; | 59 return UIStatusBarStyleLightContent; |
58 } | 60 } |
59 | 61 |
60 #pragma mark - Required subclass override | 62 #pragma mark - Required subclass override |
61 | 63 |
62 - (UICollectionViewLayout*)collectionViewLayout { | 64 - (UICollectionViewLayout*)collectionViewLayout { |
63 NOTREACHED() << "You must override " | 65 NOTREACHED() << "You must override " |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
97 dequeueReusableCellWithReuseIdentifier:[TabCollectionTabCell | 99 dequeueReusableCellWithReuseIdentifier:[TabCollectionTabCell |
98 identifier] | 100 identifier] |
99 forIndexPath:indexPath]); | 101 forIndexPath:indexPath]); |
100 cell.delegate = self; | 102 cell.delegate = self; |
101 [cell setSessionType:TabSwitcherSessionType::REGULAR_SESSION]; | 103 [cell setSessionType:TabSwitcherSessionType::REGULAR_SESSION]; |
102 DCHECK_LE(indexPath.item, INT_MAX); | 104 DCHECK_LE(indexPath.item, INT_MAX); |
103 int index = static_cast<int>(indexPath.item); | 105 int index = static_cast<int>(indexPath.item); |
104 [cell setAppearanceForTabTitle:self.items[index].title | 106 [cell setAppearanceForTabTitle:self.items[index].title |
105 favicon:nil | 107 favicon:nil |
106 cellSize:CGSizeZero]; | 108 cellSize:CGSizeZero]; |
109 [cell setSelected:(index == self.selectedIndex)]; | |
107 return cell; | 110 return cell; |
108 } | 111 } |
109 | 112 |
110 #pragma mark - UICollectionViewDelegate methods | 113 #pragma mark - UICollectionViewDelegate methods |
111 | 114 |
112 - (BOOL)collectionView:(UICollectionView*)collectionView | 115 - (BOOL)collectionView:(UICollectionView*)collectionView |
113 shouldSelectItemAtIndexPath:(NSIndexPath*)indexPath { | 116 shouldSelectItemAtIndexPath:(NSIndexPath*)indexPath { |
114 // Prevent user selection of items. | 117 // Prevent user selection of items. |
115 return NO; | 118 return NO; |
116 } | 119 } |
(...skipping 15 matching lines...) Expand all Loading... | |
132 - (void)deleteButtonPressedForCell:(UICollectionViewCell*)cell { | 135 - (void)deleteButtonPressedForCell:(UICollectionViewCell*)cell { |
133 NSInteger item = [[self.tabs indexPathForCell:cell] item]; | 136 NSInteger item = [[self.tabs indexPathForCell:cell] item]; |
134 DCHECK_LE(item, INT_MAX); | 137 DCHECK_LE(item, INT_MAX); |
135 int index = static_cast<int>(item); | 138 int index = static_cast<int>(item); |
136 [self closeTabAtIndex:index]; | 139 [self closeTabAtIndex:index]; |
137 } | 140 } |
138 | 141 |
139 #pragma mark - TabCollectionConsumer methods | 142 #pragma mark - TabCollectionConsumer methods |
140 | 143 |
141 - (void)insertItem:(TabCollectionItem*)item atIndex:(int)index { | 144 - (void)insertItem:(TabCollectionItem*)item atIndex:(int)index { |
145 DCHECK_GE(index, 0); | |
142 DCHECK_LE(static_cast<NSUInteger>(index), self.items.count); | 146 DCHECK_LE(static_cast<NSUInteger>(index), self.items.count); |
147 if (self.selectedIndex >= index) { | |
148 self.selectedIndex++; | |
rohitrao (ping after 24h)
2017/05/26 12:44:01
Is this VC independently trying to keep track of h
edchin
2017/05/26 18:09:00
The VC will get into an internally inconsistent st
| |
149 } | |
143 [self.items insertObject:item atIndex:index]; | 150 [self.items insertObject:item atIndex:index]; |
144 [self.tabs insertItemsAtIndexPaths:@[ [self indexPathForIndex:index] ]]; | 151 [self.tabs insertItemsAtIndexPaths:@[ [self indexPathForIndex:index] ]]; |
145 } | 152 } |
146 | 153 |
147 - (void)deleteItemAtIndex:(int)index { | 154 - (void)deleteItemAtIndex:(int)index { |
155 DCHECK_GE(index, 0); | |
148 DCHECK_LT(static_cast<NSUInteger>(index), self.items.count); | 156 DCHECK_LT(static_cast<NSUInteger>(index), self.items.count); |
157 if (self.selectedIndex == index) { | |
sczs
2017/05/26 16:12:52
I also agree with Rohit, is it possible to push th
edchin
2017/05/26 18:09:00
See comment above.
| |
158 self.selectedIndex = kInvalidIndex; | |
159 } else if (self.selectedIndex > index) { | |
160 self.selectedIndex--; | |
161 } | |
149 [self.items removeObjectAtIndex:index]; | 162 [self.items removeObjectAtIndex:index]; |
150 [self.tabs deleteItemsAtIndexPaths:@[ [self indexPathForIndex:index] ]]; | 163 [self.tabs deleteItemsAtIndexPaths:@[ [self indexPathForIndex:index] ]]; |
151 } | 164 } |
152 | 165 |
153 - (void)moveItemFromIndex:(int)fromIndex toIndex:(int)toIndex { | 166 - (void)moveItemFromIndex:(int)fromIndex toIndex:(int)toIndex { |
154 TabCollectionItem* item = self.items[fromIndex]; | 167 TabCollectionItem* item = self.items[fromIndex]; |
155 [self.items removeObjectAtIndex:fromIndex]; | 168 [self.items removeObjectAtIndex:fromIndex]; |
156 [self.items insertObject:item atIndex:toIndex]; | 169 [self.items insertObject:item atIndex:toIndex]; |
157 [self.tabs moveItemAtIndexPath:[self indexPathForIndex:fromIndex] | 170 [self.tabs moveItemAtIndexPath:[self indexPathForIndex:fromIndex] |
158 toIndexPath:[self indexPathForIndex:toIndex]]; | 171 toIndexPath:[self indexPathForIndex:toIndex]]; |
159 } | 172 } |
160 | 173 |
161 - (void)replaceItemAtIndex:(int)index withItem:(TabCollectionItem*)item { | 174 - (void)replaceItemAtIndex:(int)index withItem:(TabCollectionItem*)item { |
162 [self.items removeObjectAtIndex:index]; | 175 DCHECK_GE(index, 0); |
163 [self.items insertObject:item atIndex:index]; | 176 DCHECK_LT(static_cast<NSUInteger>(index), self.items.count); |
177 self.items[index] = item; | |
178 [self.tabs reloadItemsAtIndexPaths:@[ [self indexPathForIndex:index] ]]; | |
164 } | 179 } |
165 | 180 |
166 - (void)selectItemAtIndex:(int)index { | 181 - (void)selectItemAtIndex:(int)index { |
182 NSMutableArray* indexPaths = [[NSMutableArray alloc] init]; | |
183 if (self.selectedIndex >= 0) { | |
184 [indexPaths addObject:[self indexPathForIndex:self.selectedIndex]]; | |
185 } | |
186 if (index >= 0) { | |
187 DCHECK_LT(static_cast<NSUInteger>(index), self.items.count); | |
188 [indexPaths addObject:[self indexPathForIndex:index]]; | |
189 } | |
167 self.selectedIndex = index; | 190 self.selectedIndex = index; |
168 [self.tabs selectItemAtIndexPath:[self indexPathForIndex:index] | 191 [self.tabs reloadItemsAtIndexPaths:indexPaths]; |
169 animated:YES | |
170 scrollPosition:UITableViewScrollPositionNone]; | |
171 } | 192 } |
172 | 193 |
173 - (void)populateItems:(NSArray<TabCollectionItem*>*)items { | 194 - (void)populateItems:(NSArray<TabCollectionItem*>*)items { |
174 self.items = [items mutableCopy]; | 195 self.items = [items mutableCopy]; |
175 [self.tabs reloadData]; | 196 [self.tabs reloadData]; |
176 } | 197 } |
177 | 198 |
178 #pragma mark - Private | 199 #pragma mark - Private |
179 | 200 |
180 - (NSIndexPath*)indexPathForIndex:(int)index { | 201 - (NSIndexPath*)indexPathForIndex:(int)index { |
181 return [NSIndexPath indexPathForItem:index inSection:0]; | 202 return [NSIndexPath indexPathForItem:index inSection:0]; |
182 } | 203 } |
183 | 204 |
184 @end | 205 @end |
OLD | NEW |