diff --git a/lib/api/firebase_meal.dart b/lib/api/firebase_meal.dart index a79bc19..459e5f1 100644 --- a/lib/api/firebase_meal.dart +++ b/lib/api/firebase_meal.dart @@ -80,27 +80,18 @@ class FirebaseMeal extends FirebaseAPI implements IMeal { } @override - Stream> followMeals(String userId, DateTime day) { - DateTime newD = DateTime(day.year, day.month, day.day, 0, 0); - - /// Start of the day - DateTime endD = newD.add(const Duration(days: 1)); - - /// End of the day - + Stream> followMeals(String userId) { Stream> mealStream = collectionReference .where("owner", isEqualTo: userId) - .where("startTime", isGreaterThanOrEqualTo: newD) - .where("startTime", isLessThanOrEqualTo: endD) .withConverter( fromFirestore: Meal.fromFirestore, toFirestore: (Meal meal, options) => meal.toFirestore()) - .snapshots(); + .snapshots(includeMetadataChanges: true); - return mealStream - .map((querySnapshot) => querySnapshot.docs) - .map((doc) => doc.map((e) => e.data())) - .cast(); + final mealList = mealStream + .map((query) => query.docs) + .map((doc) => doc.map((querydoc) => querydoc.data()).toList()); + return mealList; } @override @@ -111,6 +102,7 @@ class FirebaseMeal extends FirebaseAPI implements IMeal { fromFirestore: Meal.fromFirestore, toFirestore: (Meal meal, options) => meal.toFirestore()) .get(); + List meals = m.docs.map((doc) => doc.data()).toList(); return meals; } diff --git a/lib/api/imeal.dart b/lib/api/imeal.dart index 43f2fae..af57643 100644 --- a/lib/api/imeal.dart +++ b/lib/api/imeal.dart @@ -17,8 +17,8 @@ abstract class IMeal { /// Get the meals of the user [userId] for the day [day] Future> getUsersMealForDay(String userId, DateTime day); - /// Subscribe to the user [userId] meals for the day [day] - Stream> followMeals(String userId, DateTime day); + /// Subscribe to the user [userId] meals + Stream> followMeals(String userId); /// Get the meals of the user [userId] Future> getUserMeal(String userId); diff --git a/lib/provider/meal_provider.dart b/lib/provider/meal_provider.dart index 0081baf..ccdfc7b 100644 --- a/lib/provider/meal_provider.dart +++ b/lib/provider/meal_provider.dart @@ -76,10 +76,10 @@ class MealProvider extends ChangeNotifier { return picUrl; } - void startNewDiaryListener(String userId, DateTime date) { + void startNewDiaryListener(String userId) { if (_subscription != null) return; - final subscription = _mealApi.followMeals(userId, date).listen((event) { + final subscription = _mealApi.followMeals(userId).listen((event) { _meals = event; notifyListeners(); }); diff --git a/lib/screens/diary.dart b/lib/screens/diary.dart index 35370ec..0fe488e 100644 --- a/lib/screens/diary.dart +++ b/lib/screens/diary.dart @@ -50,7 +50,7 @@ class _DiaryScreenState extends State { } List _getEventsForDay(BuildContext context, DateTime day) { - return context.read().meals; + return context.watch().meals; } @override @@ -65,12 +65,11 @@ class _DiaryScreenState extends State { builder: (context, child) { context.watch().meals; - mealProvider = context.read(); + mealProvider = context.watch(); AuthProvider authProvider = GetIt.I.get(); if (isAdmin) { - mealProvider.startNewDiaryListener( - widget._client!.uid, _selectedDate); + mealProvider.startNewDiaryListener(widget._client!.uid); } return LoadingOverlay( @@ -78,11 +77,6 @@ class _DiaryScreenState extends State { child: Diary( onDaySelected: (date) { _selectedDate = date; - if (isAdmin) { - mealProvider.stopNewDiaryListener(); - mealProvider.startNewDiaryListener( - widget._client!.uid, date); - } }, getDiariesForDay: (day) { return _getEventsForDay(context, day);