FIDCardViewController.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright 2019 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 "FIDCardViewController.h"
  17. #import "FIRCore+InAppMessagingDisplay.h"
  18. @interface FIDCardViewController ()
  19. @property(nonatomic, readwrite) FIRInAppMessagingCardDisplay *cardDisplayMessage;
  20. @property(weak, nonatomic) IBOutlet UIView *cardView;
  21. @property(weak, nonatomic) IBOutlet UIImageView *imageView;
  22. @property(weak, nonatomic) IBOutlet UILabel *titleLabel;
  23. @property(weak, nonatomic) IBOutlet UIButton *primaryActionButton;
  24. @property(weak, nonatomic) IBOutlet UIButton *secondaryActionButton;
  25. @property(weak, nonatomic) IBOutlet UITextView *bodyTextView;
  26. @property(weak, nonatomic) IBOutlet UIScrollView *textAreaScrollView;
  27. @end
  28. @implementation FIDCardViewController
  29. + (FIDCardViewController *)
  30. instantiateViewControllerWithResourceBundle:(NSBundle *)resourceBundle
  31. displayMessage:(FIRInAppMessagingCardDisplay *)cardMessage
  32. displayDelegate:
  33. (id<FIRInAppMessagingDisplayDelegate>)displayDelegate
  34. timeFetcher:(id<FIDTimeFetcher>)timeFetcher {
  35. UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"FIRInAppMessageDisplayStoryboard"
  36. bundle:resourceBundle];
  37. if (!storyboard) {
  38. FIRLogError(kFIRLoggerInAppMessagingDisplay, @"I-FID300001",
  39. @"Storyboard '"
  40. "FIRInAppMessageDisplayStoryboard' not found in bundle %@",
  41. resourceBundle);
  42. return nil;
  43. }
  44. FIDCardViewController *cardVC =
  45. (FIDCardViewController *)[storyboard instantiateViewControllerWithIdentifier:@"card-view-vc"];
  46. cardVC.displayDelegate = displayDelegate;
  47. cardVC.cardDisplayMessage = cardMessage;
  48. cardVC.timeFetcher = timeFetcher;
  49. return cardVC;
  50. }
  51. - (IBAction)primaryActionButtonTapped:(id)sender {
  52. if (self.cardDisplayMessage.primaryActionURL) {
  53. FIRInAppMessagingAction *primaryAction = [[FIRInAppMessagingAction alloc]
  54. initWithActionText:self.cardDisplayMessage.primaryActionButton.buttonText
  55. actionURL:self.cardDisplayMessage.primaryActionURL];
  56. [self followAction:primaryAction];
  57. } else {
  58. [self dismissView:FIRInAppMessagingDismissTypeUserTapClose];
  59. }
  60. }
  61. - (IBAction)secondaryActionButtonTapped:(id)sender {
  62. if (self.cardDisplayMessage.secondaryActionURL) {
  63. FIRInAppMessagingAction *secondaryAction = [[FIRInAppMessagingAction alloc]
  64. initWithActionText:self.cardDisplayMessage.secondaryActionButton.buttonText
  65. actionURL:self.cardDisplayMessage.secondaryActionURL];
  66. [self followAction:secondaryAction];
  67. } else {
  68. [self dismissView:FIRInAppMessagingDismissTypeUserTapClose];
  69. }
  70. }
  71. - (FIRInAppMessagingDisplayMessage *)inAppMessage {
  72. return self.cardDisplayMessage;
  73. }
  74. - (void)viewDidLoad {
  75. [super viewDidLoad];
  76. self.cardView.backgroundColor = self.cardDisplayMessage.displayBackgroundColor;
  77. self.bodyTextView.contentInset = UIEdgeInsetsZero;
  78. self.bodyTextView.textContainer.lineFragmentPadding = 0;
  79. // Make the background half transparent.
  80. [self.view setBackgroundColor:[UIColor.grayColor colorWithAlphaComponent:0.5]];
  81. self.titleLabel.text = self.cardDisplayMessage.title;
  82. self.titleLabel.textColor = self.cardDisplayMessage.textColor;
  83. self.bodyTextView.text = self.cardDisplayMessage.body;
  84. [self.primaryActionButton setTitle:self.cardDisplayMessage.primaryActionButton.buttonText
  85. forState:UIControlStateNormal];
  86. [self.primaryActionButton
  87. setTitleColor:self.cardDisplayMessage.primaryActionButton.buttonTextColor
  88. forState:UIControlStateNormal];
  89. if (self.cardDisplayMessage.secondaryActionButton) {
  90. self.secondaryActionButton.hidden = NO;
  91. [self.secondaryActionButton setTitle:self.cardDisplayMessage.secondaryActionButton.buttonText
  92. forState:UIControlStateNormal];
  93. [self.secondaryActionButton
  94. setTitleColor:self.cardDisplayMessage.secondaryActionButton.buttonTextColor
  95. forState:UIControlStateNormal];
  96. }
  97. }
  98. - (void)viewDidLayoutSubviews {
  99. [super viewDidLayoutSubviews];
  100. // The landscape image is optional and only displayed if:
  101. // 1. Landscape image exists.
  102. // 2. The iOS device is in "landscape" mode (regular width or compact height).
  103. if (self.traitCollection.horizontalSizeClass == UIUserInterfaceSizeClassRegular ||
  104. self.traitCollection.verticalSizeClass == UIUserInterfaceSizeClassCompact) {
  105. NSData *imageData = self.cardDisplayMessage.landscapeImageData
  106. ? self.cardDisplayMessage.landscapeImageData.imageRawData
  107. : self.cardDisplayMessage.portraitImageData.imageRawData;
  108. self.imageView.image = [UIImage imageWithData:imageData];
  109. } else {
  110. self.imageView.image =
  111. [UIImage imageWithData:self.cardDisplayMessage.portraitImageData.imageRawData];
  112. }
  113. self.textAreaScrollView.contentSize = self.bodyTextView.frame.size;
  114. [self.textAreaScrollView setContentOffset:CGPointZero];
  115. }
  116. @end