From a4c4b825d5980ff2c253a6699972b66bff1e12f6 Mon Sep 17 00:00:00 2001 From: ijunaid Date: Wed, 2 Oct 2024 18:18:58 +0500 Subject: [PATCH 1/3] Update content request and response with new model changes --- CountlyContentBuilderInternal.m | 34 +++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/CountlyContentBuilderInternal.m b/CountlyContentBuilderInternal.m index 9af91e28..21eae28b 100644 --- a/CountlyContentBuilderInternal.m +++ b/CountlyContentBuilderInternal.m @@ -102,9 +102,23 @@ - (void)fetchContents { return; } - NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; - NSString *pathToHtml = jsonResponse[@"pathToHtml"]; - NSDictionary *placementCoordinates = jsonResponse[@"placementCoordinates"]; + NSError *jsonError; + NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError]; + + if (jsonError) { + CLY_LOG_I(@"Failed to parse JSON: %@", jsonError); + self->_isRequestQueueLocked = NO; + return; + } + + if (!jsonResponse) { + CLY_LOG_I(@"Received empty or null response."); + self->_isRequestQueueLocked = NO; + return; + } + + NSString *pathToHtml = jsonResponse[@"html"]; + NSDictionary *placementCoordinates = jsonResponse[@"geo"]; if(pathToHtml) { [self showContentWithHtmlPath:pathToHtml placementCoordinates:placementCoordinates]; } @@ -119,8 +133,8 @@ - (NSURLRequest *)fetchContentsRequest NSString* queryString = [CountlyConnectionManager.sharedInstance queryEssentials]; NSString *resolutionJson = [self resolutionJson]; queryString = [queryString stringByAppendingFormat:@"&%@=%@&%@=%@", - kCountlyQSKeyMethod, kCountlyCBFetchContent, - @"res", resolutionJson]; + @"app_id", @"66fa992b8757e0f5c3a52cfb", + @"resolution", resolutionJson]; queryString = [CountlyConnectionManager.sharedInstance appendChecksum:queryString]; @@ -155,8 +169,8 @@ - (NSString *)resolutionJson { CGFloat height = screenBounds.size.height; NSDictionary *resolutionDict = @{ - @"p": @{@"h": @(height), @"w": @(width)}, - @"l": @{@"h": @(width), @"w": @(height)} + @"portrait": @{@"height": @(height), @"width": @(width)}, + @"landscape": @{@"height": @(width), @"width": @(height)} }; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:resolutionDict options:0 error:nil]; @@ -180,12 +194,12 @@ - (void)showContentWithHtmlPath:(NSString *)urlString placementCoordinates:(NSDi // Get the appropriate coordinates based on the orientation - NSDictionary *coordinates = isLandscape ? placementCoordinates[@"landscape"] : placementCoordinates[@"portrait"]; + NSDictionary *coordinates = isLandscape ? placementCoordinates[@"l"] : placementCoordinates[@"p"]; CGFloat x = [coordinates[@"x"] floatValue]; CGFloat y = [coordinates[@"y"] floatValue]; - CGFloat width = [coordinates[@"width"] floatValue]; - CGFloat height = [coordinates[@"height"] floatValue]; + CGFloat width = [coordinates[@"w"] floatValue]; + CGFloat height = [coordinates[@"h"] floatValue]; CGRect frame = CGRectMake(x, y, width, height); From 82e0b1ae2bb194821b968c557ef641f832e3ebdb Mon Sep 17 00:00:00 2001 From: ijunaid Date: Wed, 2 Oct 2024 18:20:04 +0500 Subject: [PATCH 2/3] Removed app_id from request --- CountlyContentBuilderInternal.m | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CountlyContentBuilderInternal.m b/CountlyContentBuilderInternal.m index 21eae28b..59589114 100644 --- a/CountlyContentBuilderInternal.m +++ b/CountlyContentBuilderInternal.m @@ -132,8 +132,7 @@ - (NSURLRequest *)fetchContentsRequest { NSString* queryString = [CountlyConnectionManager.sharedInstance queryEssentials]; NSString *resolutionJson = [self resolutionJson]; - queryString = [queryString stringByAppendingFormat:@"&%@=%@&%@=%@", - @"app_id", @"66fa992b8757e0f5c3a52cfb", + queryString = [queryString stringByAppendingFormat:@"&%@=%@", @"resolution", resolutionJson]; queryString = [CountlyConnectionManager.sharedInstance appendChecksum:queryString]; From fd4d8273b5a8d7cd36699f28656d4224d3ab6816 Mon Sep 17 00:00:00 2001 From: ijunaid Date: Thu, 3 Oct 2024 23:13:56 +0500 Subject: [PATCH 3/3] some safe checks when searlizing events --- CountlyPersistency.m | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/CountlyPersistency.m b/CountlyPersistency.m index 276f70db..5edff471 100644 --- a/CountlyPersistency.m +++ b/CountlyPersistency.m @@ -282,23 +282,27 @@ - (void)recordEvent:(CountlyEvent *)event - (NSString *)serializedRecordedEvents { - NSMutableArray* tempArray = NSMutableArray.new; - + NSMutableArray *tempArray = NSMutableArray.new; + @synchronized (self.recordedEvents) { if (self.recordedEvents.count == 0) return nil; - - for (CountlyEvent* event in self.recordedEvents.copy) + + NSArray *eventsCopy = self.recordedEvents.copy; + + for (CountlyEvent *event in eventsCopy) { [tempArray addObject:[event dictionaryRepresentation]]; - [self.recordedEvents removeObject:event]; } + + [self.recordedEvents removeObjectsInArray:eventsCopy]; } - + return [tempArray cly_JSONify]; } + - (void)flushEvents { @synchronized (self.recordedEvents)