modified usermodel to have an events array
This commit is contained in:
parent
43cae32cf1
commit
41298ae655
5 changed files with 42 additions and 5 deletions
|
|
@ -10,7 +10,7 @@ export '../resources/authService.dart' show FirebaseUser;
|
||||||
|
|
||||||
class HomeBloc {
|
class HomeBloc {
|
||||||
final AuthService _auth = authService;
|
final AuthService _auth = authService;
|
||||||
final FirestoreProvider _firestore = firestoreProvider;
|
final FirestoreProvider _repository = firestoreProvider;
|
||||||
final _tasks = BehaviorSubject<List<TaskModel>>();
|
final _tasks = BehaviorSubject<List<TaskModel>>();
|
||||||
|
|
||||||
// Stream getters.
|
// Stream getters.
|
||||||
|
|
@ -30,7 +30,7 @@ class HomeBloc {
|
||||||
|
|
||||||
Future<void> fetchTasks() async {
|
Future<void> fetchTasks() async {
|
||||||
final user = await _auth.currentUser;
|
final user = await _auth.currentUser;
|
||||||
_firestore.getUserTasks(user.email).pipe(_tasks);
|
_repository.getUserTasks(user.email).pipe(_tasks);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<String> getUserAvatarUrl() async {
|
Future<String> getUserAvatarUrl() async {
|
||||||
|
|
@ -44,7 +44,7 @@ class HomeBloc {
|
||||||
}
|
}
|
||||||
|
|
||||||
void markTaskAsDone(TaskModel task) async {
|
void markTaskAsDone(TaskModel task) async {
|
||||||
_firestore.updateTask(
|
_repository.updateTask(
|
||||||
task.id,
|
task.id,
|
||||||
done: true,
|
done: true,
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,9 @@ class UserModel {
|
||||||
/// An array of task ids.
|
/// An array of task ids.
|
||||||
final List<String> tasks;
|
final List<String> tasks;
|
||||||
|
|
||||||
|
/// An array of event names.
|
||||||
|
final List<String> events;
|
||||||
|
|
||||||
/// Added and finished tasks for the current week.
|
/// Added and finished tasks for the current week.
|
||||||
final SummaryModel summary;
|
final SummaryModel summary;
|
||||||
|
|
||||||
|
|
@ -34,6 +37,7 @@ class UserModel {
|
||||||
@required this.pendingHigh,
|
@required this.pendingHigh,
|
||||||
@required this.pendingMedium,
|
@required this.pendingMedium,
|
||||||
@required this.pendingLow,
|
@required this.pendingLow,
|
||||||
|
@required this.events,
|
||||||
});
|
});
|
||||||
|
|
||||||
///Returns a [UserModel] from a map.
|
///Returns a [UserModel] from a map.
|
||||||
|
|
@ -41,6 +45,7 @@ class UserModel {
|
||||||
: id = id,
|
: id = id,
|
||||||
username = firestoreMap["username"],
|
username = firestoreMap["username"],
|
||||||
tasks = firestoreMap["tasks"].cast<String>(),
|
tasks = firestoreMap["tasks"].cast<String>(),
|
||||||
|
events = firestoreMap["events"].cast<String>(),
|
||||||
summary =
|
summary =
|
||||||
SummaryModel.fromMap(firestoreMap["summary"].cast<String, int>()),
|
SummaryModel.fromMap(firestoreMap["summary"].cast<String, int>()),
|
||||||
pendingHigh = firestoreMap["pendingHigh"],
|
pendingHigh = firestoreMap["pendingHigh"],
|
||||||
|
|
@ -51,6 +56,7 @@ class UserModel {
|
||||||
return <String, dynamic>{
|
return <String, dynamic>{
|
||||||
"username": username,
|
"username": username,
|
||||||
"tasks": tasks,
|
"tasks": tasks,
|
||||||
|
"events": events,
|
||||||
"summary": summary.toMap(),
|
"summary": summary.toMap(),
|
||||||
"pendingHigh": pendingHigh,
|
"pendingHigh": pendingHigh,
|
||||||
"pendingMedium": pendingMedium,
|
"pendingMedium": pendingMedium,
|
||||||
|
|
|
||||||
|
|
@ -33,6 +33,7 @@ class AuthService {
|
||||||
final newUserModel = UserModel(
|
final newUserModel = UserModel(
|
||||||
username: user.email,
|
username: user.email,
|
||||||
tasks: <String>[],
|
tasks: <String>[],
|
||||||
|
events: <String>[],
|
||||||
summary: SummaryModel(),
|
summary: SummaryModel(),
|
||||||
pendingHigh: 0,
|
pendingHigh: 0,
|
||||||
pendingMedium: 0,
|
pendingMedium: 0,
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ class FirestoreProvider {
|
||||||
//-----------------------User related operations------------------------------
|
//-----------------------User related operations------------------------------
|
||||||
|
|
||||||
/// Returns a stream of [UserModel].
|
/// Returns a stream of [UserModel].
|
||||||
Observable<UserModel> getUser(String username) {
|
Observable<UserModel> getUserObservable(String username) {
|
||||||
final mappedStream = _firestore
|
final mappedStream = _firestore
|
||||||
.collection('users')
|
.collection('users')
|
||||||
.where('username', isEqualTo: username)
|
.where('username', isEqualTo: username)
|
||||||
|
|
@ -42,6 +42,28 @@ class FirestoreProvider {
|
||||||
return Observable(mappedStream);
|
return Observable(mappedStream);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//TODO: add tests for this method.
|
||||||
|
|
||||||
|
/// Returns a [UserModel].
|
||||||
|
/// Only one out of id or username can be provided, if both are provided id
|
||||||
|
/// will have higher priority.
|
||||||
|
Future<UserModel> getUser({String id, String username}) async {
|
||||||
|
DocumentSnapshot documentSnapshot;
|
||||||
|
if (id != null) {
|
||||||
|
documentSnapshot = await _firestore.document('users/$id').get();
|
||||||
|
} else {
|
||||||
|
final querySnapshot = await _firestore
|
||||||
|
.collection('users')
|
||||||
|
.where('username', isEqualTo: username)
|
||||||
|
.getDocuments();
|
||||||
|
documentSnapshot = querySnapshot.documents.first;
|
||||||
|
}
|
||||||
|
return UserModel.fromFirestore(
|
||||||
|
documentSnapshot.data,
|
||||||
|
id: documentSnapshot.documentID,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/// Creates a new instance of a user in Firestore.
|
/// Creates a new instance of a user in Firestore.
|
||||||
Future<void> createUser(UserModel user, String uid) async {
|
Future<void> createUser(UserModel user, String uid) async {
|
||||||
try {
|
try {
|
||||||
|
|
@ -68,6 +90,7 @@ class FirestoreProvider {
|
||||||
Future<void> updateUser(
|
Future<void> updateUser(
|
||||||
String id, {
|
String id, {
|
||||||
List<String> tasks,
|
List<String> tasks,
|
||||||
|
List<String> events,
|
||||||
SummaryModel summary,
|
SummaryModel summary,
|
||||||
int pendingHigh,
|
int pendingHigh,
|
||||||
int pendingMedium,
|
int pendingMedium,
|
||||||
|
|
@ -75,6 +98,7 @@ class FirestoreProvider {
|
||||||
}) async {
|
}) async {
|
||||||
final newData = <String, dynamic>{
|
final newData = <String, dynamic>{
|
||||||
'tasks': tasks,
|
'tasks': tasks,
|
||||||
|
'events': events,
|
||||||
'summary': summary,
|
'summary': summary,
|
||||||
'pendingHigh': pendingHigh,
|
'pendingHigh': pendingHigh,
|
||||||
'pendingMedium': pendingMedium,
|
'pendingMedium': pendingMedium,
|
||||||
|
|
@ -200,6 +224,11 @@ class FirestoreProvider {
|
||||||
try {
|
try {
|
||||||
final dataMap = event.toFirestoreMap();
|
final dataMap = event.toFirestoreMap();
|
||||||
await _firestore.collection('users/$userId/Events').add(dataMap);
|
await _firestore.collection('users/$userId/Events').add(dataMap);
|
||||||
|
// After the event was added successfully we have to update the events a
|
||||||
|
// user has.
|
||||||
|
final user = await getUser(id: userId);
|
||||||
|
final newEventsArray = user.events..add(event.name);
|
||||||
|
await updateUser(userId, events: newEventsArray);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
print('Error adding Event to firestore: $e');
|
print('Error adding Event to firestore: $e');
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,7 @@ main() {
|
||||||
final user = UserModel(
|
final user = UserModel(
|
||||||
id: '123',
|
id: '123',
|
||||||
tasks: <String>[],
|
tasks: <String>[],
|
||||||
|
events: <String>[],
|
||||||
pendingHigh: 0,
|
pendingHigh: 0,
|
||||||
pendingMedium: 0,
|
pendingMedium: 0,
|
||||||
pendingLow: 0,
|
pendingLow: 0,
|
||||||
|
|
@ -81,7 +82,7 @@ main() {
|
||||||
when(querySnapshot.documents).thenReturn([snapshot]);
|
when(querySnapshot.documents).thenReturn([snapshot]);
|
||||||
when(snapshot.documentID).thenReturn(user.id);
|
when(snapshot.documentID).thenReturn(user.id);
|
||||||
|
|
||||||
expect(provider.getUser(user.username), emits(user));
|
expect(provider.getUserObservable(user.username), emits(user));
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should create task', () {
|
test('should create task', () {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue