Commit bdc6a6a8 authored by Kurt Horimoto's avatar Kurt Horimoto Committed by Commit Bot

[iOS] Update app launcher overlays to use AlertOverlayCoordinator.

This eliminates the boilerplate code of UIViewController setup in the
coordinator, as well as the delegate and datasource capabilities
provided to the mediator.

Bug: 990070
Change-Id: Ia0c4514476dd7f34770a649816e049c32d26c34a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1809898
Commit-Queue: Kurt Horimoto <kkhorimoto@chromium.org>
Reviewed-by: default avatarMike Dougherty <michaeldo@chromium.org>
Cr-Commit-Position: refs/heads/master@{#699445}
parent 1e44b8fe
...@@ -20,9 +20,8 @@ source_set("app_launcher") { ...@@ -20,9 +20,8 @@ source_set("app_launcher") {
"//ios/chrome/browser/overlays", "//ios/chrome/browser/overlays",
"//ios/chrome/browser/overlays/public/web_content_area", "//ios/chrome/browser/overlays/public/web_content_area",
"//ios/chrome/browser/ui/alert_view_controller", "//ios/chrome/browser/ui/alert_view_controller",
"//ios/chrome/browser/ui/elements",
"//ios/chrome/browser/ui/overlays:coordinators", "//ios/chrome/browser/ui/overlays:coordinators",
"//ios/chrome/browser/ui/presenters", "//ios/chrome/browser/ui/overlays/common/alerts",
"//ui/base", "//ui/base",
] ]
} }
...@@ -47,7 +46,7 @@ source_set("unit_tests") { ...@@ -47,7 +46,7 @@ source_set("unit_tests") {
"//ios/chrome/browser/ui/alert_view_controller", "//ios/chrome/browser/ui/alert_view_controller",
"//ios/chrome/browser/ui/alert_view_controller/test", "//ios/chrome/browser/ui/alert_view_controller/test",
"//ios/chrome/browser/ui/dialogs", "//ios/chrome/browser/ui/dialogs",
"//ios/chrome/browser/ui/elements", "//ios/chrome/browser/ui/overlays/common/alerts/test",
"//ios/chrome/browser/ui/overlays/test", "//ios/chrome/browser/ui/overlays/test",
"//testing/gmock", "//testing/gmock",
"//testing/gtest", "//testing/gtest",
......
...@@ -5,11 +5,11 @@ ...@@ -5,11 +5,11 @@
#ifndef IOS_CHROME_BROWSER_UI_OVERLAYS_WEB_CONTENT_AREA_APP_LAUNCHER_APP_LAUNCHER_ALERT_OVERLAY_COORDINATOR_H_ #ifndef IOS_CHROME_BROWSER_UI_OVERLAYS_WEB_CONTENT_AREA_APP_LAUNCHER_APP_LAUNCHER_ALERT_OVERLAY_COORDINATOR_H_
#define IOS_CHROME_BROWSER_UI_OVERLAYS_WEB_CONTENT_AREA_APP_LAUNCHER_APP_LAUNCHER_ALERT_OVERLAY_COORDINATOR_H_ #define IOS_CHROME_BROWSER_UI_OVERLAYS_WEB_CONTENT_AREA_APP_LAUNCHER_APP_LAUNCHER_ALERT_OVERLAY_COORDINATOR_H_
#import "ios/chrome/browser/ui/overlays/overlay_request_coordinator.h" #import "ios/chrome/browser/ui/overlays/common/alerts/alert_overlay_coordinator.h"
// A coordinator that is used to display UI for HTTP authentication dialogs via // A coordinator that is used to display UI for HTTP authentication dialogs via
// OverlayPresenter. // OverlayPresenter.
@interface AppLauncherAlertOverlayCoordinator : OverlayRequestCoordinator @interface AppLauncherAlertOverlayCoordinator : AlertOverlayCoordinator
@end @end
#endif // IOS_CHROME_BROWSER_UI_OVERLAYS_WEB_CONTENT_AREA_APP_LAUNCHER_APP_LAUNCHER_ALERT_OVERLAY_COORDINATOR_H_ #endif // IOS_CHROME_BROWSER_UI_OVERLAYS_WEB_CONTENT_AREA_APP_LAUNCHER_APP_LAUNCHER_ALERT_OVERLAY_COORDINATOR_H_
...@@ -6,97 +6,27 @@ ...@@ -6,97 +6,27 @@
#include "ios/chrome/browser/overlays/public/overlay_request.h" #include "ios/chrome/browser/overlays/public/overlay_request.h"
#include "ios/chrome/browser/overlays/public/web_content_area/app_launcher_alert_overlay.h" #include "ios/chrome/browser/overlays/public/web_content_area/app_launcher_alert_overlay.h"
#import "ios/chrome/browser/ui/alert_view_controller/alert_view_controller.h" #import "ios/chrome/browser/ui/overlays/common/alerts/alert_overlay_coordinator+subclassing.h"
#import "ios/chrome/browser/ui/overlays/overlay_request_coordinator_delegate.h"
#import "ios/chrome/browser/ui/overlays/web_content_area/app_launcher/app_launcher_alert_overlay_mediator.h" #import "ios/chrome/browser/ui/overlays/web_content_area/app_launcher/app_launcher_alert_overlay_mediator.h"
#import "ios/chrome/browser/ui/presenters/contained_presenter_delegate.h"
#import "ios/chrome/browser/ui/presenters/non_modal_view_controller_presenter.h"
#if !defined(__has_feature) || !__has_feature(objc_arc) #if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support." #error "This file requires ARC support."
#endif #endif
@interface AppLauncherAlertOverlayCoordinator () <
AppLauncherAlertOverlayMediatorDelegate,
ContainedPresenterDelegate>
// Whether the coordinator has been started.
@property(nonatomic, getter=isStarted) BOOL started;
@property(nonatomic) AlertViewController* alertViewController;
@property(nonatomic) NonModalViewControllerPresenter* presenter;
@property(nonatomic) AppLauncherAlertOverlayMediator* mediator;
@end
@implementation AppLauncherAlertOverlayCoordinator @implementation AppLauncherAlertOverlayCoordinator
#pragma mark - Accessors
- (void)setMediator:(AppLauncherAlertOverlayMediator*)mediator {
if (_mediator == mediator)
return;
_mediator.delegate = nil;
_mediator = mediator;
_mediator.delegate = self;
}
#pragma mark - AppLauncherAlertOverlayMediatorDelegate
- (void)stopDialogForMediator:(AppLauncherAlertOverlayMediator*)mediator {
DCHECK_EQ(self.mediator, mediator);
[self stopAnimated:YES];
}
#pragma mark - ContainedPresenterDelegate
- (void)containedPresenterDidPresent:(id<ContainedPresenter>)presenter {
self.delegate->OverlayUIDidFinishPresentation(self.request);
}
- (void)containedPresenterDidDismiss:(id<ContainedPresenter>)presenter {
self.alertViewController = nil;
self.presenter = nil;
self.delegate->OverlayUIDidFinishDismissal(self.request);
}
#pragma mark - OverlayCoordinator
+ (BOOL)supportsRequest:(OverlayRequest*)request { + (BOOL)supportsRequest:(OverlayRequest*)request {
return !!request->GetConfig<AppLauncherAlertOverlayRequestConfig>(); return !!request->GetConfig<AppLauncherAlertOverlayRequestConfig>();
} }
+ (BOOL)usesChildViewController { @end
return YES;
}
- (UIViewController*)viewController { @implementation AppLauncherAlertOverlayCoordinator (Subclassing)
return self.alertViewController;
}
- (void)startAnimated:(BOOL)animated { #pragma mark - AlertOverlayCoordinator
if (self.started)
return;
self.alertViewController = [[AlertViewController alloc] init];
self.alertViewController.modalPresentationStyle =
UIModalPresentationOverCurrentContext;
self.alertViewController.modalTransitionStyle =
UIModalTransitionStyleCrossDissolve;
self.mediator =
[[AppLauncherAlertOverlayMediator alloc] initWithRequest:self.request];
self.mediator.consumer = self.alertViewController;
self.presenter = [[NonModalViewControllerPresenter alloc] init];
self.presenter.delegate = self;
self.presenter.baseViewController = self.baseViewController;
self.presenter.presentedViewController = self.alertViewController;
[self.presenter prepareForPresentation];
[self.presenter presentAnimated:animated];
self.started = YES;
}
- (void)stopAnimated:(BOOL)animated { - (AlertOverlayMediator*)newMediator {
if (!self.started) return [[AppLauncherAlertOverlayMediator alloc] initWithRequest:self.request];
return;
[self.presenter dismissAnimated:animated];
self.started = NO;
} }
@end @end
...@@ -5,38 +5,20 @@ ...@@ -5,38 +5,20 @@
#ifndef IOS_CHROME_BROWSER_UI_OVERLAYS_WEB_CONTENT_AREA_APP_LAUNCHER_APP_LAUNCHER_ALERT_OVERLAY_MEDIATOR_H_ #ifndef IOS_CHROME_BROWSER_UI_OVERLAYS_WEB_CONTENT_AREA_APP_LAUNCHER_APP_LAUNCHER_ALERT_OVERLAY_MEDIATOR_H_
#define IOS_CHROME_BROWSER_UI_OVERLAYS_WEB_CONTENT_AREA_APP_LAUNCHER_APP_LAUNCHER_ALERT_OVERLAY_MEDIATOR_H_ #define IOS_CHROME_BROWSER_UI_OVERLAYS_WEB_CONTENT_AREA_APP_LAUNCHER_APP_LAUNCHER_ALERT_OVERLAY_MEDIATOR_H_
#import <Foundation/Foundation.h> #import "ios/chrome/browser/ui/overlays/common/alerts/alert_overlay_mediator.h"
class OverlayRequest; class OverlayRequest;
@protocol AppLauncherAlertOverlayMediatorDelegate;
@protocol AppLauncherAlertOverlayMediatorDataSource;
@protocol AlertConsumer;
// Mediator object that uses a AppLauncherAlertOverlayRequestConfig to set up // Mediator object that uses a AppLauncherAlertOverlayRequestConfig to set up
// the UI for an alert notifying the user that a navigation will open an // the UI for an alert notifying the user that a navigation will open an
// external app. // external app.
@interface AppLauncherAlertOverlayMediator : NSObject @interface AppLauncherAlertOverlayMediator : AlertOverlayMediator
// The consumer to be updated by this mediator. Setting to a new value uses the
// AppLauncherAlertOverlayRequestConfig to update the new consumer.
@property(nonatomic, weak) id<AlertConsumer> consumer;
// The delegate that handles action button functionality set up by the mediator.
@property(nonatomic, weak) id<AppLauncherAlertOverlayMediatorDelegate> delegate;
// Designated initializer for a mediator that uses |request|'s configuration to // Designated initializer for a mediator that uses |request|'s configuration to
// set up an AlertConsumer. // set up an AlertConsumer.
- (instancetype)initWithRequest:(OverlayRequest*)request; - (instancetype)initWithRequest:(OverlayRequest*)request
NS_DESIGNATED_INITIALIZER;
@end - (instancetype)init NS_UNAVAILABLE;
// Protocol used by the actions set up by the
// AppLauncherAlertOverlayMediator.
@protocol AppLauncherAlertOverlayMediatorDelegate <NSObject>
// Called by |mediator| to dismiss the dialog overlay when
// an action is tapped.
- (void)stopDialogForMediator:(AppLauncherAlertOverlayMediator*)mediator;
@end @end
......
...@@ -10,8 +10,7 @@ ...@@ -10,8 +10,7 @@
#include "ios/chrome/browser/overlays/public/overlay_response.h" #include "ios/chrome/browser/overlays/public/overlay_response.h"
#include "ios/chrome/browser/overlays/public/web_content_area/app_launcher_alert_overlay.h" #include "ios/chrome/browser/overlays/public/web_content_area/app_launcher_alert_overlay.h"
#import "ios/chrome/browser/ui/alert_view_controller/alert_action.h" #import "ios/chrome/browser/ui/alert_view_controller/alert_action.h"
#import "ios/chrome/browser/ui/alert_view_controller/alert_consumer.h" #import "ios/chrome/browser/ui/overlays/common/alerts/alert_overlay_mediator+subclassing.h"
#import "ios/chrome/browser/ui/elements/text_field_configuration.h"
#include "ios/chrome/grit/ios_strings.h" #include "ios/chrome/grit/ios_strings.h"
#include "ui/base/l10n/l10n_util.h" #include "ui/base/l10n/l10n_util.h"
...@@ -22,6 +21,10 @@ ...@@ -22,6 +21,10 @@
@interface AppLauncherAlertOverlayMediator () @interface AppLauncherAlertOverlayMediator ()
@property(nonatomic, readonly) OverlayRequest* request; @property(nonatomic, readonly) OverlayRequest* request;
@property(nonatomic, readonly) AppLauncherAlertOverlayRequestConfig* config; @property(nonatomic, readonly) AppLauncherAlertOverlayRequestConfig* config;
// Sets the OverlayResponse. |allowAppLaunch| indicates whether the alert's
// allow button was tapped to allow the navigation to open in another app.
- (void)updateResponseAllowingAppLaunch:(BOOL)allowAppLaunch;
@end @end
@implementation AppLauncherAlertOverlayMediator @implementation AppLauncherAlertOverlayMediator
...@@ -42,26 +45,32 @@ ...@@ -42,26 +45,32 @@
return self.request->GetConfig<AppLauncherAlertOverlayRequestConfig>(); return self.request->GetConfig<AppLauncherAlertOverlayRequestConfig>();
} }
- (void)setConsumer:(id<AlertConsumer>)consumer { #pragma mark - Response helpers
if (self.consumer == consumer)
return; - (void)updateResponseAllowingAppLaunch:(BOOL)allowAppLaunch {
_consumer = consumer; self.request->set_response(
OverlayResponse::CreateWithInfo<AppLauncherAlertOverlayResponseInfo>(
allowAppLaunch));
}
@end
@implementation AppLauncherAlertOverlayMediator (Subclassing)
NSString* message = nil; - (NSString*)alertMessage {
NSString* allowActionTitle = nil; return self.config->is_repeated_request()
? l10n_util::GetNSString(IDS_IOS_OPEN_REPEATEDLY_ANOTHER_APP)
: l10n_util::GetNSString(IDS_IOS_OPEN_IN_ANOTHER_APP);
}
- (NSArray<AlertAction*>*)alertActions {
NSString* rejectActionTitle = l10n_util::GetNSString(IDS_CANCEL); NSString* rejectActionTitle = l10n_util::GetNSString(IDS_CANCEL);
if (self.config->is_repeated_request()) { NSString* allowActionTitle =
message = l10n_util::GetNSString(IDS_IOS_OPEN_REPEATEDLY_ANOTHER_APP); self.config->is_repeated_request()
allowActionTitle = ? l10n_util::GetNSString(IDS_IOS_OPEN_REPEATEDLY_ANOTHER_APP_ALLOW)
l10n_util::GetNSString(IDS_IOS_OPEN_REPEATEDLY_ANOTHER_APP_ALLOW); : l10n_util::GetNSString(IDS_IOS_APP_LAUNCHER_OPEN_APP_BUTTON_LABEL);
} else {
message = l10n_util::GetNSString(IDS_IOS_OPEN_IN_ANOTHER_APP);
allowActionTitle =
l10n_util::GetNSString(IDS_IOS_APP_LAUNCHER_OPEN_APP_BUTTON_LABEL);
}
[self.consumer setMessage:message];
__weak __typeof__(self) weakSelf = self; __weak __typeof__(self) weakSelf = self;
[self.consumer setActions:@[ return @[
[AlertAction actionWithTitle:allowActionTitle [AlertAction actionWithTitle:allowActionTitle
style:UIAlertActionStyleDefault style:UIAlertActionStyleDefault
handler:^(AlertAction* action) { handler:^(AlertAction* action) {
...@@ -78,17 +87,7 @@ ...@@ -78,17 +87,7 @@
[strongSelf.delegate [strongSelf.delegate
stopDialogForMediator:strongSelf]; stopDialogForMediator:strongSelf];
}], }],
]]; ];
}
#pragma mark - Response helpers
// Sets the OverlayResponse. |allowAppLaunch| indicates whether the alert's
// allow button was tapped to allow the navigation to open in another app.
- (void)updateResponseAllowingAppLaunch:(BOOL)allowAppLaunch {
self.request->set_response(
OverlayResponse::CreateWithInfo<AppLauncherAlertOverlayResponseInfo>(
allowAppLaunch));
} }
@end @end
...@@ -10,50 +10,41 @@ ...@@ -10,50 +10,41 @@
#import "ios/chrome/browser/overlays/public/web_content_area/app_launcher_alert_overlay.h" #import "ios/chrome/browser/overlays/public/web_content_area/app_launcher_alert_overlay.h"
#import "ios/chrome/browser/ui/alert_view_controller/alert_action.h" #import "ios/chrome/browser/ui/alert_view_controller/alert_action.h"
#import "ios/chrome/browser/ui/alert_view_controller/test/fake_alert_consumer.h" #import "ios/chrome/browser/ui/alert_view_controller/test/fake_alert_consumer.h"
#import "ios/chrome/browser/ui/overlays/common/alerts/test/alert_overlay_mediator_test.h"
#include "ios/chrome/grit/ios_strings.h" #include "ios/chrome/grit/ios_strings.h"
#include "testing/gtest_mac.h" #include "testing/gtest_mac.h"
#include "testing/platform_test.h"
#include "ui/base/l10n/l10n_util.h" #include "ui/base/l10n/l10n_util.h"
#if !defined(__has_feature) || !__has_feature(objc_arc) #if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support." #error "This file requires ARC support."
#endif #endif
class AppLauncherAlertOverlayMediatorTest : public PlatformTest { class AppLauncherAlertOverlayMediatorTest : public AlertOverlayMediatorTest {
protected: protected:
AppLauncherAlertOverlayMediatorTest() AppLauncherAlertOverlayMediatorTest() { UpdateMediator(); }
: consumer_([[FakeAlertConsumer alloc] init]) {
UpdateConsumer();
}
FakeAlertConsumer* consumer() const { return consumer_; }
// Setter for whether the test is for a repeated app launch request. // Setter for whether the test is for a repeated app launch request.
void set_is_repeated_request(bool is_repeated_request) { void set_is_repeated_request(bool is_repeated_request) {
if (is_repeated_request_ == is_repeated_request) if (is_repeated_request_ == is_repeated_request)
return; return;
is_repeated_request_ = is_repeated_request; is_repeated_request_ = is_repeated_request;
UpdateConsumer(); UpdateMediator();
} }
private: private:
// Instantiates |request_| with an OverlayRequest configured with an // Instantiates |request_| with an OverlayRequest configured with an
// AppLauncherAlertOverlayRequestConfig set up using |is_repeated_request_|. // AppLauncherAlertOverlayRequestConfig set up using |is_repeated_request_|.
// Resets |mediator_| to a new AppLauncherAlertOverlayMediator created with // Creates a new mediator using using |request_|.
// |request_| and sets its consumer to |consumer_|. void UpdateMediator() {
void UpdateConsumer() {
request_ = request_ =
OverlayRequest::CreateWithConfig<AppLauncherAlertOverlayRequestConfig>( OverlayRequest::CreateWithConfig<AppLauncherAlertOverlayRequestConfig>(
is_repeated_request_); is_repeated_request_);
mediator_ = [[AppLauncherAlertOverlayMediator alloc] SetMediator([[AppLauncherAlertOverlayMediator alloc]
initWithRequest:request_.get()]; initWithRequest:request_.get()]);
mediator_.consumer = consumer_;
} }
FakeAlertConsumer* consumer_;
bool is_repeated_request_ = false; bool is_repeated_request_ = false;
std::unique_ptr<OverlayRequest> request_; std::unique_ptr<OverlayRequest> request_;
AppLauncherAlertOverlayMediator* mediator_ = nil;
}; };
// Tests that the consumer values are set correctly for the first app launch // Tests that the consumer values are set correctly for the first app launch
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment