FIRIAMBookKeeper.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. NS_ASSUME_NONNULL_BEGIN
  18. @interface FIRIAMImpressionRecord : NSObject
  19. @property(nonatomic, readonly, copy) NSString *messageID;
  20. @property(nonatomic, readonly) long impressionTimeInSeconds;
  21. - (NSString *)description;
  22. - (instancetype)init NS_UNAVAILABLE;
  23. - (instancetype)initWithMessageID:(NSString *)messageID
  24. impressionTimeInSeconds:(long)impressionTime NS_DESIGNATED_INITIALIZER;
  25. @end
  26. // this protocol defines the interface for classes that can be used to track info regarding
  27. // display & fetch of iam messages. The info tracked here can be used to decide if it's due for
  28. // next display and/or fetch of iam messages.
  29. @protocol FIRIAMBookKeeper
  30. @property(nonatomic, readonly) double lastDisplayTime;
  31. @property(nonatomic, readonly) double lastFetchTime;
  32. @property(nonatomic, readonly) NSTimeInterval nextFetchWaitTime;
  33. // only call this when it's considered to be a valid impression (for example, meeting the minimum
  34. // display time requirement).
  35. - (void)recordNewImpressionForMessage:(NSString *)messageID
  36. withStartTimestampInSeconds:(double)timestamp;
  37. - (void)recordNewFetchWithFetchCount:(NSInteger)fetchedMsgCount
  38. withTimestampInSeconds:(double)fetchTimestamp
  39. nextFetchWaitTime:(nullable NSNumber *)nextFetchWaitTime;
  40. // When we fetch the eligible message list from the sdk server, it can contain messages that are
  41. // already impressed for those that are defined to be displayed repeatedly (messages with custom
  42. // display frequency). We need then clean up the impression records for these messages so that
  43. // they can be displayed again on client side.
  44. - (void)clearImpressionsWithMessageList:(NSArray<NSString *> *)messageList;
  45. // fetch the impression list
  46. - (NSArray<FIRIAMImpressionRecord *> *)getImpressions;
  47. // For certain clients, they only need to get the list of the message ids in existing impression
  48. // records. This is a helper method for that.
  49. - (NSArray<NSString *> *)getMessageIDsFromImpressions;
  50. @end
  51. // implementation of FIRIAMBookKeeper protocol by storing data within iOS UserDefaults.
  52. // TODO: switch to something else if there is risks for the data being unintentionally deleted by
  53. // the app
  54. @interface FIRIAMBookKeeperViaUserDefaults : NSObject <FIRIAMBookKeeper>
  55. - (instancetype)init NS_UNAVAILABLE;
  56. - (instancetype)initWithUserDefaults:(NSUserDefaults *)userDefaults NS_DESIGNATED_INITIALIZER;
  57. // for testing, don't use them for production purpose
  58. - (void)cleanupImpressions;
  59. - (void)cleanupFetchRecords;
  60. @end
  61. NS_ASSUME_NONNULL_END