Skip to content

Commit

Permalink
refactor: uniformized the way to show the nutrition page (#3362)
Browse files Browse the repository at this point in the history
Impacted files:
* `add_new_product_page.dart`: now using the new method `showNutritionPage`
* `add_nutrition_button.dart`: now using the new method `showNutritionPage`
* `edit_product_page.dart`: now using the new method `showNutritionPage`
* `nutrition_page_loaded.dart`: new method `showNutritionPage`
  • Loading branch information
monsieurtanuki committed Nov 27, 2022
1 parent 7831d9d commit 2621b3a
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 78 deletions.
33 changes: 5 additions & 28 deletions packages/smooth_app/lib/pages/product/add_new_product_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import 'package:smooth_app/generic_lib/design_constants.dart';
import 'package:smooth_app/pages/image_crop_page.dart';
import 'package:smooth_app/pages/product/add_basic_details_page.dart';
import 'package:smooth_app/pages/product/nutrition_page_loaded.dart';
import 'package:smooth_app/pages/product/ordered_nutrients_cache.dart';
import 'package:smooth_app/widgets/smooth_scaffold.dart';

const EdgeInsetsGeometry _ROW_PADDING_TOP = EdgeInsetsDirectional.only(
Expand Down Expand Up @@ -203,33 +202,11 @@ class _AddNewProductPageState extends State<AddNewProductPage> {
child: SmoothLargeButtonWithIcon(
text: AppLocalizations.of(context).nutritional_facts_input_button_label,
icon: Icons.edit,
onPressed: () async {
final OrderedNutrientsCache? cache =
await OrderedNutrientsCache.getCache(context);
if (!mounted) {
return;
}
if (cache == null) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
AppLocalizations.of(context).nutrition_cache_loading_error),
),
);
return;
}
await Navigator.push<void>(
context,
MaterialPageRoute<void>(
builder: (BuildContext context) => NutritionPageLoaded(
Product(barcode: widget.barcode),
cache.orderedNutrients,
isLoggedInMandatory: false,
),
fullscreenDialog: true,
),
);
},
onPressed: () async => NutritionPageLoaded.showNutritionPage(
product: Product(barcode: widget.barcode),
isLoggedInMandatory: false,
widget: this,
),
),
);
}
Expand Down
30 changes: 5 additions & 25 deletions packages/smooth_app/lib/pages/product/add_nutrition_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:openfoodfacts/model/Product.dart';
import 'package:smooth_app/helpers/product_cards_helper.dart';
import 'package:smooth_app/pages/product/common/product_refresher.dart';
import 'package:smooth_app/pages/product/nutrition_page_loaded.dart';
import 'package:smooth_app/pages/product/ordered_nutrients_cache.dart';

/// "Add nutrition facts" button for user contribution.
class AddNutritionButton extends StatefulWidget {
Expand All @@ -20,28 +18,10 @@ class _AddNutritionButtonState extends State<AddNutritionButton> {
@override
Widget build(BuildContext context) => addPanelButton(
AppLocalizations.of(context).score_add_missing_nutrition_facts,
onPressed: () async {
if (!await ProductRefresher().checkIfLoggedIn(context)) {
return;
}
final OrderedNutrientsCache? cache =
await OrderedNutrientsCache.getCache(context);
if (cache == null) {
return;
}
if (!mounted) {
return;
}
await Navigator.push<void>(
context,
MaterialPageRoute<void>(
builder: (BuildContext context) => NutritionPageLoaded(
widget.product,
cache.orderedNutrients,
),
fullscreenDialog: true,
),
);
},
onPressed: () async => NutritionPageLoaded.showNutritionPage(
product: widget.product,
isLoggedInMandatory: true,
widget: this,
),
);
}
29 changes: 5 additions & 24 deletions packages/smooth_app/lib/pages/product/edit_product_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import 'package:smooth_app/pages/product/edit_ingredients_page.dart';
import 'package:smooth_app/pages/product/nutrition_page_loaded.dart';
import 'package:smooth_app/pages/product/ocr_ingredients_helper.dart';
import 'package:smooth_app/pages/product/ocr_packaging_helper.dart';
import 'package:smooth_app/pages/product/ordered_nutrients_cache.dart';
import 'package:smooth_app/pages/product/product_image_gallery_view.dart';
import 'package:smooth_app/pages/product/simple_input_page.dart';
import 'package:smooth_app/pages/product/simple_input_page_helpers.dart';
Expand Down Expand Up @@ -194,29 +193,11 @@ class _EditProductPageState extends State<EditProductPage> {
.edit_product_form_item_nutrition_facts_title,
subtitle: appLocalizations
.edit_product_form_item_nutrition_facts_subtitle,
onTap: () async {
if (!await ProductRefresher().checkIfLoggedIn(context)) {
return;
}
final OrderedNutrientsCache? cache =
await OrderedNutrientsCache.getCache(context);
if (cache == null) {
return;
}
if (!mounted) {
return;
}
await Navigator.push<void>(
context,
MaterialPageRoute<void>(
builder: (BuildContext context) => NutritionPageLoaded(
_product,
cache.orderedNutrients,
),
fullscreenDialog: true,
),
);
},
onTap: () async => NutritionPageLoaded.showNutritionPage(
product: _product,
isLoggedInMandatory: true,
widget: this,
),
),
_getSimpleListTileItem(SimpleInputPageLabelHelper()),
_ListTitleItem(
Expand Down
49 changes: 48 additions & 1 deletion packages/smooth_app/lib/pages/product/nutrition_page_loaded.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ 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/widgets/smooth_card.dart';
import 'package:smooth_app/helpers/text_input_formatters_helper.dart';
import 'package:smooth_app/pages/product/common/product_refresher.dart';
import 'package:smooth_app/pages/product/nutrition_add_nutrient_button.dart';
import 'package:smooth_app/pages/product/nutrition_container.dart';
import 'package:smooth_app/pages/product/ordered_nutrients_cache.dart';
import 'package:smooth_app/pages/text_field_helper.dart';
import 'package:smooth_app/query/product_query.dart';
import 'package:smooth_app/widgets/smooth_app_bar.dart';
Expand All @@ -27,7 +29,7 @@ class NutritionPageLoaded extends StatefulWidget {
const NutritionPageLoaded(
this.product,
this.orderedNutrients, {
this.isLoggedInMandatory = true,
required this.isLoggedInMandatory,
});

final Product product;
Expand All @@ -36,6 +38,51 @@ class NutritionPageLoaded extends StatefulWidget {

@override
State<NutritionPageLoaded> createState() => _NutritionPageLoadedState();

/// Shows the nutrition page after loading the ordered nutrient list.
static Future<void> showNutritionPage({
required final Product product,
required final bool isLoggedInMandatory,
required final State<StatefulWidget> widget,
}) async {
if (!widget.mounted) {
return;
}
if (isLoggedInMandatory) {
if (!await ProductRefresher().checkIfLoggedIn(widget.context)) {
return;
}
}
if (!widget.mounted) {
return;
}
final OrderedNutrientsCache? cache =
await OrderedNutrientsCache.getCache(widget.context);
if (!widget.mounted) {
return;
}
if (cache == null) {
ScaffoldMessenger.of(widget.context).showSnackBar(
SnackBar(
content: Text(
AppLocalizations.of(widget.context).nutrition_cache_loading_error,
),
),
);
return;
}
await Navigator.push<void>(
widget.context,
MaterialPageRoute<void>(
builder: (BuildContext context) => NutritionPageLoaded(
product,
cache.orderedNutrients,
isLoggedInMandatory: isLoggedInMandatory,
),
fullscreenDialog: true,
),
);
}
}

class _NutritionPageLoadedState extends State<NutritionPageLoaded> {
Expand Down

0 comments on commit 2621b3a

Please sign in to comment.