This commit is contained in:
doman 2023-07-31 12:49:01 +02:00
parent a000f49144
commit a961580e2d
3 changed files with 62 additions and 61 deletions

View file

@ -63,21 +63,28 @@ class _AddEntryScreen extends State<AddEntryScreen> {
); );
} }
Future<double?> _parseDouble(String text, String name) async {
try {
return double.parse(text.replaceAll(",", "."));
} catch (e) {
showError("$name must be a number");
return null;
}
}
Future<void> _addEntry() async { Future<void> _addEntry() async {
if (products.length != 1) { if (products.length != 1) {
showError("Pick product first"); showError("Pick product first");
return; return;
} }
try { var grams = await _parseDouble(gramsController.text, "Grams");
double.parse(gramsController.text); if (grams == null) {
} catch (e) {
showError("Grams must be a number");
return; return;
} }
await widget.apiClient.addEntry( await widget.apiClient.addEntry(
grams: double.parse(gramsController.text), grams: grams,
productId: products[0].id, productId: products[0].id,
mealId: meal!.id, mealId: meal!.id,
); );
@ -122,7 +129,7 @@ class _AddEntryScreen extends State<AddEntryScreen> {
), ),
keyboardType:const TextInputType.numberWithOptions(decimal: true), keyboardType:const TextInputType.numberWithOptions(decimal: true),
inputFormatters: <TextInputFormatter>[ inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.allow(RegExp(r'^(\d+)?\.?\d{0,2}')), FilteringTextInputFormatter.allow(RegExp(r'^(\d+)?[\.,]?\d{0,2}')),
], ],
controller: gramsController, controller: gramsController,
), ),

View file

@ -46,41 +46,31 @@ class _AddProductScreen extends State<AddProductScreen> {
); );
} }
Future<double?> _parseDouble(String text, String name) async {
try {
return double.parse(text.replaceAll(",", "."));
} catch (e) {
showError("$name must be a number");
return null;
}
}
Future<void> _addProduct() async { Future<void> _addProduct() async {
try { var carb = await _parseDouble(carbController.text, "Carbs");
double.parse(carbController.text); var fat = await _parseDouble(fatController.text, "Fat");
} catch (e) { var protein = await _parseDouble(proteinController.text, "Protein");
showError("Carbs must be a number"); var fiber = await _parseDouble(fiberController.text, "Fiber");
return;
}
try { if (carb == null || fat == null || protein == null || fiber == null) {
double.parse(fatController.text);
} catch (e) {
showError("Fat must be a number");
return;
}
try {
double.parse(fiberController.text);
} catch (e) {
showError("Fiber must be a number");
return;
}
try {
double.parse(proteinController.text);
} catch (e) {
showError("Protein must be a number");
return; return;
} }
try { try {
var productJson = await widget.apiClient.addProduct( var productJson = await widget.apiClient.addProduct(
carb: double.parse(carbController.text), carb: carb,
fat: double.parse(fatController.text), fat: fat,
protein: double.parse(proteinController.text), protein: protein,
fiber: double.parse(fiberController.text), fiber: fiber,
name: nameController.text, name: nameController.text,
); );
var product = Product.fromJson(productJson); var product = Product.fromJson(productJson);
@ -94,28 +84,25 @@ class _AddProductScreen extends State<AddProductScreen> {
double calculateCalories() { double calculateCalories() {
double calories = 0; double calories = 0;
try { var carb = double.tryParse(carbController.text.replaceAll(",", "."));
calories += double.parse(carbController.text) * 4; var fat = double.tryParse(fatController.text.replaceAll(",", "."));
} catch (e) { var protein = double.tryParse(proteinController.text.replaceAll(",", "."));
// ignore var fiber = double.tryParse(fiberController.text.replaceAll(",", "."));
if (carb != null) {
calories += carb * 4;
} }
try { if (fat != null) {
calories += double.parse(fatController.text) * 9; calories += fat * 9;
} catch (e) {
// ignore
} }
try { if (protein != null) {
calories += double.parse(proteinController.text) * 4; calories += protein * 4;
} catch (e) {
// ignore
} }
try { if (fiber != null) {
calories += double.parse(fiberController.text) * 2; calories -= fiber * 2;
} catch (e) {
// ignore
} }
return calories; return calories;
@ -146,7 +133,7 @@ class _AddProductScreen extends State<AddProductScreen> {
), ),
keyboardType:const TextInputType.numberWithOptions(decimal: true), keyboardType:const TextInputType.numberWithOptions(decimal: true),
inputFormatters: <TextInputFormatter>[ inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.allow(RegExp(r'^(\d+)?\.?\d{0,2}')), FilteringTextInputFormatter.allow(RegExp(r'^(\d+)?[\.,]?\d{0,2}')),
], ],
controller: carbController, controller: carbController,
onChanged: (String value) { onChanged: (String value) {
@ -159,7 +146,7 @@ class _AddProductScreen extends State<AddProductScreen> {
), ),
keyboardType:const TextInputType.numberWithOptions(decimal: true), keyboardType:const TextInputType.numberWithOptions(decimal: true),
inputFormatters: <TextInputFormatter>[ inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.allow(RegExp(r'^(\d+)?\.?\d{0,2}')), FilteringTextInputFormatter.allow(RegExp(r'^(\d+)?[\.,]?\d{0,2}')),
], ],
controller: fatController, controller: fatController,
onChanged: (String value) { onChanged: (String value) {
@ -172,7 +159,7 @@ class _AddProductScreen extends State<AddProductScreen> {
), ),
keyboardType:const TextInputType.numberWithOptions(decimal: true), keyboardType:const TextInputType.numberWithOptions(decimal: true),
inputFormatters: <TextInputFormatter>[ inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.allow(RegExp(r'^(\d+)?\.?\d{0,2}')), FilteringTextInputFormatter.allow(RegExp(r'^(\d+)?[\.,]?\d{0,2}')),
], ],
controller: proteinController, controller: proteinController,
onChanged: (String value) { onChanged: (String value) {
@ -185,7 +172,7 @@ class _AddProductScreen extends State<AddProductScreen> {
), ),
keyboardType:const TextInputType.numberWithOptions(decimal: true), keyboardType:const TextInputType.numberWithOptions(decimal: true),
inputFormatters: <TextInputFormatter>[ inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.allow(RegExp(r'^(\d+)?\.?\d{0,2}')), FilteringTextInputFormatter.allow(RegExp(r'^(\d+)?[\.,]?\d{0,2}')),
], ],
controller: fiberController, controller: fiberController,
onChanged: (String value) { onChanged: (String value) {
@ -193,7 +180,7 @@ class _AddProductScreen extends State<AddProductScreen> {
}, },
), ),
Text( Text(
"${calculateCalories()} kcal", "${calculateCalories().toStringAsFixed(2)} kcal",
textAlign: TextAlign.right, textAlign: TextAlign.right,
), ),
] ]

View file

@ -61,22 +61,29 @@ class _EditEntryScreen extends State<EditEntryScreen> {
); );
} }
Future<double?> _parseDouble(String text, String name) async {
try {
return double.parse(text.replaceAll(",", "."));
} catch (e) {
showError("$name must be a number");
return null;
}
}
Future<void> _saveEntry() async { Future<void> _saveEntry() async {
if (products.length != 1) { if (products.length != 1) {
showError("Pick product first"); showError("Pick product first");
return; return;
} }
try { var grams = await _parseDouble(gramsController.text, "Grams");
double.parse(gramsController.text); if (grams == null) {
} catch (e) {
showError("Grams must be a number");
return; return;
} }
await widget.apiClient.updateEntry( await widget.apiClient.updateEntry(
widget.entry.id, widget.entry.id,
grams: double.parse(gramsController.text), grams: grams,
productId: products[0].id, productId: products[0].id,
mealId: widget.entry.mealId, mealId: widget.entry.mealId,
); );
@ -107,7 +114,7 @@ class _EditEntryScreen extends State<EditEntryScreen> {
), ),
keyboardType:const TextInputType.numberWithOptions(decimal: true), keyboardType:const TextInputType.numberWithOptions(decimal: true),
inputFormatters: <TextInputFormatter>[ inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.allow(RegExp(r'^(\d+)?\.?\d{0,2}')), FilteringTextInputFormatter.allow(RegExp(r'^(\d+)?[\.,]?\d{0,2}')),
], ],
controller: gramsController, controller: gramsController,
), ),