From a943f7ea9c945b95cc5395396e955786b7f9a3ce Mon Sep 17 00:00:00 2001 From: WolfTheZelda Date: Fri, 28 Feb 2020 15:35:21 -0300 Subject: [PATCH 1/4] Implemented 'exists' and 'timestamp' --- lib/cloud_firestore_all.dart | 2 + lib/src/interfaces/document.dart | 3 + lib/src/mobile/document.dart | 1 + lib/src/timestamp.dart | 107 +++++++++++++++++++++++++++++++ lib/src/web/document.dart | 3 + 5 files changed, 116 insertions(+) create mode 100644 lib/src/timestamp.dart diff --git a/lib/cloud_firestore_all.dart b/lib/cloud_firestore_all.dart index e337c50..4100308 100644 --- a/lib/cloud_firestore_all.dart +++ b/lib/cloud_firestore_all.dart @@ -3,3 +3,5 @@ library cloud_firestore_all; export 'src/interface.dart' if (dart.library.io) 'src/mobile.dart' if (dart.library.js) 'src/web.dart'; + +export 'src/timestamp.dart'; \ No newline at end of file diff --git a/lib/src/interfaces/document.dart b/lib/src/interfaces/document.dart index 76e2b0a..c86746f 100644 --- a/lib/src/interfaces/document.dart +++ b/lib/src/interfaces/document.dart @@ -15,6 +15,9 @@ abstract class DocumentSnapshot { /// Contains all the data of this snapshot Map get data; + /// Returns `true` if the document exists. + bool get exists => data != null; + /// Reads individual values from the snapshot dynamic operator [](String key); } diff --git a/lib/src/mobile/document.dart b/lib/src/mobile/document.dart index 7f0cb86..e870557 100644 --- a/lib/src/mobile/document.dart +++ b/lib/src/mobile/document.dart @@ -9,6 +9,7 @@ class DocumentSnapshot implements intf.DocumentSnapshot { DocumentReference get ref => DocumentReference(_documentSnapshot.reference); String get id => _documentSnapshot.documentID; Map get data => _documentSnapshot.data; + bool get exists => data != null; operator [](String key) => _documentSnapshot[key]; } diff --git a/lib/src/timestamp.dart b/lib/src/timestamp.dart new file mode 100644 index 0000000..1172add --- /dev/null +++ b/lib/src/timestamp.dart @@ -0,0 +1,107 @@ +// Copyright 2018, the Chromium project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. +import 'dart:ui'; + +const int _kThousand = 1000; +const int _kMillion = 1000000; +const int _kBillion = 1000000000; + +void _check(bool expr, String name, int value) { + if (!expr) { + throw ArgumentError("Timestamp $name out of range: $value"); + } +} + +/// A Timestamp represents a point in time independent of any time zone or calendar, +/// represented as seconds and fractions of seconds at nanosecond resolution in UTC +/// Epoch time. It is encoded using the Proleptic Gregorian Calendar which extends +/// the Gregorian calendar backwards to year one. It is encoded assuming all minutes +/// are 60 seconds long, i.e. leap seconds are "smeared" so that no leap second table +/// is needed for interpretation. Range is from 0001-01-01T00:00:00Z to +/// 9999-12-31T23:59:59.999999999Z. By restricting to that range, we ensure that we +/// can convert to and from RFC 3339 date strings. +/// +/// For more information, see [the reference timestamp definition](https://github.com/google/protobuf/blob/master/src/google/protobuf/timestamp.proto) +class Timestamp implements Comparable { + /// Creates a [Timestamp] + Timestamp(this._seconds, this._nanoseconds) { + _validateRange(_seconds, _nanoseconds); + } + + /// Create a [Timestamp] fromMillisecondsSinceEpoch + factory Timestamp.fromMillisecondsSinceEpoch(int milliseconds) { + final int seconds = (milliseconds / _kThousand).floor(); + final int nanoseconds = (milliseconds - seconds * _kThousand) * _kMillion; + return Timestamp(seconds, nanoseconds); + } + + /// Create a [Timestamp] fromMicrosecondsSinceEpoch + factory Timestamp.fromMicrosecondsSinceEpoch(int microseconds) { + final int seconds = (microseconds / _kMillion).floor(); + final int nanoseconds = (microseconds - seconds * _kMillion) * _kThousand; + return Timestamp(seconds, nanoseconds); + } + + /// Create a [Timestamp] from [DateTime] instance + factory Timestamp.fromDate(DateTime date) { + return Timestamp.fromMicrosecondsSinceEpoch(date.microsecondsSinceEpoch); + } + + /// Create a [Timestamp] from [DateTime].now() + factory Timestamp.now() { + return Timestamp.fromMicrosecondsSinceEpoch( + DateTime.now().microsecondsSinceEpoch); + } + + final int _seconds; + final int _nanoseconds; + + static const int _kStartOfTime = -62135596800; + static const int _kEndOfTime = 253402300800; + + // ignore: public_member_api_docs + int get seconds => _seconds; + + // ignore: public_member_api_docs + int get nanoseconds => _nanoseconds; + + // ignore: public_member_api_docs + int get millisecondsSinceEpoch => + (seconds * _kThousand + nanoseconds / _kMillion).floor(); + + // ignore: public_member_api_docs + int get microsecondsSinceEpoch => + (seconds * _kMillion + nanoseconds / _kThousand).floor(); + + /// Converts [Timestamp] to [DateTime] + DateTime toDate() { + return DateTime.fromMicrosecondsSinceEpoch(microsecondsSinceEpoch); + } + + @override + int get hashCode => hashValues(seconds, nanoseconds); + @override + bool operator ==(dynamic o) => + o is Timestamp && o.seconds == seconds && o.nanoseconds == nanoseconds; + @override + int compareTo(Timestamp other) { + if (seconds == other.seconds) { + return nanoseconds.compareTo(other.nanoseconds); + } + + return seconds.compareTo(other.seconds); + } + + @override + String toString() { + return "Timestamp(seconds=$seconds, nanoseconds=$nanoseconds)"; + } + + static void _validateRange(int seconds, int nanoseconds) { + _check(nanoseconds >= 0, 'nanoseconds', nanoseconds); + _check(nanoseconds < _kBillion, 'nanoseconds', nanoseconds); + _check(seconds >= _kStartOfTime, 'seconds', seconds); + _check(seconds < _kEndOfTime, 'seconds', seconds); + } +} diff --git a/lib/src/web/document.dart b/lib/src/web/document.dart index 2bbddde..dd89db2 100644 --- a/lib/src/web/document.dart +++ b/lib/src/web/document.dart @@ -15,6 +15,9 @@ class DocumentSnapshot implements intf.DocumentSnapshot { @override Map get data => documentSnapshot.data(); + @override + bool get exists => data != null; + @override operator [](String key) => documentSnapshot.get(key); } From f64eae3f0be8b92f326be6b95f2434ff12517336 Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Fri, 28 Feb 2020 18:41:07 +0000 Subject: [PATCH 2/4] Restyled by whitespace --- lib/cloud_firestore_all.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cloud_firestore_all.dart b/lib/cloud_firestore_all.dart index 4100308..e7091a5 100644 --- a/lib/cloud_firestore_all.dart +++ b/lib/cloud_firestore_all.dart @@ -4,4 +4,4 @@ export 'src/interface.dart' if (dart.library.io) 'src/mobile.dart' if (dart.library.js) 'src/web.dart'; -export 'src/timestamp.dart'; \ No newline at end of file +export 'src/timestamp.dart'; From 07fe87569bea6eedd4085c1f17026a2a30b41db4 Mon Sep 17 00:00:00 2001 From: WolfTheZelda Date: Fri, 28 Feb 2020 17:54:01 -0300 Subject: [PATCH 3/4] Fixed 'timestamp' error --- example/pubspec.lock | 14 ++++++++++++++ lib/cloud_firestore_all.dart | 3 ++- pubspec.lock | 14 ++++++++++++++ pubspec.yaml | 3 +++ 4 files changed, 33 insertions(+), 1 deletion(-) diff --git a/example/pubspec.lock b/example/pubspec.lock index 3e17f6e..ef4dcc6 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -50,6 +50,13 @@ packages: relative: true source: path version: "0.1.1+4" + cloud_firestore_platform_interface: + dependency: transitive + description: + name: cloud_firestore_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.0" collection: dependency: transitive description: @@ -184,6 +191,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.4.0" + plugin_platform_interface: + dependency: transitive + description: + name: plugin_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.2" quiver: dependency: transitive description: diff --git a/lib/cloud_firestore_all.dart b/lib/cloud_firestore_all.dart index e7091a5..3a2b66c 100644 --- a/lib/cloud_firestore_all.dart +++ b/lib/cloud_firestore_all.dart @@ -4,4 +4,5 @@ export 'src/interface.dart' if (dart.library.io) 'src/mobile.dart' if (dart.library.js) 'src/web.dart'; -export 'src/timestamp.dart'; +//export 'src/timestamp.dart'; +export 'package:cloud_firestore_platform_interface/src/timestamp.dart'; diff --git a/pubspec.lock b/pubspec.lock index 1368cff..a2e27f5 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -22,6 +22,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.13.0+1" + cloud_firestore_platform_interface: + dependency: "direct main" + description: + name: cloud_firestore_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.0" collection: dependency: transitive description: @@ -116,6 +123,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.9.0" + plugin_platform_interface: + dependency: transitive + description: + name: plugin_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.2" quiver: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 4005574..e865586 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -16,3 +16,6 @@ dependencies: # Web: firebase: ^7.1.0 + + # Interface: + cloud_firestore_platform_interface: ^1.1.0 \ No newline at end of file From 02bc028b8b5d90ab0829be0a1ffb23ac82f8f62b Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Fri, 28 Feb 2020 20:54:20 +0000 Subject: [PATCH 4/4] Restyled by prettier-yaml --- pubspec.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pubspec.yaml b/pubspec.yaml index e865586..ab91543 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -16,6 +16,6 @@ dependencies: # Web: firebase: ^7.1.0 - + # Interface: - cloud_firestore_platform_interface: ^1.1.0 \ No newline at end of file + cloud_firestore_platform_interface: ^1.1.0