FIRIAMImageOnlyViewController.m 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. if (self.imageOnlyMessage.imageData) {
  77. UIImage *image = [UIImage imageWithData:self.imageOnlyMessage.imageData.imageRawData];
  78. self.imageOriginalSize = image.size;
  79. [self.imageView setImage:image];
  80. self.imageView.contentMode = UIViewContentModeScaleAspectFit;
  81. }
  82. [self setupRecognizers];
  83. }
  84. - (void)viewDidLayoutSubviews {
  85. [super viewDidLayoutSubviews];
  86. if (!self.imageOnlyMessage.imageData) {
  87. return;
  88. }
  89. // do the calculation in viewDidLayoutSubViews since self.view.window.frame is only
  90. // reliable at this time
  91. // Calculate the size of the image view under the constraints:
  92. // 1 Retain the image ratio
  93. // 2 Have at least 30 point of margines around four sides of the image view
  94. CGFloat minimalMargine = 30; // 30 points
  95. CGFloat maxImageViewWidth = self.view.window.frame.size.width - minimalMargine * 2;
  96. CGFloat maxImageViewHeight = self.view.window.frame.size.height - minimalMargine * 2;
  97. // Factor in space for the top notch on iPhone X*.
  98. #if defined(__IPHONE_11_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000
  99. if (@available(iOS 11.0, *)) {
  100. maxImageViewHeight -= self.view.safeAreaInsets.top;
  101. }
  102. #endif // defined(__IPHONE_11_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000
  103. CGFloat adjustedImageViewHeight = self.imageOriginalSize.height;
  104. CGFloat adjustedImageViewWidth = self.imageOriginalSize.width;
  105. if (adjustedImageViewWidth > maxImageViewWidth || adjustedImageViewHeight > maxImageViewHeight) {
  106. if (maxImageViewHeight / maxImageViewWidth >
  107. self.imageOriginalSize.height / self.imageOriginalSize.width) {
  108. // the image is relatively too wide compared against displayable area
  109. adjustedImageViewWidth = maxImageViewWidth;
  110. adjustedImageViewHeight =
  111. adjustedImageViewWidth * self.imageOriginalSize.height / self.imageOriginalSize.width;
  112. FIRLogDebug(kFIRLoggerInAppMessagingDisplay, @"I-FID110002",
  113. @"Use max available image display width as %lf", adjustedImageViewWidth);
  114. } else {
  115. // the image is relatively too narrow compared against displayable area
  116. adjustedImageViewHeight = maxImageViewHeight;
  117. adjustedImageViewWidth =
  118. adjustedImageViewHeight * self.imageOriginalSize.width / self.imageOriginalSize.height;
  119. FIRLogDebug(kFIRLoggerInAppMessagingDisplay, @"I-FID110003",
  120. @"Use max avilable image display height as %lf", adjustedImageViewHeight);
  121. }
  122. } else {
  123. // image can be rendered fully at its original size
  124. FIRLogDebug(kFIRLoggerInAppMessagingDisplay, @"I-FID110001",
  125. @"Image can be fully displayed in image only mode");
  126. }
  127. CGRect rect = CGRectMake(0, 0, adjustedImageViewWidth, adjustedImageViewHeight);
  128. self.imageView.frame = rect;
  129. self.imageView.center = self.view.center;
  130. CGFloat closeButtonCenterX = CGRectGetMaxX(self.imageView.frame);
  131. CGFloat closeButtonCenterY = CGRectGetMinY(self.imageView.frame);
  132. self.closeButton.center = CGPointMake(closeButtonCenterX, closeButtonCenterY);
  133. [self.view bringSubviewToFront:self.closeButton];
  134. }
  135. - (void)viewWillAppear:(BOOL)animated {
  136. [super viewWillAppear:animated];
  137. // close any potential keyboard, which would conflict with the modal in-app messagine view
  138. [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder)
  139. to:nil
  140. from:nil
  141. forEvent:nil];
  142. if (self.imageOnlyMessage.campaignInfo.renderAsTestMessage) {
  143. FIRLogDebug(kFIRLoggerInAppMessagingDisplay, @"I-FID110004",
  144. @"Flashing the close button since this is a test message.");
  145. [self flashCloseButton:self.closeButton];
  146. }
  147. }
  148. - (void)flashCloseButton:(UIButton *)closeButton {
  149. closeButton.alpha = 1.0f;
  150. [UIView animateWithDuration:2.0
  151. delay:0.0
  152. options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionRepeat |
  153. UIViewAnimationOptionAutoreverse |
  154. UIViewAnimationOptionAllowUserInteraction
  155. animations:^{
  156. closeButton.alpha = 0.1f;
  157. }
  158. completion:^(BOOL finished){
  159. // Do nothing
  160. }];
  161. }
  162. @end
  163. #endif // TARGET_OS_IOS