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..58839e6bbda687110437202ee12a8eca962361f4 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 populateConsumerItems]; |
+ 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 populateConsumerItems]; |
} |
#pragma mark - WebStateListObserving |
@@ -75,30 +67,33 @@ |
- (void)webStateList:(WebStateList*)webStateList |
didInsertWebState:(web::WebState*)webState |
atIndex:(int)index { |
- [self.consumer insertItemAtIndex:index]; |
+ DCHECK(self.consumer); |
+ [self.consumer insertItem:[self tabCollectionItemFromWebState: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]; |
+ DCHECK(self.consumer); |
+ [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]]; |
+ DCHECK(self.consumer); |
+ [self.consumer |
+ replaceItemAtIndex:index |
+ withItem:[self tabCollectionItemFromWebState:newWebState]]; |
} |
- (void)webStateList:(WebStateList*)webStateList |
didDetachWebState:(web::WebState*)webState |
atIndex:(int)index { |
+ DCHECK(self.consumer); |
[self.consumer deleteItemAtIndex:index]; |
} |
@@ -107,27 +102,45 @@ |
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]; |
+ DCHECK(self.consumer); |
+ [self.consumer selectItemAtIndex:atIndex]; |
} |
#pragma mark - Private |
-- (NSString*)titleFromWebState:(const web::WebState*)webState { |
+- (TabCollectionItem*)tabCollectionItemFromWebState: |
+ (const web::WebState*)webState { |
// PLACEHOLDER: Use real webstate title in the future. |
+ DCHECK(webState); |
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*>*)tabCollectionItemsFromWebStateList: |
+ (const WebStateList*)webStateList { |
+ DCHECK(webStateList); |
+ NSMutableArray<TabCollectionItem*>* items = [[NSMutableArray alloc] init]; |
+ for (int i = 0; i < webStateList->count(); i++) { |
+ [items |
+ addObject:[self |
+ tabCollectionItemFromWebState:webStateList->GetWebStateAt( |
+ i)]]; |
+ } |
+ return [items copy]; |
+} |
+ |
+- (void)populateConsumerItems { |
+ if (self.consumer && self.webStateList) { |
+ [self.consumer populateItems:[self tabCollectionItemsFromWebStateList: |
+ self.webStateList]]; |
+ [self.consumer selectItemAtIndex:self.webStateList->active_index()]; |
+ } |
} |
@end |