FIRIAMImageOnlyViewController.m 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright 2018 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <TargetConditionals.h>
  17. #if TARGET_OS_IOS
  18. #import "FirebaseInAppMessaging/Sources/DefaultUI/FIRCore+InAppMessagingDisplay.h"
  19. #import "FirebaseInAppMessaging/Sources/DefaultUI/ImageOnly/FIRIAMImageOnlyViewController.h"
  20. @interface FIRIAMImageOnlyViewController ()
  21. @property(nonatomic, readwrite) FIRInAppMessagingImageOnlyDisplay *imageOnlyMessage;
  22. @property(weak, nonatomic) IBOutlet UIImageView *imageView;
  23. @property(weak, nonatomic) IBOutlet UIButton *closeButton;
  24. @property(nonatomic, assign) CGSize imageOriginalSize;
  25. @end
  26. @implementation FIRIAMImageOnlyViewController
  27. + (FIRIAMImageOnlyViewController *)
  28. instantiateViewControllerWithResourceBundle:(NSBundle *)resourceBundle
  29. displayMessage:
  30. (FIRInAppMessagingImageOnlyDisplay *)imageOnlyMessage
  31. displayDelegate:
  32. (id<FIRInAppMessagingDisplayDelegate>)displayDelegate
  33. timeFetcher:(id<FIRIAMTimeFetcher>)timeFetcher {
  34. UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"FIRInAppMessageDisplayStoryboard"
  35. bundle:resourceBundle];
  36. if (storyboard == nil) {
  37. FIRLogError(kFIRLoggerInAppMessagingDisplay, @"I-FID300002",
  38. @"Storyboard '"
  39. "FIRInAppMessageDisplayStoryboard' not found in bundle %@",
  40. resourceBundle);
  41. return nil;
  42. }
  43. FIRIAMImageOnlyViewController *imageOnlyVC = (FIRIAMImageOnlyViewController *)[storyboard
  44. instantiateViewControllerWithIdentifier:@"image-only-vc"];
  45. imageOnlyVC.displayDelegate = displayDelegate;
  46. imageOnlyVC.imageOnlyMessage = imageOnlyMessage;
  47. imageOnlyVC.timeFetcher = timeFetcher;
  48. return imageOnlyVC;
  49. }
  50. - (FIRInAppMessagingDisplayMessage *)inAppMessage {
  51. return self.imageOnlyMessage;
  52. }
  53. - (IBAction)closeButtonClicked:(id)sender {
  54. [self dismissView:FIRInAppMessagingDismissTypeUserTapClose];
  55. }
  56. - (void)setupRecognizers {
  57. UITapGestureRecognizer *tapGestureRecognizer =
  58. [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(messageTapped:)];
  59. tapGestureRecognizer.delaysTouchesBegan = YES;
  60. tapGestureRecognizer.numberOfTapsRequired = 1;
  61. self.imageView.userInteractionEnabled = YES;
  62. [self.imageView addGestureRecognizer:tapGestureRecognizer];
  63. }
  64. - (void)messageTapped:(UITapGestureRecognizer *)recognizer {
  65. #pragma clang diagnostic push
  66. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  67. FIRInAppMessagingAction *action =
  68. [[FIRInAppMessagingAction alloc] initWithActionText:nil
  69. actionURL:self.imageOnlyMessage.actionURL];
  70. #pragma clang diagnostic pop
  71. [self followAction:action];
  72. }
  73. - (void)viewDidLoad {
  74. [super viewDidLoad];
  75. [self.view setBackgroundColor:[UIColor.grayColor colorWithAlphaComponent:0.5]];
  76. // Close button should be announced last for better VoiceOver experience.
  77. self.view.accessibilityElements = @[ self.imageView, self.closeButton ];
  78. if (self.imageOnlyMessage.imageData) {
  79. UIImage *image = [UIImage imageWithData:self.imageOnlyMessage.imageData.imageRawData];
  80. self.imageOriginalSize = image.size;
  81. [self.imageView setImage:image];
  82. self.imageView.contentMode = UIViewContentModeScaleAspectFit;
  83. self.imageView.accessibilityLabel = self.inAppMessage.campaignInfo.campaignName;
  84. } else {
  85. self.imageView.isAccessibilityElement = NO;
  86. }
  87. [self setupRecognizers];
  88. }
  89. - (void)viewDidLayoutSubviews {
  90. [super viewDidLayoutSubviews];
  91. if (!self.imageOnlyMessage.imageData) {
  92. return;
  93. }
  94. // do the calculation in viewDidLayoutSubViews since self.view.window.frame is only
  95. // reliable at this time
  96. // Calculate the size of the image view under the constraints:
  97. // 1 Retain the image ratio
  98. // 2 Have at least 30 point of margines around four sides of the image view
  99. CGFloat minimalMargine = 30; // 30 points
  100. CGFloat maxImageViewWidth = self.view.window.frame.size.width - minimalMargine * 2;
  101. CGFloat maxImageViewHeight = self.view.window.frame.size.height - minimalMargine * 2;
  102. // Factor in space for the top notch on iPhone X*.
  103. maxImageViewHeight -= self.view.safeAreaInsets.top;
  104. CGFloat adjustedImageViewHeight = self.imageOriginalSize.height;
  105. CGFloat adjustedImageViewWidth = self.imageOriginalSize.width;
  106. if (adjustedImageViewWidth > maxImageViewWidth || adjustedImageViewHeight > maxImageViewHeight) {
  107. if (maxImageViewHeight / maxImageViewWidth >
  108. self.imageOriginalSize.height / self.imageOriginalSize.width) {
  109. // the image is relatively too wide compared against displayable area
  110. adjustedImageViewWidth = maxImageViewWidth;
  111. adjustedImageViewHeight =
  112. adjustedImageViewWidth * self.imageOriginalSize.height / self.imageOriginalSize.width;
  113. FIRLogDebug(kFIRLoggerInAppMessagingDisplay, @"I-FID110002",
  114. @"Use max available image display width as %lf", adjustedImageViewWidth);
  115. } else {
  116. // the image is relatively too narrow compared against displayable area
  117. adjustedImageViewHeight = maxImageViewHeight;
  118. adjustedImageViewWidth =
  119. adjustedImageViewHeight * self.imageOriginalSize.width / self.imageOriginalSize.height;
  120. FIRLogDebug(kFIRLoggerInAppMessagingDisplay, @"I-FID110003",
  121. @"Use max available image display height as %lf", adjustedImageViewHeight);
  122. }
  123. } else {
  124. // image can be rendered fully at its original size
  125. FIRLogDebug(kFIRLoggerInAppMessagingDisplay, @"I-FID110001",
  126. @"Image can be fully displayed in image only mode");
  127. }
  128. CGRect rect = CGRectMake(0, 0, adjustedImageViewWidth, adjustedImageViewHeight);
  129. self.imageView.frame = rect;
  130. self.imageView.center = self.view.center;
  131. CGFloat closeButtonCenterX = CGRectGetMaxX(self.imageView.frame);
  132. CGFloat closeButtonCenterY = CGRectGetMinY(self.imageView.frame);
  133. self.closeButton.center = CGPointMake(closeButtonCenterX, closeButtonCenterY);
  134. [self.view bringSubviewToFront:self.closeButton];
  135. }
  136. - (void)viewWillAppear:(BOOL)animated {
  137. [super viewWillAppear:animated];
  138. // close any potential keyboard, which would conflict with the modal in-app messagine view
  139. [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder)
  140. to:nil
  141. from:nil
  142. forEvent:nil];
  143. if (self.imageOnlyMessage.campaignInfo.renderAsTestMessage) {
  144. FIRLogDebug(kFIRLoggerInAppMessagingDisplay, @"I-FID110004",
  145. @"Flashing the close button since this is a test message.");
  146. [self flashCloseButton:self.closeButton];
  147. }
  148. }
  149. - (void)viewDidAppear:(BOOL)animated {
  150. [super viewDidAppear:animated];
  151. // Announce via VoiceOver that the image-only message has appeared. Highlight the image.
  152. UIAccessibilityPostNotification(UIAccessibilityScreenChangedNotification, self.imageView);
  153. }
  154. - (void)flashCloseButton:(UIButton *)closeButton {
  155. closeButton.alpha = 1.0f;
  156. [UIView animateWithDuration:2.0
  157. delay:0.0
  158. options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionRepeat |
  159. UIViewAnimationOptionAutoreverse |
  160. UIViewAnimationOptionAllowUserInteraction
  161. animations:^{
  162. closeButton.alpha = 0.1f;
  163. }
  164. completion:^(BOOL finished){
  165. // Do nothing
  166. }];
  167. }
  168. @end
  169. #endif // TARGET_OS_IOS