-
Notifications
You must be signed in to change notification settings - Fork 36
/
iCloudStorage.m
271 lines (218 loc) · 8.55 KB
/
iCloudStorage.m
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
//
// iCloudStorage.m
// iCloudStorage
//
// Created by Mani Ghasemlou on 12/18/16.
// Copyright © 2016 Mani Ghasemlou. All rights reserved.
//
#import "iCloudStorage.h"
#import <React/RCTBridge.h>
#import <React/RCTEventDispatcher.h>
static NSString* const ICLOUDSTORAGE_PREFIX = @"@com.manicakes.iCloudStorage/";
static NSString* const ICLOUD_STORE_CHANGED = @"ICLOUD_STORE_CHANGED";
static NSString* const kStoreChangedEvent = @"iCloudStoreDidChangeRemotely";
static NSString* const kChangedKeys = @"changedKeys";
@implementation iCloudStorage
+ (NSString*)appendPrefixToKey:(NSString*)key {
return [NSString stringWithFormat:@"%@%@", ICLOUDSTORAGE_PREFIX, key];
}
+ (NSString*)removePrefixFromKey:(NSString*)key {
if (![key hasPrefix:ICLOUDSTORAGE_PREFIX]) {
return nil;
}
return [key substringFromIndex:[ICLOUDSTORAGE_PREFIX length]];
}
+ (NSDictionary*)storeDictionary {
NSUbiquitousKeyValueStore* store = [NSUbiquitousKeyValueStore defaultStore];
return [store dictionaryRepresentation];
}
+ (NSArray*)allKeysInStore {
return [[iCloudStorage storeDictionary] allKeys];
}
+ (id) getObjectForKey:(NSString*)key {
return [[NSUbiquitousKeyValueStore defaultStore] objectForKey:[iCloudStorage appendPrefixToKey:key]];
}
+ (void) setValue:(NSString*)value forKey:(NSString*)key {
[[NSUbiquitousKeyValueStore defaultStore] setObject:value forKey:[iCloudStorage appendPrefixToKey:key]];
[[NSUbiquitousKeyValueStore defaultStore] synchronize];
}
+ (void) removeKey:(NSString*)key {
[[NSUbiquitousKeyValueStore defaultStore] removeObjectForKey:[iCloudStorage appendPrefixToKey:key]];
[[NSUbiquitousKeyValueStore defaultStore] synchronize];
}
+ (NSString*) getMergedItemWithKey:(NSString*)key value:(NSString*)value rejecter:(RCTPromiseRejectBlock)reject {
NSDictionary* storedItem = @{};
NSDictionary* newItem = @{};
NSString* storedString = [iCloudStorage getObjectForKey:key];
if (storedString != nil) {
NSError* error = nil;
id object = [NSJSONSerialization JSONObjectWithData:[storedString dataUsingEncoding:NSUTF8StringEncoding]
options:0
error:&error];
if (error != nil) {
reject(@"json_decode_err", @"Error parsing stored value as JSON string.", error);
return nil;
}
if (![object isKindOfClass:[NSDictionary class]]) {
reject(@"json_not_object_err", @"The stored JSON string does not parse into an object.", nil);
return nil;
}
if (value != nil) {
id newObject = [NSJSONSerialization JSONObjectWithData:[value dataUsingEncoding:NSUTF8StringEncoding]
options:0
error:&error];
if (error != nil) {
reject(@"json_decode_err", @"The provided value is not valid JSON.", error);
return nil;
}
if (![newItem isKindOfClass:[NSDictionary class]]) {
reject(@"json_not_object_err", @"The provided JSON string does not parse into an object.", nil);
return nil;
}
newItem = newObject;
}
storedItem = object;
}
NSMutableDictionary* mergedItem = [NSMutableDictionary dictionaryWithDictionary:storedItem];
[mergedItem addEntriesFromDictionary:newItem];
NSError* error = nil;
NSData* data = [NSJSONSerialization dataWithJSONObject:mergedItem options:0 error:&error];
if (error != nil) {
reject(@"json_encode_err", @"Error encoding the merged JSON data to string.", error);
return nil;
}
return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}
- (instancetype)init {
self = [super init];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(ubiquitousStoreUpdated:)
name:NSUbiquitousKeyValueStoreDidChangeExternallyNotification
object:nil];
[[NSUbiquitousKeyValueStore defaultStore] synchronize];
}
return self;
}
+(BOOL)requiresMainQueueSetup {
return YES;
}
- (dispatch_queue_t)methodQueue
{
return dispatch_get_main_queue();
}
- (NSArray<NSString *> *)supportedEvents {
return @[ kStoreChangedEvent ];
}
- (void) ubiquitousStoreUpdated:(NSNotification*)notification {
// if this notification comes in before bridge has initialized,
// don't try to send the event (app crashes if you do).
if (!self.bridge) {
return;
}
NSArray* changedKeys = [[notification userInfo] objectForKey:NSUbiquitousKeyValueStoreChangedKeysKey];
NSMutableArray* reportedChangedKeys = [NSMutableArray array];
for (NSString* key in changedKeys) {
NSString* reportedKey = [iCloudStorage removePrefixFromKey:key];
if (reportedKey) {
[reportedChangedKeys addObject:reportedKey];
}
}
if ([reportedChangedKeys count]) {
NSDictionary* body = @{ kChangedKeys : reportedChangedKeys };
[self sendEventWithName:kStoreChangedEvent body:body];
}
}
RCT_EXPORT_MODULE(RNICloudStorage)
RCT_EXPORT_METHOD(getItem: (NSString*)key resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {
resolve([iCloudStorage getObjectForKey:key]);
}
RCT_EXPORT_METHOD(setItem: (NSString*)key value: (NSString*)value resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {
[iCloudStorage setValue:value forKey:key];
resolve(@{});
}
RCT_EXPORT_METHOD(removeItem: (NSString*)key resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {
[iCloudStorage removeKey:key];
resolve(@{});
}
RCT_EXPORT_METHOD(mergeItem: (NSString*)key value: (NSString*)value resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {
NSString* newValue = [iCloudStorage getMergedItemWithKey:key value:value rejecter:reject];
if (newValue == nil) {
// we failed and reject block was called.
return;
}
[iCloudStorage setValue:newValue forKey:key];
resolve(@{});
}
RCT_REMAP_METHOD(clear, clearResolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {
for (NSString* key in [iCloudStorage allKeysInStore]) {
if ([key hasPrefix:ICLOUDSTORAGE_PREFIX]) {
[[NSUbiquitousKeyValueStore defaultStore] removeObjectForKey:key];
}
}
resolve(@{});
}
RCT_REMAP_METHOD(getAllKeys, getAllKeysResolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {
NSMutableArray* allKeys = [NSMutableArray array];
for (NSString* storeKey in [iCloudStorage allKeysInStore]) {
NSString* key = [iCloudStorage removePrefixFromKey:storeKey];
if (key != nil) {
[allKeys addObject:key];
}
}
resolve(allKeys);
}
RCT_EXPORT_METHOD(multiGet: (NSArray*)keys resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {
NSMutableArray *result = [NSMutableArray arrayWithCapacity:[keys count]];
for (NSString* key in keys) {
NSObject* object = [iCloudStorage getObjectForKey:key];
if (object == nil) {
object = [NSNull null];
}
[result addObject:@[key, object]];
}
resolve(result);
}
RCT_EXPORT_METHOD(multiSet: (NSArray<NSArray<NSString *> *> *)keyValuePairs resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {
for (NSArray<NSString *> *entry in keyValuePairs) {
[iCloudStorage setValue:entry[1] forKey:entry[0]];
}
resolve(@{});
}
RCT_EXPORT_METHOD(multiRemove: (NSArray*)keys resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {
for (NSString* key in keys) {
[iCloudStorage removeKey:key];
}
resolve(@{});
}
RCT_EXPORT_METHOD(multiMerge: (NSDictionary*)keyValuePairs resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {
NSMutableDictionary* result = [NSMutableDictionary dictionaryWithCapacity:[keyValuePairs count]];
BOOL failed = NO;
for (NSString* key in [keyValuePairs allKeys]) {
NSString* newValue = [iCloudStorage getMergedItemWithKey:key value:[keyValuePairs objectForKey:key] rejecter:reject];
if (newValue == nil) {
break;
}
}
if (failed) {
return;
}
for (NSString* key in [result allKeys]) {
[iCloudStorage setValue:[result objectForKey:key] forKey:key];
}
resolve(@{});
}
- (NSDictionary<NSString *,id> *)constantsToExport {
return @{ ICLOUD_STORE_CHANGED : kStoreChangedEvent };
}
@end