FIRIAMBannerViewController.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. // TODO(#13068) : Fix keyWindow deprecation.
  132. #pragma clang diagnostic push
  133. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  134. UIStatusBarManager *manager =
  135. [UIApplication sharedApplication].keyWindow.windowScene.statusBarManager;
  136. #pragma clang diagnostic pop
  137. CGFloat statusBarHeight = manager.statusBarFrame.size.height;
  138. // Pin title label below status bar with cushion.
  139. [[self.titleLabel.topAnchor constraintEqualToAnchor:self.view.topAnchor
  140. constant:statusBarHeight + 3] setActive:YES];
  141. // When created, we are hiding it for later animation
  142. self.hidingForAnimation = YES;
  143. [self setupAutoDismissTimer];
  144. }
  145. - (void)dismissViewWithAnimation:(void (^)(void))completion {
  146. CGRect rectInNormalState = self.view.frame;
  147. CGAffineTransform hidingTransform =
  148. CGAffineTransformMakeTranslation(0, -rectInNormalState.size.height);
  149. [UIView animateWithDuration:kBannerViewAnimationDuration
  150. delay:0
  151. options:UIViewAnimationOptionCurveEaseInOut
  152. animations:^{
  153. self.view.transform = hidingTransform;
  154. }
  155. completion:^(BOOL finished) {
  156. completion();
  157. }];
  158. }
  159. - (void)closeViewFromAutoDismiss {
  160. FIRLogDebug(kFIRLoggerInAppMessagingDisplay, @"I-FID300001", @"Auto dismiss the banner view");
  161. [self dismissViewWithAnimation:^(void) {
  162. [self dismissView:FIRInAppMessagingDismissTypeAuto];
  163. }];
  164. }
  165. - (void)closeViewFromManualDismiss {
  166. FIRLogDebug(kFIRLoggerInAppMessagingDisplay, @"I-FID300003", @"Manually dismiss the banner view");
  167. [self.autoDismissTimer invalidate];
  168. [self dismissViewWithAnimation:^(void) {
  169. [self dismissView:FIRInAppMessagingDismissTypeUserSwipe];
  170. }];
  171. }
  172. - (void)messageTapped:(UITapGestureRecognizer *)recognizer {
  173. [self.autoDismissTimer invalidate];
  174. [self dismissViewWithAnimation:^(void) {
  175. #pragma clang diagnostic push
  176. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  177. FIRInAppMessagingAction *action =
  178. [[FIRInAppMessagingAction alloc] initWithActionText:nil
  179. actionURL:self.bannerDisplayMessage.actionURL];
  180. #pragma clang diagnostic pop
  181. [self followAction:action];
  182. }];
  183. }
  184. - (void)adjustBodyLabelViewHeight {
  185. // These lines make sure that we only change the height of the label view
  186. // to fit the content. Doing [self.bodyLabel sizeToFit] only could potentially
  187. // change the width as well.
  188. CGRect theFrame = self.bodyLabel.frame;
  189. [self.bodyLabel sizeToFit];
  190. theFrame.size.height = self.bodyLabel.frame.size.height;
  191. self.bodyLabel.frame = theFrame;
  192. }
  193. - (void)viewDidLayoutSubviews {
  194. [super viewDidLayoutSubviews];
  195. CGFloat bannerViewHeight = 0;
  196. [self adjustBodyLabelViewHeight];
  197. if (self.bannerDisplayMessage.imageData) {
  198. CGFloat imageBottom = CGRectGetMaxY(self.imageView.frame);
  199. CGFloat bodyBottom = CGRectGetMaxY(self.bodyLabel.frame);
  200. bannerViewHeight = MAX(imageBottom, bodyBottom);
  201. } else {
  202. bannerViewHeight = CGRectGetMaxY(self.bodyLabel.frame);
  203. }
  204. bannerViewHeight += 5; // Add some padding margin on the bottom of the view
  205. CGFloat appWindowWidth = [self.view.window bounds].size.width;
  206. CGFloat bannerViewWidth = appWindowWidth;
  207. if (bannerViewWidth > kBannerViewMaxWidth) {
  208. bannerViewWidth = kBannerViewMaxWidth;
  209. self.view.layer.cornerRadius = 4;
  210. }
  211. CGRect viewRect =
  212. CGRectMake((appWindowWidth - bannerViewWidth) / 2, 0, bannerViewWidth, bannerViewHeight);
  213. self.view.frame = viewRect;
  214. if (self.hidingForAnimation) {
  215. // Move the banner to be just above the top of the window to hide it.
  216. self.view.center = CGPointMake(appWindowWidth / 2, -viewRect.size.height / 2);
  217. }
  218. }
  219. - (void)viewDidAppear:(BOOL)animated {
  220. [super viewDidAppear:animated];
  221. CGRect rectInNormalState = self.view.frame;
  222. CGPoint normalCenterPoint =
  223. CGPointMake(rectInNormalState.origin.x + rectInNormalState.size.width / 2,
  224. rectInNormalState.size.height / 2);
  225. self.hidingForAnimation = NO;
  226. [UIView animateWithDuration:kBannerViewAnimationDuration
  227. delay:0
  228. options:UIViewAnimationOptionCurveEaseInOut
  229. animations:^{
  230. self.view.center = normalCenterPoint;
  231. }
  232. completion:nil];
  233. // Announce via VoiceOver that the banner has appeared. Highlight the title label.
  234. UIAccessibilityPostNotification(UIAccessibilityScreenChangedNotification, self.titleLabel);
  235. }
  236. - (void)setupAutoDismissTimer {
  237. NSTimeInterval remaining = kBannerAutoDismissTime - super.aggregateImpressionTimeInSeconds;
  238. FIRLogDebug(kFIRLoggerInAppMessagingDisplay, @"I-FID300004",
  239. @"Remaining banner auto dismiss time is %lf", remaining);
  240. // Set up the auto dismiss behavior.
  241. __weak id weakSelf = self;
  242. self.autoDismissTimer =
  243. [NSTimer scheduledTimerWithTimeInterval:remaining
  244. target:weakSelf
  245. selector:@selector(closeViewFromAutoDismiss)
  246. userInfo:nil
  247. repeats:NO];
  248. }
  249. // Handlers for app become active inactive so that we can better adjust our auto dismiss feature
  250. - (void)appWillBecomeInactive:(NSNotification *)notification {
  251. [super appWillBecomeInactive:notification];
  252. [self.autoDismissTimer invalidate];
  253. }
  254. - (void)appDidBecomeActive:(NSNotification *)notification {
  255. [super appDidBecomeActive:notification];
  256. [self setupAutoDismissTimer];
  257. }
  258. - (void)dealloc {
  259. FIRLogDebug(kFIRLoggerInAppMessagingDisplay, @"I-FID300005",
  260. @"-[FIRIAMBannerViewController dealloc] triggered for %p", self);
  261. [self.autoDismissTimer invalidate];
  262. }
  263. @end
  264. #endif // TARGET_OS_IOS