FIRIAMMessageClientCache.h 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 <Foundation/Foundation.h>
  17. #import "FirebaseInAppMessaging/Sources/Private/Data/FIRIAMFetchResponseParser.h"
  18. #import "FirebaseInAppMessaging/Sources/Private/Data/FIRIAMMessageDefinition.h"
  19. #import "FirebaseInAppMessaging/Sources/Private/Flows/FIRIAMBookKeeper.h"
  20. NS_ASSUME_NONNULL_BEGIN
  21. @class FIRIAMServerMsgFetchStorage;
  22. @class FIRIAMDisplayCheckOnAnalyticEventsFlow;
  23. @interface FIRIAMContextualTrigger
  24. @property(nonatomic, copy, readonly) NSString *eventName;
  25. @end
  26. @interface FIRIAMContextualTriggerListener
  27. + (void)listenForTriggers:(NSArray<FIRIAMContextualTrigger *> *)triggers
  28. withCallback:(void (^)(FIRIAMContextualTrigger *matchedTrigger))callback;
  29. @end
  30. @protocol FIRIAMCacheDataObserver
  31. - (void)dataChanged;
  32. @end
  33. // This class serves as an in-memory cache of the messages that would be searched for finding next
  34. // message to be rendered. Its content can be loaded from client persistent storage upon SDK
  35. // initialization and then updated whenever a new fetch is made to server to receive the last
  36. // list. In the case a message has been rendered, it's removed from the cache so that it's not
  37. // considered next time for the message search.
  38. //
  39. // This class is also responsible for setting up and tearing down appropriate analytics event
  40. // listening flow based on whether the current active event list contains any analytics event
  41. // trigger based messages.
  42. //
  43. // This class exists so that we can do message match more efficiently (in-memory search vs search
  44. // in local persistent storage) by using appropriate in-memory data structure.
  45. NS_EXTENSION_UNAVAILABLE("Firebase In App Messaging is not supported for iOS extensions.")
  46. @interface FIRIAMMessageClientCache : NSObject
  47. // used to inform the analytics event display check flow about whether it should start/stop
  48. // analytics event listening based on the latest message definitions
  49. // make it weak to avoid retaining cycle
  50. @property(nonatomic, weak, nullable)
  51. FIRIAMDisplayCheckOnAnalyticEventsFlow *analycisEventDislayCheckFlow;
  52. - (instancetype)init NS_UNAVAILABLE;
  53. - (instancetype)initWithBookkeeper:(id<FIRIAMBookKeeper>)bookKeeper
  54. usingResponseParser:(FIRIAMFetchResponseParser *)responseParser;
  55. // set an observer for watching for data changes in the cache
  56. - (void)setDataObserver:(id<FIRIAMCacheDataObserver>)observer;
  57. // Returns YES if there are any test messages in the cache.
  58. - (BOOL)hasTestMessage;
  59. // read all the messages as a copy stored in cache
  60. - (NSArray<FIRIAMMessageDefinition *> *)allRegularMessages;
  61. // clients that are to display messages should use nextOnAppOpenDisplayMsg or
  62. // nextOnFirebaseAnalyticEventDisplayMsg to fetch the next eligible message and use
  63. // removeMessageWithId to remove it from cache once the message has been correctly rendered
  64. // Fetch next eligible messages that are appropriate for display at app launch time
  65. - (nullable FIRIAMMessageDefinition *)nextOnAppLaunchDisplayMsg;
  66. // Fetch next eligible messages that are appropriate for display at app open time
  67. - (nullable FIRIAMMessageDefinition *)nextOnAppOpenDisplayMsg;
  68. // Fetch next eligible message that matches the event triggering condition
  69. - (nullable FIRIAMMessageDefinition *)nextOnFirebaseAnalyticEventDisplayMsg:(NSString *)eventName;
  70. // Call this after a message has been rendered to remove it from the cache.
  71. - (void)removeMessageWithId:(NSString *)messgeId;
  72. // reset messages data
  73. - (void)setMessageData:(NSArray<FIRIAMMessageDefinition *> *)messages;
  74. // load messages from persistent storage
  75. - (void)loadMessageDataFromServerFetchStorage:(FIRIAMServerMsgFetchStorage *)fetchStorage
  76. withCompletion:(void (^)(BOOL success))completion;
  77. @end
  78. NS_ASSUME_NONNULL_END