FIRIAMCardViewController.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 "FIRIAMCardViewController.h"
  17. #import "FIRCore+InAppMessagingDisplay.h"
  18. @interface FIRIAMCardViewController ()
  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 FIRIAMCardViewController
  29. + (FIRIAMCardViewController *)
  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. FIRIAMCardViewController *cardVC = (FIRIAMCardViewController *)[storyboard
  45. 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. #pragma clang diagnostic push
  54. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  55. FIRInAppMessagingAction *primaryAction = [[FIRInAppMessagingAction alloc]
  56. initWithActionText:self.cardDisplayMessage.primaryActionButton.buttonText
  57. actionURL:self.cardDisplayMessage.primaryActionURL];
  58. #pragma clang diagnostic pop
  59. [self followAction:primaryAction];
  60. } else {
  61. [self dismissView:FIRInAppMessagingDismissTypeUserTapClose];
  62. }
  63. }
  64. - (IBAction)secondaryActionButtonTapped:(id)sender {
  65. if (self.cardDisplayMessage.secondaryActionURL) {
  66. #pragma clang diagnostic push
  67. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  68. FIRInAppMessagingAction *secondaryAction = [[FIRInAppMessagingAction alloc]
  69. initWithActionText:self.cardDisplayMessage.secondaryActionButton.buttonText
  70. actionURL:self.cardDisplayMessage.secondaryActionURL];
  71. #pragma clang diagnostic pop
  72. [self followAction:secondaryAction];
  73. } else {
  74. [self dismissView:FIRInAppMessagingDismissTypeUserTapClose];
  75. }
  76. }
  77. - (FIRInAppMessagingDisplayMessage *)inAppMessage {
  78. return self.cardDisplayMessage;
  79. }
  80. - (void)viewDidLoad {
  81. [super viewDidLoad];
  82. self.cardView.backgroundColor = self.cardDisplayMessage.displayBackgroundColor;
  83. self.cardView.layer.cornerRadius = 4;
  84. self.bodyTextView.contentInset = UIEdgeInsetsZero;
  85. self.bodyTextView.textContainer.lineFragmentPadding = 0;
  86. // Make the background half transparent.
  87. [self.view setBackgroundColor:[UIColor.grayColor colorWithAlphaComponent:0.5]];
  88. self.titleLabel.text = self.cardDisplayMessage.title;
  89. self.titleLabel.textColor = self.cardDisplayMessage.textColor;
  90. self.bodyTextView.text = self.cardDisplayMessage.body;
  91. self.bodyTextView.textColor = self.cardDisplayMessage.textColor;
  92. [self.primaryActionButton setTitle:self.cardDisplayMessage.primaryActionButton.buttonText
  93. forState:UIControlStateNormal];
  94. [self.primaryActionButton
  95. setTitleColor:self.cardDisplayMessage.primaryActionButton.buttonTextColor
  96. forState:UIControlStateNormal];
  97. if (self.cardDisplayMessage.secondaryActionButton) {
  98. self.secondaryActionButton.hidden = NO;
  99. [self.secondaryActionButton setTitle:self.cardDisplayMessage.secondaryActionButton.buttonText
  100. forState:UIControlStateNormal];
  101. [self.secondaryActionButton
  102. setTitleColor:self.cardDisplayMessage.secondaryActionButton.buttonTextColor
  103. forState:UIControlStateNormal];
  104. }
  105. }
  106. - (void)viewDidLayoutSubviews {
  107. [super viewDidLayoutSubviews];
  108. // The landscape image is optional and only displayed if:
  109. // 1. Landscape image exists.
  110. // 2. The iOS device is in "landscape" mode (regular width or compact height).
  111. if (self.traitCollection.horizontalSizeClass == UIUserInterfaceSizeClassRegular ||
  112. self.traitCollection.verticalSizeClass == UIUserInterfaceSizeClassCompact) {
  113. NSData *imageData = self.cardDisplayMessage.landscapeImageData
  114. ? self.cardDisplayMessage.landscapeImageData.imageRawData
  115. : self.cardDisplayMessage.portraitImageData.imageRawData;
  116. self.imageView.image = [UIImage imageWithData:imageData];
  117. } else {
  118. self.imageView.image =
  119. [UIImage imageWithData:self.cardDisplayMessage.portraitImageData.imageRawData];
  120. }
  121. self.textAreaScrollView.contentSize = self.bodyTextView.frame.size;
  122. [self.textAreaScrollView setContentOffset:CGPointZero];
  123. }
  124. @end