FIDBaseRenderingViewController.m 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 "FIDBaseRenderingViewController.h"
  17. #import "FIDTimeFetcher.h"
  18. #import "FIRCore+InAppMessagingDisplay.h"
  19. @interface FIDBaseRenderingViewController ()
  20. // For fiam messages, it's required to be kMinValidImpressionTime to
  21. // be considered as a valid impression help. If the app is closed before that's reached,
  22. // SDK may try to render the same message again in the future.
  23. @property(nonatomic, nullable) NSTimer *minImpressionTimer;
  24. // Tracking the start time when the current impression session start.
  25. @property(nonatomic) double currentImpressionStartTime;
  26. @end
  27. static const NSTimeInterval kMinValidImpressionTime = 3.0;
  28. @implementation FIDBaseRenderingViewController
  29. - (nullable FIRInAppMessagingDisplayMessage *)inAppMessage {
  30. return nil;
  31. }
  32. - (void)viewDidLoad {
  33. [super viewDidLoad];
  34. // In order to track display time for this message, we need to respond to
  35. // app foreground/background events since viewDidAppear/viewDidDisappear are not
  36. // triggered when app switches happen.
  37. [[NSNotificationCenter defaultCenter] addObserver:self
  38. selector:@selector(appDidBecomeInactive:)
  39. name:UIApplicationWillResignActiveNotification
  40. object:nil];
  41. [[NSNotificationCenter defaultCenter] addObserver:self
  42. selector:@selector(appDidBecomeActive:)
  43. name:UIApplicationDidBecomeActiveNotification
  44. object:nil];
  45. self.aggregateImpressionTimeInSeconds = 0;
  46. }
  47. - (void)viewDidAppear:(BOOL)animated {
  48. [super viewDidAppear:animated];
  49. [self impressionStartCheckpoint];
  50. }
  51. - (void)viewDidDisappear:(BOOL)animated {
  52. [super viewDidDisappear:animated];
  53. [self impressionStopCheckpoint];
  54. }
  55. // Call this when the view starts to be rendered so that we can track the aggregate impression
  56. // time for the current message
  57. - (void)impressionStartCheckpoint {
  58. self.currentImpressionStartTime = [self.timeFetcher currentTimestampInSeconds];
  59. [self setupMinImpressionTimer];
  60. }
  61. // Trigger this when the view stops to be rendered so that we can track the aggregate impression
  62. // time for the current message
  63. - (void)impressionStopCheckpoint {
  64. // Pause the impression timer.
  65. [self.minImpressionTimer invalidate];
  66. // Track the effective impression time for this impression session.
  67. double effectiveImpressionTime =
  68. [self.timeFetcher currentTimestampInSeconds] - self.currentImpressionStartTime;
  69. self.aggregateImpressionTimeInSeconds += effectiveImpressionTime;
  70. }
  71. - (void)dealloc {
  72. FIRLogDebug(kFIRLoggerInAppMessagingDisplay, @"I-FID200001",
  73. @"[FIDBaseRenderingViewController dealloc] triggered");
  74. [self.minImpressionTimer invalidate];
  75. [NSNotificationCenter.defaultCenter removeObserver:self];
  76. }
  77. - (void)appDidBecomeInactive:(UIApplication *)application {
  78. [self impressionStopCheckpoint];
  79. }
  80. - (void)appDidBecomeActive:(UIApplication *)application {
  81. [self impressionStartCheckpoint];
  82. }
  83. - (void)minImpressionTimeReached {
  84. FIRLogDebug(kFIRLoggerInAppMessagingDisplay, @"I-FID200004",
  85. @"Min impression time has been reached.");
  86. if ([self.displayDelegate respondsToSelector:@selector(impressionDetectedForMessage:)]) {
  87. [self.displayDelegate impressionDetectedForMessage:[self inAppMessage]];
  88. }
  89. [NSNotificationCenter.defaultCenter removeObserver:self];
  90. }
  91. - (void)setupMinImpressionTimer {
  92. NSTimeInterval remaining = kMinValidImpressionTime - self.aggregateImpressionTimeInSeconds;
  93. FIRLogDebug(kFIRLoggerInAppMessagingDisplay, @"I-FID200006",
  94. @"Remaining minimal impression time is %lf", remaining);
  95. if (remaining < 0.00001) {
  96. return;
  97. }
  98. __weak id weakSelf = self;
  99. self.minImpressionTimer =
  100. [NSTimer scheduledTimerWithTimeInterval:remaining
  101. target:weakSelf
  102. selector:@selector(minImpressionTimeReached)
  103. userInfo:nil
  104. repeats:NO];
  105. }
  106. - (void)dismissView:(FIRInAppMessagingDismissType)dismissType {
  107. [self.view.window setHidden:YES];
  108. // This is for the purpose of releasing the potential memory associated with the image view.
  109. self.view.window.rootViewController = nil;
  110. if (self.displayDelegate) {
  111. [self.displayDelegate messageDismissed:[self inAppMessage] dismissType:dismissType];
  112. } else {
  113. FIRLogWarning(kFIRLoggerInAppMessagingDisplay, @"I-FID200007",
  114. @"Display delegate is nil while message is being dismissed.");
  115. }
  116. return;
  117. }
  118. - (void)followAction:(FIRInAppMessagingAction *)action {
  119. [self.view.window setHidden:YES];
  120. // This is for the purpose of releasing the potential memory associated with the image view.
  121. self.view.window.rootViewController = nil;
  122. if (self.displayDelegate) {
  123. [self.displayDelegate messageClicked:[self inAppMessage] withAction:action];
  124. } else {
  125. FIRLogWarning(kFIRLoggerInAppMessagingDisplay, @"I-FID200008",
  126. @"Display delegate is nil while trying to follow action :%@.", action.actionText);
  127. }
  128. return;
  129. }
  130. @end