Chromium Code Reviews| Index: ios/chrome/share_extension/share_view_controller.mm |
| diff --git a/ios/chrome/share_extension/share_view_controller.mm b/ios/chrome/share_extension/share_view_controller.mm |
| index 8ea9e871b2560d788e77bc23c826b08596f3b913..fa70d581c9c1ac82989a84522157da3c494e23ce 100644 |
| --- a/ios/chrome/share_extension/share_view_controller.mm |
| +++ b/ios/chrome/share_extension/share_view_controller.mm |
| @@ -40,6 +40,7 @@ const CGFloat kMediumAlpha = 0.5; |
| NSURL* _shareURL; |
| NSString* _shareTitle; |
| + UIImage* _image; |
| NSExtensionItem* _shareItem; |
| } |
| @@ -64,6 +65,12 @@ const CGFloat kMediumAlpha = 0.5; |
| // kShareExtensionMaxWidth points. |
| - (void)constrainWidgetWidth; |
| +// Display the normal share view. |
|
gambard
2017/01/02 09:11:41
s/Display/Displays
Olivier
2017/01/02 10:40:40
Done.
|
| +- (void)displayShareView; |
| + |
| +// Display an alert to report an error to the user. |
|
gambard
2017/01/02 09:11:41
s/Display/Displays
Olivier
2017/01/02 10:40:40
Done.
|
| +- (void)displayErrorView; |
| + |
| @end |
| @implementation ShareViewController |
| @@ -98,8 +105,7 @@ const CGFloat kMediumAlpha = 0.5; |
| _centerYConstraint = [[shareView centerYAnchor] |
| constraintEqualToAnchor:[self.view centerYAnchor]]; |
| [_centerYConstraint setConstant:(self.view.frame.size.height + |
| - self.shareView.frame.size.height) / |
| - 2]; |
|
gambard
2017/01/02 09:11:41
Why removing the /2?
Olivier
2017/01/02 10:40:40
In view did load, the size of Shareview is not fin
|
| + self.shareView.frame.size.height)]; |
| [_centerYConstraint setActive:YES]; |
| [[[shareView centerXAnchor] constraintEqualToAnchor:[self.view centerXAnchor]] |
| setActive:YES]; |
| @@ -110,20 +116,51 @@ const CGFloat kMediumAlpha = 0.5; |
| [self loadElementsFromContext]; |
| } |
| -- (void)viewWillAppear:(BOOL)animated { |
| - [super viewWillAppear:animated]; |
| +#pragma mark - Private methods |
| - // Center the widget. |
| - [_centerYConstraint setConstant:0]; |
| - [self.maskView setAlpha:0]; |
| - [UIView animateWithDuration:kAnimationDuration |
| - animations:^{ |
| - [self.maskView setAlpha:1]; |
| - [self.view layoutIfNeeded]; |
| - }]; |
| +- (void)displayShareView { |
| + [self.shareView setTitle:_shareTitle]; |
| + [self.shareView setURL:_shareURL]; |
| + if (_image) { |
| + [self.shareView setScreenshot:_image]; |
| + } |
| + dispatch_async(dispatch_get_main_queue(), ^{ |
| + // Center the widget. |
| + [_centerYConstraint setConstant:0]; |
| + [self.maskView setAlpha:0]; |
| + [UIView animateWithDuration:kAnimationDuration |
| + animations:^{ |
| + [self.maskView setAlpha:1]; |
| + [self.view layoutIfNeeded]; |
| + }]; |
| + }); |
| } |
| -#pragma mark - Private methods |
| +- (void)displayErrorView { |
| + NSString* errorMessage = |
| + NSLocalizedString(@"IDS_IOS_ERROR_MESSAGE_SHARE_EXTENSION", |
| + @"The error message to display to the user."); |
| + NSString* okButton = |
| + NSLocalizedString(@"IDS_IOS_OK_BUTTON_SHARE_EXTENSION", |
| + @"The label of the OK button in share extension."); |
| + NSString* applicationName = [[[NSBundle mainBundle] infoDictionary] |
| + valueForKey:@"CFBundleDisplayName"]; |
| + errorMessage = |
| + [errorMessage stringByReplacingOccurrencesOfString:@"APPLICATION_NAME" |
| + withString:applicationName]; |
| + UIAlertController* alert = |
| + [UIAlertController alertControllerWithTitle:errorMessage |
| + message:[_shareURL absoluteString] |
| + preferredStyle:UIAlertControllerStyleAlert]; |
| + UIAlertAction* defaultAction = |
| + [UIAlertAction actionWithTitle:okButton |
| + style:UIAlertActionStyleDefault |
| + handler:^(UIAlertAction* action) { |
| + [self dismissAndReturnItem:nil]; |
| + }]; |
| + [alert addAction:defaultAction]; |
| + [self presentViewController:alert animated:YES completion:nil]; |
| +} |
| - (void)constrainWidgetWidth { |
| // Setting the constraints. |
| @@ -171,8 +208,12 @@ const CGFloat kMediumAlpha = 0.5; |
| if ([_shareTitle length] == 0) { |
| _shareTitle = [URL host]; |
| } |
| - [self.shareView setURL:URL]; |
| - [self.shareView setTitle:_shareTitle]; |
| + if ([[_shareURL scheme] isEqualToString:@"http"] || |
| + [[_shareURL scheme] isEqualToString:@"https"]) { |
| + [self displayShareView]; |
| + } else { |
| + [self displayErrorView]; |
| + } |
| }); |
| }; |
| @@ -183,18 +224,17 @@ const CGFloat kMediumAlpha = 0.5; |
| NSItemProviderPreferredImageSizeKey : [NSValue |
| valueWithCGSize:CGSizeMake(kScreenShotWidth, kScreenShotHeight)] |
| }; |
| - ItemBlock ImageCompletion = ^(id item, NSError* error) { |
| - |
| - UIImage* image = static_cast<UIImage*>(item); |
| - if (image) { |
| + ItemBlock imageCompletion = ^(id item, NSError* error) { |
| + _image = static_cast<UIImage*>(item); |
| + if (_image && self.shareView) { |
| dispatch_async(dispatch_get_main_queue(), ^{ |
| - [self.shareView setScreenshot:image]; |
| + [self.shareView setScreenshot:_image]; |
| }); |
| } |
| }; |
| [itemProvider loadPreviewImageWithOptions:imageOptions |
| - completionHandler:ImageCompletion]; |
| + completionHandler:imageCompletion]; |
| } |
| } |
| } |