FIDCardViewController.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. #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.bodyTextView.contentInset = UIEdgeInsetsZero;
  84. self.bodyTextView.textContainer.lineFragmentPadding = 0;
  85. // Make the background half transparent.
  86. [self.view setBackgroundColor:[UIColor.grayColor colorWithAlphaComponent:0.5]];
  87. self.titleLabel.text = self.cardDisplayMessage.title;
  88. self.titleLabel.textColor = self.cardDisplayMessage.textColor;
  89. self.bodyTextView.text = self.cardDisplayMessage.body;
  90. self.bodyTextView.textColor = self.cardDisplayMessage.textColor;
  91. [self.primaryActionButton setTitle:self.cardDisplayMessage.primaryActionButton.buttonText
  92. forState:UIControlStateNormal];
  93. [self.primaryActionButton
  94. setTitleColor:self.cardDisplayMessage.primaryActionButton.buttonTextColor
  95. forState:UIControlStateNormal];
  96. if (self.cardDisplayMessage.secondaryActionButton) {
  97. self.secondaryActionButton.hidden = NO;
  98. [self.secondaryActionButton setTitle:self.cardDisplayMessage.secondaryActionButton.buttonText
  99. forState:UIControlStateNormal];
  100. [self.secondaryActionButton
  101. setTitleColor:self.cardDisplayMessage.secondaryActionButton.buttonTextColor
  102. forState:UIControlStateNormal];
  103. }
  104. }
  105. - (void)viewDidLayoutSubviews {
  106. [super viewDidLayoutSubviews];
  107. // The landscape image is optional and only displayed if:
  108. // 1. Landscape image exists.
  109. // 2. The iOS device is in "landscape" mode (regular width or compact height).
  110. if (self.traitCollection.horizontalSizeClass == UIUserInterfaceSizeClassRegular ||
  111. self.traitCollection.verticalSizeClass == UIUserInterfaceSizeClassCompact) {
  112. NSData *imageData = self.cardDisplayMessage.landscapeImageData
  113. ? self.cardDisplayMessage.landscapeImageData.imageRawData
  114. : self.cardDisplayMessage.portraitImageData.imageRawData;
  115. self.imageView.image = [UIImage imageWithData:imageData];
  116. } else {
  117. self.imageView.image =
  118. [UIImage imageWithData:self.cardDisplayMessage.portraitImageData.imageRawData];
  119. }
  120. self.textAreaScrollView.contentSize = self.bodyTextView.frame.size;
  121. [self.textAreaScrollView setContentOffset:CGPointZero];
  122. }
  123. @end