Skip to content

Commit

Permalink
fix: New product added to history (#3446)
Browse files Browse the repository at this point in the history
* fix: New product added to history

* fix: Show newly added products in history

* Update add_new_product_page.dart
  • Loading branch information
M123-dev authored Dec 21, 2022
1 parent 7b2930f commit 07d1f87
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
21 changes: 0 additions & 21 deletions packages/app/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -541,27 +541,6 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "3.2.0"
image_cropper:
dependency: transitive
description:
name: image_cropper
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.3"
image_cropper_for_web:
dependency: transitive
description:
name: image_cropper_for_web
url: "https://pub.dartlang.org"
source: hosted
version: "0.0.4"
image_cropper_platform_interface:
dependency: transitive
description:
name: image_cropper_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
image_picker:
dependency: transitive
description:
Expand Down
27 changes: 27 additions & 0 deletions packages/smooth_app/lib/pages/product/add_new_product_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:openfoodfacts/model/Product.dart';
import 'package:openfoodfacts/model/ProductImage.dart';
import 'package:provider/provider.dart';
import 'package:smooth_app/data_models/product_list.dart';
import 'package:smooth_app/database/dao_product_list.dart';
import 'package:smooth_app/database/local_database.dart';
import 'package:smooth_app/generic_lib/buttons/smooth_large_button_with_icon.dart';
import 'package:smooth_app/generic_lib/design_constants.dart';
Expand Down Expand Up @@ -45,17 +47,24 @@ class _AddNewProductPageState extends State<AddNewProductPage> {
late Product _product;
late final Product _initialProduct;
late final LocalDatabase _localDatabase;
late DaoProductList _daoProductList;

final ProductList _history = ProductList.history();

//likely broken: see https://github.com/openfoodfacts/smooth-app/issues/3445
bool get _nutritionFactsAdded => _product.nutriments != null;
bool get _basicDetailsAdded =>
AddBasicDetailsPage.isProductBasicValid(_product);

bool _alreadyPushedtToHistory = false;

@override
void initState() {
super.initState();
_initialProduct = Product(barcode: widget.barcode);
_localDatabase = context.read<LocalDatabase>();
_localDatabase.upToDate.showInterest(widget.barcode);
_daoProductList = DaoProductList(_localDatabase);
}

@override
Expand All @@ -67,10 +76,14 @@ class _AddNewProductPageState extends State<AddNewProductPage> {
@override
Widget build(BuildContext context) {
final AppLocalizations appLocalizations = AppLocalizations.of(context);

context.watch<LocalDatabase>();
final ThemeData themeData = Theme.of(context);
_product = _localDatabase.upToDate.getLocalUpToDate(_initialProduct);
final bool empty = _uploadedImages.isEmpty && _otherUploadedImages.isEmpty;

_addToHistory();

return SmoothScaffold(
appBar: AppBar(
title: Text(appLocalizations.new_product),
Expand Down Expand Up @@ -106,6 +119,20 @@ class _AddNewProductPageState extends State<AddNewProductPage> {
);
}

/// Adds the product to history if at least one of the fields is set.
Future<void> _addToHistory() async {
if (_alreadyPushedtToHistory) {
return;
}
// TODO(open): Add _nutritionFactsAdded , see (https://github.com/openfoodfacts/smooth-app/issues/3445)
if (_basicDetailsAdded ||
_uploadedImages.isNotEmpty ||
_otherUploadedImages.isNotEmpty) {
await _daoProductList.push(_history, _product.barcode!);
_alreadyPushedtToHistory = true;
}
}

List<Widget> _buildImageCaptureRows(BuildContext context) {
final List<Widget> rows = <Widget>[];
// First build rows for buttons to ask user to upload images.
Expand Down

0 comments on commit 07d1f87

Please sign in to comment.