FIRIAMCardViewController.m 6.4 KB

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