Skip to content

Commit

Permalink
Merge pull request #345 from Countly/contentr-response-changes
Browse files Browse the repository at this point in the history
Content response changes
  • Loading branch information
turtledreams authored Oct 4, 2024
2 parents 8b937d0 + fd4d827 commit c788008
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
35 changes: 24 additions & 11 deletions CountlyContentBuilderInternal.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand All @@ -118,9 +132,8 @@ - (NSURLRequest *)fetchContentsRequest
{
NSString* queryString = [CountlyConnectionManager.sharedInstance queryEssentials];
NSString *resolutionJson = [self resolutionJson];
queryString = [queryString stringByAppendingFormat:@"&%@=%@&%@=%@",
kCountlyQSKeyMethod, kCountlyCBFetchContent,
@"res", resolutionJson];
queryString = [queryString stringByAppendingFormat:@"&%@=%@",
@"resolution", resolutionJson];

queryString = [CountlyConnectionManager.sharedInstance appendChecksum:queryString];

Expand Down Expand Up @@ -155,8 +168,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];
Expand All @@ -180,12 +193,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);

Expand Down
16 changes: 10 additions & 6 deletions CountlyPersistency.m
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit c788008

Please sign in to comment.