From 37f1dc5e75a92e3f54596db759cd4d89bc4620a2 Mon Sep 17 00:00:00 2001 From: Peter Mandeljc Date: Mon, 3 Oct 2022 21:13:28 +0200 Subject: [PATCH 1/2] fix: Correctly format commas between product brands --- .../cards/product_cards/product_title_card.dart | 2 +- .../product_cards/smooth_product_card_found.dart | 2 +- .../lib/helpers/product_cards_helper.dart | 15 +++++++++++++++ .../lib/pages/product/add_basic_details_page.dart | 5 +++-- 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/packages/smooth_app/lib/cards/product_cards/product_title_card.dart b/packages/smooth_app/lib/cards/product_cards/product_title_card.dart index 41a2e249669..19ace1f1e3e 100644 --- a/packages/smooth_app/lib/cards/product_cards/product_title_card.dart +++ b/packages/smooth_app/lib/cards/product_cards/product_title_card.dart @@ -28,7 +28,7 @@ class ProductTitleCard extends StatelessWidget { final ThemeData themeData = Theme.of(context); final String subtitleText; final Widget trailingWidget; - final String brands = product.brands ?? appLocalizations.unknownBrand; + final String brands = getProductBrands(product, appLocalizations); final String quantity = product.quantity ?? ''; if (isRemovable && !isSelectable) { diff --git a/packages/smooth_app/lib/cards/product_cards/smooth_product_card_found.dart b/packages/smooth_app/lib/cards/product_cards/smooth_product_card_found.dart index cf26810b395..be0fc5511d5 100644 --- a/packages/smooth_app/lib/cards/product_cards/smooth_product_card_found.dart +++ b/packages/smooth_app/lib/cards/product_cards/smooth_product_card_found.dart @@ -107,7 +107,7 @@ class SmoothProductCardFound extends StatelessWidget { style: themeData.textTheme.headline4, ), Text( - product.brands ?? appLocalizations.unknownBrand, + getProductBrands(product, appLocalizations), overflow: TextOverflow.ellipsis, style: themeData.textTheme.subtitle1, ), diff --git a/packages/smooth_app/lib/helpers/product_cards_helper.dart b/packages/smooth_app/lib/helpers/product_cards_helper.dart index 52dca216172..c23ee8f83db 100644 --- a/packages/smooth_app/lib/helpers/product_cards_helper.dart +++ b/packages/smooth_app/lib/helpers/product_cards_helper.dart @@ -6,9 +6,24 @@ import 'package:smooth_app/data_models/product_image_data.dart'; import 'package:smooth_app/generic_lib/design_constants.dart'; import 'package:smooth_app/generic_lib/widgets/smooth_card.dart'; +RegExp _commaRegex = RegExp(r'\s*,\s*'); + String getProductName(Product product, AppLocalizations appLocalizations) => product.productName ?? appLocalizations.unknownProductName; +String getProductBrands(Product product, AppLocalizations appLocalizations) { + final String? brands = product.brands; + if (brands == null) { + return appLocalizations.unknownBrand; + } else { + return formatProductBrands(brands); + } +} + +String formatProductBrands(String brands) => + // Correctly format commas between words + brands.replaceAll(_commaRegex, ', '); + /// Padding to be used while building the SmoothCard on any Product card. const EdgeInsets SMOOTH_CARD_PADDING = EdgeInsets.symmetric( horizontal: MEDIUM_SPACE, diff --git a/packages/smooth_app/lib/pages/product/add_basic_details_page.dart b/packages/smooth_app/lib/pages/product/add_basic_details_page.dart index 99f0a5072e9..96438410940 100644 --- a/packages/smooth_app/lib/pages/product/add_basic_details_page.dart +++ b/packages/smooth_app/lib/pages/product/add_basic_details_page.dart @@ -11,6 +11,7 @@ import 'package:smooth_app/generic_lib/design_constants.dart'; import 'package:smooth_app/generic_lib/dialogs/smooth_alert_dialog.dart'; import 'package:smooth_app/generic_lib/duration_constants.dart'; import 'package:smooth_app/generic_lib/widgets/smooth_text_form_field.dart'; +import 'package:smooth_app/helpers/product_cards_helper.dart'; import 'package:smooth_app/widgets/smooth_scaffold.dart'; class AddBasicDetailsPage extends StatefulWidget { @@ -45,14 +46,14 @@ class _AddBasicDetailsPageState extends State { void _initializeProduct() { _productNameController.text = _product.productName ?? ''; _weightController.text = _product.quantity ?? ''; - _brandNameController.text = _product.brands ?? ''; + _brandNameController.text = formatProductBrands(_product.brands ?? ''); } /// Sets a [Product] with the values from the text fields. void _setChangedProduct(Product product) { product.productName = _productNameController.text; product.quantity = _weightController.text; - product.brands = _brandNameController.text; + product.brands = formatProductBrands(_brandNameController.text); } @override From 1f0a6cf645741ef03369eab0c907fa2013d356e0 Mon Sep 17 00:00:00 2001 From: Peter Mandeljc Date: Tue, 4 Oct 2022 16:46:42 +0200 Subject: [PATCH 2/2] fix: Move comma formatting strings to localizations --- .../lib/helpers/product_cards_helper.dart | 15 +++++++++------ packages/smooth_app/lib/l10n/app_en.arb | 8 ++++++++ .../pages/product/add_basic_details_page.dart | 19 +++++++++---------- 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/packages/smooth_app/lib/helpers/product_cards_helper.dart b/packages/smooth_app/lib/helpers/product_cards_helper.dart index c23ee8f83db..8b9ab838be0 100644 --- a/packages/smooth_app/lib/helpers/product_cards_helper.dart +++ b/packages/smooth_app/lib/helpers/product_cards_helper.dart @@ -6,8 +6,6 @@ import 'package:smooth_app/data_models/product_image_data.dart'; import 'package:smooth_app/generic_lib/design_constants.dart'; import 'package:smooth_app/generic_lib/widgets/smooth_card.dart'; -RegExp _commaRegex = RegExp(r'\s*,\s*'); - String getProductName(Product product, AppLocalizations appLocalizations) => product.productName ?? appLocalizations.unknownProductName; @@ -16,13 +14,18 @@ String getProductBrands(Product product, AppLocalizations appLocalizations) { if (brands == null) { return appLocalizations.unknownBrand; } else { - return formatProductBrands(brands); + return formatProductBrands(brands, appLocalizations); } } -String formatProductBrands(String brands) => - // Correctly format commas between words - brands.replaceAll(_commaRegex, ', '); +/// Correctly format word separators between words (e.g. comma in English) +String formatProductBrands(String brands, AppLocalizations appLocalizations) { + final String separator = appLocalizations.word_separator; + final String separatorChar = + RegExp.escape(appLocalizations.word_separator_char); + final RegExp regex = RegExp('\\s*$separatorChar\\s*'); + return brands.replaceAll(regex, separator); +} /// Padding to be used while building the SmoothCard on any Product card. const EdgeInsets SMOOTH_CARD_PADDING = EdgeInsets.symmetric( diff --git a/packages/smooth_app/lib/l10n/app_en.arb b/packages/smooth_app/lib/l10n/app_en.arb index d802b522c9c..12d15d055a8 100644 --- a/packages/smooth_app/lib/l10n/app_en.arb +++ b/packages/smooth_app/lib/l10n/app_en.arb @@ -1678,5 +1678,13 @@ "upload_image": "Upload Photo", "@upload_image": { "description": "Message shown on asking to upload image" + }, + "word_separator_char": ",", + "@word_separator_char": { + "description": "Word separator character. In English language, this is a comma: ','" + }, + "word_separator": ", ", + "@word_separator": { + "description": "Word separator string. In English, this is a comma followed by a space: ', '" } } \ No newline at end of file diff --git a/packages/smooth_app/lib/pages/product/add_basic_details_page.dart b/packages/smooth_app/lib/pages/product/add_basic_details_page.dart index 96438410940..80da26a1688 100644 --- a/packages/smooth_app/lib/pages/product/add_basic_details_page.dart +++ b/packages/smooth_app/lib/pages/product/add_basic_details_page.dart @@ -35,30 +35,29 @@ class _AddBasicDetailsPageState extends State { final double _heightSpace = LARGE_SPACE; final GlobalKey _formKey = GlobalKey(); late Product _product; - - @override - void initState() { - super.initState(); - _product = widget.product; - _initializeProduct(); - } + late AppLocalizations appLocalizations = AppLocalizations.of(context); void _initializeProduct() { + _product = widget.product; _productNameController.text = _product.productName ?? ''; _weightController.text = _product.quantity ?? ''; - _brandNameController.text = formatProductBrands(_product.brands ?? ''); + _brandNameController.text = _formatProductBrands(_product.brands); } /// Sets a [Product] with the values from the text fields. void _setChangedProduct(Product product) { product.productName = _productNameController.text; product.quantity = _weightController.text; - product.brands = formatProductBrands(_brandNameController.text); + product.brands = _formatProductBrands(_brandNameController.text); + } + + String _formatProductBrands(String? text) { + return text == null ? '' : formatProductBrands(text, appLocalizations); } @override Widget build(BuildContext context) { - final AppLocalizations appLocalizations = AppLocalizations.of(context); + _initializeProduct(); final Size size = MediaQuery.of(context).size; final LocalDatabase localDatabase = context.read(); final UpToDateProductProvider provider =