FIRIAMBannerViewController.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 <TargetConditionals.h>
  17. #if TARGET_OS_IOS
  18. #import "FirebaseInAppMessaging/Sources/DefaultUI/Banner/FIRIAMBannerViewController.h"
  19. #import "FirebaseInAppMessaging/Sources/DefaultUI/FIRCore+InAppMessagingDisplay.h"
  20. @interface FIRIAMBannerViewController ()
  21. @property(nonatomic, readwrite) FIRInAppMessagingBannerDisplay *bannerDisplayMessage;
  22. @property(weak, nonatomic) IBOutlet NSLayoutConstraint *imageViewWidthConstraint;
  23. @property(weak, nonatomic) IBOutlet NSLayoutConstraint *imageViewHeightConstraint;
  24. @property(weak, nonatomic)
  25. IBOutlet NSLayoutConstraint *imageBottomAlignWithBodyLabelBottomConstraint;
  26. @property(weak, nonatomic) IBOutlet UIImageView *imageView;
  27. @property(weak, nonatomic) IBOutlet UILabel *titleLabel;
  28. @property(weak, nonatomic) IBOutlet UILabel *bodyLabel;
  29. // Banner view will be rendered and dismissed with animation. Within viewDidLayoutSubviews function,
  30. // we would position the view so that it's out of UIWindow range on the top so that later on it can
  31. // slide in with animation. However, viewDidLayoutSubviews is also triggred in other scenarios
  32. // like split view on iPad or device orientation changes where we don't want to hide the banner for
  33. // animations. So to have different logic, we use this property to tell the two different
  34. // cases apart and apply different positioning logic accordingly in viewDidLayoutSubviews.
  35. @property(nonatomic) BOOL hidingForAnimation;
  36. @property(nonatomic, nullable) NSTimer *autoDismissTimer;
  37. @end
  38. // The image display area dimension in points
  39. static const CGFloat kBannerViewImageWidth = 60;
  40. static const CGFloat kBannerViewImageHeight = 60;
  41. static const NSTimeInterval kBannerViewAnimationDuration = 0.3; // in seconds
  42. // Banner view will auto dismiss after this amount of time of showing if user does not take
  43. // any other actions. It's in seconds.
  44. static const NSTimeInterval kBannerAutoDismissTime = 12;
  45. // If the window width is larger than this threshold, we cap banner view width
  46. // by it: showing a non full-width banner when it happens.
  47. static const CGFloat kBannerViewMaxWidth = 736;
  48. static const CGFloat kSwipeUpThreshold = -10.0f;
  49. @implementation FIRIAMBannerViewController
  50. + (FIRIAMBannerViewController *)
  51. instantiateViewControllerWithResourceBundle:(NSBundle *)resourceBundle
  52. displayMessage:(FIRInAppMessagingBannerDisplay *)bannerMessage
  53. displayDelegate:
  54. (id<FIRInAppMessagingDisplayDelegate>)displayDelegate
  55. timeFetcher:(id<FIRIAMTimeFetcher>)timeFetcher {
  56. UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"FIRInAppMessageDisplayStoryboard"
  57. bundle:resourceBundle];
  58. if (storyboard == nil) {
  59. FIRLogError(kFIRLoggerInAppMessagingDisplay, @"I-FID300002",
  60. @"Storyboard '"
  61. "FIRInAppMessageDisplayStoryboard' not found in bundle %@",
  62. resourceBundle);
  63. return nil;
  64. }
  65. FIRIAMBannerViewController *bannerVC = (FIRIAMBannerViewController *)[storyboard
  66. instantiateViewControllerWithIdentifier:@"banner-view-vc"];
  67. bannerVC.displayDelegate = displayDelegate;
  68. bannerVC.bannerDisplayMessage = bannerMessage;
  69. bannerVC.timeFetcher = timeFetcher;
  70. return bannerVC;
  71. }
  72. - (FIRInAppMessagingDisplayMessage *)inAppMessage {
  73. return self.bannerDisplayMessage;
  74. }
  75. - (void)setupRecognizers {
  76. UIPanGestureRecognizer *panSwipeRecognizer =
  77. [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanSwipe:)];
  78. [self.view addGestureRecognizer:panSwipeRecognizer];
  79. UITapGestureRecognizer *tapGestureRecognizer =
  80. [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(messageTapped:)];
  81. tapGestureRecognizer.delaysTouchesBegan = YES;
  82. tapGestureRecognizer.numberOfTapsRequired = 1;
  83. [self.view addGestureRecognizer:tapGestureRecognizer];
  84. }
  85. - (void)handlePanSwipe:(UIPanGestureRecognizer *)recognizer {
  86. // Detect the swipe gesture
  87. if (recognizer.state == UIGestureRecognizerStateEnded) {
  88. CGPoint vel = [recognizer velocityInView:recognizer.view];
  89. if (vel.y < kSwipeUpThreshold) {
  90. [self closeViewFromManualDismiss];
  91. }
  92. }
  93. }
  94. - (void)viewDidLoad {
  95. [super viewDidLoad];
  96. // Do any additional setup after loading the view from its nib.
  97. [self setupRecognizers];
  98. self.titleLabel.text = self.bannerDisplayMessage.title;
  99. self.bodyLabel.text = self.bannerDisplayMessage.bodyText;
  100. if (self.bannerDisplayMessage.imageData) {
  101. self.imageView.contentMode = UIViewContentModeScaleAspectFit;
  102. UIImage *image = [UIImage imageWithData:self.bannerDisplayMessage.imageData.imageRawData];
  103. if (fabs(image.size.width / image.size.height - 1) > 0.02) {
  104. // width and height differ by at least 2%, need to adjust image view
  105. // size to respect the ratio
  106. // reduce height or width of the image view to retain the ratio of the image
  107. if (image.size.width > image.size.height) {
  108. CGFloat newImageHeight = kBannerViewImageWidth * image.size.height / image.size.width;
  109. self.imageViewHeightConstraint.constant = newImageHeight;
  110. } else {
  111. CGFloat newImageWidth = kBannerViewImageHeight * image.size.width / image.size.height;
  112. self.imageViewWidthConstraint.constant = newImageWidth;
  113. }
  114. }
  115. self.imageView.image = image;
  116. self.imageView.accessibilityLabel = self.inAppMessage.campaignInfo.campaignName;
  117. } else {
  118. // Hide image and remove the bottom constraint between body label and image view.
  119. self.imageViewWidthConstraint.constant = 0;
  120. self.imageBottomAlignWithBodyLabelBottomConstraint.active = NO;
  121. }
  122. // Set some rendering effects based on settings.
  123. self.view.backgroundColor = self.bannerDisplayMessage.displayBackgroundColor;
  124. self.titleLabel.textColor = self.bannerDisplayMessage.textColor;
  125. self.bodyLabel.textColor = self.bannerDisplayMessage.textColor;
  126. self.view.layer.masksToBounds = NO;
  127. self.view.layer.shadowOffset = CGSizeMake(2, 1);
  128. self.view.layer.shadowRadius = 2;
  129. self.view.layer.shadowOpacity = 0.4;
  130. // Calculate status bar height.
  131. CGFloat statusBarHeight = 0;
  132. #if defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
  133. if (@available(iOS 13.0, tvOS 13.0, *)) {
  134. UIStatusBarManager *manager =
  135. [UIApplication sharedApplication].keyWindow.windowScene.statusBarManager;
  136. statusBarHeight = manager.statusBarFrame.size.height;
  137. } else {
  138. #endif
  139. statusBarHeight = [[UIApplication sharedApplication] statusBarFrame].size.height;
  140. #if defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
  141. }
  142. #endif
  143. // Pin title label below status bar with cushion.
  144. [[self.titleLabel.topAnchor constraintEqualToAnchor:self.view.topAnchor
  145. constant:statusBarHeight + 3] setActive:YES];
  146. // When created, we are hiding it for later animation
  147. self.hidingForAnimation = YES;
  148. [self setupAutoDismissTimer];
  149. }
  150. - (void)dismissViewWithAnimation:(void (^)(void))completion {
  151. CGRect rectInNormalState = self.view.frame;
  152. CGAffineTransform hidingTransform =
  153. CGAffineTransformMakeTranslation(0, -rectInNormalState.size.height);
  154. [UIView animateWithDuration:kBannerViewAnimationDuration
  155. delay:0
  156. options:UIViewAnimationOptionCurveEaseInOut
  157. animations:^{
  158. self.view.transform = hidingTransform;
  159. }
  160. completion:^(BOOL finished) {
  161. completion();
  162. }];
  163. }
  164. - (void)closeViewFromAutoDismiss {
  165. FIRLogDebug(kFIRLoggerInAppMessagingDisplay, @"I-FID300001", @"Auto dismiss the banner view");
  166. [self dismissViewWithAnimation:^(void) {
  167. [self dismissView:FIRInAppMessagingDismissTypeAuto];
  168. }];
  169. }
  170. - (void)closeViewFromManualDismiss {
  171. FIRLogDebug(kFIRLoggerInAppMessagingDisplay, @"I-FID300003", @"Manually dismiss the banner view");
  172. [self.autoDismissTimer invalidate];
  173. [self dismissViewWithAnimation:^(void) {
  174. [self dismissView:FIRInAppMessagingDismissTypeUserSwipe];
  175. }];
  176. }
  177. - (void)messageTapped:(UITapGestureRecognizer *)recognizer {
  178. [self.autoDismissTimer invalidate];
  179. [self dismissViewWithAnimation:^(void) {
  180. #pragma clang diagnostic push
  181. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  182. FIRInAppMessagingAction *action =
  183. [[FIRInAppMessagingAction alloc] initWithActionText:nil
  184. actionURL:self.bannerDisplayMessage.actionURL];
  185. #pragma clang diagnostic pop
  186. [self followAction:action];
  187. }];
  188. }
  189. - (void)adjustBodyLabelViewHeight {
  190. // These lines make sure that we only change the height of the label view
  191. // to fit the content. Doing [self.bodyLabel sizeToFit] only could potentially
  192. // change the width as well.
  193. CGRect theFrame = self.bodyLabel.frame;
  194. [self.bodyLabel sizeToFit];
  195. theFrame.size.height = self.bodyLabel.frame.size.height;
  196. self.bodyLabel.frame = theFrame;
  197. }
  198. - (void)viewDidLayoutSubviews {
  199. [super viewDidLayoutSubviews];
  200. CGFloat bannerViewHeight = 0;
  201. [self adjustBodyLabelViewHeight];
  202. if (self.bannerDisplayMessage.imageData) {
  203. CGFloat imageBottom = CGRectGetMaxY(self.imageView.frame);
  204. CGFloat bodyBottom = CGRectGetMaxY(self.bodyLabel.frame);
  205. bannerViewHeight = MAX(imageBottom, bodyBottom);
  206. } else {
  207. bannerViewHeight = CGRectGetMaxY(self.bodyLabel.frame);
  208. }
  209. bannerViewHeight += 5; // Add some padding margin on the bottom of the view
  210. CGFloat appWindowWidth = [self.view.window bounds].size.width;
  211. CGFloat bannerViewWidth = appWindowWidth;
  212. if (bannerViewWidth > kBannerViewMaxWidth) {
  213. bannerViewWidth = kBannerViewMaxWidth;
  214. self.view.layer.cornerRadius = 4;
  215. }
  216. CGRect viewRect =
  217. CGRectMake((appWindowWidth - bannerViewWidth) / 2, 0, bannerViewWidth, bannerViewHeight);
  218. self.view.frame = viewRect;
  219. if (self.hidingForAnimation) {
  220. // Move the banner to be just above the top of the window to hide it.
  221. self.view.center = CGPointMake(appWindowWidth / 2, -viewRect.size.height / 2);
  222. }
  223. }
  224. - (void)viewDidAppear:(BOOL)animated {
  225. [super viewDidAppear:animated];
  226. CGRect rectInNormalState = self.view.frame;
  227. CGPoint normalCenterPoint =
  228. CGPointMake(rectInNormalState.origin.x + rectInNormalState.size.width / 2,
  229. rectInNormalState.size.height / 2);
  230. self.hidingForAnimation = NO;
  231. [UIView animateWithDuration:kBannerViewAnimationDuration
  232. delay:0
  233. options:UIViewAnimationOptionCurveEaseInOut
  234. animations:^{
  235. self.view.center = normalCenterPoint;
  236. }
  237. completion:nil];
  238. // Announce via VoiceOver that the banner has appeared. Highlight the title label.
  239. UIAccessibilityPostNotification(UIAccessibilityScreenChangedNotification, self.titleLabel);
  240. }
  241. - (void)setupAutoDismissTimer {
  242. NSTimeInterval remaining = kBannerAutoDismissTime - super.aggregateImpressionTimeInSeconds;
  243. FIRLogDebug(kFIRLoggerInAppMessagingDisplay, @"I-FID300004",
  244. @"Remaining banner auto dismiss time is %lf", remaining);
  245. // Set up the auto dismiss behavior.
  246. __weak id weakSelf = self;
  247. self.autoDismissTimer =
  248. [NSTimer scheduledTimerWithTimeInterval:remaining
  249. target:weakSelf
  250. selector:@selector(closeViewFromAutoDismiss)
  251. userInfo:nil
  252. repeats:NO];
  253. }
  254. // Handlers for app become active inactive so that we can better adjust our auto dismiss feature
  255. - (void)appWillBecomeInactive:(NSNotification *)notification {
  256. [super appWillBecomeInactive:notification];
  257. [self.autoDismissTimer invalidate];
  258. }
  259. - (void)appDidBecomeActive:(NSNotification *)notification {
  260. [super appDidBecomeActive:notification];
  261. [self setupAutoDismissTimer];
  262. }
  263. - (void)dealloc {
  264. FIRLogDebug(kFIRLoggerInAppMessagingDisplay, @"I-FID300005",
  265. @"-[FIRIAMBannerViewController dealloc] triggered for %p", self);
  266. [self.autoDismissTimer invalidate];
  267. }
  268. @end
  269. #endif // TARGET_OS_IOS