FIDImageOnlyViewController.m 6.9 KB

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