2323#import " FIRTimestamp.h"
2424
2525#import " Firestore/Source/API/FIRGeoPoint+Internal.h"
26+ #import " Firestore/Source/API/FIRTimestamp+Internal.h"
2627#import " Firestore/Source/API/converters.h"
2728#import " Firestore/Source/Model/FSTDocumentKey.h"
2829#import " Firestore/Source/Util/FSTClasses.h"
@@ -127,66 +128,6 @@ - (double)doubleValue {
127128
128129@end
129130
130- #pragma mark - FSTTimestampValue
131-
132- @implementation FSTTimestampValue {
133- Timestamp _internalValue;
134- }
135-
136- + (instancetype )timestampValue : (const Timestamp &)value {
137- return [[FSTTimestampValue alloc ] initWithValue: value];
138- }
139-
140- - (id )initWithValue : (const Timestamp &)value {
141- self = [super init ];
142- if (self) {
143- _internalValue = value;
144- }
145- return self;
146- }
147-
148- - (FieldValue::Type)type {
149- return FieldValue::Type::Timestamp;
150- }
151-
152- - (FSTTypeOrder)typeOrder {
153- return FSTTypeOrderTimestamp;
154- }
155-
156- - (id )value {
157- return MakeFIRTimestamp (_internalValue);
158- }
159-
160- - (id )valueWithOptions : (const FieldValueOptions &)options {
161- if (options.timestamps_in_snapshots_enabled ()) {
162- return self.value ;
163- } else {
164- return [self .value dateValue ];
165- }
166- }
167-
168- - (BOOL )isEqual : (id )other {
169- return [other isKindOfClass: [FSTFieldValue class ]] &&
170- ((FSTFieldValue *)other).type == FieldValue::Type::Timestamp &&
171- _internalValue == ((FSTTimestampValue *)other)->_internalValue ;
172- }
173-
174- - (NSUInteger )hash {
175- return TimestampInternal::Hash (_internalValue);
176- }
177-
178- - (NSComparisonResult )compare : (FSTFieldValue *)other {
179- if (other.type == FieldValue::Type::Timestamp) {
180- return WrapCompare (_internalValue, ((FSTTimestampValue *)other)->_internalValue );
181- } else if (other.type == FieldValue::Type::ServerTimestamp) {
182- // Concrete timestamps come before server timestamps.
183- return NSOrderedAscending;
184- } else {
185- return [self defaultCompare: other];
186- }
187- }
188-
189- @end
190131#pragma mark - FSTServerTimestampValue
191132
192133@implementation FSTServerTimestampValue {
@@ -226,7 +167,7 @@ - (id)valueWithOptions:(const FieldValueOptions &)options {
226167 case ServerTimestampBehavior::kNone :
227168 return [NSNull null ];
228169 case ServerTimestampBehavior::kEstimate :
229- return [[FSTTimestampValue timestampValue: self .localWriteTime] valueWithOptions: options];
170+ return [FieldValue::FromTimestamp ( self .localWriteTime). Wrap () valueWithOptions: options];
230171 case ServerTimestampBehavior::kPrevious :
231172 return self.previousValue ? [self .previousValue valueWithOptions: options] : [NSNull null ];
232173 default :
@@ -683,7 +624,11 @@ - (id)value {
683624 return @(self.internalValue .integer_value ());
684625 case FieldValue::Type::Double:
685626 return @(self.internalValue .double_value ());
686- case FieldValue::Type::Timestamp:
627+ case FieldValue::Type::Timestamp: {
628+ auto timestamp = self.internalValue .timestamp_value ();
629+ return [[FIRTimestamp alloc ] initWithSeconds: timestamp.seconds ()
630+ nanoseconds: timestamp.nanoseconds ()];
631+ }
687632 case FieldValue::Type::ServerTimestamp:
688633 HARD_FAIL (" TODO(rsgowman): implement" );
689634 case FieldValue::Type::String:
@@ -701,6 +646,20 @@ - (id)value {
701646 UNREACHABLE ();
702647}
703648
649+ - (id )valueWithOptions : (const model::FieldValueOptions &)options {
650+ switch (self.internalValue .type ()) {
651+ case FieldValue::Type::Timestamp:
652+ if (options.timestamps_in_snapshots_enabled ()) {
653+ return [self value ];
654+ } else {
655+ return [[self value ] dateValue ];
656+ }
657+
658+ default :
659+ return [self value ];
660+ }
661+ }
662+
704663- (NSComparisonResult )compare : (FSTFieldValue *)other {
705664 // TODO(rsgowman): Port the other FST*Value's, and then remove this comment:
706665 //
@@ -710,8 +669,14 @@ - (NSComparisonResult)compare:(FSTFieldValue *)other {
710669 // FSTDelegateValue handles (eg) booleans to ensure this case never occurs.
711670
712671 if (FieldValue::Comparable (self.type , other.type )) {
713- HARD_ASSERT ([other isKindOfClass: [FSTDelegateValue class ]]);
714- return WrapCompare<FieldValue>(self.internalValue , ((FSTDelegateValue *)other).internalValue );
672+ if ([other isKindOfClass: [FSTServerTimestampValue class ]]) {
673+ HARD_ASSERT (self.type == FieldValue::Type::Timestamp);
674+ // Server timestamps come after all concrete timestamps.
675+ return NSOrderedAscending;
676+ } else {
677+ HARD_ASSERT ([other isKindOfClass: [FSTDelegateValue class ]]);
678+ return WrapCompare<FieldValue>(self.internalValue , ((FSTDelegateValue *)other).internalValue );
679+ }
715680 } else {
716681 return [self defaultCompare: other];
717682 }
0 commit comments