Skip to content

[cloud_firestore] Created firestore platform interface #1686

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Feb 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
0803aef
Created firestore platform interface
amrfarid140 Dec 20, 2019
3c57adb
Fixes for multiple apps support
amrfarid140 Dec 21, 2019
e309a8e
Fixed format issues
amrfarid140 Dec 21, 2019
0c7ccf1
Fix lint issues and remove analysis_options.yml
amrfarid140 Dec 21, 2019
d1898fd
Fix typo
amrfarid140 Dec 21, 2019
6ce4a4b
Fix analyze issues
amrfarid140 Dec 21, 2019
872959d
Added visiblefortesting annotation
amrfarid140 Dec 23, 2019
41d5117
Appease analyzer
ditman Dec 23, 2019
e53dad1
use instanceFor
amrfarid140 Jan 8, 2020
a6b319b
Apply suggestions for cloud_firestore_platform_interface
amrfarid140 Jan 8, 2020
5dea377
Merge remote-tracking branch 'origin/firestore_web/platform_interface…
amrfarid140 Jan 8, 2020
89d0ddb
add visible for testing annotation on `channel`
amrfarid140 Jan 8, 2020
b0e5c5e
Apply suggestions from code review
amrfarid140 Jan 8, 2020
49abc85
Merge remote-tracking branch 'origin/firestore_web/platform_interface…
amrfarid140 Jan 8, 2020
41e9d92
Formatting fixes
amrfarid140 Jan 8, 2020
8747a19
make transaction finish as private
amrfarid140 Jan 8, 2020
6f34bec
Use correct Type for Transaction and WriteBatch & revert privatisation
amrfarid140 Jan 8, 2020
e2faaaf
Expose transaction finish as ppublic
amrfarid140 Jan 17, 2020
595b747
Fixed documentation comments
amrfarid140 Jan 17, 2020
8933d26
Added latest changes from @ditman and @collinjackson
amrfarid140 Feb 5, 2020
e09e384
Merge branch 'master' of github.com:FirebaseExtended/flutterfire into…
amrfarid140 Feb 5, 2020
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.buildlog/
.history
.svn/

# IntelliJ related
*.iml
*.ipr
*.iws
.idea/
.metadata

# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
#.vscode/

# Flutter/Dart/Pub related
**/doc/api/
.dart_tool/
.flutter-plugins
.flutter-plugins-dependencies
.packages
.pub-cache/
.pub/
build/

# Android related
**/android/**/gradle-wrapper.jar
**/android/.gradle
**/android/captures/
**/android/gradlew
**/android/gradlew.bat
**/android/local.properties
**/android/**/GeneratedPluginRegistrant.java

# iOS/XCode related
**/ios/**/*.mode1v3
**/ios/**/*.mode2v3
**/ios/**/*.moved-aside
**/ios/**/*.pbxuser
**/ios/**/*.perspectivev3
**/ios/**/*sync/
**/ios/**/.sconsign.dblite
**/ios/**/.tags*
**/ios/**/.vagrant/
**/ios/**/DerivedData/
**/ios/**/Icon?
**/ios/**/Pods/
**/ios/**/.symlinks/
**/ios/**/profile
**/ios/**/xcuserdata
**/ios/.generated/
**/ios/Flutter/App.framework
**/ios/Flutter/Flutter.framework
**/ios/Flutter/Flutter.podspec
**/ios/Flutter/Generated.xcconfig
**/ios/Flutter/app.flx
**/ios/Flutter/app.zip
**/ios/Flutter/flutter_assets/
**/ios/Flutter/flutter_export_environment.sh
**/ios/ServiceDefinitions.json
**/ios/Runner/GeneratedPluginRegistrant.*

# Exceptions to above rules.
!**/ios/**/default.mode1v3
!**/ios/**/default.mode2v3
!**/ios/**/default.pbxuser
!**/ios/**/default.perspectivev3
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## [1.0.0]

* Created Platform Interface
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Copyright 2017, the Chromium project authors. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# cloud_firestore_platform_interface

A common platform interface for the [`cloud_firestore`][1] plugin.

This interface allows platform-specific implementations of the `cloud_firestore`
plugin, as well as the plugin itself, to ensure they are supporting the
same interface.

# Usage

To implement a new platform-specific implementation of `cloud_firebase`, extend
[`FirestorePlatform`][2] with an implementation that performs the
platform-specific behavior, and when you register your plugin, set the default
`FirestorePlatform` by calling
`FirestorePlatform.instance = MyFirestore()`.

# Note on breaking changes

Strongly prefer non-breaking changes (such as adding a method to the interface)
over breaking changes for this package.

See https://flutter.dev/go/platform-interface-breaking-changes for a discussion
on why a less-clean interface is preferable to a breaking change.

[1]: ../cloud_firestore
[2]: lib/cloud_firestore_platform_interface.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
// Copyright 2017, 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.

library cloud_firestore_platform_interface;

import 'dart:async';

import 'package:firebase_core/firebase_core.dart';
import 'package:plugin_platform_interface/plugin_platform_interface.dart';

import 'src/method_channel/method_channel_firestore.dart';

import 'src/platform_interface/collection_reference.dart';
import 'src/platform_interface/document_reference.dart';
import 'src/platform_interface/query.dart';
import 'src/platform_interface/transaction.dart';
import 'src/platform_interface/write_batch.dart';

// Shared types
export 'src/blob.dart';
export 'src/document_snapshot.dart';
export 'src/field_path.dart';
export 'src/geo_point.dart';
export 'src/snapshot_metadata.dart';
export 'src/source.dart';
export 'src/timestamp.dart';

// Platform interface parts
export 'src/platform_interface/collection_reference.dart';
export 'src/platform_interface/document_change.dart';
export 'src/platform_interface/document_reference.dart';
export 'src/platform_interface/field_value_factory.dart';
export 'src/platform_interface/query.dart';
export 'src/platform_interface/query_snapshot.dart';
export 'src/platform_interface/transaction.dart';
export 'src/platform_interface/write_batch.dart';

/// Defines an interface to work with [FirestorePlatform] on web and mobile
abstract class FirestorePlatform extends PlatformInterface {
/// The app associated with this Firestore instance.
final FirebaseApp app;

/// Create an instance using [app]
FirestorePlatform({FirebaseApp app})
: app = app ?? FirebaseApp.instance,
super(token: _token);

static final Object _token = Object();

/// Create an instance using [app] using the existing implementation
factory FirestorePlatform.instanceFor({FirebaseApp app}) {
return FirestorePlatform.instance.withApp(app);
}

/// The current default [FirestorePlatform] instance.
///
/// It will always default to [MethodChannelFirestore]
/// if no web implementation was provided.
static FirestorePlatform get instance {
if (_instance == null) {
_instance = MethodChannelFirestore();
}
return _instance;
}

static FirestorePlatform _instance;

/// Sets the [FirestorePlatform.instance]
static set instance(FirestorePlatform instance) {
PlatformInterface.verifyToken(instance, _token);
_instance = instance;
}

/// Create a new [FirestorePlatform] with a [FirebaseApp] instance
FirestorePlatform withApp(FirebaseApp app) {
throw UnimplementedError("withApp() not implemented");
}

/// Gets a [CollectionReferencePlatform] for the specified Firestore path.
CollectionReferencePlatform collection(String path) {
throw UnimplementedError('collection() is not implemented');
}

/// Gets a [QueryPlatform] for the specified collection group.
QueryPlatform collectionGroup(String path) {
throw UnimplementedError('collectionGroup() is not implemented');
}

/// Gets a [DocumentReferencePlatform] for the specified Firestore path.
DocumentReferencePlatform document(String path) {
throw UnimplementedError('document() is not implemented');
}

/// Creates a write batch, used for performing multiple writes as a single
/// atomic operation.
///
/// Unlike transactions, write batches are persisted offline and therefore are
/// preferable when you don’t need to condition your writes on read data.
WriteBatchPlatform batch() {
throw UnimplementedError('batch() is not implemented');
}

/// Executes the given [TransactionHandler] and then attempts to commit the
/// changes applied within an atomic transaction.
///
/// In the [TransactionHandler], a set of reads and writes can be performed
/// atomically using the [MethodChannelTransaction] object passed to the [TransactionHandler].
/// After the [TransactionHandler] is run, Firestore will attempt to apply the
/// changes to the server. If any of the data read has been modified outside
/// of this transaction since being read, then the transaction will be
/// retried by executing the updateBlock again. If the transaction still
/// fails after 5 retries, then the transaction will fail.
///
/// The [TransactionHandler] may be executed multiple times, it should be able
/// to handle multiple executions.
///
/// Data accessed with the transaction will not reflect local changes that
/// have not been committed. For this reason, it is required that all
/// reads are performed before any writes. Transactions must be performed
/// while online. Otherwise, reads will fail, and the final commit will fail.
///
/// By default transactions are limited to 5 seconds of execution time. This
/// timeout can be adjusted by setting the timeout parameter.
Future<Map<String, dynamic>> runTransaction(
TransactionHandler transactionHandler,
{Duration timeout = const Duration(seconds: 5)}) async {
throw UnimplementedError('runTransaction() is not implemented');
}

@deprecated
// Suppressing due to deprecation
// ignore: public_member_api_docs
Future<void> enablePersistence(bool enable) async {
throw UnimplementedError('enablePersistence() is not implemented');
}

/// Setup [FirestorePlatform] with settings.
///
/// If [sslEnabled] has a non-null value, the [host] must have non-null value as well.
///
/// If [cacheSizeBytes] is `null`, then default values are used.
Future<void> settings(
{bool persistenceEnabled,
String host,
bool sslEnabled,
int cacheSizeBytes}) async {
throw UnimplementedError('settings() is not implemented');
}

@override
int get hashCode => app.name.hashCode;

@override
bool operator ==(dynamic o) => o is FirestorePlatform && o.app == app;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2017, 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:typed_data';
import 'dart:ui';

import 'package:collection/collection.dart';

/// Represents binary data stored in [Uint8List].
class Blob {
/// Create a blob.
const Blob(this.bytes);

/// The bytes that are contained in this blob.
final Uint8List bytes;

@override
bool operator ==(dynamic other) =>
other is Blob &&
const DeepCollectionEquality().equals(other.bytes, bytes);

@override
int get hashCode => hashList(bytes);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2017, 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 'package:cloud_firestore_platform_interface/cloud_firestore_platform_interface.dart';

/// Contains data read from a document in your Firestore
/// database.
///
/// The data can be extracted with the [data] property or by using subscript
/// syntax to access a specific field.
class DocumentSnapshotPlatform {
/// Constructs a [DocumentSnapshotPlatform] using the provided [FirestorePlatform].
DocumentSnapshotPlatform(
this._path, this.data, this.metadata, this.firestore);

final String _path;

/// The [FirestorePlatform] used to produce this [DocumentSnapshotPlatform].
final FirestorePlatform firestore;

/// The reference that produced this snapshot.
DocumentReferencePlatform get reference => firestore.document(_path);

/// Contains all the data of this snapshot.
final Map<String, dynamic> data;

/// Metadata about this snapshot concerning its source and if it has local
/// modifications.
final SnapshotMetadataPlatform metadata;

/// Reads individual values from the snapshot.
dynamic operator [](String key) => data[key];

/// The database ID of the snapshot's document.
String get documentID => _path.split('/').last;

/// Returns `true` if the document exists.
bool get exists => data != null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2017, 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 'internal/field_path_type.dart';

/// A [FieldPath] refers to a field in a document.
class FieldPath {
const FieldPath._(this.type);

/// The type of this field path (used in [FirestoreMessageCodec])
final FieldPathType type;

/// The path to the document id, which can be used in queries.
static FieldPath get documentId =>
const FieldPath._(FieldPathType.documentId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2017, 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';

/// Represents a geographical point by its longitude and latitude
class GeoPoint {
/// Create [GeoPoint] instance
const GeoPoint(this.latitude, this.longitude);

final double latitude; // ignore: public_member_api_docs
final double longitude; // ignore: public_member_api_docs

@override
bool operator ==(dynamic o) =>
o is GeoPoint && o.latitude == latitude && o.longitude == longitude;

@override
int get hashCode => hashValues(latitude, longitude);
}
Loading