AutoDisplayFlowViewController.m 6.5 KB

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