2017-09-28 12:48:28 +00:00
|
|
|
#import "RNFirebaseFirestoreDocumentReference.h"
|
|
|
|
|
|
|
|
@implementation RNFirebaseFirestoreDocumentReference
|
|
|
|
|
2017-10-03 16:23:28 +00:00
|
|
|
#if __has_include(<FirebaseFirestore/FirebaseFirestore.h>)
|
2017-09-28 12:48:28 +00:00
|
|
|
|
2017-10-03 09:12:25 +00:00
|
|
|
static NSMutableDictionary *_listeners;
|
|
|
|
|
2017-10-02 14:45:07 +00:00
|
|
|
- (id)initWithPath:(RCTEventEmitter *)emitter
|
2018-01-03 20:00:38 +00:00
|
|
|
appDisplayName:(NSString *) appDisplayName
|
2017-09-28 12:48:28 +00:00
|
|
|
path:(NSString *) path {
|
|
|
|
self = [super init];
|
|
|
|
if (self) {
|
2017-10-02 14:45:07 +00:00
|
|
|
_emitter = emitter;
|
2018-01-03 20:00:38 +00:00
|
|
|
_appDisplayName = appDisplayName;
|
2017-09-28 12:48:28 +00:00
|
|
|
_path = path;
|
2018-01-03 20:00:38 +00:00
|
|
|
_ref = [[RNFirebaseFirestore getFirestoreForApp:_appDisplayName] documentWithPath:_path];
|
2017-10-03 09:12:25 +00:00
|
|
|
}
|
|
|
|
// Initialise the static listeners object if required
|
|
|
|
if (!_listeners) {
|
2017-10-02 14:45:07 +00:00
|
|
|
_listeners = [[NSMutableDictionary alloc] init];
|
2017-09-28 12:48:28 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2017-10-05 09:18:24 +00:00
|
|
|
- (void)delete:(RCTPromiseResolveBlock) resolve
|
2017-09-28 12:48:28 +00:00
|
|
|
rejecter:(RCTPromiseRejectBlock) reject {
|
|
|
|
[_ref deleteDocumentWithCompletion:^(NSError * _Nullable error) {
|
|
|
|
[RNFirebaseFirestoreDocumentReference handleWriteResponse:error resolver:resolve rejecter:reject];
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)get:(RCTPromiseResolveBlock) resolve
|
|
|
|
rejecter:(RCTPromiseRejectBlock) reject {
|
|
|
|
[_ref getDocumentWithCompletion:^(FIRDocumentSnapshot * _Nullable snapshot, NSError * _Nullable error) {
|
|
|
|
if (error) {
|
|
|
|
[RNFirebaseFirestore promiseRejectException:reject error:error];
|
|
|
|
} else {
|
|
|
|
NSDictionary *data = [RNFirebaseFirestoreDocumentReference snapshotToDictionary:snapshot];
|
|
|
|
resolve(data);
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2017-10-03 09:12:25 +00:00
|
|
|
+ (void)offSnapshot:(NSString *) listenerId {
|
2017-10-02 14:45:07 +00:00
|
|
|
id<FIRListenerRegistration> listener = _listeners[listenerId];
|
|
|
|
if (listener) {
|
|
|
|
[_listeners removeObjectForKey:listenerId];
|
|
|
|
[listener remove];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-06 11:00:40 +00:00
|
|
|
- (void)onSnapshot:(NSString *) listenerId
|
|
|
|
docListenOptions:(NSDictionary *) docListenOptions {
|
2017-10-02 14:45:07 +00:00
|
|
|
if (_listeners[listenerId] == nil) {
|
|
|
|
id listenerBlock = ^(FIRDocumentSnapshot * _Nullable snapshot, NSError * _Nullable error) {
|
|
|
|
if (error) {
|
|
|
|
id<FIRListenerRegistration> listener = _listeners[listenerId];
|
|
|
|
if (listener) {
|
|
|
|
[_listeners removeObjectForKey:listenerId];
|
|
|
|
[listener remove];
|
|
|
|
}
|
|
|
|
[self handleDocumentSnapshotError:listenerId error:error];
|
|
|
|
} else {
|
|
|
|
[self handleDocumentSnapshotEvent:listenerId documentSnapshot:snapshot];
|
|
|
|
}
|
|
|
|
};
|
2017-10-06 11:00:40 +00:00
|
|
|
FIRDocumentListenOptions *options = [[FIRDocumentListenOptions alloc] init];
|
|
|
|
if (docListenOptions && docListenOptions[@"includeMetadataChanges"]) {
|
|
|
|
[options includeMetadataChanges:TRUE];
|
|
|
|
}
|
|
|
|
id<FIRListenerRegistration> listener = [_ref addSnapshotListenerWithOptions:options listener:listenerBlock];
|
2017-10-02 14:45:07 +00:00
|
|
|
_listeners[listenerId] = listener;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-28 12:48:28 +00:00
|
|
|
- (void)set:(NSDictionary *) data
|
|
|
|
options:(NSDictionary *) options
|
|
|
|
resolver:(RCTPromiseResolveBlock) resolve
|
|
|
|
rejecter:(RCTPromiseRejectBlock) reject {
|
2018-01-03 20:00:38 +00:00
|
|
|
NSDictionary *dictionary = [RNFirebaseFirestoreDocumentReference parseJSMap:[RNFirebaseFirestore getFirestoreForApp:_appDisplayName] jsMap:data];
|
2017-09-28 12:48:28 +00:00
|
|
|
if (options && options[@"merge"]) {
|
2017-10-10 16:22:12 +00:00
|
|
|
[_ref setData:dictionary options:[FIRSetOptions merge] completion:^(NSError * _Nullable error) {
|
2017-09-28 12:48:28 +00:00
|
|
|
[RNFirebaseFirestoreDocumentReference handleWriteResponse:error resolver:resolve rejecter:reject];
|
|
|
|
}];
|
|
|
|
} else {
|
2017-10-10 16:22:12 +00:00
|
|
|
[_ref setData:dictionary completion:^(NSError * _Nullable error) {
|
2017-09-28 12:48:28 +00:00
|
|
|
[RNFirebaseFirestoreDocumentReference handleWriteResponse:error resolver:resolve rejecter:reject];
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)update:(NSDictionary *) data
|
|
|
|
resolver:(RCTPromiseResolveBlock) resolve
|
|
|
|
rejecter:(RCTPromiseRejectBlock) reject {
|
2018-01-03 20:00:38 +00:00
|
|
|
NSDictionary *dictionary = [RNFirebaseFirestoreDocumentReference parseJSMap:[RNFirebaseFirestore getFirestoreForApp:_appDisplayName] jsMap:data];
|
2017-10-10 16:22:12 +00:00
|
|
|
[_ref updateData:dictionary completion:^(NSError * _Nullable error) {
|
2017-09-28 12:48:28 +00:00
|
|
|
[RNFirebaseFirestoreDocumentReference handleWriteResponse:error resolver:resolve rejecter:reject];
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2017-10-02 14:45:07 +00:00
|
|
|
- (BOOL)hasListeners {
|
|
|
|
return [[_listeners allKeys] count] > 0;
|
|
|
|
}
|
|
|
|
|
2017-09-28 12:48:28 +00:00
|
|
|
+ (void)handleWriteResponse:(NSError *) error
|
|
|
|
resolver:(RCTPromiseResolveBlock) resolve
|
|
|
|
rejecter:(RCTPromiseRejectBlock) reject {
|
|
|
|
if (error) {
|
|
|
|
[RNFirebaseFirestore promiseRejectException:reject error:error];
|
|
|
|
} else {
|
2017-10-05 09:18:24 +00:00
|
|
|
resolve(nil);
|
2017-09-28 12:48:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSDictionary *)snapshotToDictionary:(FIRDocumentSnapshot *)documentSnapshot {
|
|
|
|
NSMutableDictionary *snapshot = [[NSMutableDictionary alloc] init];
|
|
|
|
[snapshot setValue:documentSnapshot.reference.path forKey:@"path"];
|
2017-09-28 16:48:13 +00:00
|
|
|
if (documentSnapshot.exists) {
|
2017-10-10 16:22:12 +00:00
|
|
|
[snapshot setValue:[RNFirebaseFirestoreDocumentReference buildNativeMap:documentSnapshot.data] forKey:@"data"];
|
2017-09-28 16:48:13 +00:00
|
|
|
}
|
2017-10-05 09:18:24 +00:00
|
|
|
if (documentSnapshot.metadata) {
|
|
|
|
NSMutableDictionary *metadata = [[NSMutableDictionary alloc] init];
|
|
|
|
[metadata setValue:@(documentSnapshot.metadata.fromCache) forKey:@"fromCache"];
|
|
|
|
[metadata setValue:@(documentSnapshot.metadata.hasPendingWrites) forKey:@"hasPendingWrites"];
|
|
|
|
[snapshot setValue:metadata forKey:@"metadata"];
|
|
|
|
}
|
2017-09-28 12:48:28 +00:00
|
|
|
return snapshot;
|
|
|
|
}
|
|
|
|
|
2017-10-03 09:12:25 +00:00
|
|
|
- (void)handleDocumentSnapshotError:(NSString *)listenerId
|
2017-10-02 14:45:07 +00:00
|
|
|
error:(NSError *)error {
|
|
|
|
NSMutableDictionary *event = [[NSMutableDictionary alloc] init];
|
2018-01-03 20:00:38 +00:00
|
|
|
[event setValue:_appDisplayName forKey:@"appName"];
|
2017-10-02 14:45:07 +00:00
|
|
|
[event setValue:_path forKey:@"path"];
|
|
|
|
[event setValue:listenerId forKey:@"listenerId"];
|
|
|
|
[event setValue:[RNFirebaseFirestore getJSError:error] forKey:@"error"];
|
2017-10-03 16:23:28 +00:00
|
|
|
|
2017-10-26 10:55:07 +00:00
|
|
|
[RNFirebaseUtil sendJSEvent:self.emitter name:FIRESTORE_DOCUMENT_SYNC_EVENT body:event];
|
2017-10-02 14:45:07 +00:00
|
|
|
}
|
|
|
|
|
2017-10-03 09:12:25 +00:00
|
|
|
- (void)handleDocumentSnapshotEvent:(NSString *)listenerId
|
2017-10-02 14:45:07 +00:00
|
|
|
documentSnapshot:(FIRDocumentSnapshot *)documentSnapshot {
|
|
|
|
NSMutableDictionary *event = [[NSMutableDictionary alloc] init];
|
2018-01-03 20:00:38 +00:00
|
|
|
[event setValue:_appDisplayName forKey:@"appName"];
|
2017-10-02 14:45:07 +00:00
|
|
|
[event setValue:_path forKey:@"path"];
|
|
|
|
[event setValue:listenerId forKey:@"listenerId"];
|
2017-10-03 09:12:25 +00:00
|
|
|
[event setValue:[RNFirebaseFirestoreDocumentReference snapshotToDictionary:documentSnapshot] forKey:@"documentSnapshot"];
|
2017-10-03 16:23:28 +00:00
|
|
|
|
2017-10-26 10:55:07 +00:00
|
|
|
[RNFirebaseUtil sendJSEvent:self.emitter name:FIRESTORE_DOCUMENT_SYNC_EVENT body:event];
|
2017-10-02 14:45:07 +00:00
|
|
|
}
|
|
|
|
|
2017-10-10 16:22:12 +00:00
|
|
|
|
|
|
|
+ (NSDictionary *)buildNativeMap:(NSDictionary *)nativeMap {
|
|
|
|
NSMutableDictionary *map = [[NSMutableDictionary alloc] init];
|
|
|
|
[nativeMap enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
|
|
|
|
NSDictionary *typeMap = [RNFirebaseFirestoreDocumentReference buildTypeMap:obj];
|
|
|
|
map[key] = typeMap;
|
|
|
|
}];
|
2017-10-18 09:29:46 +00:00
|
|
|
|
2017-10-10 16:22:12 +00:00
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSArray *)buildNativeArray:(NSArray *)nativeArray {
|
|
|
|
NSMutableArray *array = [[NSMutableArray alloc] init];
|
|
|
|
[nativeArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
|
|
|
|
NSDictionary *typeMap = [RNFirebaseFirestoreDocumentReference buildTypeMap:obj];
|
|
|
|
[array addObject:typeMap];
|
|
|
|
}];
|
2017-10-18 09:29:46 +00:00
|
|
|
|
2017-10-10 16:22:12 +00:00
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSDictionary *)buildTypeMap:(id) value {
|
|
|
|
NSMutableDictionary *typeMap = [[NSMutableDictionary alloc] init];
|
|
|
|
if (!value) {
|
|
|
|
typeMap[@"type"] = @"null";
|
|
|
|
} else if ([value isKindOfClass:[NSString class]]) {
|
|
|
|
typeMap[@"type"] = @"string";
|
|
|
|
typeMap[@"value"] = value;
|
|
|
|
} else if ([value isKindOfClass:[NSDictionary class]]) {
|
|
|
|
typeMap[@"type"] = @"object";
|
|
|
|
typeMap[@"value"] = [RNFirebaseFirestoreDocumentReference buildNativeMap:value];
|
|
|
|
} else if ([value isKindOfClass:[NSArray class]]) {
|
|
|
|
typeMap[@"type"] = @"array";
|
|
|
|
typeMap[@"value"] = [RNFirebaseFirestoreDocumentReference buildNativeArray:value];
|
|
|
|
} else if ([value isKindOfClass:[FIRDocumentReference class]]) {
|
|
|
|
typeMap[@"type"] = @"reference";
|
|
|
|
FIRDocumentReference *ref = (FIRDocumentReference *)value;
|
|
|
|
typeMap[@"value"] = [ref path];
|
|
|
|
} else if ([value isKindOfClass:[FIRGeoPoint class]]) {
|
|
|
|
typeMap[@"type"] = @"geopoint";
|
|
|
|
FIRGeoPoint *point = (FIRGeoPoint *)value;
|
|
|
|
NSMutableDictionary *geopoint = [[NSMutableDictionary alloc] init];
|
|
|
|
geopoint[@"latitude"] = @([point latitude]);
|
|
|
|
geopoint[@"longitude"] = @([point longitude]);
|
|
|
|
typeMap[@"value"] = geopoint;
|
|
|
|
} else if ([value isKindOfClass:[NSDate class]]) {
|
|
|
|
typeMap[@"type"] = @"date";
|
2017-10-24 08:09:48 +00:00
|
|
|
// NOTE: The round() is important as iOS ends up giving .999 otherwise,
|
|
|
|
// and loses a millisecond when going between native and JS
|
|
|
|
typeMap[@"value"] = @(round([(NSDate *)value timeIntervalSince1970] * 1000.0));
|
2017-10-10 16:22:12 +00:00
|
|
|
} else if ([value isKindOfClass:[NSNumber class]]) {
|
|
|
|
NSNumber *number = (NSNumber *)value;
|
|
|
|
if (number == (void*)kCFBooleanFalse || number == (void*)kCFBooleanTrue) {
|
|
|
|
typeMap[@"type"] = @"boolean";
|
|
|
|
} else {
|
|
|
|
typeMap[@"type"] = @"number";
|
|
|
|
}
|
|
|
|
typeMap[@"value"] = value;
|
2018-05-03 19:12:46 +00:00
|
|
|
} else if ([value isKindOfClass:[NSData class]]) {
|
|
|
|
typeMap[@"type"] = @"blob";
|
|
|
|
NSData *blob = (NSData *)value;
|
|
|
|
typeMap[@"value"] = [blob base64EncodedStringWithOptions:0];
|
2017-10-10 16:22:12 +00:00
|
|
|
} else {
|
|
|
|
// TODO: Log an error
|
|
|
|
typeMap[@"type"] = @"null";
|
|
|
|
}
|
2017-10-18 09:29:46 +00:00
|
|
|
|
2017-10-10 16:22:12 +00:00
|
|
|
return typeMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
+(NSDictionary *)parseJSMap:(FIRFirestore *) firestore
|
|
|
|
jsMap:(NSDictionary *) jsMap {
|
|
|
|
NSMutableDictionary* map = [[NSMutableDictionary alloc] init];
|
|
|
|
if (jsMap) {
|
|
|
|
[jsMap enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
|
|
|
|
map[key] = [RNFirebaseFirestoreDocumentReference parseJSTypeMap:firestore jsTypeMap:obj];
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
|
|
|
+(NSArray *)parseJSArray:(FIRFirestore *) firestore
|
|
|
|
jsArray:(NSArray *) jsArray {
|
|
|
|
NSMutableArray* array = [[NSMutableArray alloc] init];
|
|
|
|
if (jsArray) {
|
|
|
|
[jsArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
|
|
|
|
[array addObject:[RNFirebaseFirestoreDocumentReference parseJSTypeMap:firestore jsTypeMap:obj]];
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
|
|
|
+(id)parseJSTypeMap:(FIRFirestore *) firestore
|
|
|
|
jsTypeMap:(NSDictionary *) jsTypeMap {
|
|
|
|
NSString *type = jsTypeMap[@"type"];
|
|
|
|
id value = jsTypeMap[@"value"];
|
|
|
|
if ([type isEqualToString:@"array"]) {
|
|
|
|
return [RNFirebaseFirestoreDocumentReference parseJSArray:firestore jsArray:value];
|
|
|
|
} else if ([type isEqualToString:@"object"]) {
|
|
|
|
return [RNFirebaseFirestoreDocumentReference parseJSMap:firestore jsMap:value];
|
|
|
|
} else if ([type isEqualToString:@"reference"]) {
|
|
|
|
return [firestore documentWithPath:value];
|
2018-05-03 19:12:46 +00:00
|
|
|
} else if ([type isEqualToString:@"blob"]) {
|
|
|
|
return [[NSData alloc] initWithBase64EncodedString:(NSString *) value options:0];
|
2017-10-10 16:22:12 +00:00
|
|
|
} else if ([type isEqualToString:@"geopoint"]) {
|
2017-10-12 08:00:46 +00:00
|
|
|
NSDictionary *geopoint = (NSDictionary*)value;
|
2017-10-10 16:22:12 +00:00
|
|
|
NSNumber *latitude = geopoint[@"latitude"];
|
|
|
|
NSNumber *longitude = geopoint[@"longitude"];
|
|
|
|
return [[FIRGeoPoint alloc] initWithLatitude:[latitude doubleValue] longitude:[longitude doubleValue]];
|
|
|
|
} else if ([type isEqualToString:@"date"]) {
|
2017-10-24 08:09:48 +00:00
|
|
|
return [NSDate dateWithTimeIntervalSince1970:([(NSNumber *)value doubleValue] / 1000.0)];
|
2018-01-11 18:28:14 +00:00
|
|
|
} else if ([type isEqualToString:@"documentid"]) {
|
|
|
|
return [FIRFieldPath documentID];
|
2017-10-12 08:00:46 +00:00
|
|
|
} else if ([type isEqualToString:@"fieldvalue"]) {
|
|
|
|
NSString *string = (NSString*)value;
|
|
|
|
if ([string isEqualToString:@"delete"]) {
|
|
|
|
return [FIRFieldValue fieldValueForDelete];
|
|
|
|
} else if ([string isEqualToString:@"timestamp"]) {
|
|
|
|
return [FIRFieldValue fieldValueForServerTimestamp];
|
|
|
|
} else {
|
|
|
|
// TODO: Log warning
|
|
|
|
return nil;
|
|
|
|
}
|
2017-10-10 16:22:12 +00:00
|
|
|
} else if ([type isEqualToString:@"boolean"] || [type isEqualToString:@"number"] || [type isEqualToString:@"string"] || [type isEqualToString:@"null"]) {
|
|
|
|
return value;
|
|
|
|
} else {
|
|
|
|
// TODO: Log error
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-28 12:48:28 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
@end
|