Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(484)

Side by Side Diff: ios/clean/chrome/browser/ui/tab_collection/tab_collection_mediator.mm

Issue 2885983003: [ios] TabCollectionItem (Closed)
Patch Set: Address comments Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 populateConsumerItems];
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 populateConsumerItems];
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 DCHECK(self.consumer);
71 [self.consumer insertItem:[self tabCollectionItemFromWebState:webState]
72 atIndex:index];
79 } 73 }
80 74
81 - (void)webStateList:(WebStateList*)webStateList 75 - (void)webStateList:(WebStateList*)webStateList
82 didMoveWebState:(web::WebState*)webState 76 didMoveWebState:(web::WebState*)webState
83 fromIndex:(int)fromIndex 77 fromIndex:(int)fromIndex
84 toIndex:(int)toIndex { 78 toIndex:(int)toIndex {
85 int minIndex = std::min(fromIndex, toIndex); 79 DCHECK(self.consumer);
86 int length = std::abs(fromIndex - toIndex) + 1; 80 [self.consumer moveItemFromIndex:fromIndex toIndex:toIndex];
87 NSIndexSet* indexes =
88 [[NSIndexSet alloc] initWithIndexesInRange:NSMakeRange(minIndex, length)];
89 [self.consumer reloadItemsAtIndexes:indexes];
90 } 81 }
91 82
92 - (void)webStateList:(WebStateList*)webStateList 83 - (void)webStateList:(WebStateList*)webStateList
93 didReplaceWebState:(web::WebState*)oldWebState 84 didReplaceWebState:(web::WebState*)oldWebState
94 withWebState:(web::WebState*)newWebState 85 withWebState:(web::WebState*)newWebState
95 atIndex:(int)index { 86 atIndex:(int)index {
96 [self.consumer reloadItemsAtIndexes:[NSIndexSet indexSetWithIndex:index]]; 87 DCHECK(self.consumer);
88 [self.consumer
89 replaceItemAtIndex:index
90 withItem:[self tabCollectionItemFromWebState:newWebState]];
97 } 91 }
98 92
99 - (void)webStateList:(WebStateList*)webStateList 93 - (void)webStateList:(WebStateList*)webStateList
100 didDetachWebState:(web::WebState*)webState 94 didDetachWebState:(web::WebState*)webState
101 atIndex:(int)index { 95 atIndex:(int)index {
96 DCHECK(self.consumer);
102 [self.consumer deleteItemAtIndex:index]; 97 [self.consumer deleteItemAtIndex:index];
103 } 98 }
104 99
105 - (void)webStateList:(WebStateList*)webStateList 100 - (void)webStateList:(WebStateList*)webStateList
106 didChangeActiveWebState:(web::WebState*)newWebState 101 didChangeActiveWebState:(web::WebState*)newWebState
107 oldWebState:(web::WebState*)oldWebState 102 oldWebState:(web::WebState*)oldWebState
108 atIndex:(int)atIndex 103 atIndex:(int)atIndex
109 userAction:(BOOL)userAction { 104 userAction:(BOOL)userAction {
110 int fromIndex = webStateList->GetIndexOfWebState(oldWebState); 105 DCHECK(self.consumer);
111 NSMutableIndexSet* indexes = [[NSMutableIndexSet alloc] init]; 106 [self.consumer selectItemAtIndex:atIndex];
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 } 107 }
120 108
121 #pragma mark - Private 109 #pragma mark - Private
122 110
123 - (NSString*)titleFromWebState:(const web::WebState*)webState { 111 - (TabCollectionItem*)tabCollectionItemFromWebState:
112 (const web::WebState*)webState {
124 // PLACEHOLDER: Use real webstate title in the future. 113 // PLACEHOLDER: Use real webstate title in the future.
114 DCHECK(webState);
125 GURL url = webState->GetVisibleURL(); 115 GURL url = webState->GetVisibleURL();
126 NSString* urlText = @"<New Tab>"; 116 NSString* urlText = @"<New Tab>";
127 if (url.is_valid()) { 117 if (url.is_valid()) {
128 urlText = base::SysUTF8ToNSString(url.spec()); 118 urlText = base::SysUTF8ToNSString(url.spec());
129 } 119 }
130 return urlText; 120 TabCollectionItem* item = [[TabCollectionItem alloc] init];
121 item.title = urlText;
122 return item;
123 }
124
125 - (NSArray<TabCollectionItem*>*)tabCollectionItemsFromWebStateList:
126 (const WebStateList*)webStateList {
127 DCHECK(webStateList);
128 NSMutableArray<TabCollectionItem*>* items = [[NSMutableArray alloc] init];
129 for (int i = 0; i < webStateList->count(); i++) {
130 [items
131 addObject:[self
132 tabCollectionItemFromWebState:webStateList->GetWebStateAt(
133 i)]];
134 }
135 return [items copy];
136 }
137
138 - (void)populateConsumerItems {
139 if (self.consumer && self.webStateList) {
140 [self.consumer populateItems:[self tabCollectionItemsFromWebStateList:
141 self.webStateList]];
142 [self.consumer selectItemAtIndex:self.webStateList->active_index()];
143 }
131 } 144 }
132 145
133 @end 146 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698