FIRIAMMessageClientCache.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 "FIRIAMBookKeeper.h"
  18. #import "FIRIAMFetchResponseParser.h"
  19. #import "FIRIAMMessageDefinition.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. @interface FIRIAMMessageClientCache : NSObject
  46. // used to inform the analytics event display check flow about whether it should start/stop
  47. // analytics event listening based on the latest message definitions
  48. // make it weak to avoid retaining cycle
  49. @property(nonatomic, weak, nullable)
  50. FIRIAMDisplayCheckOnAnalyticEventsFlow *analycisEventDislayCheckFlow;
  51. - (instancetype)init NS_UNAVAILABLE;
  52. - (instancetype)initWithBookkeeper:(id<FIRIAMBookKeeper>)bookKeeper
  53. usingResponseParser:(FIRIAMFetchResponseParser *)responseParser;
  54. // set an observer for watching for data changes in the cache
  55. - (void)setDataObserver:(id<FIRIAMCacheDataObserver>)observer;
  56. // Returns YES if there are any test messages in the cache.
  57. - (BOOL)hasTestMessage;
  58. // read all the messages as a copy stored in cache
  59. - (NSArray<FIRIAMMessageDefinition *> *)allRegularMessages;
  60. // clients that are to display messages should use nextOnAppOpenDisplayMsg or
  61. // nextOnFirebaseAnalyticEventDisplayMsg to fetch the next eligible message and use
  62. // removeMessageWithId to remove it from cache once the message has been correctly rendered
  63. // Fetch next eligible messages that are appropriate for display at app launch time
  64. - (nullable FIRIAMMessageDefinition *)nextOnAppLaunchDisplayMsg;
  65. // Fetch next eligible messages that are appropriate for display at app open time
  66. - (nullable FIRIAMMessageDefinition *)nextOnAppOpenDisplayMsg;
  67. // Fetch next eligible message that matches the event triggering condition
  68. - (nullable FIRIAMMessageDefinition *)nextOnFirebaseAnalyticEventDisplayMsg:(NSString *)eventName;
  69. // Call this after a message has been rendered to remove it from the cache.
  70. - (void)removeMessageWithId:(NSString *)messgeId;
  71. // reset messages data
  72. - (void)setMessageData:(NSArray<FIRIAMMessageDefinition *> *)messages;
  73. // load messages from persistent storage
  74. - (void)loadMessageDataFromServerFetchStorage:(FIRIAMServerMsgFetchStorage *)fetchStorage
  75. withCompletion:(void (^)(BOOL success))completion;
  76. @end
  77. NS_ASSUME_NONNULL_END