Skip to content

Commit 29f6c87

Browse files
committed
show charts for all nutritional plans + more elegant filtering
1 parent 40f94c2 commit 29f6c87

File tree

4 files changed

+29
-37
lines changed

4 files changed

+29
-37
lines changed

lib/providers/nutrition.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ class NutritionPlansProvider with ChangeNotifier {
8888
}
8989

9090
// Sort by creation date (newest first) and return the first one
91+
// TODO: this should already be done on _plans. this whole function can be a firstWhere() ?
9192
activePlans.sort((a, b) => b.creationDate.compareTo(a.creationDate));
9293
return activePlans.first;
9394
}

lib/widgets/measurements/entries.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class EntriesList extends StatelessWidget {
3636

3737
@override
3838
Widget build(BuildContext context) {
39-
final plan = Provider.of<NutritionPlansProvider>(context, listen: false).currentPlan;
39+
final plans = Provider.of<NutritionPlansProvider>(context, listen: false).items;
4040

4141
final entriesAll =
4242
_category.entries.map((e) => MeasurementChartEntry(e.value, e.date)).toList();
@@ -47,7 +47,7 @@ class EntriesList extends StatelessWidget {
4747
_category.name,
4848
entriesAll,
4949
entries7dAvg,
50-
plan,
50+
plans,
5151
_category.unit,
5252
context,
5353
),

lib/widgets/measurements/helpers.dart

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:flutter/material.dart';
2+
import 'package:wger/helpers/measurements.dart';
23
import 'package:wger/l10n/generated/app_localizations.dart';
34
import 'package:wger/models/nutrition/nutritional_plan.dart';
45
import 'package:wger/widgets/measurements/charts.dart';
@@ -19,7 +20,16 @@ List<Widget> getOverviewWidgets(
1920
Container(
2021
padding: const EdgeInsets.all(15),
2122
height: 220,
22-
child: MeasurementChartWidgetFl(raw, unit, avgs: avg),
23+
child: raw.isEmpty
24+
? Center(
25+
child: Text(
26+
'No data available',
27+
style: Theme.of(context).textTheme.bodyLarge?.copyWith(
28+
color: Theme.of(context).colorScheme.secondary.withValues(alpha: 0.7),
29+
),
30+
),
31+
)
32+
: MeasurementChartWidgetFl(raw, unit, avgs: avg),
2333
),
2434
if (avg.isNotEmpty) MeasurementOverallChangeWidget(avg.first, avg.last, unit),
2535
const SizedBox(height: 8),
@@ -34,16 +44,11 @@ List<Widget> getOverviewWidgetsSeries(
3444
String name,
3545
List<MeasurementChartEntry> entriesAll,
3646
List<MeasurementChartEntry> entries7dAvg,
37-
NutritionalPlan? plan,
47+
List<NutritionalPlan> plans,
3848
String unit,
3949
BuildContext context,
4050
) {
4151
final monthAgo = DateTime.now().subtract(const Duration(days: 30));
42-
final showPlan = plan != null &&
43-
entriesAll.any((e) =>
44-
e.date.isAfter(plan.startDate) &&
45-
(plan.endDate == null || e.date.isBefore(plan.endDate!)));
46-
4752
return [
4853
...getOverviewWidgets(
4954
AppLocalizations.of(context).chartAllTimeTitle(name),
@@ -52,39 +57,25 @@ List<Widget> getOverviewWidgetsSeries(
5257
unit,
5358
context,
5459
),
55-
if (showPlan)
60+
// Show overview widgets for each plan in plans
61+
for (final plan in plans)
5662
...getOverviewWidgets(
5763
AppLocalizations.of(context).chartDuringPlanTitle(name, plan.description),
58-
entriesAll
59-
.where((e) =>
60-
e.date.isAfter(plan.startDate) &&
61-
(plan.endDate == null || e.date.isBefore(plan.endDate!)))
62-
.toList(),
63-
entries7dAvg
64-
.where((e) =>
65-
e.date.isAfter(plan.startDate) &&
66-
(plan.endDate == null || e.date.isBefore(plan.endDate!)))
67-
.toList(),
64+
entriesAll.whereDate(plan.startDate, plan.endDate),
65+
entries7dAvg.whereDate(plan.startDate, plan.endDate),
6866
unit,
6967
context,
7068
),
7169
// if all time is significantly longer than 30 days (let's say > 75 days)
72-
// and any plan was also > 75 days,
7370
// then let's show a separate chart just focusing on the last 30 days,
7471
// if there is data for it.
7572
if (entriesAll.isNotEmpty &&
7673
entriesAll.first.date.isBefore(entriesAll.last.date.subtract(const Duration(days: 75))) &&
77-
(plan == null ||
78-
(showPlan &&
79-
entriesAll
80-
.firstWhere((e) => e.date.isAfter(plan.startDate))
81-
.date
82-
.isBefore(entriesAll.last.date.subtract(const Duration(days: 30))))) &&
8374
entriesAll.any((e) => e.date.isAfter(monthAgo)))
8475
...getOverviewWidgets(
8576
AppLocalizations.of(context).chart30DaysTitle(name),
86-
entriesAll.where((e) => e.date.isAfter(monthAgo)).toList(),
87-
entries7dAvg.where((e) => e.date.isAfter(monthAgo)).toList(),
77+
entriesAll.whereDate(monthAgo, null),
78+
entries7dAvg.whereDate(monthAgo, null),
8879
unit,
8980
context,
9081
),
@@ -119,16 +110,16 @@ List<Widget> getOverviewWidgetsSeries(
119110
final twoMonthsAgo = DateTime.now().subtract(const Duration(days: 61));
120111
final fourMonthsAgo = DateTime.now().subtract(const Duration(days: 122));
121112

122-
if (entriesAll.where((e) => e.date.isAfter(twoMonthsAgo)).length > 4) {
113+
if (entriesAll.whereDate(twoMonthsAgo, null).length > 4) {
123114
return (
124-
entriesAll.where((e) => e.date.isAfter(twoMonthsAgo)).toList(),
125-
entries7dAvg.where((e) => e.date.isAfter(twoMonthsAgo)).toList(),
115+
entriesAll.whereDate(twoMonthsAgo, null),
116+
entries7dAvg.whereDate(twoMonthsAgo, null),
126117
);
127118
}
128-
if (entriesAll.where((e) => e.date.isAfter(fourMonthsAgo)).length > 4) {
119+
if (entriesAll.whereDate(fourMonthsAgo, null).length > 4) {
129120
return (
130-
entriesAll.where((e) => e.date.isAfter(fourMonthsAgo)).toList(),
131-
entries7dAvg.where((e) => e.date.isAfter(fourMonthsAgo)).toList(),
121+
entriesAll.whereDate(fourMonthsAgo, null),
122+
entries7dAvg.whereDate(fourMonthsAgo, null),
132123
);
133124
}
134125
return (entriesAll, entries7dAvg);

lib/widgets/weight/weight_overview.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class WeightOverview extends StatelessWidget {
3737
@override
3838
Widget build(BuildContext context) {
3939
final profile = context.read<UserProvider>().profile;
40-
final plan = Provider.of<NutritionPlansProvider>(context, listen: false).currentPlan;
40+
final plans = Provider.of<NutritionPlansProvider>(context, listen: false).items;
4141

4242
final entriesAll = _provider.items.map((e) => MeasurementChartEntry(e.weight, e.date)).toList();
4343
final entries7dAvg = moving7dAverage(entriesAll);
@@ -50,7 +50,7 @@ class WeightOverview extends StatelessWidget {
5050
AppLocalizations.of(context).weight,
5151
entriesAll,
5252
entries7dAvg,
53-
plan,
53+
plans,
5454
unit,
5555
context,
5656
),

0 commit comments

Comments
 (0)