Skip to content

Commit

Permalink
Update FirebasePlugin.m
Browse files Browse the repository at this point in the history
Added 'created' and 'lastUpdate' variable in Timestamp format inside functions 'addDocumentToFirestoreCollection', 'setDocumentInFirestoreCollection' and 'updateDocumentInFirestoreCollection'
  • Loading branch information
magancete authored Jun 18, 2022
1 parent d30aa59 commit 24b77da
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions src/ios/FirebasePlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -1742,8 +1742,17 @@ - (void)addDocumentToFirestoreCollection:(CDVInvokedUrlCommand*)command {
@try {
NSDictionary* document = [command.arguments objectAtIndex:0];
NSString* collection = [command.arguments objectAtIndex:1];
bool timestamp = [command.arguments objectAtIndex:2];

NSMutableDictionary *document_mutable = [document mutableCopy];

if(timestamp){
document_mutable[@"created"] = [FIRTimestamp timestampWithDate:[NSDate date]];
document_mutable[@"lastUpdate"] = [FIRTimestamp timestampWithDate:[NSDate date]];
}

__block FIRDocumentReference *ref =
[[firestore collectionWithPath:collection] addDocumentWithData:document completion:^(NSError * _Nullable error) {
[[firestore collectionWithPath:collection] addDocumentWithData:document_mutable completion:^(NSError * _Nullable error) {
[self handleStringResultWithPotentialError:error command:command result:ref.documentID];
}];
}@catch (NSException *exception) {
Expand All @@ -1757,9 +1766,15 @@ - (void)setDocumentInFirestoreCollection:(CDVInvokedUrlCommand*)command {
@try {
NSString* documentId = [command.arguments objectAtIndex:0];
NSDictionary* document = [command.arguments objectAtIndex:1];
NSString* collection = [command.arguments objectAtIndex:2];
bool timestamp = [command.arguments objectAtIndex:3];

NSMutableDictionary *document_mutable = [document mutableCopy];

[[[firestore collectionWithPath:collection] documentWithPath:documentId] setData:document completion:^(NSError * _Nullable error) {
if(timestamp){
document_mutable[@"lastUpdate"] = [FIRTimestamp timestampWithDate:[NSDate date]];
}

[[[firestore collectionWithPath:collection] documentWithPath:documentId] setData:document_mutable completion:^(NSError * _Nullable error) {
[self handleEmptyResultWithPotentialError:error command:command];
}];
}@catch (NSException *exception) {
Expand All @@ -1774,10 +1789,17 @@ - (void)updateDocumentInFirestoreCollection:(CDVInvokedUrlCommand*)command {
NSString* documentId = [command.arguments objectAtIndex:0];
NSDictionary* document = [command.arguments objectAtIndex:1];
NSString* collection = [command.arguments objectAtIndex:2];
bool timestamp = [command.arguments objectAtIndex:3];

NSMutableDictionary *document_mutable = [document mutableCopy];

if(timestamp){
document_mutable[@"lastUpdate"] = [FIRTimestamp timestampWithDate:[NSDate date]];
}

FIRDocumentReference* docRef = [[firestore collectionWithPath:collection] documentWithPath:documentId];
if(docRef != nil){
[docRef updateData:document completion:^(NSError * _Nullable error) {
[docRef updateData:document_mutable completion:^(NSError * _Nullable error) {
[self handleEmptyResultWithPotentialError:error command:command];
}];
}else{
Expand Down

0 comments on commit 24b77da

Please sign in to comment.