-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
FLTFirebaseFirestoreWriter.m
214 lines (188 loc) 路 7.37 KB
/
FLTFirebaseFirestoreWriter.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
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import <Firebase/Firebase.h>
#import <firebase_core/FLTFirebasePlugin.h>
#import "Private/FLTFirebaseFirestoreUtils.h"
#import "Private/FLTFirebaseFirestoreWriter.h"
#import "Public/FLTFirebaseFirestorePlugin.h"
@implementation FLTFirebaseFirestoreWriter : FlutterStandardWriter
- (void)writeValue:(id)value {
if ([value isKindOfClass:[NSDate class]]) {
[self writeByte:FirestoreDataTypeDateTime];
NSDate *date = value;
NSTimeInterval time = date.timeIntervalSince1970;
SInt64 ms = (SInt64)(time * 1000.0);
[self writeBytes:&ms length:8];
} else if ([value isKindOfClass:[FIRTimestamp class]]) {
FIRTimestamp *timestamp = value;
SInt64 seconds = timestamp.seconds;
int nanoseconds = timestamp.nanoseconds;
[self writeByte:FirestoreDataTypeTimestamp];
[self writeBytes:(UInt8 *)&seconds length:8];
[self writeBytes:(UInt8 *)&nanoseconds length:4];
} else if ([value isKindOfClass:[FIRGeoPoint class]]) {
FIRGeoPoint *geoPoint = value;
Float64 latitude = geoPoint.latitude;
Float64 longitude = geoPoint.longitude;
[self writeByte:FirestoreDataTypeGeoPoint];
[self writeAlignment:8];
[self writeBytes:(UInt8 *)&latitude length:8];
[self writeBytes:(UInt8 *)&longitude length:8];
} else if ([value isKindOfClass:[FIRDocumentReference class]]) {
FIRDocumentReference *document = value;
NSString *documentPath = [document path];
NSString *appName = [FLTFirebasePlugin firebaseAppNameFromIosName:document.firestore.app.name];
[self writeByte:FirestoreDataTypeDocumentReference];
[self writeValue:appName];
[self writeValue:documentPath];
} else if ([value isKindOfClass:[FIRDocumentSnapshot class]]) {
[super writeValue:[self FIRDocumentSnapshot:value]];
} else if ([value isKindOfClass:[FIRLoadBundleTaskProgress class]]) {
[super writeValue:[self FIRLoadBundleTaskProgress:value]];
} else if ([value isKindOfClass:[FIRQuerySnapshot class]]) {
[super writeValue:[self FIRQuerySnapshot:value]];
} else if ([value isKindOfClass:[FIRDocumentChange class]]) {
[super writeValue:[self FIRDocumentChange:value]];
} else if ([value isKindOfClass:[FIRSnapshotMetadata class]]) {
[super writeValue:[self FIRSnapshotMetadata:value]];
} else if ([value isKindOfClass:[NSNumber class]]) {
NSNumber *number = (NSNumber *)value;
// Infinity
if ([number isEqual:@(INFINITY)]) {
[self writeByte:FirestoreDataTypeInfinity];
return;
}
// -Infinity
if ([number isEqual:@(-INFINITY)]) {
[self writeByte:FirestoreDataTypeNegativeInfinity];
return;
}
// NaN
if ([[value description].lowercaseString isEqual:@"nan"]) {
[self writeByte:FirestoreDataTypeNaN];
return;
}
[super writeValue:value];
} else if ([value isKindOfClass:[NSData class]]) {
NSData *blob = value;
[self writeByte:FirestoreDataTypeBlob];
[self writeSize:(UInt32)blob.length];
[self writeData:blob];
} else {
[super writeValue:value];
}
}
- (NSDictionary *)FIRSnapshotMetadata:(FIRSnapshotMetadata *)snapshotMetadata {
return @{
@"hasPendingWrites" : @(snapshotMetadata.hasPendingWrites),
@"isFromCache" : @(snapshotMetadata.isFromCache),
};
}
- (NSDictionary *)FIRDocumentChange:(FIRDocumentChange *)documentChange {
NSString *type;
switch (documentChange.type) {
case FIRDocumentChangeTypeAdded:
type = @"DocumentChangeType.added";
break;
case FIRDocumentChangeTypeModified:
type = @"DocumentChangeType.modified";
break;
case FIRDocumentChangeTypeRemoved:
type = @"DocumentChangeType.removed";
break;
}
NSNumber *oldIndex;
NSNumber *newIndex;
// Note the Firestore C++ SDK here returns a maxed UInt that is != NSUIntegerMax, so we make one
// ourselves so we can convert to -1 for Dart.
NSUInteger MAX_VAL = (NSUInteger)[@(-1) integerValue];
if (documentChange.newIndex == NSNotFound || documentChange.newIndex == 4294967295 ||
documentChange.newIndex == MAX_VAL) {
newIndex = @([@(-1) intValue]);
} else {
newIndex = @([@(documentChange.newIndex) intValue]);
}
if (documentChange.oldIndex == NSNotFound || documentChange.oldIndex == 4294967295 ||
documentChange.oldIndex == MAX_VAL) {
oldIndex = @([@(-1) intValue]);
} else {
oldIndex = @([@(documentChange.oldIndex) intValue]);
}
return @{
@"type" : type,
@"data" : documentChange.document.data,
@"path" : documentChange.document.reference.path,
@"oldIndex" : oldIndex,
@"newIndex" : newIndex,
@"metadata" : documentChange.document.metadata,
};
}
- (FIRServerTimestampBehavior)toServerTimestampBehavior:(NSString *)serverTimestampBehavior {
if (serverTimestampBehavior == nil) {
return FIRServerTimestampBehaviorNone;
}
if ([serverTimestampBehavior isEqualToString:@"estimate"]) {
return FIRServerTimestampBehaviorEstimate;
} else if ([serverTimestampBehavior isEqualToString:@"previous"]) {
return FIRServerTimestampBehaviorPrevious;
} else {
return FIRServerTimestampBehaviorNone;
}
}
- (NSDictionary *)FIRDocumentSnapshot:(FIRDocumentSnapshot *)documentSnapshot {
FIRServerTimestampBehavior serverTimestampBehavior =
[self toServerTimestampBehavior:FLTFirebaseFirestorePlugin
.serverTimestampMap[@([documentSnapshot hash])]];
[FLTFirebaseFirestorePlugin.serverTimestampMap removeObjectForKey:@([documentSnapshot hash])];
return @{
@"path" : documentSnapshot.reference.path,
@"data" : documentSnapshot.exists
? (id)[documentSnapshot dataWithServerTimestampBehavior:serverTimestampBehavior]
: [NSNull null],
@"metadata" : documentSnapshot.metadata,
};
}
- (NSDictionary *)FIRLoadBundleTaskProgress:(FIRLoadBundleTaskProgress *)progress {
NSString *state;
switch (progress.state) {
case FIRLoadBundleTaskStateError:
state = @"error";
break;
case FIRLoadBundleTaskStateSuccess:
state = @"success";
break;
case FIRLoadBundleTaskStateInProgress:
state = @"running";
break;
}
return @{
@"bytesLoaded" : @(progress.bytesLoaded),
@"documentsLoaded" : @(progress.documentsLoaded),
@"totalBytes" : @(progress.totalBytes),
@"totalDocuments" : @(progress.totalDocuments),
@"taskState" : state,
};
}
- (NSDictionary *)FIRQuerySnapshot:(FIRQuerySnapshot *)querySnapshot {
NSMutableArray *paths = [NSMutableArray array];
NSMutableArray *documents = [NSMutableArray array];
NSMutableArray *metadatas = [NSMutableArray array];
FIRServerTimestampBehavior serverTimestampBehavior =
[self toServerTimestampBehavior:FLTFirebaseFirestorePlugin
.serverTimestampMap[@([querySnapshot hash])]];
[FLTFirebaseFirestorePlugin.serverTimestampMap removeObjectForKey:@([querySnapshot hash])];
for (FIRDocumentSnapshot *document in querySnapshot.documents) {
[paths addObject:document.reference.path];
[documents addObject:[document dataWithServerTimestampBehavior:serverTimestampBehavior]];
[metadatas addObject:document.metadata];
}
return @{
@"paths" : paths,
@"documentChanges" : querySnapshot.documentChanges,
@"documents" : documents,
@"metadatas" : metadatas,
@"metadata" : querySnapshot.metadata,
};
}
@end