FIRIAMBannerViewController.m 12 KB

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