Chromium Code Reviews| 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_mediator.h" | 5 #import "ios/clean/chrome/browser/ui/tab_collection/tab_collection_mediator.h" |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/scoped_observer.h" | 8 #include "base/scoped_observer.h" |
| 9 #include "base/strings/sys_string_conversions.h" | 9 #include "base/strings/sys_string_conversions.h" |
| 10 #import "ios/chrome/browser/web_state_list/web_state_list.h" | 10 #import "ios/chrome/browser/web_state_list/web_state_list.h" |
| 11 #import "ios/clean/chrome/browser/ui/tab_collection/tab_collection_consumer.h" | 11 #import "ios/clean/chrome/browser/ui/tab_collection/tab_collection_consumer.h" |
| 12 #import "ios/clean/chrome/browser/ui/tab_collection/tab_collection_item.h" | |
| 12 #include "ios/web/public/web_state/web_state.h" | 13 #include "ios/web/public/web_state/web_state.h" |
| 13 | 14 |
| 14 #if !defined(__has_feature) || !__has_feature(objc_arc) | 15 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 15 #error "This file requires ARC support." | 16 #error "This file requires ARC support." |
| 16 #endif | 17 #endif |
| 17 | 18 |
| 18 @implementation TabCollectionMediator { | 19 @implementation TabCollectionMediator { |
| 19 std::unique_ptr<WebStateListObserverBridge> _webStateListObserver; | 20 std::unique_ptr<WebStateListObserverBridge> _webStateListObserver; |
| 20 std::unique_ptr<ScopedObserver<WebStateList, WebStateListObserverBridge>> | 21 std::unique_ptr<ScopedObserver<WebStateList, WebStateListObserverBridge>> |
| 21 _scopedWebStateListObserver; | 22 _scopedWebStateListObserver; |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 43 | 44 |
| 44 - (void)disconnect { | 45 - (void)disconnect { |
| 45 self.webStateList = nullptr; | 46 self.webStateList = nullptr; |
| 46 } | 47 } |
| 47 | 48 |
| 48 #pragma mark - Properties | 49 #pragma mark - Properties |
| 49 | 50 |
| 50 - (void)setWebStateList:(WebStateList*)webStateList { | 51 - (void)setWebStateList:(WebStateList*)webStateList { |
| 51 _scopedWebStateListObserver->RemoveAll(); | 52 _scopedWebStateListObserver->RemoveAll(); |
| 52 _webStateList = webStateList; | 53 _webStateList = webStateList; |
| 53 if (!_webStateList) { | 54 [self initializeConsumerItems]; |
| 54 return; | 55 if (_webStateList) { |
| 56 _scopedWebStateListObserver->Add(_webStateList); | |
| 55 } | 57 } |
| 56 _scopedWebStateListObserver->Add(_webStateList); | |
| 57 } | 58 } |
| 58 | 59 |
| 59 #pragma mark - TabCollectionDataSource | 60 - (void)setConsumer:(id<TabCollectionConsumer>)consumer { |
| 60 | 61 _consumer = consumer; |
| 61 - (int)numberOfTabs { | 62 [self initializeConsumerItems]; |
| 62 return self.webStateList->count(); | |
| 63 } | |
| 64 | |
| 65 - (NSString*)titleAtIndex:(int)index { | |
| 66 return [self titleFromWebState:self.webStateList->GetWebStateAt(index)]; | |
| 67 } | |
| 68 | |
| 69 - (int)indexOfActiveTab { | |
| 70 return self.webStateList->active_index(); | |
| 71 } | 63 } |
| 72 | 64 |
| 73 #pragma mark - WebStateListObserving | 65 #pragma mark - WebStateListObserving |
| 74 | 66 |
| 75 - (void)webStateList:(WebStateList*)webStateList | 67 - (void)webStateList:(WebStateList*)webStateList |
| 76 didInsertWebState:(web::WebState*)webState | 68 didInsertWebState:(web::WebState*)webState |
| 77 atIndex:(int)index { | 69 atIndex:(int)index { |
| 78 [self.consumer insertItemAtIndex:index]; | 70 [self.consumer insertItem:[self itemFromWebState:webState] atIndex:index]; |
| 79 } | 71 } |
| 80 | 72 |
| 81 - (void)webStateList:(WebStateList*)webStateList | 73 - (void)webStateList:(WebStateList*)webStateList |
| 82 didMoveWebState:(web::WebState*)webState | 74 didMoveWebState:(web::WebState*)webState |
| 83 fromIndex:(int)fromIndex | 75 fromIndex:(int)fromIndex |
| 84 toIndex:(int)toIndex { | 76 toIndex:(int)toIndex { |
| 85 int minIndex = std::min(fromIndex, toIndex); | 77 [self.consumer moveItemFromIndex:fromIndex toIndex:toIndex]; |
| 86 int length = std::abs(fromIndex - toIndex) + 1; | |
| 87 NSIndexSet* indexes = | |
| 88 [[NSIndexSet alloc] initWithIndexesInRange:NSMakeRange(minIndex, length)]; | |
| 89 [self.consumer reloadItemsAtIndexes:indexes]; | |
| 90 } | 78 } |
| 91 | 79 |
| 92 - (void)webStateList:(WebStateList*)webStateList | 80 - (void)webStateList:(WebStateList*)webStateList |
| 93 didReplaceWebState:(web::WebState*)oldWebState | 81 didReplaceWebState:(web::WebState*)oldWebState |
| 94 withWebState:(web::WebState*)newWebState | 82 withWebState:(web::WebState*)newWebState |
| 95 atIndex:(int)index { | 83 atIndex:(int)index { |
| 96 [self.consumer reloadItemsAtIndexes:[NSIndexSet indexSetWithIndex:index]]; | 84 [self.consumer replaceItemAtIndex:index |
| 85 withItem:[self itemFromWebState:newWebState]]; | |
| 97 } | 86 } |
| 98 | 87 |
| 99 - (void)webStateList:(WebStateList*)webStateList | 88 - (void)webStateList:(WebStateList*)webStateList |
| 100 didDetachWebState:(web::WebState*)webState | 89 didDetachWebState:(web::WebState*)webState |
| 101 atIndex:(int)index { | 90 atIndex:(int)index { |
| 102 [self.consumer deleteItemAtIndex:index]; | 91 [self.consumer deleteItemAtIndex:index]; |
| 103 } | 92 } |
| 104 | 93 |
| 105 - (void)webStateList:(WebStateList*)webStateList | 94 - (void)webStateList:(WebStateList*)webStateList |
| 106 didChangeActiveWebState:(web::WebState*)newWebState | 95 didChangeActiveWebState:(web::WebState*)newWebState |
| 107 oldWebState:(web::WebState*)oldWebState | 96 oldWebState:(web::WebState*)oldWebState |
| 108 atIndex:(int)atIndex | 97 atIndex:(int)atIndex |
| 109 userAction:(BOOL)userAction { | 98 userAction:(BOOL)userAction { |
| 110 int fromIndex = webStateList->GetIndexOfWebState(oldWebState); | 99 [self.consumer selectItemAtIndex:atIndex]; |
| 111 NSMutableIndexSet* indexes = [[NSMutableIndexSet alloc] init]; | |
| 112 if (fromIndex >= 0 && fromIndex < [self numberOfTabs]) { | |
| 113 [indexes addIndex:fromIndex]; | |
| 114 } | |
| 115 if (atIndex >= 0 && atIndex < [self numberOfTabs]) { | |
| 116 [indexes addIndex:atIndex]; | |
| 117 } | |
| 118 [self.consumer reloadItemsAtIndexes:indexes]; | |
| 119 } | 100 } |
| 120 | 101 |
| 121 #pragma mark - Private | 102 #pragma mark - Private |
| 122 | 103 |
| 123 - (NSString*)titleFromWebState:(const web::WebState*)webState { | 104 - (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.
| |
| 124 // PLACEHOLDER: Use real webstate title in the future. | 105 // PLACEHOLDER: Use real webstate title in the future. |
| 125 GURL url = webState->GetVisibleURL(); | 106 GURL url = webState->GetVisibleURL(); |
| 126 NSString* urlText = @"<New Tab>"; | 107 NSString* urlText = @"<New Tab>"; |
| 127 if (url.is_valid()) { | 108 if (url.is_valid()) { |
| 128 urlText = base::SysUTF8ToNSString(url.spec()); | 109 urlText = base::SysUTF8ToNSString(url.spec()); |
| 129 } | 110 } |
| 130 return urlText; | 111 TabCollectionItem* item = [[TabCollectionItem alloc] init]; |
| 112 item.title = urlText; | |
| 113 return item; | |
| 114 } | |
| 115 | |
| 116 - (NSArray<TabCollectionItem*>*)itemsFromWebStateList: | |
| 117 (const WebStateList*)webStateList { | |
| 118 DCHECK(webStateList); | |
| 119 NSMutableArray<TabCollectionItem*>* items = [[NSMutableArray alloc] init]; | |
| 120 for (int i = 0; i < webStateList->count(); i++) { | |
| 121 [items addObject:[self itemFromWebState:webStateList->GetWebStateAt(i)]]; | |
| 122 } | |
| 123 return [items copy]; | |
| 124 } | |
| 125 | |
| 126 - (void)initializeConsumerItems { | |
| 127 if (self.consumer && self.webStateList) { | |
| 128 [self.consumer | |
| 129 initializeItems:[self itemsFromWebStateList:self.webStateList]]; | |
| 130 [self.consumer selectItemAtIndex:self.webStateList->active_index()]; | |
| 131 } | |
| 131 } | 132 } |
| 132 | 133 |
| 133 @end | 134 @end |
| OLD | NEW |