FIDImageOnlyViewController.m 7.1 KB

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