FIDImageOnlyViewController.m 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. CGFloat adjustedImageViewHeight = self.imageOriginalSize.height;
  96. CGFloat adjustedImageViewWidth = self.imageOriginalSize.width;
  97. if (adjustedImageViewWidth > maxImageViewWidth || adjustedImageViewHeight > maxImageViewHeight) {
  98. if (maxImageViewHeight / maxImageViewWidth >
  99. self.imageOriginalSize.height / self.imageOriginalSize.width) {
  100. // the image is relatively too wide compared against displayable area
  101. adjustedImageViewWidth = maxImageViewWidth;
  102. adjustedImageViewHeight =
  103. adjustedImageViewWidth * self.imageOriginalSize.height / self.imageOriginalSize.width;
  104. FIRLogDebug(kFIRLoggerInAppMessagingDisplay, @"I-FID110002",
  105. @"Use max available image display width as %lf", adjustedImageViewWidth);
  106. } else {
  107. // the image is relatively too narrow compared against displayable area
  108. adjustedImageViewHeight = maxImageViewHeight;
  109. adjustedImageViewWidth =
  110. adjustedImageViewHeight * self.imageOriginalSize.width / self.imageOriginalSize.height;
  111. FIRLogDebug(kFIRLoggerInAppMessagingDisplay, @"I-FID110003",
  112. @"Use max avilable image display height as %lf", adjustedImageViewHeight);
  113. }
  114. } else {
  115. // image can be rendered fully at its original size
  116. FIRLogDebug(kFIRLoggerInAppMessagingDisplay, @"I-FID110001",
  117. @"Image can be fully displayed in image only mode");
  118. }
  119. CGRect rect = CGRectMake(0, 0, adjustedImageViewWidth, adjustedImageViewHeight);
  120. self.imageView.frame = rect;
  121. self.imageView.center = self.view.center;
  122. CGFloat closeButtonCenterX = CGRectGetMaxX(self.imageView.frame);
  123. CGFloat closeButtonCenterY = CGRectGetMinY(self.imageView.frame);
  124. self.closeButton.center = CGPointMake(closeButtonCenterX, closeButtonCenterY);
  125. [self.view bringSubviewToFront:self.closeButton];
  126. }
  127. - (void)viewWillAppear:(BOOL)animated {
  128. [super viewWillAppear:animated];
  129. // close any potential keyboard, which would conflict with the modal in-app messagine view
  130. [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder)
  131. to:nil
  132. from:nil
  133. forEvent:nil];
  134. if (self.imageOnlyMessage.campaignInfo.renderAsTestMessage) {
  135. FIRLogDebug(kFIRLoggerInAppMessagingDisplay, @"I-FID110004",
  136. @"Flashing the close button since this is a test message.");
  137. [self flashCloseButton:self.closeButton];
  138. }
  139. }
  140. - (void)flashCloseButton:(UIButton *)closeButton {
  141. closeButton.alpha = 1.0f;
  142. [UIView animateWithDuration:2.0
  143. delay:0.0
  144. options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionRepeat |
  145. UIViewAnimationOptionAutoreverse |
  146. UIViewAnimationOptionAllowUserInteraction
  147. animations:^{
  148. closeButton.alpha = 0.1f;
  149. }
  150. completion:^(BOOL finished){
  151. // Do nothing
  152. }];
  153. }
  154. @end