Skip to content

Commit

Permalink
Merge pull request #393 from CleverTap/complete-todos
Browse files Browse the repository at this point in the history
Complete Todos and connect database code
  • Loading branch information
akashvercetti authored Dec 9, 2024
2 parents 8f6d3fb + 95bba9a commit ea243f0
Show file tree
Hide file tree
Showing 12 changed files with 116 additions and 97 deletions.
27 changes: 4 additions & 23 deletions CleverTapSDK/CTLocalDataStore.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ - (instancetype)initWithConfig:(CleverTapInstanceConfig *)config profileValues:(
_deviceInfo = deviceInfo;
self.dispatchQueueManager = dispatchQueueManager;
self.userEventLogs = [NSMutableSet set];
self.dbHelper = [CTEventDatabase sharedInstanceWithConfig:self.config];
localProfileUpdateExpiryStore = [NSMutableDictionary new];
_backgroundQueue = dispatch_queue_create([[NSString stringWithFormat:@"com.clevertap.profileBackgroundQueue:%@", _config.accountId] UTF8String], DISPATCH_QUEUE_SERIAL);
dispatch_queue_set_specific(_backgroundQueue, kProfileBackgroundQueueKey, (__bridge void *)self, NULL);
Expand Down Expand Up @@ -191,24 +192,8 @@ - (void)persistEvent:(NSDictionary *)event {
if (!event || !event[CLTAP_EVENT_NAME]) return;
[self runOnBackgroundQueue:^{
NSString *eventName = event[CLTAP_EVENT_NAME];
NSDictionary *storedEvents = [self getStoredEvents];
if (!storedEvents) storedEvents = @{};
NSTimeInterval now = [[[NSDate alloc] init] timeIntervalSince1970];
NSArray *eventData = storedEvents[eventName];
if (!eventData || eventData.count < 3) {
// This event has been recorded for the very first time
// Set the count to 0, first and last to now
// Count will be incremented soon after this block
eventData = @[@0.0f, @(now), @(now)];
}
NSMutableArray *eventDataCopy = [eventData mutableCopy];
double currentCount = ((NSNumber *) eventDataCopy[0]).doubleValue;
currentCount++;
eventDataCopy[0] = @(currentCount);
eventDataCopy[2] = @(now);
NSMutableDictionary *store = [storedEvents mutableCopy];
store[eventName] = eventDataCopy;
[self setStoredEvents:store];
// TODO: add normalisation
[self.dbHelper upsertEvent:eventName normalizedEventName:[CTUtils getNormalizedName:eventName] deviceID:self.deviceInfo.deviceId];
}];
}

Expand Down Expand Up @@ -624,11 +609,8 @@ - (BOOL)isEventLoggedFirstTime:(NSString*)eventName {
return NO;
}
}
if (!self.dbHelper) {
self.dbHelper = [CTEventDatabase sharedInstanceWithConfig:self.config];
}
// TODO: Add normalized name here
NSInteger count = [self.dbHelper getEventCount:eventName deviceID:self.deviceInfo.deviceId];
NSInteger count = [self.dbHelper getEventCount:[CTUtils getNormalizedName:eventName] deviceID:self.deviceInfo.deviceId];
if (count > 1) {
@synchronized (self.userEventLogs) {
[self.userEventLogs addObject:eventName];
Expand Down Expand Up @@ -706,7 +688,6 @@ - (void)_setProfileValue:(id)value forKey:(NSString *)key fromUpstream:(BOOL)fro
NSArray *systemProfileKeys = @[CLTAP_SYS_CARRIER, CLTAP_SYS_CC, CLTAP_SYS_TZ];
if (![systemProfileKeys containsObject:key]) {
NSDictionary *profileEvent = @{CLTAP_EVENT_NAME: key};
// TODO: Call appropriate persist method from the new db/localDataStore class
[self persistEvent:profileEvent];
}
}
Expand Down
2 changes: 1 addition & 1 deletion CleverTapSDK/CleverTap.m
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ - (void)initializeInAppSupport {
templatesManager:templatesManager
fileDownloader:self.fileDownloader];

CTInAppEvaluationManager *evaluationManager = [[CTInAppEvaluationManager alloc] initWithAccountId:self.config.accountId deviceId:self.deviceInfo.deviceId delegateManager:self.delegateManager impressionManager:impressionManager inAppDisplayManager:displayManager inAppStore:inAppStore inAppTriggerManager:triggerManager];
CTInAppEvaluationManager *evaluationManager = [[CTInAppEvaluationManager alloc] initWithAccountId:self.config.accountId deviceId:self.deviceInfo.deviceId delegateManager:self.delegateManager impressionManager:impressionManager inAppDisplayManager:displayManager inAppStore:inAppStore inAppTriggerManager:triggerManager localDataStore:self.localDataStore];

self.customTemplatesManager = templatesManager;
self.inAppFCManager = inAppFCManager;
Expand Down
2 changes: 1 addition & 1 deletion CleverTapSDK/EventDatabase/CTEventDatabase.m
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ - (BOOL)deleteLeastRecentlyUsedRows:(NSInteger)maxRowLimit

- (BOOL)openDatabase {
NSString *databasePath = [self databasePath];
if (sqlite3_open([databasePath UTF8String], &_eventDatabase) == SQLITE_OK) {
if (sqlite3_open_v2([databasePath UTF8String], &_eventDatabase, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX, NULL) == SQLITE_OK) {
// Create table, check and update the version if needed
[self createTable];
[self checkAndUpdateDatabaseVersion];
Expand Down
4 changes: 3 additions & 1 deletion CleverTapSDK/InApps/CTInAppEvaluationManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#import <CoreLocation/CoreLocation.h>
#import "CTBatchSentDelegate.h"
#import "CTAttachToBatchHeaderDelegate.h"
#import "CTLocalDataStore.h"

@class CTMultiDelegateManager;
@class CTImpressionManager;
Expand All @@ -30,7 +31,8 @@ NS_ASSUME_NONNULL_BEGIN
impressionManager:(CTImpressionManager *)impressionManager
inAppDisplayManager:(CTInAppDisplayManager *)inAppDisplayManager
inAppStore:(CTInAppStore *)inAppStore
inAppTriggerManager:(CTInAppTriggerManager *)inAppTriggerManager;
inAppTriggerManager:(CTInAppTriggerManager *)inAppTriggerManager
localDataStore:(CTLocalDataStore *)dataStore;

- (void)evaluateOnEvent:(NSString *)eventName withProps:(NSDictionary *)properties;
- (void)evaluateOnChargedEvent:(NSDictionary *)chargeDetails andItems:(NSArray *)items;
Expand Down
5 changes: 3 additions & 2 deletions CleverTapSDK/InApps/CTInAppEvaluationManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ - (instancetype)initWithAccountId:(NSString *)accountId
impressionManager:(CTImpressionManager *)impressionManager
inAppDisplayManager:(CTInAppDisplayManager *)inAppDisplayManager
inAppStore:(CTInAppStore *)inAppStore
inAppTriggerManager:(CTInAppTriggerManager *)inAppTriggerManager {
inAppTriggerManager:(CTInAppTriggerManager *)inAppTriggerManager
localDataStore:(CTLocalDataStore *)dataStore {
if (self = [super init]) {
self.accountId = accountId;
self.deviceId = deviceId;
Expand Down Expand Up @@ -84,7 +85,7 @@ - (instancetype)initWithAccountId:(NSString *)accountId
}

self.inAppStore = inAppStore;
self.triggersMatcher = [CTTriggersMatcher new];
self.triggersMatcher = [[CTTriggersMatcher alloc]initWithDataStore:dataStore];
self.limitsMatcher = [CTLimitsMatcher new];
self.triggerManager = inAppTriggerManager;

Expand Down
2 changes: 2 additions & 0 deletions CleverTapSDK/InApps/Matchers/CTTriggersMatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@

#import <Foundation/Foundation.h>
#import "CTEventAdapter.h"
#import "CTLocalDataStore.h"

NS_ASSUME_NONNULL_BEGIN

@interface CTTriggersMatcher : NSObject

- (instancetype)initWithDataStore:(CTLocalDataStore *)dataStore;
- (BOOL)matchEventWhenTriggers:(NSArray *)whenTriggers event:(CTEventAdapter *)event;

@end
Expand Down
22 changes: 15 additions & 7 deletions CleverTapSDK/InApps/Matchers/CTTriggersMatcher.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,19 @@
#import "CTTriggerEvaluator.h"
#import "CTUtils.h"

@interface CTTriggersMatcher () {}
@property (nonatomic, strong) CTLocalDataStore *dataStore;
@end

@implementation CTTriggersMatcher

- (instancetype)initWithDataStore:(CTLocalDataStore *)dataStore {
if (self = [super init]) {
self.dataStore = dataStore;
}
return self;
}

- (BOOL)matchEventWhenTriggers:(NSArray *)whenTriggers event:(CTEventAdapter *)event {
// Events in the array are OR-ed
for (NSDictionary *triggerObject in whenTriggers) {
Expand All @@ -41,7 +52,7 @@ - (BOOL)match:(CTTriggerAdapter *)trigger event:(CTEventAdapter *)event {
return NO;
}

if (![self matchFirstTimeOnly:event trigger:trigger]) {
if (![self matchFirstTimeOnlyForTrigger:trigger]) {
return NO;
}

Expand Down Expand Up @@ -120,15 +131,12 @@ - (BOOL)matchCharged:(CTTriggerAdapter *)trigger event:(CTEventAdapter *)event {
return YES;
}

- (BOOL)matchFirstTimeOnly:(CTEventAdapter *)event trigger:(CTTriggerAdapter *)trigger {
- (BOOL)matchFirstTimeOnlyForTrigger:(CTTriggerAdapter *)trigger {
if (!trigger.firstTimeOnly) {
return YES;
}
// TODO: Call IsEventFirstTime from the new db/localDataStore class
// NSString *nameToCheck = trigger.profileAttrName ?: trigger.eventName;
// return [CTLocalDataStore IsEventFirstTime:nameToCheck];

return YES;
NSString *nameToCheck = trigger.profileAttrName ?: trigger.eventName;
return [self.dataStore isEventLoggedFirstTime:nameToCheck];
}

@end
2 changes: 1 addition & 1 deletion CleverTapSDKTests/CTLocalDataStoreTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ - (void)testPersistEventAndGetEventDetail {
NSDictionary *event = @{CLTAP_EVENT_NAME: eventName};
[self.dataStore persistEvent:event];
sleep(1);
CleverTapEventDetail *eventDetails = [self.dataStore getEventDetail:eventName];
CleverTapEventDetail *eventDetails = [self.dataStore readUserEventLog:eventName];
XCTAssertEqual(eventDetails.count, 1);
XCTAssertGreaterThan(eventDetails.firstTime, 0);
XCTAssertGreaterThan(eventDetails.lastTime, 0);
Expand Down
2 changes: 1 addition & 1 deletion CleverTapSDKTests/InApps/CTInAppEvaluationManagerTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ - (void)testDelegatesAdded {
NSUInteger batchHeaderDelegatesCount = [[delegateManager attachToHeaderDelegates] count];
NSUInteger batchSentDelegatesCount = [[delegateManager batchSentDelegates] count];

__unused CTInAppEvaluationManager *manager = [[CTInAppEvaluationManager alloc] initWithAccountId:self.helper.accountId deviceId:self.helper.deviceId delegateManager:delegateManager impressionManager:self.helper.impressionManager inAppDisplayManager:self.helper.inAppDisplayManager inAppStore:self.helper.inAppStore inAppTriggerManager:self.helper.inAppTriggerManager];
__unused CTInAppEvaluationManager *manager = [[CTInAppEvaluationManager alloc] initWithAccountId:self.helper.accountId deviceId:self.helper.deviceId delegateManager:delegateManager impressionManager:self.helper.impressionManager inAppDisplayManager:self.helper.inAppDisplayManager inAppStore:self.helper.inAppStore inAppTriggerManager:self.helper.inAppTriggerManager localDataStore:self.helper.dataStore];

XCTAssertEqual([[delegateManager attachToHeaderDelegates] count], batchHeaderDelegatesCount + 1);
XCTAssertEqual([[delegateManager batchSentDelegates] count], batchSentDelegatesCount + 1);
Expand Down
Loading

0 comments on commit ea243f0

Please sign in to comment.