FIDImageOnlyViewController.m 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 "FIDImageOnlyViewController.h"
  17. #import "FIRCore+InAppMessagingDisplay.h"
  18. @interface FIDImageOnlyViewController ()
  19. @property(nonatomic, readwrite) FIRInAppMessagingImageOnlyDisplay *imageOnlyMessage;
  20. @property(weak, nonatomic) IBOutlet UIImageView *imageView;
  21. @property(weak, nonatomic) IBOutlet UIButton *closeButton;
  22. @property(nonatomic, assign) CGSize imageOriginalSize;
  23. @end
  24. @implementation FIDImageOnlyViewController
  25. + (FIDImageOnlyViewController *)
  26. instantiateViewControllerWithResourceBundle:(NSBundle *)resourceBundle
  27. displayMessage:
  28. (FIRInAppMessagingImageOnlyDisplay *)imageOnlyMessage
  29. displayDelegate:
  30. (id<FIRInAppMessagingDisplayDelegate>)displayDelegate
  31. timeFetcher:(id<FIDTimeFetcher>)timeFetcher {
  32. UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"FIRInAppMessageDisplayStoryboard"
  33. bundle:resourceBundle];
  34. if (storyboard == nil) {
  35. FIRLogError(kFIRLoggerInAppMessagingDisplay, @"I-FID300002",
  36. @"Storyboard '"
  37. "FIRInAppMessageDisplayStoryboard' not found in bundle %@",
  38. resourceBundle);
  39. return nil;
  40. }
  41. FIDImageOnlyViewController *imageOnlyVC = (FIDImageOnlyViewController *)[storyboard
  42. instantiateViewControllerWithIdentifier:@"image-only-vc"];
  43. imageOnlyVC.displayDelegate = displayDelegate;
  44. imageOnlyVC.imageOnlyMessage = imageOnlyMessage;
  45. imageOnlyVC.timeFetcher = timeFetcher;
  46. return imageOnlyVC;
  47. }
  48. - (FIRInAppMessagingDisplayMessage *)inAppMessage {
  49. return self.imageOnlyMessage;
  50. }
  51. - (IBAction)closeButtonClicked:(id)sender {
  52. [self dismissView:FIRInAppMessagingDismissTypeUserTapClose];
  53. }
  54. - (void)setupRecognizers {
  55. UITapGestureRecognizer *tapGestureRecognizer =
  56. [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(messageTapped:)];
  57. tapGestureRecognizer.delaysTouchesBegan = YES;
  58. tapGestureRecognizer.numberOfTapsRequired = 1;
  59. self.imageView.userInteractionEnabled = YES;
  60. [self.imageView addGestureRecognizer:tapGestureRecognizer];
  61. }
  62. - (void)messageTapped:(UITapGestureRecognizer *)recognizer {
  63. #pragma clang diagnostic push
  64. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  65. FIRInAppMessagingAction *action =
  66. [[FIRInAppMessagingAction alloc] initWithActionText:nil
  67. actionURL:self.imageOnlyMessage.actionURL];
  68. #pragma clang diagnostic pop
  69. [self followAction:action];
  70. }
  71. - (void)viewDidLoad {
  72. [super viewDidLoad];
  73. [self.view setBackgroundColor:[UIColor.grayColor colorWithAlphaComponent:0.5]];
  74. if (self.imageOnlyMessage.imageData) {
  75. UIImage *image = [UIImage imageWithData:self.imageOnlyMessage.imageData.imageRawData];
  76. self.imageOriginalSize = image.size;
  77. [self.imageView setImage:image];
  78. self.imageView.contentMode = UIViewContentModeScaleAspectFit;
  79. }
  80. [self setupRecognizers];
  81. }
  82. - (void)viewDidLayoutSubviews {
  83. [super viewDidLayoutSubviews];
  84. if (!self.imageOnlyMessage.imageData) {
  85. return;
  86. }
  87. // do the calculation in viewDidLayoutSubViews since self.view.window.frame is only
  88. // reliable at this time
  89. // Calculate the size of the image view under the constraints:
  90. // 1 Retain the image ratio
  91. // 2 Have at least 30 point of margines around four sides of the image view
  92. CGFloat minimalMargine = 30; // 30 points
  93. CGFloat maxImageViewWidth = self.view.window.frame.size.width - minimalMargine * 2;
  94. CGFloat maxImageViewHeight = self.view.window.frame.size.height - minimalMargine * 2;
  95. // Factor in space for the top notch on iPhone X*.
  96. #if defined(__IPHONE_11_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000
  97. if (@available(iOS 11.0, *)) {
  98. maxImageViewHeight -= self.view.safeAreaInsets.top;
  99. }
  100. #endif // defined(__IPHONE_11_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000
  101. CGFloat adjustedImageViewHeight = self.imageOriginalSize.height;
  102. CGFloat adjustedImageViewWidth = self.imageOriginalSize.width;
  103. if (adjustedImageViewWidth > maxImageViewWidth || adjustedImageViewHeight > maxImageViewHeight) {
  104. if (maxImageViewHeight / maxImageViewWidth >
  105. self.imageOriginalSize.height / self.imageOriginalSize.width) {
  106. // the image is relatively too wide compared against displayable area
  107. adjustedImageViewWidth = maxImageViewWidth;
  108. adjustedImageViewHeight =
  109. adjustedImageViewWidth * self.imageOriginalSize.height / self.imageOriginalSize.width;
  110. FIRLogDebug(kFIRLoggerInAppMessagingDisplay, @"I-FID110002",
  111. @"Use max available image display width as %lf", adjustedImageViewWidth);
  112. } else {
  113. // the image is relatively too narrow compared against displayable area
  114. adjustedImageViewHeight = maxImageViewHeight;
  115. adjustedImageViewWidth =
  116. adjustedImageViewHeight * self.imageOriginalSize.width / self.imageOriginalSize.height;
  117. FIRLogDebug(kFIRLoggerInAppMessagingDisplay, @"I-FID110003",
  118. @"Use max avilable image display height as %lf", adjustedImageViewHeight);
  119. }
  120. } else {
  121. // image can be rendered fully at its original size
  122. FIRLogDebug(kFIRLoggerInAppMessagingDisplay, @"I-FID110001",
  123. @"Image can be fully displayed in image only mode");
  124. }
  125. CGRect rect = CGRectMake(0, 0, adjustedImageViewWidth, adjustedImageViewHeight);
  126. self.imageView.frame = rect;
  127. self.imageView.center = self.view.center;
  128. CGFloat closeButtonCenterX = CGRectGetMaxX(self.imageView.frame);
  129. CGFloat closeButtonCenterY = CGRectGetMinY(self.imageView.frame);
  130. self.closeButton.center = CGPointMake(closeButtonCenterX, closeButtonCenterY);
  131. [self.view bringSubviewToFront:self.closeButton];
  132. }
  133. - (void)viewWillAppear:(BOOL)animated {
  134. [super viewWillAppear:animated];
  135. // close any potential keyboard, which would conflict with the modal in-app messagine view
  136. [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder)
  137. to:nil
  138. from:nil
  139. forEvent:nil];
  140. if (self.imageOnlyMessage.campaignInfo.renderAsTestMessage) {
  141. FIRLogDebug(kFIRLoggerInAppMessagingDisplay, @"I-FID110004",
  142. @"Flashing the close button since this is a test message.");
  143. [self flashCloseButton:self.closeButton];
  144. }
  145. }
  146. - (void)flashCloseButton:(UIButton *)closeButton {
  147. closeButton.alpha = 1.0f;
  148. [UIView animateWithDuration:2.0
  149. delay:0.0
  150. options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionRepeat |
  151. UIViewAnimationOptionAutoreverse |
  152. UIViewAnimationOptionAllowUserInteraction
  153. animations:^{
  154. closeButton.alpha = 0.1f;
  155. }
  156. completion:^(BOOL finished){
  157. // Do nothing
  158. }];
  159. }
  160. @end