Last active
August 31, 2019 15:45
-
-
Save benoitjadinon/f7e534ba03501269dd9e8643f68ae8a7 to your computer and use it in GitHub Desktop.
FirebaseApi dart `appsModel = FirebaseModel<App>("apps", App.fromFirebaseStatic);`
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 'jsonable.dart'; | |
class FireBaseApi { | |
final String path; | |
final Firestore _db = Firestore.instance; | |
CollectionReference _ref; | |
FireBaseApi(this.path) { | |
_ref = _db.collection(path); | |
} | |
Future<QuerySnapshot> getDataCollectionFuture() | |
=> _ref.getDocuments(); | |
Stream<QuerySnapshot> getDataCollectionStream([String orderBy, bool desc, int limit]) { | |
if (orderBy != null && limit != null) | |
return _ref.orderBy(orderBy, descending: desc ?? false).limit(limit).snapshots(); | |
if (orderBy != null) | |
return _ref.orderBy(orderBy, descending: desc ?? false).snapshots(); | |
if (limit != null) | |
return _ref.limit(limit).snapshots(); | |
return _ref.snapshots(); | |
} | |
Future<DocumentSnapshot> getDocumentById(String id) | |
=> _ref.document(id).get(); | |
Future<void> removeDocument(String id) | |
=> _ref.document(id).delete(); | |
Future<DocumentReference> addDocument(Map data) | |
=> _ref.add(data); | |
Future<void> updateDocument(Map data , String id) | |
=> _ref.document(id).updateData(data); | |
Future addOrUpdateDocument(Map data, [String id]) | |
=> (id == null) ? addDocument(data) : updateDocument(data, id); | |
} |
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 'jsonable.dart'; | |
// class AppsModel extends FirebaseModel<App>{ | |
// AppsModel(FireBaseApi api) : super(api, App.fromMapStatic); | |
// } | |
class FirebaseModel<T extends Jsonable> { | |
final FireBaseApi _api; | |
final FromJsonAndIdFunc<T> _fromFunc; | |
FirebaseModel(String collectionName, this._fromFunc) | |
: _api = FireBaseApi(collectionName); | |
FirebaseModel.api(this._api, this._fromFunc); | |
Future<List<T>> getFuture() async { | |
var result = await _api.getDataCollectionFuture(); | |
var products = result.documents | |
.map((doc) => _fromFunc(doc.documentID, doc.data)) | |
.toList(); | |
return products; | |
} | |
Stream<List<T>> getStream([String orderBy, bool desc, int limit]) { | |
return _api.getDataCollectionStream(orderBy, desc, limit) | |
.map((snap) => snap.documents | |
.map((docSnap) => _fromFunc(docSnap.documentID, docSnap.data)) | |
); | |
} | |
Future<T> getById(String id) async { | |
var docSnap = await _api.getDocumentById(id); | |
return _fromFunc(docSnap.documentID, docSnap.data); | |
} | |
Future remove(String id) async { | |
await _api.removeDocument(id) ; | |
return ; | |
} | |
Future update(T data, String id) async { | |
await _api.updateDocument(data.toJson(), id) ; | |
return ; | |
} | |
Future add(T data) async { | |
var docRef = await _api.addDocument(data.toJson()); | |
return; | |
} | |
} |
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
typedef FromJsonFunc<T> = T Function(String id, Map<String, dynamic> json); | |
typedef FromJsonAndIdFunc<T> = T Function(String id, Map<String, dynamic> json); | |
abstract class Jsonable { | |
Map<String, dynamic> toJson(); | |
} |
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
@JsonSerializable(nullable: true, ) | |
class App implements Jsonable { | |
@JsonKey(ignore: true) | |
String id; | |
String package, label; | |
@JsonKey(ignore: true) | |
Uint8List icon; | |
int openings; | |
@DateTimeStampConverter() | |
DateTime last; | |
bool isRecent = false; | |
bool isFavorite = false; | |
List<String> tags = []; | |
App(this.package, this.label, {this.isRecent=false, this.isFavorite=false}); | |
factory App.fromJson(Map<String, dynamic> json) => _$AppFromJson(json); | |
Map<String, dynamic> toJson() => _$AppToJson(this); | |
factory App.fromFirebase(String id, Map<String, dynamic> json) => App.fromJson(json)..id=id; | |
static App fromFirebaseStatic(String id, Map<String, dynamic> json) => App.fromFirebase(id, json); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment