AutoDisplayFlowViewController.m 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright 2017 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 "AppDelegate.h"
  17. #import "AutoDisplayFlowViewController.h"
  18. #import "AutoDisplayMessagesTableVC.h"
  19. #import <FirebaseInAppMessaging/FIRIAMDisplayCheckOnAppForegroundFlow.h>
  20. #import <FirebaseInAppMessaging/FIRIAMMessageClientCache.h>
  21. #import <FirebaseInAppMessaging/FIRIAMMessageContentDataWithImageURL.h>
  22. #import <FirebaseInAppMessaging/FIRIAMMessageDefinition.h>
  23. #import <FirebaseInAppMessaging/FIRIAMActivityLogger.h>
  24. #import <FirebaseInAppMessaging/FIRIAMDisplayCheckOnAnalyticEventsFlow.h>
  25. #import <FirebaseInAppMessaging/FIRIAMFetchOnAppForegroundFlow.h>
  26. #import <FirebaseInAppMessaging/FIRIAMMessageClientCache.h>
  27. #import <FirebaseInAppMessaging/FIRIAMMsgFetcherUsingRestful.h>
  28. #import <FirebaseInAppMessaging/FIRIAMRuntimeManager.h>
  29. #import "FIRInAppMessaging.h"
  30. #import <FirebaseAnalytics/FIRAnalytics.h>
  31. @interface AutoDisplayFlowViewController ()
  32. @property(weak, nonatomic) IBOutlet UISwitch *autoDisplayFlowSwitch;
  33. @property(nonatomic, weak) AutoDisplayMessagesTableVC *messageTableVC;
  34. @property(weak, nonatomic) IBOutlet UITextField *autoDisplayIntervalText;
  35. @property(weak, nonatomic) IBOutlet UITextField *autoFetchIntervalText;
  36. @property(weak, nonatomic) IBOutlet UITextField *eventNameText;
  37. @property(weak, nonatomic) IBOutlet UITextField *programmaticTriggerNameText;
  38. @property(nonatomic) FIRIAMRuntimeManager *sdkRuntime;
  39. @property(weak, nonatomic) IBOutlet UIButton *disableEnableSDKBtn;
  40. @property(weak, nonatomic) IBOutlet UIButton *changeDataCollectionBtn;
  41. @end
  42. @implementation AutoDisplayFlowViewController
  43. - (IBAction)clearClientStorage:(id)sender {
  44. [self.sdkRuntime.fetchResultStorage
  45. saveResponseDictionary:@{}
  46. withCompletion:^(BOOL success) {
  47. [self.sdkRuntime.messageCache
  48. loadMessageDataFromServerFetchStorage:self.sdkRuntime.fetchResultStorage
  49. withCompletion:^(BOOL success) {
  50. NSLog(@"load from storage result is %d", success);
  51. }];
  52. }];
  53. }
  54. - (IBAction)disableEnableClicked:(id)sender {
  55. FIRInAppMessaging *sdk = [FIRInAppMessaging inAppMessaging];
  56. sdk.messageDisplaySuppressed = !sdk.messageDisplaySuppressed;
  57. [self setupDisableEnableButtonLabel];
  58. }
  59. - (void)setupDisableEnableButtonLabel {
  60. FIRInAppMessaging *sdk = [FIRInAppMessaging inAppMessaging];
  61. NSString *title = sdk.messageDisplaySuppressed ? @"allow rendering" : @"disallow rendering";
  62. [self.disableEnableSDKBtn setTitle:title forState:UIControlStateNormal];
  63. }
  64. - (IBAction)triggerAnalyticEventTapped:(id)sender {
  65. NSLog(@"triggering an analytics event: %@", self.eventNameText.text);
  66. [FIRAnalytics logEventWithName:self.eventNameText.text parameters:@{}];
  67. }
  68. - (IBAction)triggerProgrammaticallyTapped:(id)sender {
  69. NSLog(@"Trigger event %@ programmatically", self.eventNameText.text);
  70. [[FIRInAppMessaging inAppMessaging] triggerEvent:self.programmaticTriggerNameText.text];
  71. }
  72. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  73. UITouch *touch = [touches anyObject];
  74. if (![touch.view isMemberOfClass:[UITextField class]]) {
  75. [touch.view endEditing:YES];
  76. }
  77. }
  78. - (IBAction)changeAutoDataCollection:(id)sender {
  79. FIRInAppMessaging *sdk = [FIRInAppMessaging inAppMessaging];
  80. sdk.automaticDataCollectionEnabled = !sdk.automaticDataCollectionEnabled;
  81. [self setupChangeAutoDataCollectionButtonLabel];
  82. }
  83. - (void)setupChangeAutoDataCollectionButtonLabel {
  84. FIRInAppMessaging *sdk = [FIRInAppMessaging inAppMessaging];
  85. NSString *title = sdk.automaticDataCollectionEnabled ? @"disable data-col" : @"enable data-col";
  86. [self.changeDataCollectionBtn setTitle:title forState:UIControlStateNormal];
  87. }
  88. - (void)viewDidLoad {
  89. [super viewDidLoad];
  90. double delayInSeconds = 2.0;
  91. dispatch_time_t setupTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
  92. dispatch_after(setupTime, dispatch_get_main_queue(), ^(void) {
  93. // code to be executed on the main queue after delay
  94. self.sdkRuntime = [FIRIAMRuntimeManager getSDKRuntimeInstance];
  95. self.messageTableVC.messageCache = self.sdkRuntime.messageCache;
  96. [self.sdkRuntime.messageCache setDataObserver:self.messageTableVC];
  97. [self.messageTableVC.tableView reloadData];
  98. [self setupDisableEnableButtonLabel];
  99. [self setupChangeAutoDataCollectionButtonLabel];
  100. });
  101. NSLog(@"done with set data observer");
  102. self.autoFetchIntervalText.text = [[NSNumber
  103. numberWithDouble:self.sdkRuntime.currentSetting.fetchMinIntervalInMinutes * 60] stringValue];
  104. self.autoDisplayIntervalText.text =
  105. [[NSNumber numberWithDouble:self.sdkRuntime.currentSetting.appFGRenderMinIntervalInMinutes *
  106. 60] stringValue];
  107. }
  108. - (IBAction)dumpImpressionsToConsole:(id)sender {
  109. NSArray *impressions = [self.sdkRuntime.bookKeeper getImpressions];
  110. NSLog(@"impressions are %@", [impressions componentsJoinedByString:@","]);
  111. }
  112. - (IBAction)clearImpressionRecord:(id)sender {
  113. [self.sdkRuntime.bookKeeper cleanupImpressions];
  114. }
  115. - (IBAction)changeAutoFetchDisplaySettings:(id)sender {
  116. FIRIAMSDKSettings *setting = self.sdkRuntime.currentSetting;
  117. // set fetch interval
  118. double intervalValue = self.autoFetchIntervalText.text.doubleValue / 60;
  119. if (intervalValue < 0.0001) {
  120. intervalValue = 1;
  121. self.autoFetchIntervalText.text = [[NSNumber numberWithDouble:intervalValue * 60] stringValue];
  122. }
  123. setting.fetchMinIntervalInMinutes = intervalValue;
  124. // set app foreground display interval
  125. double displayIntervalValue = self.autoDisplayIntervalText.text.doubleValue / 60;
  126. if (displayIntervalValue < 0.0001) {
  127. displayIntervalValue = 1;
  128. self.autoDisplayIntervalText.text =
  129. [[NSNumber numberWithDouble:displayIntervalValue * 60] stringValue];
  130. }
  131. setting.appFGRenderMinIntervalInMinutes = displayIntervalValue;
  132. [self.sdkRuntime startRuntimeWithSDKSettings:setting];
  133. }
  134. - (void)didReceiveMemoryWarning {
  135. [super didReceiveMemoryWarning];
  136. // Dispose of any resources that can be recreated.
  137. }
  138. #pragma mark - Navigation
  139. // In a storyboard-based application, you will often want to do a little preparation before
  140. // navigation
  141. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  142. // Get the new view controller using [segue destinationViewController].
  143. // Pass the selected object to the new view controller.
  144. if ([segue.identifier isEqualToString:@"message-table-segue"]) {
  145. self.messageTableVC = (AutoDisplayMessagesTableVC *)[segue destinationViewController];
  146. }
  147. }
  148. @end