From a961580e2def27a26add4106c21b7cd4291dc665 Mon Sep 17 00:00:00 2001 From: doman Date: Mon, 31 Jul 2023 12:49:01 +0200 Subject: [PATCH] allow , --- lib/screens/add_entry.dart | 19 +++++--- lib/screens/add_product.dart | 85 +++++++++++++++--------------------- lib/screens/edit_entry.dart | 19 +++++--- 3 files changed, 62 insertions(+), 61 deletions(-) diff --git a/lib/screens/add_entry.dart b/lib/screens/add_entry.dart index 53ee2a9..47e68a1 100644 --- a/lib/screens/add_entry.dart +++ b/lib/screens/add_entry.dart @@ -63,21 +63,28 @@ class _AddEntryScreen extends State { ); } + Future _parseDouble(String text, String name) async { + try { + return double.parse(text.replaceAll(",", ".")); + } catch (e) { + showError("$name must be a number"); + return null; + } + } + Future _addEntry() async { if (products.length != 1) { showError("Pick product first"); return; } - try { - double.parse(gramsController.text); - } catch (e) { - showError("Grams must be a number"); + var grams = await _parseDouble(gramsController.text, "Grams"); + if (grams == null) { return; } await widget.apiClient.addEntry( - grams: double.parse(gramsController.text), + grams: grams, productId: products[0].id, mealId: meal!.id, ); @@ -122,7 +129,7 @@ class _AddEntryScreen extends State { ), keyboardType:const TextInputType.numberWithOptions(decimal: true), inputFormatters: [ - FilteringTextInputFormatter.allow(RegExp(r'^(\d+)?\.?\d{0,2}')), + FilteringTextInputFormatter.allow(RegExp(r'^(\d+)?[\.,]?\d{0,2}')), ], controller: gramsController, ), diff --git a/lib/screens/add_product.dart b/lib/screens/add_product.dart index bc93bfe..6477c67 100644 --- a/lib/screens/add_product.dart +++ b/lib/screens/add_product.dart @@ -46,41 +46,31 @@ class _AddProductScreen extends State { ); } + Future _parseDouble(String text, String name) async { + try { + return double.parse(text.replaceAll(",", ".")); + } catch (e) { + showError("$name must be a number"); + return null; + } + } + Future _addProduct() async { - try { - double.parse(carbController.text); - } catch (e) { - showError("Carbs must be a number"); - return; - } + var carb = await _parseDouble(carbController.text, "Carbs"); + var fat = await _parseDouble(fatController.text, "Fat"); + var protein = await _parseDouble(proteinController.text, "Protein"); + var fiber = await _parseDouble(fiberController.text, "Fiber"); - try { - 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"); + if (carb == null || fat == null || protein == null || fiber == null) { return; } try { var productJson = await widget.apiClient.addProduct( - carb: double.parse(carbController.text), - fat: double.parse(fatController.text), - protein: double.parse(proteinController.text), - fiber: double.parse(fiberController.text), + carb: carb, + fat: fat, + protein: protein, + fiber: fiber, name: nameController.text, ); var product = Product.fromJson(productJson); @@ -94,28 +84,25 @@ class _AddProductScreen extends State { double calculateCalories() { double calories = 0; - try { - calories += double.parse(carbController.text) * 4; - } catch (e) { - // ignore + var carb = double.tryParse(carbController.text.replaceAll(",", ".")); + var fat = double.tryParse(fatController.text.replaceAll(",", ".")); + var protein = double.tryParse(proteinController.text.replaceAll(",", ".")); + var fiber = double.tryParse(fiberController.text.replaceAll(",", ".")); + + if (carb != null) { + calories += carb * 4; } - try { - calories += double.parse(fatController.text) * 9; - } catch (e) { - // ignore + if (fat != null) { + calories += fat * 9; } - try { - calories += double.parse(proteinController.text) * 4; - } catch (e) { - // ignore + if (protein != null) { + calories += protein * 4; } - try { - calories += double.parse(fiberController.text) * 2; - } catch (e) { - // ignore + if (fiber != null) { + calories -= fiber * 2; } return calories; @@ -146,7 +133,7 @@ class _AddProductScreen extends State { ), keyboardType:const TextInputType.numberWithOptions(decimal: true), inputFormatters: [ - FilteringTextInputFormatter.allow(RegExp(r'^(\d+)?\.?\d{0,2}')), + FilteringTextInputFormatter.allow(RegExp(r'^(\d+)?[\.,]?\d{0,2}')), ], controller: carbController, onChanged: (String value) { @@ -159,7 +146,7 @@ class _AddProductScreen extends State { ), keyboardType:const TextInputType.numberWithOptions(decimal: true), inputFormatters: [ - FilteringTextInputFormatter.allow(RegExp(r'^(\d+)?\.?\d{0,2}')), + FilteringTextInputFormatter.allow(RegExp(r'^(\d+)?[\.,]?\d{0,2}')), ], controller: fatController, onChanged: (String value) { @@ -172,7 +159,7 @@ class _AddProductScreen extends State { ), keyboardType:const TextInputType.numberWithOptions(decimal: true), inputFormatters: [ - FilteringTextInputFormatter.allow(RegExp(r'^(\d+)?\.?\d{0,2}')), + FilteringTextInputFormatter.allow(RegExp(r'^(\d+)?[\.,]?\d{0,2}')), ], controller: proteinController, onChanged: (String value) { @@ -185,7 +172,7 @@ class _AddProductScreen extends State { ), keyboardType:const TextInputType.numberWithOptions(decimal: true), inputFormatters: [ - FilteringTextInputFormatter.allow(RegExp(r'^(\d+)?\.?\d{0,2}')), + FilteringTextInputFormatter.allow(RegExp(r'^(\d+)?[\.,]?\d{0,2}')), ], controller: fiberController, onChanged: (String value) { @@ -193,7 +180,7 @@ class _AddProductScreen extends State { }, ), Text( - "${calculateCalories()} kcal", + "${calculateCalories().toStringAsFixed(2)} kcal", textAlign: TextAlign.right, ), ] diff --git a/lib/screens/edit_entry.dart b/lib/screens/edit_entry.dart index d57b2d2..f462e76 100644 --- a/lib/screens/edit_entry.dart +++ b/lib/screens/edit_entry.dart @@ -61,22 +61,29 @@ class _EditEntryScreen extends State { ); } + Future _parseDouble(String text, String name) async { + try { + return double.parse(text.replaceAll(",", ".")); + } catch (e) { + showError("$name must be a number"); + return null; + } + } + Future _saveEntry() async { if (products.length != 1) { showError("Pick product first"); return; } - try { - double.parse(gramsController.text); - } catch (e) { - showError("Grams must be a number"); + var grams = await _parseDouble(gramsController.text, "Grams"); + if (grams == null) { return; } await widget.apiClient.updateEntry( widget.entry.id, - grams: double.parse(gramsController.text), + grams: grams, productId: products[0].id, mealId: widget.entry.mealId, ); @@ -107,7 +114,7 @@ class _EditEntryScreen extends State { ), keyboardType:const TextInputType.numberWithOptions(decimal: true), inputFormatters: [ - FilteringTextInputFormatter.allow(RegExp(r'^(\d+)?\.?\d{0,2}')), + FilteringTextInputFormatter.allow(RegExp(r'^(\d+)?[\.,]?\d{0,2}')), ], controller: gramsController, ),