From bffaa9a9c9680da9b377acdf559021390c976f08 Mon Sep 17 00:00:00 2001 From: Dieter Plaetinck Date: Sat, 4 May 2024 15:53:12 +0200 Subject: [PATCH] Revert freezed attempt. it needs a constructor without args... :/ This reverts commit 55aac23033dda47895ffb63154fcbc6fe5524d40. --- lib/models/nutrition/nutritional_goals.dart | 117 ++++++++------------ 1 file changed, 47 insertions(+), 70 deletions(-) diff --git a/lib/models/nutrition/nutritional_goals.dart b/lib/models/nutrition/nutritional_goals.dart index 6e7dd802..5d7086fe 100644 --- a/lib/models/nutrition/nutritional_goals.dart +++ b/lib/models/nutrition/nutritional_goals.dart @@ -16,67 +16,20 @@ * along with this program. If not, see . */ -import 'package:freezed_annotation/freezed_annotation.dart'; import 'package:wger/helpers/consts.dart'; import 'package:wger/models/nutrition/nutritional_values.dart'; -part 'nutritional_goals.freezed.dart'; - -@freezed class NutritionalGoals { - final double? energy; - final double? protein; - final double? carbohydrates; - final double? carbohydratesSugar; - final double? fat; - final double? fatSaturated; - final double? fibres; - final double? sodium; + double? energy = 0; + double? protein = 0; + double? carbohydrates = 0; + double? carbohydratesSugar = 0; + double? fat = 0; + double? fatSaturated = 0; + double? fibres = 0; + double? sodium = 0; - factory NutritionalGoals({ - double? energy, - double? protein, - double? carbohydrates, - double? carbohydratesSugar, - double? fat, - double? fatSaturated, - double? fibres, - double? sodium, - }) { - // infer values where we can - if (energy == null) { - if (protein != null && carbohydrates != null && fat != null) { - energy = protein * ENERGY_PROTEIN + carbohydrates * ENERGY_CARBOHYDRATES + fat * ENERGY_FAT; - } - } else { - // TODO: input validation when the user modifies/creates the plan, to assure energy is high enough - if (protein == null && carbohydrates != null && fat != null) { - protein = - (energy - carbohydrates * ENERGY_CARBOHYDRATES - fat * ENERGY_FAT) / ENERGY_PROTEIN; - assert(protein > 0); - } else if (carbohydrates == null && protein != null && fat != null) { - carbohydrates = - (energy - protein * ENERGY_PROTEIN - fat * ENERGY_FAT) / ENERGY_CARBOHYDRATES; - assert(carbohydrates > 0); - } else if (fat == null && protein != null && carbohydrates != null) { - fat = - (energy - protein * ENERGY_PROTEIN - carbohydrates * ENERGY_CARBOHYDRATES) / ENERGY_FAT; - assert(fat > 0); - } - } - return NutritionalGoals._( - energy: energy, - protein: protein, - carbohydrates: carbohydrates, - carbohydratesSugar: carbohydratesSugar, - fat: fat, - fatSaturated: fatSaturated, - fibres: fibres, - sodium: sodium, - ); - } - - NutritionalGoals._({ + NutritionalGoals({ this.energy, this.protein, this.carbohydrates, @@ -85,7 +38,30 @@ class NutritionalGoals { this.fatSaturated, this.fibres, this.sodium, - }); + }) { + // infer values where we can + if (energy == null) { + if (protein != null && carbohydrates != null && fat != null) { + energy = + protein! * ENERGY_PROTEIN + carbohydrates! * ENERGY_CARBOHYDRATES + fat! * ENERGY_FAT; + } + return; + } + // TODO: input validation when the user modifies/creates the plan, to assure energy is high enough + if (protein == null && carbohydrates != null && fat != null) { + protein = + (energy! - carbohydrates! * ENERGY_CARBOHYDRATES - fat! * ENERGY_FAT) / ENERGY_PROTEIN; + assert(protein! > 0); + } else if (carbohydrates == null && protein != null && fat != null) { + carbohydrates = + (energy! - protein! * ENERGY_PROTEIN - fat! * ENERGY_FAT) / ENERGY_CARBOHYDRATES; + assert(carbohydrates! > 0); + } else if (fat == null && protein != null && carbohydrates != null) { + fat = (energy! - protein! * ENERGY_PROTEIN - carbohydrates! * ENERGY_CARBOHYDRATES) / + ENERGY_FAT; + assert(fat! > 0); + } + } NutritionalGoals operator /(double v) { return NutritionalGoals( @@ -128,25 +104,16 @@ class NutritionalGoals { } assert(energy! > 0); - double? proteinPct; - double? carbohydratesPct; - double? fatPct; - if (protein != null) { - proteinPct = (100 * protein! * ENERGY_PROTEIN) / energy!; + goals.protein = (100 * protein! * ENERGY_PROTEIN) / energy!; } if (carbohydrates != null) { - carbohydratesPct = (100 * carbohydrates! * ENERGY_CARBOHYDRATES) / energy!; + goals.carbohydrates = (100 * carbohydrates! * ENERGY_CARBOHYDRATES) / energy!; } if (fat != null) { - fatPct = (100 * fat! * ENERGY_FAT) / energy!; + goals.fat = (100 * fat! * ENERGY_FAT) / energy!; } - return NutritionalGoals._( - energy: energy, - protein: proteinPct, - carbohydrates: carbohydratesPct, - fat: fatPct, - ); + return goals; } double? prop(String name) { @@ -162,4 +129,14 @@ class NutritionalGoals { _ => 0, }; } + + @override + String toString() { + return 'e: $energy, p: $protein, c: $carbohydrates, cS: $carbohydratesSugar, f: $fat, fS: $fatSaturated, fi: $fibres, s: $sodium'; + } + + @override + //ignore: avoid_equals_and_hash_code_on_mutable_classes + int get hashCode => Object.hash( + energy, protein, carbohydrates, carbohydratesSugar, fat, fatSaturated, fibres, sodium); }