Skip to content

Commit

Permalink
Merge pull request #215 from les-crepes/214-actualisation-en-direct-d…
Browse files Browse the repository at this point in the history
…u-diary-client

214 actualisation en direct du diary client
  • Loading branch information
LucaCoduriV authored Sep 7, 2022
2 parents dbe467b + 8413355 commit d8f22ee
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 28 deletions.
22 changes: 7 additions & 15 deletions lib/api/firebase_meal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,27 +80,18 @@ class FirebaseMeal extends FirebaseAPI implements IMeal {
}

@override
Stream<List<Meal>> 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<List<Meal>> followMeals(String userId) {
Stream<QuerySnapshot<Meal>> 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
Expand All @@ -111,6 +102,7 @@ class FirebaseMeal extends FirebaseAPI implements IMeal {
fromFirestore: Meal.fromFirestore,
toFirestore: (Meal meal, options) => meal.toFirestore())
.get();

List<Meal> meals = m.docs.map((doc) => doc.data()).toList();
return meals;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/api/imeal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ abstract class IMeal {
/// Get the meals of the user [userId] for the day [day]
Future<List<Meal>> getUsersMealForDay(String userId, DateTime day);

/// Subscribe to the user [userId] meals for the day [day]
Stream<List<Meal>> followMeals(String userId, DateTime day);
/// Subscribe to the user [userId] meals
Stream<List<Meal>> followMeals(String userId);

/// Get the meals of the user [userId]
Future<List<Meal>> getUserMeal(String userId);
Expand Down
4 changes: 2 additions & 2 deletions lib/provider/meal_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down
12 changes: 3 additions & 9 deletions lib/screens/diary.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class _DiaryScreenState extends State<DiaryScreen> {
}

List<Meal> _getEventsForDay(BuildContext context, DateTime day) {
return context.read<MealProvider>().meals;
return context.watch<MealProvider>().meals;
}

@override
Expand All @@ -65,24 +65,18 @@ class _DiaryScreenState extends State<DiaryScreen> {
builder: (context, child) {
context.watch<MealProvider>().meals;

mealProvider = context.read<MealProvider>();
mealProvider = context.watch<MealProvider>();
AuthProvider authProvider = GetIt.I.get<AuthProvider>();

if (isAdmin) {
mealProvider.startNewDiaryListener(
widget._client!.uid, _selectedDate);
mealProvider.startNewDiaryListener(widget._client!.uid);
}

return LoadingOverlay(
controller: _loadingOverlayController,
child: Diary(
onDaySelected: (date) {
_selectedDate = date;
if (isAdmin) {
mealProvider.stopNewDiaryListener();
mealProvider.startNewDiaryListener(
widget._client!.uid, date);
}
},
getDiariesForDay: (day) {
return _getEventsForDay(context, day);
Expand Down

0 comments on commit d8f22ee

Please sign in to comment.