Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: #511 - added a "nutrition edit page" #904

Merged
merged 14 commits into from
Jan 9, 2022
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import 'package:openfoodfacts/model/KnowledgePanels.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:smooth_app/cards/product_cards/knowledge_panels/knowledge_panel_element_card.dart';
import 'package:smooth_app/helpers/product_cards_helper.dart';
import 'package:smooth_app/pages/product/nutrition_page.dart';

class KnowledgePanelsBuilder {
const KnowledgePanelsBuilder();

List<Widget> build(
KnowledgePanels knowledgePanels, {
final Product? product,
final AppLocalizations? appLocalizations,
final BuildContext? context,
}) {
final List<Widget> rootPanelWidgets = <Widget>[];
if (knowledgePanels.panelIdToPanelMap['root'] == null) {
Expand All @@ -39,17 +40,27 @@ class KnowledgePanelsBuilder {
allPanels: knowledgePanels,
));
}
if (product != null && appLocalizations != null) {
if (product != null && context != null) {
if (panelId == 'health_card') {
if (product.statesTags
final bool nutritionAddOrUpdate = product.statesTags
?.contains('en:nutrition-facts-to-be-completed') ??
false) {
knowledgePanelElementWidgets.add(
dummyAddButton(
appLocalizations.score_add_missing_nutrition_facts,
false;
final AppLocalizations appLocalizations =
AppLocalizations.of(context)!;
knowledgePanelElementWidgets.add(
dummyAddButton(
nutritionAddOrUpdate
? appLocalizations.score_add_missing_nutrition_facts
: appLocalizations.score_update_nutrition_facts,
iconData: nutritionAddOrUpdate ? Icons.add : Icons.edit,
onPressed: () => Navigator.push<Widget>(
context,
MaterialPageRoute<Widget>(
builder: (BuildContext context) => NutritionPage(product),
),
),
);
}
),
);
if (product.statesTags?.contains('en:ingredients-to-be-completed') ??
false) {
knowledgePanelElementWidgets.add(
Expand Down
11 changes: 8 additions & 3 deletions packages/smooth_app/lib/helpers/product_cards_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,16 @@ List<Attribute> getPopulatedAttributes(
return result;
}

Widget dummyAddButton(final String label) => SizedBox(
Widget dummyAddButton(
final String label, {
final IconData? iconData,
final Function()? onPressed,
}) =>
SizedBox(
width: double.infinity,
child: ElevatedButton.icon(
icon: const Icon(Icons.add),
icon: Icon(iconData ?? Icons.add),
label: Text(label),
onPressed: () {}, // TODO(monsieurtanuki): to be implemented
onPressed: onPressed ?? () {},
),
);
6 changes: 6 additions & 0 deletions packages/smooth_app/lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,12 @@
"score_add_missing_ingredients": "Add missing ingredients",
"score_add_missing_nutrition_facts": "Add missing nutrition facts",
"score_add_missing_product_category": "Add missing product category",
"score_update_nutrition_facts": "Update nutrition facts",
"nutrition_page_title": "Product Nutrition Facts",
"nutrition_page_unspecified": "Nutrition facts are not specified on the product",
"nutrition_page_per_100g": "per 100g",
"nutrition_page_per_serving": "per serving",
"nutrition_page_add_nutrient": "Add a nutrient",
"more_photos": "More interesting photos",
"@more_photos": {},
"no_product_found": "No product found",
Expand Down
6 changes: 6 additions & 0 deletions packages/smooth_app/lib/l10n/app_fr.arb
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,12 @@
"score_add_missing_ingredients": "Ajouter les ingrédients",
"score_add_missing_nutrition_facts": "Ajouter les données nutritionnelles",
"score_add_missing_product_category": "Ajouter une catégorie de produit",
"score_update_nutrition_facts": "Mettre à jour les données nutritionnelles",
"nutrition_page_title": "Information nutritionnelles du produit",
"nutrition_page_unspecified": "Pas d'informations nutritionnelles pour ce produit",
"nutrition_page_per_100g": "pour 100g",
"nutrition_page_per_serving": "par portion",
"nutrition_page_add_nutrient": "Ajouter un nutriment",
"more_photos": "Plus de photos intéressantes",
"@more_photos": {},
"no_product_found": "Aucun produit trouvé",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ class _ProductPageState extends State<ProductPage> {
knowledgePanelWidgets = const KnowledgePanelsBuilder().build(
snapshot.data!,
product: _product,
appLocalizations: AppLocalizations.of(context),
context: context,
);
} else if (snapshot.hasError) {
// TODO(jasmeet): Retry the request.
Expand Down
52 changes: 52 additions & 0 deletions packages/smooth_app/lib/pages/product/nutrition_page.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import 'package:flutter/material.dart';
import 'package:openfoodfacts/model/OrderedNutrients.dart';
import 'package:openfoodfacts/model/Product.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:openfoodfacts/utils/CountryHelper.dart';
import 'package:openfoodfacts/utils/OpenFoodAPIConfiguration.dart';
import 'package:smooth_app/pages/product/nutrition_page_loaded.dart';

// TODO(monsieurtanuki): load the ordered nutrients somewhere else
/// Preparatory nutrition page where data is loaded.
class NutritionPage extends StatefulWidget {
const NutritionPage(this.product);

final Product product;

@override
State<NutritionPage> createState() => _NutritionPageState();
}

class _NutritionPageState extends State<NutritionPage> {
late Future<OrderedNutrients> _initFuture;

@override
void initState() {
super.initState();
_initFuture = _init();
}

Future<OrderedNutrients> _init() async {
final OpenFoodFactsCountry country =
OpenFoodAPIConfiguration.globalCountry!;
return OpenFoodAPIClient.getOrderedNutrients(
cc: country.iso2Code,
language: OpenFoodAPIConfiguration.globalLanguages![0],
M123-dev marked this conversation as resolved.
Show resolved Hide resolved
);
}

@override
Widget build(BuildContext context) => FutureBuilder<OrderedNutrients>(
future: _initFuture,
builder:
(BuildContext context, AsyncSnapshot<OrderedNutrients> snapshot) {
if (snapshot.hasError) {
return Text('Fatal Error: ${snapshot.error}');
}
if (snapshot.connectionState != ConnectionState.done) {
return const CircularProgressIndicator();
M123-dev marked this conversation as resolved.
Show resolved Hide resolved
}
return NutritionPageLoaded(widget.product, snapshot.data!);
},
);
}
Loading