Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
Multiple app support for cloud_firestore
Browse files Browse the repository at this point in the history
  • Loading branch information
collinjackson committed Apr 9, 2018
1 parent 6e5de75 commit 805cb06
Show file tree
Hide file tree
Showing 18 changed files with 294 additions and 137 deletions.
4 changes: 4 additions & 0 deletions packages/cloud_firestore/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.4.1

* Support for multiple Firebase apps

## 0.4.0

* **Breaking change**. Hide Firestore codec class from public API.
Expand Down
1 change: 0 additions & 1 deletion packages/cloud_firestore/android/gradle.properties

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.TaskCompletionSource;
import com.google.android.gms.tasks.Tasks;
import com.google.firebase.FirebaseApp;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.DocumentChange;
import com.google.firebase.firestore.DocumentReference;
Expand Down Expand Up @@ -72,14 +73,19 @@ private CloudFirestorePlugin(MethodChannel channel) {
this.channel = channel;
}

private FirebaseFirestore getFirestore(Map<String, Object> arguments) {
String appName = (String) arguments.get("app");
return FirebaseFirestore.getInstance(FirebaseApp.getInstance(appName));
}

private CollectionReference getCollectionReference(Map<String, Object> arguments) {
String path = (String) arguments.get("path");
return FirebaseFirestore.getInstance().collection(path);
return getFirestore(arguments).collection(path);
}

private DocumentReference getDocumentReference(Map<String, Object> arguments) {
String path = (String) arguments.get("path");
return FirebaseFirestore.getInstance().document(path);
return getFirestore(arguments).document(path);
}

private Map<String, Object> parseQuerySnapshot(QuerySnapshot querySnapshot) {
Expand Down Expand Up @@ -248,7 +254,7 @@ public void onMethodCall(MethodCall call, final Result result) {
final Task<Map<String, Object>> transactionTCSTask = transactionTCS.getTask();

final Map<String, Object> arguments = call.arguments();
FirebaseFirestore.getInstance()
getFirestore(arguments)
.runTransaction(
new Transaction.Function<Void>() {
@Nullable
Expand Down Expand Up @@ -379,7 +385,8 @@ protected Void doInBackground(Void... voids) {
case "WriteBatch#create":
{
int handle = nextBatchHandle++;
WriteBatch batch = FirebaseFirestore.getInstance().batch();
final Map<String, Object> arguments = call.arguments();
WriteBatch batch = getFirestore(arguments).batch();
batches.put(handle, batch);
result.success(handle);
break;
Expand Down Expand Up @@ -573,6 +580,9 @@ protected void writeValue(ByteArrayOutputStream stream, Object value) {
writeDouble(stream, ((GeoPoint) value).getLongitude());
} else if (value instanceof DocumentReference) {
stream.write(DOCUMENT_REFERENCE);
// TODO(jackson): Null checking
writeBytes(
stream, ((DocumentReference) value).getFirestore().getApp().getName().getBytes(UTF8));
writeBytes(stream, ((DocumentReference) value).getPath().getBytes(UTF8));
} else {
super.writeValue(stream, value);
Expand All @@ -588,9 +598,13 @@ protected Object readValueOfType(byte type, ByteBuffer buffer) {
readAlignment(buffer, 8);
return new GeoPoint(buffer.getDouble(), buffer.getDouble());
case DOCUMENT_REFERENCE:
final byte[] bytes = readBytes(buffer);
final String path = new String(bytes, UTF8);
return FirebaseFirestore.getInstance().document(path);
final byte[] appNameBytes = readBytes(buffer);
String appName = new String(appNameBytes, UTF8);
final FirebaseFirestore firestore =
FirebaseFirestore.getInstance(FirebaseApp.getInstance(appName));
final byte[] pathBytes = readBytes(buffer);
final String path = new String(pathBytes, UTF8);
return firestore.document(path);
default:
return super.readValueOfType(type, buffer);
}
Expand Down
35 changes: 29 additions & 6 deletions packages/cloud_firestore/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,35 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:cloud_firestore/cloud_firestore.dart';

void main() {
runApp(new MaterialApp(title: 'Firestore Example', home: new MyHomePage()));
Future main() async {
final FirebaseOptions options = const FirebaseOptions(
googleAppID: '1:79601577497:ios:5f2bcc6ba8cecddd',
gcmSenderID: '79601577497',
apiKey: 'AIzaSyArgmRGfB5kiQT6CunAOmKRVKEsxKmy6YI-G72PVU',
projectID: 'flutter-firestore',
);
FirebaseApp.configure(
name: 'test',
options: options,
);
final Firestore firestore = new Firestore(app: new FirebaseApp.named('test'));

runApp(new MaterialApp(
title: 'Firestore Example', home: new MyHomePage(firestore: firestore)));
}

class MessageList extends StatelessWidget {
MessageList({this.firestore});

final Firestore firestore;

@override
Widget build(BuildContext context) {
return new StreamBuilder<QuerySnapshot>(
stream: Firestore.instance.collection('messages').snapshots,
stream: firestore.collection('messages').snapshots,
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (!snapshot.hasData) return const Text('Loading...');
final int messageCount = snapshot.data.documents.length;
Expand All @@ -35,10 +53,15 @@ class MessageList extends StatelessWidget {
}

class MyHomePage extends StatelessWidget {
CollectionReference get messages => Firestore.instance.collection('messages');
MyHomePage({this.firestore});
final Firestore firestore;
CollectionReference get messages => firestore.collection('messages');

Future<Null> _addMessage() async {
messages.document().setData(<String, String>{'message': 'Hello world!'});
final DocumentReference document = messages.document();
document.setData(<String, dynamic>{
'message': 'Hello world!',
});
}

@override
Expand All @@ -47,7 +70,7 @@ class MyHomePage extends StatelessWidget {
appBar: new AppBar(
title: const Text('Firestore Example'),
),
body: new MessageList(),
body: new MessageList(firestore: firestore),
floatingActionButton: new FloatingActionButton(
onPressed: _addMessage,
tooltip: 'Increment',
Expand Down
2 changes: 2 additions & 0 deletions packages/cloud_firestore/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ dependencies:
sdk: flutter
cloud_firestore:
path: ../
firebase_core: # "^0.1.2"
path: ../../firebase_core

flutter:
uses-material-design: true
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

#import <Flutter/Flutter.h>

@interface FLTCloudFirestorePlugin : NSObject<FlutterPlugin>
@interface FLTCloudFirestorePlugin : NSObject <FlutterPlugin>
@end
Loading

0 comments on commit 805cb06

Please sign in to comment.