-
-
Notifications
You must be signed in to change notification settings - Fork 287
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
fix: #2146 refresh product edition #2201
Changes from 2 commits
f43720d
853ad17
42e3db6
59986c0
4865c66
1af30a0
2714bcd
e718072
c2b3471
116bf66
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,13 +4,11 @@ import 'package:flutter_gen/gen_l10n/app_localizations.dart'; | |
import 'package:openfoodfacts/openfoodfacts.dart'; | ||
import 'package:provider/provider.dart'; | ||
import 'package:smooth_app/cards/product_cards/product_image_carousel.dart'; | ||
import 'package:smooth_app/database/dao_product.dart'; | ||
import 'package:smooth_app/database/local_database.dart'; | ||
import 'package:smooth_app/database/product_query.dart'; | ||
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/loading_dialog.dart'; | ||
import 'package:smooth_app/generic_lib/widgets/smooth_text_form_field.dart'; | ||
import 'package:smooth_app/pages/product/common/product_refresher.dart'; | ||
|
||
class AddBasicDetailsPage extends StatefulWidget { | ||
const AddBasicDetailsPage(this.product); | ||
|
@@ -130,7 +128,7 @@ class _AddBasicDetailsPageState extends State<AddBasicDetailsPage> { | |
return; | ||
} | ||
final bool savedAndRefreshed = | ||
await _saveData(localDatabase); | ||
await _saveData(localDatabase, widget.product); | ||
if (savedAndRefreshed) { | ||
if (!mounted) { | ||
return; | ||
|
@@ -168,39 +166,23 @@ class _AddBasicDetailsPageState extends State<AddBasicDetailsPage> { | |
), | ||
); | ||
|
||
Future<bool> _saveData(LocalDatabase localDatabase) async { | ||
Future<bool> _saveData( | ||
LocalDatabase localDatabase, Product inputProduct) async { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. First thing first, please add a ',' after Then, I would not recommend using the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for your suggestion, I removed the parameter inputProduct |
||
final AppLocalizations appLocalizations = AppLocalizations.of(context); | ||
final Product product = Product( | ||
productName: _productNameController.text, | ||
quantity: _weightController.text, | ||
brands: _brandNameController.text, | ||
barcode: widget.product.barcode, | ||
); | ||
final Status? status = await LoadingDialog.run<Status>( | ||
inputProduct.productName = _productNameController.text; | ||
inputProduct.quantity = _weightController.text; | ||
inputProduct.brands = _brandNameController.text; | ||
inputProduct.barcode = widget.product.barcode; | ||
final Product? savedAndRefreshed = await ProductRefresher().saveAndRefresh( | ||
context: context, | ||
future: OpenFoodAPIClient.saveProduct( | ||
ProductQuery.getUser(), | ||
product, | ||
), | ||
title: appLocalizations.nutrition_page_update_running, | ||
localDatabase: localDatabase, | ||
product: inputProduct, | ||
); | ||
if (status == null || status.error != null) { | ||
if (savedAndRefreshed != null) { | ||
return true; | ||
} else { | ||
_errorMessageAlert(appLocalizations.basic_details_add_error); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's already an error message in |
||
return false; | ||
} else { | ||
final ProductQueryConfiguration configuration = ProductQueryConfiguration( | ||
widget.product.barcode!, | ||
fields: ProductQuery.fields, | ||
language: ProductQuery.getLanguage(), | ||
country: ProductQuery.getCountry(), | ||
); | ||
final ProductResult result = await OpenFoodAPIClient.getProduct( | ||
configuration, | ||
); | ||
if (result.product != null) { | ||
await DaoProduct(localDatabase).put(result.product!); | ||
} | ||
} | ||
return true; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -119,28 +119,29 @@ class ProductRefresher { | |
return const _MetaProductRefresher.error(null); | ||
} | ||
|
||
Future<bool> fetchAndRefresh({ | ||
Future<Product?> fetchAndRefresh({ | ||
required final BuildContext context, | ||
required final LocalDatabase localDatabase, | ||
required final String barcode, | ||
}) async { | ||
final AppLocalizations appLocalizations = AppLocalizations.of(context); | ||
final bool? savedAndRefreshed = await LoadingDialog.run<bool>( | ||
final _MetaProductRefresher? fetchAndRefreshed = | ||
await LoadingDialog.run<_MetaProductRefresher>( | ||
future: _fetchAndRefresh(localDatabase, barcode), | ||
context: context, | ||
title: appLocalizations.nutrition_page_update_running, | ||
); | ||
if (savedAndRefreshed == null) { | ||
return false; | ||
if (fetchAndRefreshed == null) { | ||
return null; | ||
} | ||
if (!savedAndRefreshed) { | ||
if (fetchAndRefreshed.product == null) { | ||
await LoadingDialog.error(context: context); | ||
return false; | ||
return null; | ||
} | ||
return true; | ||
return fetchAndRefreshed.product; | ||
} | ||
|
||
Future<bool> _fetchAndRefresh( | ||
Future<_MetaProductRefresher> _fetchAndRefresh( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fetchAndRefresh calls this method instead |
||
final LocalDatabase localDatabase, | ||
final String barcode, | ||
) async { | ||
|
@@ -155,10 +156,10 @@ class ProductRefresher { | |
); | ||
if (result.product != null) { | ||
await DaoProduct(localDatabase).put(result.product!); | ||
return true; | ||
localDatabase.notifyListeners(); | ||
return _MetaProductRefresher.product(result.product); | ||
} | ||
|
||
return false; | ||
return const _MetaProductRefresher.error(null); | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,6 @@ import 'package:flutter_gen/gen_l10n/app_localizations.dart'; | |
import 'package:openfoodfacts/openfoodfacts.dart'; | ||
import 'package:provider/provider.dart'; | ||
import 'package:smooth_app/data_models/product_image_data.dart'; | ||
import 'package:smooth_app/database/dao_product.dart'; | ||
import 'package:smooth_app/database/local_database.dart'; | ||
import 'package:smooth_app/helpers/product_cards_helper.dart'; | ||
import 'package:smooth_app/pages/product/add_basic_details_page.dart'; | ||
|
@@ -86,7 +85,6 @@ class _EditProductPageState extends State<EditProductPage> { | |
); | ||
if (refreshed ?? false) { | ||
_changes++; | ||
await _refreshProduct(); | ||
} | ||
}, | ||
), | ||
|
@@ -112,7 +110,21 @@ class _EditProductPageState extends State<EditProductPage> { | |
); | ||
if (refreshed ?? false) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know you didn't write it, but I'm sure |
||
_changes++; | ||
await _refreshProduct(); | ||
//Refetch product if needed for new urls | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please be more generous in your comment, and explain why you do it there and not in the other places where you code There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added some more details |
||
if (!mounted) { | ||
return; | ||
} | ||
final LocalDatabase localDatabase = | ||
context.read<LocalDatabase>(); | ||
final Product? refreshedProduct = | ||
await ProductRefresher().fetchAndRefresh( | ||
context: context, | ||
localDatabase: localDatabase, | ||
barcode: _product.barcode!, | ||
); | ||
if (refreshedProduct != null) { | ||
_product = refreshedProduct; | ||
} | ||
} | ||
}, | ||
), | ||
|
@@ -135,7 +147,6 @@ class _EditProductPageState extends State<EditProductPage> { | |
); | ||
if (refreshed ?? false) { | ||
_changes++; | ||
await _refreshProduct(); | ||
} | ||
}, | ||
), | ||
|
@@ -176,7 +187,6 @@ class _EditProductPageState extends State<EditProductPage> { | |
); | ||
if (refreshed ?? false) { | ||
_changes++; | ||
await _refreshProduct(); | ||
} | ||
}, | ||
), | ||
|
@@ -186,15 +196,6 @@ class _EditProductPageState extends State<EditProductPage> { | |
); | ||
} | ||
|
||
Future<void> _refreshProduct() async { | ||
final LocalDatabase localDatabase = context.read<LocalDatabase>(); | ||
final Product? refreshedProduct = | ||
await DaoProduct(localDatabase).get(_product.barcode ?? ''); | ||
if (refreshedProduct != null) { | ||
_product = refreshedProduct; | ||
} | ||
} | ||
|
||
Widget _getSimpleListTileItem(final AbstractSimpleInputPageHelper helper) => | ||
_ListTitleItem( | ||
title: helper.getTitle(), | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should probably return a
Product?
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✅