FIDModalViewController.m 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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 <UIKit/UIKit.h>
  17. #import "FIDModalViewController.h"
  18. #import "FIRCore+InAppMessagingDisplay.h"
  19. @interface FIDModalViewController ()
  20. @property(nonatomic, readwrite) FIRInAppMessagingModalDisplay *modalDisplayMessage;
  21. @property(weak, nonatomic) IBOutlet UIImageView *imageView;
  22. @property(weak, nonatomic) IBOutlet UILabel *titleLabel;
  23. @property(weak, nonatomic) IBOutlet UIButton *actionButton;
  24. @property(weak, nonatomic) IBOutlet UIView *messageCardView;
  25. @property(weak, nonatomic) IBOutlet UITextView *bodyTextView;
  26. @property(weak, nonatomic) IBOutlet UIButton *closeButton;
  27. // this is only needed for removing the layout errors in interface builder. At runtime
  28. // we determine the height via its content size. So disable this at runtime.
  29. @property(weak, nonatomic) IBOutlet NSLayoutConstraint *fixedMessageCardHeightConstraint;
  30. @property(weak, nonatomic) IBOutlet NSLayoutConstraint *messageCardHeightMaxInTabletCase;
  31. @property(weak, nonatomic) IBOutlet NSLayoutConstraint *maxActionButtonHeight;
  32. @property(weak, nonatomic) IBOutlet NSLayoutConstraint *bodyTextViewHeightConstraint;
  33. @property(weak, nonatomic) IBOutlet NSLayoutConstraint *buttonTopToBodyBottomConstraint;
  34. @property(weak, nonatomic) IBOutlet NSLayoutConstraint *imageActualHeightConstraint;
  35. // constraints manipulated further in portrait mode
  36. @property(weak, nonatomic) IBOutlet NSLayoutConstraint *titleLabelHeightConstraint;
  37. @property(weak, nonatomic) IBOutlet NSLayoutConstraint *buttonBottomToContainerBottomInPortraitMode;
  38. @property(weak, nonatomic) IBOutlet NSLayoutConstraint *imageTopToTitleBottomInPortraitMode;
  39. // constraints manipulated further in landscape mode
  40. @property(weak, nonatomic) IBOutlet NSLayoutConstraint *imageWidthInLandscapeMode;
  41. @property(weak, nonatomic) IBOutlet NSLayoutConstraint *titleTopToCardViewTop;
  42. @property(weak, nonatomic) IBOutlet NSLayoutConstraint *cardLeadingMarginInLandscapeMode;
  43. @property(weak, nonatomic) IBOutlet NSLayoutConstraint *maxCardHeightInLandscapeMode;
  44. @property(weak, nonatomic) IBOutlet NSLayoutConstraint *imageTopToCardTopInLandscapeMode;
  45. @property(weak, nonatomic) IBOutlet NSLayoutConstraint *bodyTopToTitleBottomInLandScapeMode;
  46. @end
  47. static CGFloat VerticalSpacingBetweenTitleAndBody = 24;
  48. static CGFloat VerticalSpacingBetweenBodyAndActionButton = 24;
  49. // the padding between the content and view card's top and bottom edges
  50. static CGFloat TopBottomPaddingAroundContent = 24;
  51. // the minimal padding size between msg card and app window's top and bottom
  52. static CGFloat TopBottomPaddingAroundMsgCard = 30;
  53. // the horizontal spacing between image column and text/button column in landscape mode
  54. static CGFloat LandScapePaddingBetweenImageAndTextColumn = 24;
  55. @implementation FIDModalViewController
  56. + (FIDModalViewController *)
  57. instantiateViewControllerWithResourceBundle:(NSBundle *)resourceBundle
  58. displayMessage:(FIRInAppMessagingModalDisplay *)modalMessage
  59. displayDelegate:
  60. (id<FIRInAppMessagingDisplayDelegate>)displayDelegate
  61. timeFetcher:(id<FIDTimeFetcher>)timeFetcher {
  62. UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"FIRInAppMessageDisplayStoryboard"
  63. bundle:resourceBundle];
  64. if (storyboard == nil) {
  65. FIRLogError(kFIRLoggerInAppMessagingDisplay, @"I-FID300001",
  66. @"Storyboard '"
  67. "FIRInAppMessageDisplayStoryboard' not found in bundle %@",
  68. resourceBundle);
  69. return nil;
  70. }
  71. FIDModalViewController *modalVC = (FIDModalViewController *)[storyboard
  72. instantiateViewControllerWithIdentifier:@"modal-view-vc"];
  73. modalVC.displayDelegate = displayDelegate;
  74. modalVC.modalDisplayMessage = modalMessage;
  75. modalVC.timeFetcher = timeFetcher;
  76. return modalVC;
  77. }
  78. - (FIRInAppMessagingDisplayMessage *)inAppMessage {
  79. return self.modalDisplayMessage;
  80. }
  81. - (IBAction)closeButtonClicked:(id)sender {
  82. [self dismissView:FIRInAppMessagingDismissTypeUserTapClose];
  83. }
  84. - (IBAction)actionButtonTapped:(id)sender {
  85. #pragma clang diagnostic push
  86. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  87. FIRInAppMessagingAction *action = [[FIRInAppMessagingAction alloc]
  88. initWithActionText:self.modalDisplayMessage.actionButton.buttonText
  89. actionURL:self.modalDisplayMessage.actionURL];
  90. #pragma clang diagnostic pop
  91. [self followAction:action];
  92. }
  93. - (void)viewDidLoad {
  94. [super viewDidLoad];
  95. // make the background half transparent
  96. [self.view setBackgroundColor:[UIColor.grayColor colorWithAlphaComponent:0.5]];
  97. // populating values for display elements
  98. self.titleLabel.text = self.modalDisplayMessage.title;
  99. self.bodyTextView.text = self.modalDisplayMessage.bodyText;
  100. if (self.modalDisplayMessage.imageData) {
  101. [self.imageView
  102. setImage:[UIImage imageWithData:self.modalDisplayMessage.imageData.imageRawData]];
  103. self.imageView.contentMode = UIViewContentModeScaleAspectFit;
  104. }
  105. self.messageCardView.backgroundColor = self.modalDisplayMessage.displayBackgroundColor;
  106. self.titleLabel.textColor = self.modalDisplayMessage.textColor;
  107. self.bodyTextView.textColor = self.modalDisplayMessage.textColor;
  108. self.bodyTextView.selectable = NO;
  109. if (self.modalDisplayMessage.actionButton.buttonText.length != 0) {
  110. [self.actionButton setTitle:self.modalDisplayMessage.actionButton.buttonText
  111. forState:UIControlStateNormal];
  112. self.actionButton.backgroundColor = self.modalDisplayMessage.actionButton.buttonBackgroundColor;
  113. [self.actionButton setTitleColor:self.modalDisplayMessage.actionButton.buttonTextColor
  114. forState:UIControlStateNormal];
  115. self.actionButton.layer.cornerRadius = 4;
  116. if (self.modalDisplayMessage.bodyText.length == 0) {
  117. self.buttonTopToBodyBottomConstraint.constant = 0;
  118. }
  119. } else {
  120. // either action button text is empty or nil
  121. // hide the action button and reclaim the space below the buttom
  122. FIRLogDebug(kFIRLoggerInAppMessagingDisplay, @"I-FID300002",
  123. @"Modal view to be rendered without action button");
  124. self.maxActionButtonHeight.constant = 0;
  125. self.actionButton.clipsToBounds = YES;
  126. self.buttonTopToBodyBottomConstraint.constant = 0;
  127. }
  128. [self.view addConstraint:self.imageActualHeightConstraint];
  129. self.imageActualHeightConstraint.active = YES;
  130. self.fixedMessageCardHeightConstraint.active = NO;
  131. }
  132. // for text display UIview, which could be a UILabel or UITextView, decide the fit height under a
  133. // given display width
  134. - (CGFloat)determineTextAreaViewFitHeightForView:(UIView *)textView
  135. withWidth:(CGFloat)displayWidth {
  136. CGSize displaySize = CGSizeMake(displayWidth, FLT_MAX);
  137. return [textView sizeThatFits:displaySize].height;
  138. }
  139. // In both landscape or portrait mode, the title, body & button are aligned vertically and they form
  140. // together have an impact on the height for that column. Many times, we need to calculate a
  141. // suitable heights for them to help decide the layout. The height calculation is influced by quite
  142. // a few factors: the text lenght of title and body, the presence/absense of body & button and
  143. // available card/window sizes. So these are wrapped within
  144. // estimateTextButtomColumnHeightWithDisplayWidth which produce a TitleBodyButtonHeightInfo struct
  145. // to give the estimates of the heights of different elements.
  146. struct TitleBodyButtonHeightInfo {
  147. CGFloat titleHeight;
  148. CGFloat bodyHeight;
  149. // this is the total height of title plus body plus the button. Notice that button or body are
  150. // optional and the result totaColumnlHeight factor in these cases correctly
  151. CGFloat totaColumnlHeight;
  152. };
  153. - (struct TitleBodyButtonHeightInfo)estimateTextBtnColumnHeightWithDisplayWidth:
  154. (CGFloat)displayWidth
  155. withMaxColumnHeight:(CGFloat)maxHeight {
  156. struct TitleBodyButtonHeightInfo resultHeightInfo;
  157. CGFloat titleFitHeight = [self determineTextAreaViewFitHeightForView:self.titleLabel
  158. withWidth:displayWidth];
  159. CGFloat bodyFitHeight = self.modalDisplayMessage.bodyText.length == 0
  160. ? 0
  161. : [self determineTextAreaViewFitHeightForView:self.bodyTextView
  162. withWidth:displayWidth];
  163. CGFloat bodyFitHeightWithPadding = self.modalDisplayMessage.bodyText.length == 0
  164. ? 0
  165. : bodyFitHeight + VerticalSpacingBetweenTitleAndBody;
  166. CGFloat buttonHeight =
  167. self.modalDisplayMessage.actionButton == nil
  168. ? 0
  169. : self.actionButton.frame.size.height + VerticalSpacingBetweenBodyAndActionButton;
  170. // we keep the spacing even if body or button is absent.
  171. CGFloat fitColumnHeight = titleFitHeight + bodyFitHeightWithPadding + buttonHeight;
  172. if (fitColumnHeight < maxHeight) {
  173. // every element get space that can fit the content
  174. resultHeightInfo.bodyHeight = bodyFitHeight;
  175. resultHeightInfo.titleHeight = titleFitHeight;
  176. resultHeightInfo.totaColumnlHeight = fitColumnHeight;
  177. } else {
  178. // need to restrict heights of certain elements
  179. resultHeightInfo.totaColumnlHeight = maxHeight;
  180. if (self.modalDisplayMessage.bodyText.length == 0) {
  181. // no message body, title will try to expand to take all the available height
  182. resultHeightInfo.bodyHeight = 0;
  183. if (self.modalDisplayMessage.actionButton == nil) {
  184. resultHeightInfo.titleHeight = maxHeight;
  185. } else {
  186. // button height, if not 0, already accommodates the space above it
  187. resultHeightInfo.titleHeight = maxHeight - buttonHeight;
  188. }
  189. } else {
  190. // first give title up to 40% of available height
  191. resultHeightInfo.titleHeight = fmin(titleFitHeight, maxHeight * 2 / 5);
  192. CGFloat availableBodyHeight = 0;
  193. if (self.modalDisplayMessage.actionButton == nil) {
  194. availableBodyHeight =
  195. maxHeight - resultHeightInfo.titleHeight - VerticalSpacingBetweenTitleAndBody;
  196. } else {
  197. // body takes the rest minus button space
  198. availableBodyHeight = maxHeight - resultHeightInfo.titleHeight - buttonHeight -
  199. VerticalSpacingBetweenTitleAndBody;
  200. }
  201. if (availableBodyHeight > bodyFitHeight) {
  202. resultHeightInfo.bodyHeight = bodyFitHeight;
  203. // give some back to title height since body does not use up all the allocation
  204. resultHeightInfo.titleHeight += (availableBodyHeight - bodyFitHeight);
  205. } else {
  206. resultHeightInfo.bodyHeight = availableBodyHeight;
  207. }
  208. }
  209. }
  210. FIRLogDebug(kFIRLoggerInAppMessagingDisplay, @"I-FID300003",
  211. @"In heights calculation (max-height = %lf, width = %lf), title heights is %lf, "
  212. "body height is %lf, button height is %lf, total column heights are %lf",
  213. maxHeight, displayWidth, resultHeightInfo.titleHeight, resultHeightInfo.bodyHeight,
  214. buttonHeight, resultHeightInfo.totaColumnlHeight);
  215. return resultHeightInfo;
  216. }
  217. // the following two layoutFineTunexx methods make additional adjustments for the view layout
  218. // in portrait and landscape mode respectively. They are supposed to be triggered from
  219. // viewDidLayoutSubviews since certain dimension sizes are only available there
  220. - (void)layoutFineTuneInPortraitMode {
  221. // for tablet case, since we use a fixed card height, the reference would be just the card height
  222. // for non-tablet case, we want to use a dynamic height , so the reference would be the window
  223. // height
  224. CGFloat heightCalcReference =
  225. self.messageCardHeightMaxInTabletCase.active
  226. ? self.messageCardView.frame.size.height - TopBottomPaddingAroundContent * 2
  227. : self.view.window.frame.size.height - TopBottomPaddingAroundContent * 2 -
  228. TopBottomPaddingAroundMsgCard * 2;
  229. FIRLogDebug(kFIRLoggerInAppMessagingDisplay, @"I-FID300004",
  230. @"The height calc reference is %lf "
  231. "with frame height as %lf",
  232. heightCalcReference, self.view.window.frame.size.height);
  233. // this makes sure titleLable gets correct width to be ready for later's height estimate for the
  234. // text & button column
  235. [self.messageCardView layoutIfNeeded];
  236. // we reserve approximately 1/3 vertical space for image
  237. CGFloat textBtnTotalAvailableHeight =
  238. self.modalDisplayMessage.imageData ? heightCalcReference * 2 / 3 : heightCalcReference;
  239. struct TitleBodyButtonHeightInfo heights =
  240. [self estimateTextBtnColumnHeightWithDisplayWidth:self.titleLabel.frame.size.width
  241. withMaxColumnHeight:textBtnTotalAvailableHeight];
  242. self.titleLabelHeightConstraint.constant = heights.titleHeight;
  243. self.bodyTextViewHeightConstraint.constant = heights.bodyHeight;
  244. if (self.modalDisplayMessage.imageData) {
  245. UIImage *image = [UIImage imageWithData:self.modalDisplayMessage.imageData.imageRawData];
  246. CGSize imageAvailableSpace = CGSizeMake(self.titleLabel.frame.size.width,
  247. heightCalcReference - heights.totaColumnlHeight -
  248. self.imageTopToTitleBottomInPortraitMode.constant);
  249. CGSize imageDisplaySize = [self fitImageInRegionSize:imageAvailableSpace
  250. withImageSize:image.size];
  251. FIRLogDebug(kFIRLoggerInAppMessagingDisplay, @"I-FID300005",
  252. @"Given actual image size %@ and available image display size %@, the actual"
  253. "image display size is %@",
  254. NSStringFromCGSize(image.size), NSStringFromCGSize(imageAvailableSpace),
  255. NSStringFromCGSize(imageDisplaySize));
  256. // for portrait mode, no need to change image width since no content is shown side to
  257. // the image
  258. self.imageActualHeightConstraint.constant = imageDisplaySize.height;
  259. } else {
  260. // no image case
  261. self.imageActualHeightConstraint.constant = 0;
  262. self.imageTopToTitleBottomInPortraitMode.constant = 0;
  263. }
  264. }
  265. - (CGSize)fitImageInRegionSize:(CGSize)regionSize withImageSize:(CGSize)imageSize {
  266. if (imageSize.height <= regionSize.height && imageSize.width <= regionSize.width) {
  267. return imageSize; // image can be fully rendered at its original dimension
  268. } else {
  269. CGFloat regionRatio = regionSize.width / regionSize.height;
  270. CGFloat imageRaio = imageSize.width / imageSize.height;
  271. if (regionRatio < imageRaio) {
  272. // bound on the width dimension
  273. return CGSizeMake(regionSize.width, regionSize.width / imageRaio);
  274. } else {
  275. return CGSizeMake(regionSize.height * imageRaio, regionSize.height);
  276. }
  277. }
  278. }
  279. // for devices of 4 inches or below (iphone se, iphone 5/5s and iphone 4s), reduce
  280. // the padding sizes between elements in the text/button column for landscape mode
  281. - (void)applySmallerSpacingForInLandscapeMode {
  282. if (self.modalDisplayMessage.bodyText.length != 0) {
  283. VerticalSpacingBetweenTitleAndBody = self.bodyTopToTitleBottomInLandScapeMode.constant = 12;
  284. }
  285. if (self.modalDisplayMessage.actionButton != nil &&
  286. self.modalDisplayMessage.bodyText.length != 0) {
  287. VerticalSpacingBetweenBodyAndActionButton = self.buttonTopToBodyBottomConstraint.constant = 12;
  288. }
  289. }
  290. - (void)layoutFineTuneInLandscapeMode {
  291. // smaller spacing threshold is applied for screens equal or larger than 4.7 inches
  292. if (self.view.window.frame.size.height <= 321) {
  293. [self applySmallerSpacingForInLandscapeMode];
  294. }
  295. if (self.modalDisplayMessage.imageData) {
  296. UIImage *image = [UIImage imageWithData:self.modalDisplayMessage.imageData.imageRawData];
  297. CGFloat maxImageHeight = self.view.window.frame.size.height -
  298. TopBottomPaddingAroundContent * 2 - TopBottomPaddingAroundMsgCard * 2;
  299. CGFloat maxImageWidth = self.messageCardView.frame.size.width * 2 / 5;
  300. CGSize imageDisplaySize = [self fitImageInRegionSize:CGSizeMake(maxImageWidth, maxImageHeight)
  301. withImageSize:image.size];
  302. FIRLogDebug(kFIRLoggerInAppMessagingDisplay, @"I-FID300008",
  303. @"In landscape mode, image fit size is %@", NSStringFromCGSize(imageDisplaySize));
  304. // resize image per imageSize
  305. self.imageWidthInLandscapeMode.constant = imageDisplaySize.width;
  306. self.imageActualHeightConstraint.constant = imageDisplaySize.height;
  307. // now we can estimate the new card width given the desired image size
  308. // this assumes we use half of the window width for diplaying the text/button column
  309. CGFloat cardFitWidth = imageDisplaySize.width + self.view.window.frame.size.width / 2 +
  310. LandScapePaddingBetweenImageAndTextColumn;
  311. self.cardLeadingMarginInLandscapeMode.constant =
  312. fmax(15, (self.view.window.frame.size.width - cardFitWidth) / 2);
  313. } else {
  314. self.imageWidthInLandscapeMode.constant = 0;
  315. self.imageActualHeightConstraint.constant = 0;
  316. // card would be of 3/5 width of the screen in landscape
  317. self.cardLeadingMarginInLandscapeMode.constant = self.view.window.frame.size.width / 5;
  318. }
  319. // this makes sure titleLable gets correct width to be ready for later's height estimate for the
  320. // text & button column
  321. [self.messageCardView layoutIfNeeded];
  322. struct TitleBodyButtonHeightInfo heights =
  323. [self estimateTextBtnColumnHeightWithDisplayWidth:self.titleLabel.frame.size.width
  324. withMaxColumnHeight:self.view.frame.size.height -
  325. TopBottomPaddingAroundContent * 2 -
  326. TopBottomPaddingAroundMsgCard * 2];
  327. self.titleLabelHeightConstraint.constant = heights.titleHeight;
  328. self.bodyTextViewHeightConstraint.constant = heights.bodyHeight;
  329. // Adjust the height of the card
  330. // are we bound by the text/button column height or image height ?
  331. CGFloat cardHeight = fmax(self.imageActualHeightConstraint.constant, heights.totaColumnlHeight) +
  332. TopBottomPaddingAroundContent * 2;
  333. self.maxCardHeightInLandscapeMode.constant = cardHeight;
  334. // with the new card height, align the image and the text/btn column to center vertically
  335. self.imageTopToCardTopInLandscapeMode.constant =
  336. (cardHeight - self.imageActualHeightConstraint.constant) / 2;
  337. self.titleTopToCardViewTop.constant = (cardHeight - heights.totaColumnlHeight) / 2;
  338. }
  339. - (void)viewDidLayoutSubviews {
  340. [super viewDidLayoutSubviews];
  341. if (self.traitCollection.horizontalSizeClass == UIUserInterfaceSizeClassRegular ||
  342. self.traitCollection.verticalSizeClass == UIUserInterfaceSizeClassCompact) {
  343. FIRLogDebug(kFIRLoggerInAppMessagingDisplay, @"I-FID300010",
  344. @"Modal view rendered in landscape mode");
  345. [self layoutFineTuneInLandscapeMode];
  346. } else {
  347. FIRLogDebug(kFIRLoggerInAppMessagingDisplay, @"I-FID300009",
  348. @"Modal view rendered in portrait mode");
  349. [self layoutFineTuneInPortraitMode];
  350. }
  351. // always scroll to the top in case the body area is scrollable
  352. [self.bodyTextView setContentOffset:CGPointZero];
  353. }
  354. - (void)viewWillAppear:(BOOL)animated {
  355. [super viewWillAppear:animated];
  356. // close any potential keyboard, which would conflict with the modal in-app messagine view
  357. [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder)
  358. to:nil
  359. from:nil
  360. forEvent:nil];
  361. if (self.modalDisplayMessage.campaignInfo.renderAsTestMessage) {
  362. FIRLogDebug(kFIRLoggerInAppMessagingDisplay, @"I-FID300011",
  363. @"Flushing the close button since this is a test message.");
  364. [self flashCloseButton:self.closeButton];
  365. }
  366. }
  367. - (void)flashCloseButton:(UIButton *)closeButton {
  368. closeButton.alpha = 1.0f;
  369. [UIView animateWithDuration:2.0
  370. delay:0.0
  371. options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionRepeat |
  372. UIViewAnimationOptionAutoreverse |
  373. UIViewAnimationOptionAllowUserInteraction
  374. animations:^{
  375. closeButton.alpha = 0.1f;
  376. }
  377. completion:^(BOOL finished){
  378. // Do nothing
  379. }];
  380. }
  381. @end