FIDModalViewController.m 21 KB

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