Revert freezed attempt. it needs a constructor without args... :/

This reverts commit 55aac23033dda47895ffb63154fcbc6fe5524d40.
This commit is contained in:
Dieter Plaetinck
2024-05-04 15:53:12 +02:00
parent 84f43bb5c2
commit bffaa9a9c9

View File

@@ -16,67 +16,20 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
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);
}