Skip to content

Commit 12c1d00

Browse files
committed
support fibers goal
1 parent 72a85e1 commit 12c1d00

File tree

6 files changed

+85
-20
lines changed

6 files changed

+85
-20
lines changed

lib/l10n/app_en.arb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@
284284
"goalProtein": "Protein goal",
285285
"goalCarbohydrates": "Carbohydrates goal",
286286
"goalFat": "Fat goal",
287+
"goalFiber": "Fiber goal",
287288
"anErrorOccurred": "An Error Occurred!",
288289
"@anErrorOccurred": {},
289290
"weight": "Weight",

lib/models/nutrition/nutritional_plan.dart

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ class NutritionalPlan {
5555
@JsonKey(required: true, name: 'goal_fat')
5656
late num? goalFat;
5757

58+
@JsonKey(required: true, name: 'goal_fibers')
59+
late num? goalFibers;
60+
5861
@JsonKey(includeFromJson: false, includeToJson: false, defaultValue: [])
5962
List<Meal> meals = [];
6063

@@ -70,6 +73,7 @@ class NutritionalPlan {
7073
this.goalProtein,
7174
this.goalCarbohydrates,
7275
this.goalFat,
76+
this.goalFibers,
7377
List<Meal>? meals,
7478
List<Log>? diaryEntries,
7579
}) {
@@ -84,6 +88,7 @@ class NutritionalPlan {
8488
goalEnergy = null;
8589
goalProtein = null;
8690
goalCarbohydrates = null;
91+
goalFibers = null;
8792
goalFat = null;
8893
}
8994

@@ -103,19 +108,24 @@ class NutritionalPlan {
103108
goalCarbohydrates != null;
104109
}
105110

111+
bool get hasAnyAdvancedGoals {
112+
return goalFibers != null;
113+
}
114+
106115
/// Calculations
107116
///
108117
/// note that (some of) this is already done on the server. It might be better
109118
/// to read it from there, but on the other hand we might want to do more locally
110119
/// so that a mostly offline mode is possible.
111120
NutritionalGoals get nutritionalGoals {
112121
// If there are set goals, they take preference over any meals
113-
if (hasAnyGoals) {
122+
if (hasAnyGoals || hasAnyAdvancedGoals) {
114123
return NutritionalGoals(
115124
energy: goalEnergy?.toDouble(),
116125
fat: goalFat?.toDouble(),
117126
protein: goalProtein?.toDouble(),
118127
carbohydrates: goalCarbohydrates?.toDouble(),
128+
fibres: goalFibers?.toDouble(),
119129
);
120130
}
121131
// if there are no set goals and no defined meals, the goals are still undefined

lib/models/nutrition/nutritional_plan.g.dart

Lines changed: 5 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/widgets/dashboard/widgets.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,6 @@ class _DashboardNutritionWidgetState extends State<DashboardNutritionWidget> {
198198
...getContent(),
199199
Container(
200200
padding: const EdgeInsets.symmetric(horizontal: 5, vertical: 15),
201-
height: 206,
202201
child: FlNutritionalPlanGoalWidget(nutritionalPlan: _plan!),
203202
),
204203
],

lib/widgets/nutrition/charts.dart

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ class FlNutritionalPlanGoalWidgetState extends State<FlNutritionalPlanGoalWidget
9999
if (goals.carbohydrates != null && goals.carbohydrates! > 0)
100100
today.carbohydrates / goals.carbohydrates!,
101101
if (goals.fat != null && goals.fat! > 0) today.fat / goals.fat!,
102-
if (goals.energy != null && goals.energy! > 0) today.energy / goals.energy!
102+
if (goals.energy != null && goals.energy! > 0) today.energy / goals.energy!,
103+
if (goals.fibres != null && goals.fibres! > 0) today.fibres / goals.fibres!,
103104
].reduce(max);
104105

105106
final normWidth = constraints.maxWidth / maxVal;
@@ -130,6 +131,15 @@ class FlNutritionalPlanGoalWidgetState extends State<FlNutritionalPlanGoalWidget
130131
AppLocalizations.of(context).kcal)),
131132
const SizedBox(height: 2),
132133
_diyGauge(context, normWidth, goals.energy, today.energy),
134+
// optionally display the advanced macro goals:
135+
if (goals.fibres != null)
136+
Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
137+
const SizedBox(height: 8),
138+
Text(fmtMacro(AppLocalizations.of(context).fibres, today.fibres, goals.fibres,
139+
AppLocalizations.of(context).g)),
140+
const SizedBox(height: 2),
141+
_diyGauge(context, normWidth, goals.fibres, today.fibres),
142+
]),
133143
],
134144
);
135145
});
@@ -257,6 +267,7 @@ class NutritionalDiaryChartWidgetFlState extends State<NutritionalDiaryChartWidg
257267
2 => AppLocalizations.of(context).sugars,
258268
3 => AppLocalizations.of(context).fat,
259269
4 => AppLocalizations.of(context).saturatedFat,
270+
5 => AppLocalizations.of(context).fibres,
260271
_ => '',
261272
};
262273
return SideTitleWidget(
@@ -389,6 +400,8 @@ class NutritionalDiaryChartWidgetFlState extends State<NutritionalDiaryChartWidg
389400
barchartGroup(2, barsSpace, barsWidth, 'carbohydratesSugar'),
390401
barchartGroup(3, barsSpace, barsWidth, 'fat'),
391402
barchartGroup(4, barsSpace, barsWidth, 'fatSaturated'),
403+
if (widget._nutritionalPlan.nutritionalGoals.fibres != null)
404+
barchartGroup(5, barsSpace, barsWidth, 'fibres'),
392405
],
393406
),
394407
),

lib/widgets/nutrition/forms.dart

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,15 @@ class IngredientLogForm extends StatelessWidget {
391391
}
392392
}
393393

394+
enum GoalType {
395+
meals('Based on my meals'),
396+
basic('Set basic macros'),
397+
advanced('Set advanced macros');
398+
399+
const GoalType(this.label);
400+
final String label;
401+
}
402+
394403
class PlanForm extends StatefulWidget {
395404
late NutritionalPlan _plan;
396405

@@ -406,17 +415,26 @@ class _PlanFormState extends State<PlanForm> {
406415
final _form = GlobalKey<FormState>();
407416

408417
bool _onlyLogging = true;
409-
bool _addGoals = false;
418+
GoalType _goalType = GoalType.meals;
410419

411420
final _descriptionController = TextEditingController();
421+
final TextEditingController colorController = TextEditingController();
422+
423+
GoalType? selectedGoal;
412424

413425
@override
414426
void initState() {
415427
super.initState();
416428

417429
_onlyLogging = widget._plan.onlyLogging;
418-
_addGoals = widget._plan.hasAnyGoals;
419430
_descriptionController.text = widget._plan.description;
431+
if (widget._plan.hasAnyAdvancedGoals) {
432+
_goalType = GoalType.advanced;
433+
} else if (widget._plan.hasAnyGoals) {
434+
_goalType = GoalType.basic;
435+
} else {
436+
_goalType = GoalType.meals;
437+
}
420438
}
421439

422440
@override
@@ -446,23 +464,36 @@ class _PlanFormState extends State<PlanForm> {
446464
widget._plan.onlyLogging = value;
447465
},
448466
),
449-
SwitchListTile(
450-
title: Text(AppLocalizations.of(context).addGoalsToPlan),
451-
subtitle: Text(AppLocalizations.of(context).addGoalsToPlanHelpText),
452-
value: _addGoals,
453-
onChanged: (value) {
467+
DropdownButtonFormField<GoalType>(
468+
value: _goalType,
469+
items: GoalType.values
470+
.map(
471+
(e) => DropdownMenuItem<GoalType>(value: e, child: Text(e.label)),
472+
)
473+
.toList(),
474+
onChanged: (GoalType? g) {
454475
setState(() {
455-
_addGoals = !_addGoals;
476+
if (g == null) {
477+
return;
478+
}
479+
switch (g) {
480+
case GoalType.meals:
481+
widget._plan.goalEnergy = null;
482+
widget._plan.goalProtein = null;
483+
widget._plan.goalCarbohydrates = null;
484+
widget._plan.goalFat = null;
485+
widget._plan.goalFibers = null;
486+
case GoalType.basic:
487+
widget._plan.goalFibers = null;
488+
break;
489+
default:
490+
break;
491+
}
492+
_goalType = g;
456493
});
457-
if (!value) {
458-
widget._plan.goalEnergy = null;
459-
widget._plan.goalProtein = null;
460-
widget._plan.goalCarbohydrates = null;
461-
widget._plan.goalFat = null;
462-
}
463494
},
464495
),
465-
if (_addGoals)
496+
if (_goalType == GoalType.basic || _goalType == GoalType.advanced)
466497
Column(
467498
children: [
468499
GoalMacros(
@@ -496,6 +527,14 @@ class _PlanFormState extends State<PlanForm> {
496527
],
497528
),
498529

530+
if (_goalType == GoalType.advanced)
531+
GoalMacros(
532+
val: widget._plan.goalFibers?.toString(),
533+
label: AppLocalizations.of(context).goalFiber,
534+
suffix: AppLocalizations.of(context).g,
535+
onSave: (double value) => widget._plan.goalFibers = value,
536+
key: const Key('field-goal-fibers'),
537+
),
499538
ElevatedButton(
500539
key: const Key(SUBMIT_BUTTON_KEY_NAME),
501540
child: Text(AppLocalizations.of(context).save),

0 commit comments

Comments
 (0)