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") {
"//ios/chrome/browser/overlays",
"//ios/chrome/browser/overlays/public/web_content_area",
"//ios/chrome/browser/ui/alert_view_controller",
"//ios/chrome/browser/ui/elements",
"//ios/chrome/browser/ui/overlays:coordinators",
"//ios/chrome/browser/ui/presenters",
"//ios/chrome/browser/ui/overlays/common/alerts",
"//ui/base",
]
}
......@@ -47,7 +46,7 @@ source_set("unit_tests") {
"//ios/chrome/browser/ui/alert_view_controller",
"//ios/chrome/browser/ui/alert_view_controller/test",
"//ios/chrome/browser/ui/dialogs",
"//ios/chrome/browser/ui/elements",
"//ios/chrome/browser/ui/overlays/common/alerts/test",
"//ios/chrome/browser/ui/overlays/test",
"//testing/gmock",
"//testing/gtest",
......
......@@ -5,11 +5,11 @@
#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_
#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
// OverlayPresenter.
@interface AppLauncherAlertOverlayCoordinator : OverlayRequestCoordinator
@interface AppLauncherAlertOverlayCoordinator : AlertOverlayCoordinator
@end
#endif // IOS_CHROME_BROWSER_UI_OVERLAYS_WEB_CONTENT_AREA_APP_LAUNCHER_APP_LAUNCHER_ALERT_OVERLAY_COORDINATOR_H_
......@@ -6,97 +6,27 @@
#include "ios/chrome/browser/overlays/public/overlay_request.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/overlay_request_coordinator_delegate.h"
#import "ios/chrome/browser/ui/overlays/common/alerts/alert_overlay_coordinator+subclassing.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)
#error "This file requires ARC support."
#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
#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 {
return !!request->GetConfig<AppLauncherAlertOverlayRequestConfig>();
}
+ (BOOL)usesChildViewController {
return YES;
}
@end
- (UIViewController*)viewController {
return self.alertViewController;
}
@implementation AppLauncherAlertOverlayCoordinator (Subclassing)
- (void)startAnimated:(BOOL)animated {
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;
}
#pragma mark - AlertOverlayCoordinator
- (void)stopAnimated:(BOOL)animated {
if (!self.started)
return;
[self.presenter dismissAnimated:animated];
self.started = NO;
- (AlertOverlayMediator*)newMediator {
return [[AppLauncherAlertOverlayMediator alloc] initWithRequest:self.request];
}
@end
......@@ -5,38 +5,20 @@
#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_
#import <Foundation/Foundation.h>
#import "ios/chrome/browser/ui/overlays/common/alerts/alert_overlay_mediator.h"
class OverlayRequest;
@protocol AppLauncherAlertOverlayMediatorDelegate;
@protocol AppLauncherAlertOverlayMediatorDataSource;
@protocol AlertConsumer;
// Mediator object that uses a AppLauncherAlertOverlayRequestConfig to set up
// the UI for an alert notifying the user that a navigation will open an
// external app.
@interface AppLauncherAlertOverlayMediator : NSObject
// 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;
@interface AppLauncherAlertOverlayMediator : AlertOverlayMediator
// Designated initializer for a mediator that uses |request|'s configuration to
// set up an AlertConsumer.
- (instancetype)initWithRequest:(OverlayRequest*)request;
@end
// 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;
- (instancetype)initWithRequest:(OverlayRequest*)request
NS_DESIGNATED_INITIALIZER;
- (instancetype)init NS_UNAVAILABLE;
@end
......
......@@ -10,8 +10,7 @@
#include "ios/chrome/browser/overlays/public/overlay_response.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_consumer.h"
#import "ios/chrome/browser/ui/elements/text_field_configuration.h"
#import "ios/chrome/browser/ui/overlays/common/alerts/alert_overlay_mediator+subclassing.h"
#include "ios/chrome/grit/ios_strings.h"
#include "ui/base/l10n/l10n_util.h"
......@@ -22,6 +21,10 @@
@interface AppLauncherAlertOverlayMediator ()
@property(nonatomic, readonly) OverlayRequest* request;
@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
@implementation AppLauncherAlertOverlayMediator
......@@ -42,26 +45,32 @@
return self.request->GetConfig<AppLauncherAlertOverlayRequestConfig>();
}
- (void)setConsumer:(id<AlertConsumer>)consumer {
if (self.consumer == consumer)
return;
_consumer = consumer;
#pragma mark - Response helpers
- (void)updateResponseAllowingAppLaunch:(BOOL)allowAppLaunch {
self.request->set_response(
OverlayResponse::CreateWithInfo<AppLauncherAlertOverlayResponseInfo>(
allowAppLaunch));
}
@end
@implementation AppLauncherAlertOverlayMediator (Subclassing)
NSString* message = nil;
NSString* allowActionTitle = nil;
- (NSString*)alertMessage {
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);
if (self.config->is_repeated_request()) {
message = l10n_util::GetNSString(IDS_IOS_OPEN_REPEATEDLY_ANOTHER_APP);
allowActionTitle =
l10n_util::GetNSString(IDS_IOS_OPEN_REPEATEDLY_ANOTHER_APP_ALLOW);
} 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];
NSString* allowActionTitle =
self.config->is_repeated_request()
? l10n_util::GetNSString(IDS_IOS_OPEN_REPEATEDLY_ANOTHER_APP_ALLOW)
: l10n_util::GetNSString(IDS_IOS_APP_LAUNCHER_OPEN_APP_BUTTON_LABEL);
__weak __typeof__(self) weakSelf = self;
[self.consumer setActions:@[
return @[
[AlertAction actionWithTitle:allowActionTitle
style:UIAlertActionStyleDefault
handler:^(AlertAction* action) {
......@@ -78,17 +87,7 @@
[strongSelf.delegate
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
......@@ -10,50 +10,41 @@
#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/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 "testing/gtest_mac.h"
#include "testing/platform_test.h"
#include "ui/base/l10n/l10n_util.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
class AppLauncherAlertOverlayMediatorTest : public PlatformTest {
class AppLauncherAlertOverlayMediatorTest : public AlertOverlayMediatorTest {
protected:
AppLauncherAlertOverlayMediatorTest()
: consumer_([[FakeAlertConsumer alloc] init]) {
UpdateConsumer();
}
FakeAlertConsumer* consumer() const { return consumer_; }
AppLauncherAlertOverlayMediatorTest() { UpdateMediator(); }
// Setter for whether the test is for a repeated app launch request.
void set_is_repeated_request(bool is_repeated_request) {
if (is_repeated_request_ == is_repeated_request)
return;
is_repeated_request_ = is_repeated_request;
UpdateConsumer();
UpdateMediator();
}
private:
// Instantiates |request_| with an OverlayRequest configured with an
// AppLauncherAlertOverlayRequestConfig set up using |is_repeated_request_|.
// Resets |mediator_| to a new AppLauncherAlertOverlayMediator created with
// |request_| and sets its consumer to |consumer_|.
void UpdateConsumer() {
// Creates a new mediator using using |request_|.
void UpdateMediator() {
request_ =
OverlayRequest::CreateWithConfig<AppLauncherAlertOverlayRequestConfig>(
is_repeated_request_);
mediator_ = [[AppLauncherAlertOverlayMediator alloc]
initWithRequest:request_.get()];
mediator_.consumer = consumer_;
SetMediator([[AppLauncherAlertOverlayMediator alloc]
initWithRequest:request_.get()]);
}
FakeAlertConsumer* consumer_;
bool is_repeated_request_ = false;
std::unique_ptr<OverlayRequest> request_;
AppLauncherAlertOverlayMediator* mediator_ = nil;
};
// 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