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 'package:flutter/material.dart';
|
||||||
|
|
||||||
|
import './models/event_model.dart';
|
||||||
|
import './models/task_model.dart';
|
||||||
import './models/user_model.dart';
|
import './models/user_model.dart';
|
||||||
import './resources/firebase_provider.dart';
|
import './resources/firestore_provider.dart';
|
||||||
|
|
||||||
class App extends StatelessWidget {
|
class App extends StatelessWidget {
|
||||||
Widget build(BuildContext context) {
|
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(
|
return MaterialApp(
|
||||||
title: 'Do more',
|
title: 'Do more',
|
||||||
//home: Text('Start'),
|
//home: Text('Start'),
|
||||||
|
|
@ -14,22 +33,28 @@ class App extends StatelessWidget {
|
||||||
title: Text('DO>'),
|
title: Text('DO>'),
|
||||||
),
|
),
|
||||||
body: StreamBuilder(
|
body: StreamBuilder(
|
||||||
stream: fire.getUser('mariano159357'),
|
stream: fire.getUserEvents('vBOvtmTeC8iPg8L4Hixh'),
|
||||||
builder:
|
builder: (BuildContext context,
|
||||||
(BuildContext context, AsyncSnapshot<UserModel> userSnapshot) {
|
AsyncSnapshot<List<EventModel>> userSnapshot) {
|
||||||
if (!userSnapshot.hasData) {
|
if (!userSnapshot.hasData) {
|
||||||
return Center(
|
return Center(
|
||||||
child: CircularProgressIndicator(),
|
child: CircularProgressIndicator(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
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(
|
return Column(
|
||||||
children: <Widget>[
|
children: children,
|
||||||
Text('${userSnapshot.data.pendingHigh}'),
|
|
||||||
MaterialButton(
|
|
||||||
onPressed: () {},
|
|
||||||
child: Text('Add task'),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
|
@ -37,3 +62,4 @@ class App extends StatelessWidget {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
import 'package:meta/meta.dart';
|
import 'package:meta/meta.dart';
|
||||||
|
|
||||||
class EventModel {
|
class EventModel {
|
||||||
|
final String id;
|
||||||
final String name;
|
final String name;
|
||||||
final int pendigTasks;
|
final int pendigTasks;
|
||||||
final List<bool> when;
|
final List<bool> when;
|
||||||
|
|
@ -11,6 +12,7 @@ class EventModel {
|
||||||
final int lowPriority;
|
final int lowPriority;
|
||||||
|
|
||||||
EventModel({
|
EventModel({
|
||||||
|
@required this.id,
|
||||||
@required this.name,
|
@required this.name,
|
||||||
@required this.pendigTasks,
|
@required this.pendigTasks,
|
||||||
@required this.when,
|
@required this.when,
|
||||||
|
|
@ -21,8 +23,9 @@ class EventModel {
|
||||||
@required this.lowPriority,
|
@required this.lowPriority,
|
||||||
});
|
});
|
||||||
|
|
||||||
EventModel.fromFirestore(Map<String, dynamic> firestoreMap)
|
EventModel.fromFirestore(Map<String, dynamic> firestoreMap, String id)
|
||||||
: name = firestoreMap["name"],
|
: id = id,
|
||||||
|
name = firestoreMap["name"],
|
||||||
pendigTasks = firestoreMap["pendingTasks"],
|
pendigTasks = firestoreMap["pendingTasks"],
|
||||||
when = firestoreMap["when"].cast<bool>(),
|
when = firestoreMap["when"].cast<bool>(),
|
||||||
media = firestoreMap["media"].cast<String>(),
|
media = firestoreMap["media"].cast<String>(),
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
import 'package:meta/meta.dart';
|
import 'package:meta/meta.dart';
|
||||||
|
|
||||||
class TaskModel {
|
class TaskModel {
|
||||||
|
final String id;
|
||||||
final String text;
|
final String text;
|
||||||
final int priority;
|
final int priority;
|
||||||
final String ownerUsername;
|
final String ownerUsername;
|
||||||
|
|
@ -8,6 +9,7 @@ class TaskModel {
|
||||||
final String event;
|
final String event;
|
||||||
|
|
||||||
TaskModel({
|
TaskModel({
|
||||||
|
@required this.id,
|
||||||
@required this.text,
|
@required this.text,
|
||||||
@required this.priority,
|
@required this.priority,
|
||||||
@required this.ownerUsername,
|
@required this.ownerUsername,
|
||||||
|
|
@ -15,8 +17,9 @@ class TaskModel {
|
||||||
@required this.event,
|
@required this.event,
|
||||||
});
|
});
|
||||||
|
|
||||||
TaskModel.fromFirestore(Map<String, dynamic> firestoreMap)
|
TaskModel.fromFirestore(Map<String, dynamic> firestoreMap, String id)
|
||||||
: text = firestoreMap["text"],
|
: id = id,
|
||||||
|
text = firestoreMap["text"],
|
||||||
priority = firestoreMap["priority"],
|
priority = firestoreMap["priority"],
|
||||||
ownerUsername = firestoreMap["ownerUsername"],
|
ownerUsername = firestoreMap["ownerUsername"],
|
||||||
done = firestoreMap["done"],
|
done = firestoreMap["done"],
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import 'summary_model.dart';
|
||||||
///
|
///
|
||||||
/// Represents all of a users data.
|
/// Represents all of a users data.
|
||||||
class UserModel {
|
class UserModel {
|
||||||
|
final String id;
|
||||||
final String username;
|
final String username;
|
||||||
final List<int> tasks;
|
final List<int> tasks;
|
||||||
final SummaryModel summary;
|
final SummaryModel summary;
|
||||||
|
|
@ -13,6 +14,7 @@ class UserModel {
|
||||||
final int pendingLow;
|
final int pendingLow;
|
||||||
|
|
||||||
UserModel({
|
UserModel({
|
||||||
|
this.id,
|
||||||
this.username,
|
this.username,
|
||||||
this.tasks,
|
this.tasks,
|
||||||
this.summary,
|
this.summary,
|
||||||
|
|
@ -20,7 +22,8 @@ class UserModel {
|
||||||
this.pendingHigh,
|
this.pendingHigh,
|
||||||
this.pendingMedium,
|
this.pendingMedium,
|
||||||
this.pendingLow,
|
this.pendingLow,
|
||||||
}) : assert(username != null),
|
}) : assert(id != null),
|
||||||
|
assert(username != null),
|
||||||
assert(tasks != null),
|
assert(tasks != null),
|
||||||
assert(summary != null),
|
assert(summary != null),
|
||||||
assert(userId != null),
|
assert(userId != null),
|
||||||
|
|
@ -29,8 +32,9 @@ class UserModel {
|
||||||
assert(pendingLow != null);
|
assert(pendingLow != null);
|
||||||
|
|
||||||
///Returns a [UserModel] from a map.
|
///Returns a [UserModel] from a map.
|
||||||
UserModel.fromFirestore(Map<String, dynamic> firestoreMap)
|
UserModel.fromFirestore(Map<String, dynamic> firestoreMap, String id)
|
||||||
: username = firestoreMap["username"],
|
: id = id,
|
||||||
|
username = firestoreMap["username"],
|
||||||
tasks = firestoreMap["tasks"].cast<int>(),
|
tasks = firestoreMap["tasks"].cast<int>(),
|
||||||
summary =
|
summary =
|
||||||
SummaryModel.fromMap(firestoreMap["summary"].cast<String, int>()),
|
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