Commit 8eae921c authored by Bosko Petreski's avatar Bosko Petreski Committed by Bosko Petreski

added queue plist

parent ca8d185a
......@@ -27,6 +27,7 @@ typedef NS_ENUM(NSUInteger, ScrollDirection) {
@interface PrimedTracker : PrimedIO {
NSInteger heartbeatIncrement;
NSMutableArray *savedPayloads;
}
......
......@@ -9,9 +9,43 @@
#import "PrimedTracker.h"
#import "PrimedIO.h"
@import CoreLocation;
@implementation PrimedTracker
#pragma mark - SimpleDatabase
-(NSString *)pathPayloads{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = paths.firstObject;
return [documentsDirectory stringByAppendingPathComponent:@"Payloads.plist"];
}
-(void)saveToPLIST:(NSDictionary *)payload{
[savedPayloads addObject:payload];
[savedPayloads writeToFile:self.pathPayloads atomically:YES];
}
-(void)removeFirstItemFromPLIST{
[savedPayloads removeObjectAtIndex:0];
[savedPayloads writeToFile:self.pathPayloads atomically:YES];
}
#pragma mark - CustomFunctions
-(BOOL)isNetworkAvailable{
CFNetDiagnosticRef dReference;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wnonnull"
dReference = CFNetDiagnosticCreateWithURL (NULL, (__bridge CFURLRef)[NSURL URLWithString:@"www.apple.com"]);
#pragma clang diagnostic pop
CFNetDiagnosticStatus status;
status = CFNetDiagnosticCopyNetworkStatusPassively (dReference, NULL);
CFRelease (dReference);
if ( status == kCFNetDiagnosticConnectionUp ){
return YES;
}
else{
return NO;
}
}
-(NSDictionary *)basicEventProperties:(NSString *)type eventObject:(NSDictionary *)eventObject{
NSMutableDictionary *payload = [NSMutableDictionary dictionaryWithDictionary:@{@"apikey":PrimedIO.sharedInstance.public_key,
@"ts":@(NSDate.date.timeIntervalSince1970),
......@@ -27,9 +61,14 @@
return payload;
}
-(void)emitPayload:(NSDictionary *)payload{
[[self.socket emitWithAck:@"event" with:@[payload]] timingOutAfter:0 callback:^(NSArray* data) {
NSLog(@"data return: %@",data);
}];
if(self.isNetworkAvailable){
[[self.socket emitWithAck:@"event" with:@[payload]] timingOutAfter:0 callback:^(NSArray* data) {
PrimedIOLog(@"data return: %@",data);
}];
}
else{
[self saveToPLIST:payload];
}
}
#pragma mark - Initialization
......@@ -44,7 +83,10 @@
-(id)init{
self = [super init];
if(self) {
savedPayloads = [NSMutableArray arrayWithContentsOfFile:self.pathPayloads];
if(!savedPayloads){
savedPayloads = [NSMutableArray new];
}
}
return self;
}
......@@ -84,10 +126,18 @@
}];
[self.socket onAny:^(SocketAnyEvent *data) {
NSLog(@"onAny: [%@] %@",data.event,data.items);
PrimedIOLog(@"onAny: [%@] %@",data.event,data.items);
}];
[self.socket connect];
[NSTimer scheduledTimerWithTimeInterval:self.eventQueueRetryInterval repeats:YES block:^(NSTimer * _Nonnull timer) {
if(self->savedPayloads.count > 0 && self.isNetworkAvailable){
PrimedIOLog(@"Left to send: %lu",self->savedPayloads.count);
[self emitPayload:self->savedPayloads.firstObject];
[self removeFirstItemFromPLIST];
}
}];
}
#pragma mark - API Main Functions
......@@ -122,6 +172,13 @@
}];
[self emitPayload:payload];
}
-(void)trackLocationChange:(CLLocation *)location{
//TODO
NSDictionary *payload = [self basicEventProperties:@"locationchange" eventObject:@{@"latitude":@(location.coordinate.latitude),
@"longitude":@(location.coordinate.longitude),
@"accuracy":@(location.horizontalAccuracy)
}];
}
-(void)trackHeartbeat{
NSDictionary *payload = [self basicEventProperties:@"heartbeat" eventObject:@{@"i":@(heartbeatIncrement)}];
[self emitPayload:payload];
......@@ -140,15 +197,9 @@
}
-(void)trackEnd{
}
-(void)trackPersonalise{
}
-(void)trackConvert{
}
-(void)trackCustomEvent:(NSDictionary *)data{
NSDictionary *payload = [self basicEventProperties:@"custom" eventObject:@{}];
}
@end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment