@@ -66,13 +66,30 @@ class NutritionPlansProvider with ChangeNotifier {
66
66
ingredients = [];
67
67
}
68
68
69
- /// Returns the current active nutritional plan. At the moment this is just
70
- /// the latest, but this might change in the future.
69
+ /// Returns the current active nutritional plan.
70
+ /// A plan is considered active if:
71
+ /// - Its start date is before now
72
+ /// - Its end date is after now or not set
73
+ /// If multiple plans match these criteria, the one with the most recent creation date is returned.
71
74
NutritionalPlan ? get currentPlan {
72
- if (_plans.isNotEmpty ) {
73
- return _plans.first ;
75
+ if (_plans.isEmpty ) {
76
+ return null ;
74
77
}
75
- return null ;
78
+
79
+ final now = DateTime .now ();
80
+ final activePlans = _plans.where ((plan) {
81
+ final isAfterStart = plan.startDate.isBefore (now);
82
+ final isBeforeEnd = plan.endDate == null || plan.endDate! .isAfter (now);
83
+ return isAfterStart && isBeforeEnd;
84
+ }).toList ();
85
+
86
+ if (activePlans.isEmpty) {
87
+ return null ;
88
+ }
89
+
90
+ // Sort by creation date (newest first) and return the first one
91
+ activePlans.sort ((a, b) => b.creationDate.compareTo (a.creationDate));
92
+ return activePlans.first;
76
93
}
77
94
78
95
NutritionalPlan findById (int id) {
@@ -109,7 +126,8 @@ class NutritionPlansProvider with ChangeNotifier {
109
126
110
127
/// Fetches and sets all plans fully, i.e. with all corresponding child objects
111
128
Future <void > fetchAndSetAllPlansFull () async {
112
- final data = await baseProvider.fetchPaginated (baseProvider.makeUrl (_nutritionalPlansPath));
129
+ final data = await baseProvider
130
+ .fetchPaginated (baseProvider.makeUrl (_nutritionalPlansPath));
113
131
await Future .wait (data.map ((e) => fetchAndSetPlanFull (e['id' ])).toList ());
114
132
}
115
133
@@ -170,7 +188,8 @@ class NutritionPlansProvider with ChangeNotifier {
170
188
// Logs
171
189
await fetchAndSetLogs (plan);
172
190
for (final meal in meals) {
173
- meal.diaryEntries = plan.diaryEntries.where ((e) => e.mealId == meal.id).toList ();
191
+ meal.diaryEntries =
192
+ plan.diaryEntries.where ((e) => e.mealId == meal.id).toList ();
174
193
}
175
194
176
195
// ... and done
@@ -204,7 +223,8 @@ class NutritionPlansProvider with ChangeNotifier {
204
223
_plans.removeAt (existingPlanIndex);
205
224
notifyListeners ();
206
225
207
- final response = await baseProvider.deleteRequest (_nutritionalPlansPath, id);
226
+ final response =
227
+ await baseProvider.deleteRequest (_nutritionalPlansPath, id);
208
228
209
229
if (response.statusCode >= 400 ) {
210
230
_plans.insert (existingPlanIndex, existingPlan);
@@ -284,7 +304,8 @@ class NutritionPlansProvider with ChangeNotifier {
284
304
notifyListeners ();
285
305
286
306
// Try to delete
287
- final response = await baseProvider.deleteRequest (_mealItemPath, mealItem.id! );
307
+ final response =
308
+ await baseProvider.deleteRequest (_mealItemPath, mealItem.id! );
288
309
if (response.statusCode >= 400 ) {
289
310
meal.mealItems.insert (mealItemIndex, existingMealItem);
290
311
notifyListeners ();
@@ -299,7 +320,8 @@ class NutritionPlansProvider with ChangeNotifier {
299
320
/// Fetch and return an ingredient
300
321
///
301
322
/// If the ingredient is not known locally, it is fetched from the server
302
- Future <Ingredient > fetchIngredient (int ingredientId, {IngredientDatabase ? database}) async {
323
+ Future <Ingredient > fetchIngredient (int ingredientId,
324
+ {IngredientDatabase ? database}) async {
303
325
database ?? = this .database;
304
326
Ingredient ingredient;
305
327
@@ -317,9 +339,11 @@ class NutritionPlansProvider with ChangeNotifier {
317
339
_logger.info ("Loaded ingredient '${ingredient .name }' from db cache" );
318
340
319
341
// Prune old entries
320
- if (DateTime .now ()
321
- .isAfter (ingredientDb.lastFetched.add (const Duration (days: DAYS_TO_CACHE )))) {
322
- (database.delete (database.ingredients)..where ((i) => i.id.equals (ingredientId))).go ();
342
+ if (DateTime .now ().isAfter (ingredientDb.lastFetched
343
+ .add (const Duration (days: DAYS_TO_CACHE )))) {
344
+ (database.delete (database.ingredients)
345
+ ..where ((i) => i.id.equals (ingredientId)))
346
+ .go ();
323
347
}
324
348
} else {
325
349
final data = await baseProvider.fetch (
@@ -347,7 +371,9 @@ class NutritionPlansProvider with ChangeNotifier {
347
371
final ingredientDb = await database.select (database.ingredients).get ();
348
372
_logger.info ('Read ${ingredientDb .length } ingredients from db cache' );
349
373
if (ingredientDb.isNotEmpty) {
350
- ingredients = ingredientDb.map ((e) => Ingredient .fromJson (jsonDecode (e.data))).toList ();
374
+ ingredients = ingredientDb
375
+ .map ((e) => Ingredient .fromJson (jsonDecode (e.data)))
376
+ .toList ();
351
377
}
352
378
}
353
379
0 commit comments