FIDModalViewController.m 21 KB

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