Created the [FirestoreProvider], added the id attribute to the user, event and task models
This commit is contained in:
parent
5c2375fb21
commit
c1a0487071
5 changed files with 146 additions and 19 deletions
|
|
@ -1,11 +1,30 @@
|
|||
import 'package:flutter/material.dart';
|
||||
|
||||
import './models/event_model.dart';
|
||||
import './models/task_model.dart';
|
||||
import './models/user_model.dart';
|
||||
import './resources/firebase_provider.dart';
|
||||
import './resources/firestore_provider.dart';
|
||||
|
||||
class App extends StatelessWidget {
|
||||
Widget build(BuildContext context) {
|
||||
final fire = FirebaseProvider();
|
||||
final fire = FirestoreProvider();
|
||||
return MaterialApp(
|
||||
title: 'Do more',
|
||||
//home: Text('Start'),
|
||||
home: Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text('DO>'),
|
||||
),
|
||||
body: Text('Tasks'),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
class App extends StatelessWidget {
|
||||
Widget build(BuildContext context) {
|
||||
final fire = FirestoreProvider();
|
||||
return MaterialApp(
|
||||
title: 'Do more',
|
||||
//home: Text('Start'),
|
||||
|
|
@ -14,22 +33,28 @@ class App extends StatelessWidget {
|
|||
title: Text('DO>'),
|
||||
),
|
||||
body: StreamBuilder(
|
||||
stream: fire.getUser('mariano159357'),
|
||||
builder:
|
||||
(BuildContext context, AsyncSnapshot<UserModel> userSnapshot) {
|
||||
stream: fire.getUserEvents('vBOvtmTeC8iPg8L4Hixh'),
|
||||
builder: (BuildContext context,
|
||||
AsyncSnapshot<List<EventModel>> userSnapshot) {
|
||||
if (!userSnapshot.hasData) {
|
||||
return Center(
|
||||
child: CircularProgressIndicator(),
|
||||
);
|
||||
}
|
||||
return Column(
|
||||
children: <Widget>[
|
||||
Text('${userSnapshot.data.pendingHigh}'),
|
||||
final children = <Widget>[
|
||||
MaterialButton(
|
||||
onPressed: () {},
|
||||
child: Text('Add task'),
|
||||
),
|
||||
],
|
||||
];
|
||||
//children.add(Text(userSnapshot.data.text));
|
||||
|
||||
userSnapshot.data.forEach((EventModel task) {
|
||||
children.add(Text(task.name));
|
||||
});
|
||||
|
||||
return Column(
|
||||
children: children,
|
||||
);
|
||||
},
|
||||
),
|
||||
|
|
@ -37,3 +62,4 @@ class App extends StatelessWidget {
|
|||
);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
import 'package:meta/meta.dart';
|
||||
|
||||
class EventModel {
|
||||
final String id;
|
||||
final String name;
|
||||
final int pendigTasks;
|
||||
final List<bool> when;
|
||||
|
|
@ -11,6 +12,7 @@ class EventModel {
|
|||
final int lowPriority;
|
||||
|
||||
EventModel({
|
||||
@required this.id,
|
||||
@required this.name,
|
||||
@required this.pendigTasks,
|
||||
@required this.when,
|
||||
|
|
@ -21,8 +23,9 @@ class EventModel {
|
|||
@required this.lowPriority,
|
||||
});
|
||||
|
||||
EventModel.fromFirestore(Map<String, dynamic> firestoreMap)
|
||||
: name = firestoreMap["name"],
|
||||
EventModel.fromFirestore(Map<String, dynamic> firestoreMap, String id)
|
||||
: id = id,
|
||||
name = firestoreMap["name"],
|
||||
pendigTasks = firestoreMap["pendingTasks"],
|
||||
when = firestoreMap["when"].cast<bool>(),
|
||||
media = firestoreMap["media"].cast<String>(),
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
import 'package:meta/meta.dart';
|
||||
|
||||
class TaskModel {
|
||||
final String id;
|
||||
final String text;
|
||||
final int priority;
|
||||
final String ownerUsername;
|
||||
|
|
@ -8,6 +9,7 @@ class TaskModel {
|
|||
final String event;
|
||||
|
||||
TaskModel({
|
||||
@required this.id,
|
||||
@required this.text,
|
||||
@required this.priority,
|
||||
@required this.ownerUsername,
|
||||
|
|
@ -15,8 +17,9 @@ class TaskModel {
|
|||
@required this.event,
|
||||
});
|
||||
|
||||
TaskModel.fromFirestore(Map<String, dynamic> firestoreMap)
|
||||
: text = firestoreMap["text"],
|
||||
TaskModel.fromFirestore(Map<String, dynamic> firestoreMap, String id)
|
||||
: id = id,
|
||||
text = firestoreMap["text"],
|
||||
priority = firestoreMap["priority"],
|
||||
ownerUsername = firestoreMap["ownerUsername"],
|
||||
done = firestoreMap["done"],
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import 'summary_model.dart';
|
|||
///
|
||||
/// Represents all of a users data.
|
||||
class UserModel {
|
||||
final String id;
|
||||
final String username;
|
||||
final List<int> tasks;
|
||||
final SummaryModel summary;
|
||||
|
|
@ -13,6 +14,7 @@ class UserModel {
|
|||
final int pendingLow;
|
||||
|
||||
UserModel({
|
||||
this.id,
|
||||
this.username,
|
||||
this.tasks,
|
||||
this.summary,
|
||||
|
|
@ -20,7 +22,8 @@ class UserModel {
|
|||
this.pendingHigh,
|
||||
this.pendingMedium,
|
||||
this.pendingLow,
|
||||
}) : assert(username != null),
|
||||
}) : assert(id != null),
|
||||
assert(username != null),
|
||||
assert(tasks != null),
|
||||
assert(summary != null),
|
||||
assert(userId != null),
|
||||
|
|
@ -29,8 +32,9 @@ class UserModel {
|
|||
assert(pendingLow != null);
|
||||
|
||||
///Returns a [UserModel] from a map.
|
||||
UserModel.fromFirestore(Map<String, dynamic> firestoreMap)
|
||||
: username = firestoreMap["username"],
|
||||
UserModel.fromFirestore(Map<String, dynamic> firestoreMap, String id)
|
||||
: id = id,
|
||||
username = firestoreMap["username"],
|
||||
tasks = firestoreMap["tasks"].cast<int>(),
|
||||
summary =
|
||||
SummaryModel.fromMap(firestoreMap["summary"].cast<String, int>()),
|
||||
|
|
|
|||
91
lib/src/resources/firestore_provider.dart
Normal file
91
lib/src/resources/firestore_provider.dart
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
import 'package:rxdart/rxdart.dart';
|
||||
|
||||
import '../models/event_model.dart';
|
||||
import '../models/user_model.dart';
|
||||
import '../models/task_model.dart';
|
||||
|
||||
/// A connection to the Cloud Firestore database
|
||||
class FirestoreProvider {
|
||||
final Firestore firestore = Firestore.instance;
|
||||
|
||||
FirestoreProvider() {
|
||||
firestore.settings(timestampsInSnapshotsEnabled: true);
|
||||
}
|
||||
|
||||
/// Returns a stream of [UserModel].
|
||||
Observable<UserModel> getUser(String username) {
|
||||
final mappedStream = firestore
|
||||
.collection('users')
|
||||
.where('username', isEqualTo: username)
|
||||
.snapshots()
|
||||
.map(
|
||||
(QuerySnapshot snapshot) {
|
||||
if (snapshot.documents.isEmpty) {
|
||||
return null;
|
||||
}
|
||||
final userSnapshot = snapshot.documents.first;
|
||||
return UserModel.fromFirestore(
|
||||
userSnapshot.data, userSnapshot.documentID);
|
||||
},
|
||||
);
|
||||
return Observable(mappedStream);
|
||||
}
|
||||
|
||||
/// Returns a stream of [List<Task>]
|
||||
///
|
||||
/// The [event] parameter is used to query tasks that only are part of a
|
||||
/// certain event.
|
||||
Observable<List<TaskModel>> getUserTasks(String username, {String event}) {
|
||||
Query query = firestore
|
||||
.collection('tasks')
|
||||
.where('ownerUsername', isEqualTo: username)
|
||||
.where('done', isEqualTo: false);
|
||||
|
||||
if (event != null) {
|
||||
query = query.where('event', isEqualTo: event);
|
||||
}
|
||||
|
||||
final mappedStream = query.snapshots().map(
|
||||
(QuerySnapshot snapshot) {
|
||||
return snapshot.documents.map(
|
||||
(DocumentSnapshot document) {
|
||||
return TaskModel.fromFirestore(document.data, document.documentID);
|
||||
},
|
||||
).toList();
|
||||
},
|
||||
);
|
||||
|
||||
return Observable(mappedStream);
|
||||
}
|
||||
|
||||
Observable<TaskModel> getTask(String id) {
|
||||
final mappedStream =
|
||||
firestore.collection('tasks').document(id).snapshots().map(
|
||||
(DocumentSnapshot snapshot) {
|
||||
return TaskModel.fromFirestore(snapshot.data, snapshot.documentID);
|
||||
},
|
||||
);
|
||||
|
||||
return Observable(mappedStream);
|
||||
}
|
||||
|
||||
// TODO: Change the Events collction name to 'events'
|
||||
Observable<List<EventModel>> getUserEvents(String userDocumentId) {
|
||||
final mappedStream = firestore
|
||||
.collection('users')
|
||||
.document(userDocumentId)
|
||||
.collection('Events')
|
||||
.snapshots()
|
||||
.map(
|
||||
(QuerySnapshot snapshot) {
|
||||
return snapshot.documents.map((DocumentSnapshot documentSnapshot) {
|
||||
return EventModel.fromFirestore(
|
||||
documentSnapshot.data, documentSnapshot.documentID);
|
||||
}).toList();
|
||||
},
|
||||
);
|
||||
|
||||
return Observable(mappedStream);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue