Skip to content
This repository has been archived by the owner on Apr 7, 2020. It is now read-only.

Restyle Implemented 'exists' and 'timestamp' #17

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions lib/cloud_firestore_all.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ 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';
export 'package:cloud_firestore_platform_interface/src/timestamp.dart';
3 changes: 3 additions & 0 deletions lib/src/interfaces/document.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ abstract class DocumentSnapshot {
/// Contains all the data of this snapshot
Map<String, dynamic> get data;

/// Returns `true` if the document exists.
bool get exists => data != null;

/// Reads individual values from the snapshot
dynamic operator [](String key);
}
Expand Down
1 change: 1 addition & 0 deletions lib/src/mobile/document.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class DocumentSnapshot implements intf.DocumentSnapshot {
DocumentReference get ref => DocumentReference(_documentSnapshot.reference);
String get id => _documentSnapshot.documentID;
Map<String, dynamic> get data => _documentSnapshot.data;
bool get exists => data != null;

operator [](String key) => _documentSnapshot[key];
}
Expand Down
107 changes: 107 additions & 0 deletions lib/src/timestamp.dart
Original file line number Diff line number Diff line change
@@ -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<Timestamp> {
/// 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);
}
}
3 changes: 3 additions & 0 deletions lib/src/web/document.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class DocumentSnapshot implements intf.DocumentSnapshot {
@override
Map<String, dynamic> get data => documentSnapshot.data();

@override
bool get exists => data != null;

@override
operator [](String key) => documentSnapshot.get(key);
}
Expand Down
14 changes: 14 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ dependencies:

# Web:
firebase: ^7.1.0

# Interface:
cloud_firestore_platform_interface: ^1.1.0