Chromium Code Reviews| Index: ios/clean/chrome/browser/ui/tab_collection/tab_collection_mediator.mm |
| diff --git a/ios/clean/chrome/browser/ui/tab_collection/tab_collection_mediator.mm b/ios/clean/chrome/browser/ui/tab_collection/tab_collection_mediator.mm |
| index a8a89f6c7da1ed06cd281bdbc741f51abe7e8c00..574be5e6d537a1e7d848a52b344babaa46a7264c 100644 |
| --- a/ios/clean/chrome/browser/ui/tab_collection/tab_collection_mediator.mm |
| +++ b/ios/clean/chrome/browser/ui/tab_collection/tab_collection_mediator.mm |
| @@ -9,6 +9,7 @@ |
| #include "base/strings/sys_string_conversions.h" |
| #import "ios/chrome/browser/web_state_list/web_state_list.h" |
| #import "ios/clean/chrome/browser/ui/tab_collection/tab_collection_consumer.h" |
| +#import "ios/clean/chrome/browser/ui/tab_collection/tab_collection_item.h" |
| #include "ios/web/public/web_state/web_state.h" |
| #if !defined(__has_feature) || !__has_feature(objc_arc) |
| @@ -50,24 +51,15 @@ |
| - (void)setWebStateList:(WebStateList*)webStateList { |
| _scopedWebStateListObserver->RemoveAll(); |
| _webStateList = webStateList; |
| - if (!_webStateList) { |
| - return; |
| + [self initializeConsumerItems]; |
| + if (_webStateList) { |
| + _scopedWebStateListObserver->Add(_webStateList); |
| } |
| - _scopedWebStateListObserver->Add(_webStateList); |
| } |
| -#pragma mark - TabCollectionDataSource |
| - |
| -- (int)numberOfTabs { |
| - return self.webStateList->count(); |
| -} |
| - |
| -- (NSString*)titleAtIndex:(int)index { |
| - return [self titleFromWebState:self.webStateList->GetWebStateAt(index)]; |
| -} |
| - |
| -- (int)indexOfActiveTab { |
| - return self.webStateList->active_index(); |
| +- (void)setConsumer:(id<TabCollectionConsumer>)consumer { |
| + _consumer = consumer; |
| + [self initializeConsumerItems]; |
| } |
| #pragma mark - WebStateListObserving |
| @@ -75,25 +67,22 @@ |
| - (void)webStateList:(WebStateList*)webStateList |
| didInsertWebState:(web::WebState*)webState |
| atIndex:(int)index { |
| - [self.consumer insertItemAtIndex:index]; |
| + [self.consumer insertItem:[self itemFromWebState:webState] atIndex:index]; |
| } |
| - (void)webStateList:(WebStateList*)webStateList |
| didMoveWebState:(web::WebState*)webState |
| fromIndex:(int)fromIndex |
| toIndex:(int)toIndex { |
| - int minIndex = std::min(fromIndex, toIndex); |
| - int length = std::abs(fromIndex - toIndex) + 1; |
| - NSIndexSet* indexes = |
| - [[NSIndexSet alloc] initWithIndexesInRange:NSMakeRange(minIndex, length)]; |
| - [self.consumer reloadItemsAtIndexes:indexes]; |
| + [self.consumer moveItemFromIndex:fromIndex toIndex:toIndex]; |
| } |
| - (void)webStateList:(WebStateList*)webStateList |
| didReplaceWebState:(web::WebState*)oldWebState |
| withWebState:(web::WebState*)newWebState |
| atIndex:(int)index { |
| - [self.consumer reloadItemsAtIndexes:[NSIndexSet indexSetWithIndex:index]]; |
| + [self.consumer replaceItemAtIndex:index |
| + withItem:[self itemFromWebState:newWebState]]; |
| } |
| - (void)webStateList:(WebStateList*)webStateList |
| @@ -107,27 +96,39 @@ |
| oldWebState:(web::WebState*)oldWebState |
| atIndex:(int)atIndex |
| userAction:(BOOL)userAction { |
| - int fromIndex = webStateList->GetIndexOfWebState(oldWebState); |
| - NSMutableIndexSet* indexes = [[NSMutableIndexSet alloc] init]; |
| - if (fromIndex >= 0 && fromIndex < [self numberOfTabs]) { |
| - [indexes addIndex:fromIndex]; |
| - } |
| - if (atIndex >= 0 && atIndex < [self numberOfTabs]) { |
| - [indexes addIndex:atIndex]; |
| - } |
| - [self.consumer reloadItemsAtIndexes:indexes]; |
| + [self.consumer selectItemAtIndex:atIndex]; |
| } |
| #pragma mark - Private |
| -- (NSString*)titleFromWebState:(const web::WebState*)webState { |
| +- (TabCollectionItem*)itemFromWebState:(const web::WebState*)webState { |
|
sczs
2017/05/18 16:11:02
nit: WDYT about changing this to tabCollectionItem
edchin
2017/05/18 17:30:26
Done.
|
| // PLACEHOLDER: Use real webstate title in the future. |
| GURL url = webState->GetVisibleURL(); |
| NSString* urlText = @"<New Tab>"; |
| if (url.is_valid()) { |
| urlText = base::SysUTF8ToNSString(url.spec()); |
| } |
| - return urlText; |
| + TabCollectionItem* item = [[TabCollectionItem alloc] init]; |
| + item.title = urlText; |
| + return item; |
| +} |
| + |
| +- (NSArray<TabCollectionItem*>*)itemsFromWebStateList: |
| + (const WebStateList*)webStateList { |
| + DCHECK(webStateList); |
| + NSMutableArray<TabCollectionItem*>* items = [[NSMutableArray alloc] init]; |
| + for (int i = 0; i < webStateList->count(); i++) { |
| + [items addObject:[self itemFromWebState:webStateList->GetWebStateAt(i)]]; |
| + } |
| + return [items copy]; |
| +} |
| + |
| +- (void)initializeConsumerItems { |
| + if (self.consumer && self.webStateList) { |
| + [self.consumer |
| + initializeItems:[self itemsFromWebStateList:self.webStateList]]; |
| + [self.consumer selectItemAtIndex:self.webStateList->active_index()]; |
| + } |
| } |
| @end |