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

Unified Diff: ios/clean/chrome/browser/ui/find_in_page/find_in_page_mediator.mm

Issue 2737563006: [ios] Adds support for Find in Page to the new architecture. (Closed)
Patch Set: ObjC mediator Created 3 years, 8 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 side-by-side diff with in-line comments
Download patch
Index: ios/clean/chrome/browser/ui/find_in_page/find_in_page_mediator.mm
diff --git a/ios/clean/chrome/browser/ui/find_in_page/find_in_page_mediator.mm b/ios/clean/chrome/browser/ui/find_in_page/find_in_page_mediator.mm
new file mode 100644
index 0000000000000000000000000000000000000000..6daaab04957270596ed9a00d946f221b2980ee65
--- /dev/null
+++ b/ios/clean/chrome/browser/ui/find_in_page/find_in_page_mediator.mm
@@ -0,0 +1,122 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#import "ios/clean/chrome/browser/ui/find_in_page/find_in_page_mediator.h"
+
+#include "base/memory/ptr_util.h"
+#import "ios/chrome/browser/find_in_page/find_in_page_model.h"
+#import "ios/chrome/browser/find_in_page/find_tab_helper.h"
+#import "ios/clean/chrome/browser/ui/commands/find_in_page_search_commands.h"
+#import "ios/clean/chrome/browser/ui/commands/find_in_page_visibility_commands.h"
+#import "ios/clean/chrome/browser/ui/find_in_page/find_in_page_consumer.h"
+#include "ios/shared/chrome/browser/tabs/web_state_list.h"
+#include "ios/shared/chrome/browser/tabs/web_state_list_observer_bridge.h"
+#import "ios/shared/chrome/browser/ui/commands/command_dispatcher.h"
+#include "ios/web/public/web_state/web_state.h"
+
+
+@interface FindInPageMediator()<FindInPageSearchCommands, WebStateListObserving>
+
+@property(nonatomic, assign) WebStateList* webStateList;
+@property(nonatomic, weak) id<FindInPageConsumerProvider> provider;
+@property(nonatomic, weak) CommandDispatcher<FindInPageVisibilityCommands>* dispatcher;
marq (ping after 24h) 2017/04/06 16:13:27 Line wrap?
rohitrao (ping after 24h) 2017/04/11 20:46:16 Ran git cl format.
+
+- (void)onFindResultsAvailable:(FindInPageModel*)model;
+
+@end
+
+@implementation FindInPageMediator {
+ // Observes the WebStateList so that this mediator can update the UI when the
+ // active WebState changes.
+ std::unique_ptr<WebStateListObserverBridge> _webStateListObserver;
+}
+
+@synthesize dispatcher = _dispatcher;
+@synthesize provider = _provider;
+@synthesize webStateList = _webStateList;
+
+- (instancetype)initWithWebStateList:(WebStateList*)webStateList
+ provider:(id<FindInPageConsumerProvider>)provider
+ dispatcher:(CommandDispatcher<FindInPageVisibilityCommands>*)dispatcher {
+ if ((self = [super init])) {
+ DCHECK(webStateList);
+ DCHECK(provider);
+
+ _webStateList = webStateList;
+ _provider = provider;
+ _dispatcher = dispatcher;
+
+ _webStateListObserver = base::MakeUnique<WebStateListObserverBridge>(self);
+ _webStateList->AddObserver(_webStateListObserver.get());
+
+
+ [_dispatcher startDispatchingToTarget:self
marq (ping after 24h) 2017/04/06 16:13:27 Another option is that the coordinator tells the d
rohitrao (ping after 24h) 2017/04/06 16:27:07 I originally had it that way. How do we feel abou
marq (ping after 24h) 2017/04/07 09:45:55 My suggestion was just that the mediator publicly
rohitrao (ping after 24h) 2017/04/11 20:46:16 Done.
+ forSelector:@selector(findStringInPage:)];
+ [_dispatcher startDispatchingToTarget:self
+ forSelector:@selector(findNextInPage)];
+ [_dispatcher startDispatchingToTarget:self
+ forSelector:@selector(findPreviousInPage)];
+ }
+ return self;
+}
+
+- (void)dealloc {
+ CommandDispatcher* dispatcher = self.dispatcher;
+ [dispatcher stopDispatchingForSelector:@selector(findStringInPage:)];
+ [dispatcher stopDispatchingForSelector:@selector(findNextInPage)];
+ [dispatcher stopDispatchingForSelector:@selector(findPreviousInPage)];
+}
+
+- (void)onFindResultsAvailable:(FindInPageModel*)model {
marq (ping after 24h) 2017/04/06 16:13:27 Nit: 'on' for delegate-y/observer-y methods is a C
rohitrao (ping after 24h) 2017/04/11 20:46:16 Done.
+ NSLog(@"%lu of %lu", model.currentIndex, model.matches);
marq (ping after 24h) 2017/04/06 16:13:27 Remove logging.
rohitrao (ping after 24h) 2017/04/11 20:46:16 Done.
+ id<FindInPageConsumer> consumer = self.provider.consumer;
+ if (!consumer) {
marq (ping after 24h) 2017/04/06 16:13:27 Remove the nil test?
rohitrao (ping after 24h) 2017/04/11 20:46:16 Removed, but for more complex updates, I think I w
+ return;
+ }
+
+ [consumer setCurrentMatch:model.currentIndex ofTotalMatches:model.matches];
+}
+
+
+- (void)webStateList:(WebStateList*)webStateList
+didChangeActiveWebState:(web::WebState*)newWebState
+ oldWebState:(web::WebState*)oldWebState
+ atIndex:(int)atIndex
+ userAction:(BOOL)userAction {
+ if (oldWebState != newWebState) {
+ [self.dispatcher hideFindInPage];
+ }
+}
+
+#pragma mark - Command handlers
+
+- (void)findStringInPage:(NSString*)searchTerm {
+ NSLog(@"findStringInPage command handler: %@", searchTerm);
+ web::WebState* webState = self.webStateList->GetActiveWebState();
+
+ FindTabHelper* helper = FindTabHelper::FromWebState(webState);
+ helper->StartFinding(searchTerm, ^(FindInPageModel* model) {
+ [self onFindResultsAvailable:model];
+ });
+}
+
+- (void)findNextInPage {
+ NSLog(@"findNextInPage command handler");
+ web::WebState* webState = self.webStateList->GetActiveWebState();
+ FindTabHelper* helper = FindTabHelper::FromWebState(webState);
+ helper->ContinueFinding(FindTabHelper::FORWARD, ^(FindInPageModel* model) {
+ [self onFindResultsAvailable:model];
+ });
+}
+
+- (void)findPreviousInPage {
+ NSLog(@"findPreviousInPage command handler");
+ web::WebState* webState = self.webStateList->GetActiveWebState();
+ FindTabHelper* helper = FindTabHelper::FromWebState(webState);
+ helper->ContinueFinding(FindTabHelper::REVERSE, ^(FindInPageModel* model) {
+ [self onFindResultsAvailable:model];
+ });
+}
+
+@end

Powered by Google App Engine
This is Rietveld 408576698