-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSyncStack.m
More file actions
126 lines (115 loc) · 4.8 KB
/
SyncStack.m
File metadata and controls
126 lines (115 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//
// SyncStack.m
// ThirdPartyExtension
//
// Created by Uttam Ukkoji on 02/07/18.
// Copyright © 2018 Contentstack. All rights reserved.
//
#import "SyncStack.h"
#import "CSIOInternalHeaders.h"
#import "BSONObjectIdGenerator.h"
@implementation SyncStack
-(instancetype)initWithParmas:(NSDictionary*) parmas {
self = [super init];
if (self) {
self.params = parmas;
if ([[self.params objectForKey:@"sync_token"] isKindOfClass:[NSString class]]) {
self.syncToken = [self.params objectForKey:@"sync_token"];
}
if ([[self.params objectForKey:@"seq_id"]
isKindOfClass:[NSString
class]]) {
self.seqId = [self.params objectForKey:@"seq_id"];
}
self.items = [NSArray array];
}
return self;
}
-(void)parseSyncResult:(NSDictionary *)dictionary {
if (![dictionary isKindOfClass:[NSNull class]]) {
self.syncToken = nil;
self.paginationToken = nil;
self.hasMorePages = false;
if ([dictionary objectForKey:@"sync_token"]) {
/* For existing persisted data sync token should be set to nil. Seq Id will be generated for sync with the help of event_at field.
*/
[self setExistingTokensNilAndGenerateSeqId:@"sync_token" dict:dictionary];
}
if ([dictionary objectForKey:@"last_seq_id"]) {
self.seqId = [dictionary objectForKey:@"last_seq_id"];
}
if ([dictionary objectForKey:@"pagination_token"]) {
self.hasMorePages = true;
self.paginationToken = [dictionary objectForKey:@"pagination_token"];
}
if ([dictionary objectForKey:@"total_count"]) {
self.totalCount = [[dictionary objectForKey:@"total_count"] unsignedIntValue];
}
if ([dictionary objectForKey:@"skip"] != nil && [[dictionary objectForKey:@"skip"] isKindOfClass:[NSNumber class]]){
self.skip = [[dictionary objectForKey:@"skip"] unsignedIntValue];
}
if ([dictionary objectForKey:@"limit"] != nil && [[dictionary objectForKey:@"limit"] isKindOfClass:[NSNumber class]]){
self.limit = [[dictionary objectForKey:@"limit"] unsignedIntValue];
}
if ([dictionary objectForKey:@"items"] && [[dictionary objectForKey:@"items"] isKindOfClass:[NSArray class]]) {
self.items = [dictionary objectForKey:@"items"];
if (self.items.count > 0) {
self.hasMorePages = true;
}
}
}
}
-(NSDictionary*)getParameters {
NSMutableDictionary *syncParams = [NSMutableDictionary dictionary];
if (self.seqId != nil) {
[syncParams setValue:self.seqId forKey:@"seq_id"];
} else if (self.syncToken != nil) {
[syncParams setValue:self.syncToken forKey:@"sync_token"];
} else if (self.paginationToken != nil) {
[syncParams setValue:self.paginationToken forKey:@"pagination_token"];
} else {
syncParams = [NSMutableDictionary dictionaryWithDictionary:self.params];
[syncParams setValue:@"true" forKey:@"seq_id"];
[syncParams setValue:@"true" forKey:@"init"];
}
return syncParams;
}
-(NSString*)generateSeqId:(NSString*) eventAt {
// Create a date formatter to parse the date string
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
NSDate *date = [dateFormatter dateFromString:eventAt];
if (date) {
// Convert the NSDate object to an NSTimeInterval
NSTimeInterval timeInterval = [date timeIntervalSince1970];
NSInteger timeIntervalInSeconds = (NSInteger)timeInterval;
return [BSONObjectIdGenerator generate:timeIntervalInSeconds];
} else {
// Handle case where date conversion failed.
[NSException raise:@"Unable to parse date string" format:@"Invalid date format %@", eventAt];
return nil;
}
}
-(void)setExistingTokensNilAndGenerateSeqId:(NSString *) key dict:(NSDictionary *)dictionary {
NSMutableArray * items = [NSMutableArray array];
items = [dictionary objectForKey: @"items"];
if ([items isKindOfClass:[NSArray class]] && items.count > 0) {
// Get the last object's event_at
NSDictionary *lastObject = nil;
for (NSInteger i = items.count - 1; i >= 0; i--) {
id object = items[i];
if ([object isKindOfClass:[NSDictionary class]]) {
lastObject = object;
break;
}
}
self.seqId = [self generateSeqId:[lastObject objectForKey:@"event_at"]];
} else {
if ([key isEqual: @"sync_token"]) {
self.syncToken = [dictionary objectForKey:@"sync_token"];
} else {
self.paginationToken = [dictionary objectForKey:@"pagination_token"];
}
}
}
@end