Created
November 20, 2019 11:04
-
-
Save ramoncardena/2f351b2d344ce8f8ea4ea2a44d3ab589 to your computer and use it in GitHub Desktop.
Futter service to manage Firebase Cloud Firestore datastores. The classes in db.dart allow us to manage collections, documents, or collections with the name of the user.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:cloud_firestore/cloud_firestore.dart'; | |
import 'package:firebase_auth/firebase_auth.dart'; | |
import 'dart:async'; | |
import 'package:rxdart/rxdart.dart'; | |
import './globals.dart'; | |
class Document<T> { | |
final Firestore _db = Firestore.instance; | |
final String path; | |
DocumentReference ref; | |
Document({this.path}) { | |
ref = _db.document(path); | |
} | |
Future<T> getData() { | |
return ref.get().then((v) => Global.models[T](v.data) as T); | |
} | |
Stream<T> streamData() { | |
return ref.snapshots().map((v) => Global.models[T](v.data) as T); | |
} | |
Future<void> upsert(Map data) { | |
return ref.setData(Map<String, dynamic>.from(data), merge: true); | |
} | |
} | |
class Collection<T> { | |
final Firestore _db = Firestore.instance; | |
final String path; | |
CollectionReference ref; | |
Collection({this.path}) { | |
ref = _db.collection(path); | |
} | |
Future<List<T>> getData() async { | |
var snapshots = await ref.getDocuments(); | |
return snapshots.documents | |
.map((doc) => Global.models[T](doc.data) as T) | |
.toList(); | |
} | |
Stream<List<T>> streamData() { | |
return ref.snapshots().map( | |
(list) => list.documents.map((doc) => Global.models[T](doc.data) as T)); | |
} | |
} | |
class UserData<T> { | |
final Firestore _db = Firestore.instance; | |
final FirebaseAuth _auth = FirebaseAuth.instance; | |
final String collection; | |
UserData({this.collection}); | |
Stream<T> get documentStream { | |
return Observable(_auth.onAuthStateChanged).switchMap((user) { | |
if (user != null) { | |
Document<T> doc = Document<T>(path: '$collection/${user.uid}'); | |
return doc.streamData(); | |
} else { | |
return Observable<T>.just(null); | |
} | |
}); //.shareReplay(maxSize: 1).doOnData((d) => print('777 $d'));// as Stream<T>; | |
} | |
Future<T> getDocument() async { | |
FirebaseUser user = await _auth.currentUser(); | |
if (user != null) { | |
Document doc = Document<T>(path: '$collection/${user.uid}'); | |
return doc.getData(); | |
} else { | |
return null; | |
} | |
} | |
Future<void> upsert(Map data) async { | |
FirebaseUser user = await _auth.currentUser(); | |
Document<T> ref = Document(path: '$collection/${user.uid}'); | |
return ref.upsert(data); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'services.dart'; | |
import 'package:firebase_analytics/firebase_analytics.dart'; | |
/// Static global state. Immutable services that do not care about build context. | |
class Global { | |
// App Data | |
static final String title = 'Fireship'; | |
// Services | |
static final FirebaseAnalytics analytics = FirebaseAnalytics(); | |
// Data Models | |
static final Map models = { | |
Site: (data) => Site.fromMap(data), | |
SitesGroup: (data) => SitesGroup.fromMap(data), | |
Report: (data) => Report.fromMap(data), | |
}; | |
// Firestore References for Writes | |
static final UserData<SitesGroup> sitesGroupRef = | |
UserData<SitesGroup>(collection: 'sitegroups'); | |
static final UserData<Report> reportRef = | |
UserData<Report>(collection: 'activity'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment