Skip to content

Commit

Permalink
fix: productName best localized choice
Browse files Browse the repository at this point in the history
Impacted files:
* `nutrition_page_loaded.dart`: minor refactoring
* `product_cards_helper.dart`: now the product name is taken from the localized version first; minor refactoring
  • Loading branch information
monsieurtanuki committed Oct 28, 2023
1 parent e463ca3 commit 8c21976
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
41 changes: 29 additions & 12 deletions packages/smooth_app/lib/helpers/product_cards_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,42 @@ Widget buildProductTitle(
);

String getProductNameAndBrands(
Product product, AppLocalizations appLocalizations) {
final String name =
product.productName?.trim() ?? appLocalizations.unknownProductName;
final String brands = product.brands?.trim() ?? appLocalizations.unknownBrand;
final Product product,
final AppLocalizations appLocalizations,
) {
final String name = getProductName(product, appLocalizations);
final String brands = getProductBrands(product, appLocalizations);
return '$name, $brands';
}

String getProductName(Product product, AppLocalizations appLocalizations) =>
product.productName ??
product.productNameInLanguages?[ProductQuery.getLanguage()] ??
/// Returns a trimmed version of the string, or null if null or empty.
String? _clearString(final String? string) {
if (string == null) {
return null;
}
if (string.trim().isEmpty) {
return null;
}
return string.trim();
}

String getProductName(
final Product product,
final AppLocalizations appLocalizations,
) =>
_clearString(product.productNameInLanguages?[ProductQuery.getLanguage()]) ??
_clearString(product.productName) ??
appLocalizations.unknownProductName;

String getProductBrands(Product product, AppLocalizations appLocalizations) {
final String? brands = product.brands?.trim();
if (brands == null || brands.isEmpty) {
String getProductBrands(
final Product product,
final AppLocalizations appLocalizations,
) {
final String? brands = _clearString(product.brands);
if (brands == null) {
return appLocalizations.unknownBrand;
} else {
return formatProductBrands(brands);
}
return formatProductBrands(brands);
}

/// Correctly format word separators between words.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ class _NutritionPageLoadedState extends State<NutritionPageLoaded>
appBar: SmoothAppBar(
title: AutoSizeText(
appLocalizations.nutrition_page_title,
maxLines: upToDateProduct.productName?.isNotEmpty == true ? 1 : 2,
maxLines: 1,
),
subTitle: buildProductTitle(upToDateProduct, appLocalizations),
),
Expand Down

0 comments on commit 8c21976

Please sign in to comment.