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

fix: #1395 - show newly added product in carousel #1520

Merged
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -12,7 +12,7 @@ class SmoothProductCardNotFound extends StatelessWidget {
this.elevation = 0.0,
});

final VoidCallback? callback;
final Function(String?)? callback;
final double elevation;
final String barcode;

Expand Down Expand Up @@ -48,16 +48,16 @@ class SmoothProductCardNotFound extends StatelessWidget {
text: appLocalizations.add_product_information_button_label,
icon: Icons.add,
padding: const EdgeInsets.symmetric(vertical: LARGE_SPACE),
onPressed: () {
Navigator.push<Widget>(
onPressed: () async {
final String? result = await Navigator.push<String>(
context,
MaterialPageRoute<Widget>(
MaterialPageRoute<String>(
builder: (BuildContext context) =>
AddNewProductPage(barcode),
),
);
if (callback != null) {
callback!();
await callback!(result);
}
},
),
Expand Down
11 changes: 10 additions & 1 deletion packages/smooth_app/lib/data_models/continuous_scan_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ class ContinuousScanModel with ChangeNotifier {
_addBarcode(code);
}

Future<bool> onCreateProduct(String? barcode) async {
if (barcode == null) {
return false;
}
return _addBarcode(barcode);
}

Future<void> retryBarcodeFetch(String barcode) async {
_setBarcodeState(barcode, ScannedProductState.LOADING);
await _updateBarcode(barcode);
Expand All @@ -113,7 +120,9 @@ class ContinuousScanModel with ChangeNotifier {
Future<bool> _addBarcode(final String barcode) async {
final ScannedProductState? state = getBarcodeState(barcode);
if (state == null) {
_barcodes.add(barcode);
if (!_barcodes.contains(barcode)) {
_barcodes.add(barcode);
}
_setBarcodeState(barcode, ScannedProductState.LOADING);
_cacheOrLoadBarcode(barcode);
return true;
Expand Down
11 changes: 11 additions & 0 deletions packages/smooth_app/lib/helpers/picture_capture_helper.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import 'package:flutter/material.dart';
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/continuous_scan_model.dart';
import 'package:smooth_app/database/local_database.dart';
import 'package:smooth_app/database/product_query.dart';
import 'package:smooth_app/generic_lib/loading_dialog.dart';

Expand Down Expand Up @@ -32,5 +35,13 @@ Future<bool> uploadCapturedPicture(
);
return false;
}
await _updateContinuousScanModel(context, barcode);
return true;
}

Future<void> _updateContinuousScanModel(
BuildContext context, String barcode) async {
final ContinuousScanModel? model =
await ContinuousScanModel().load(context.read<LocalDatabase>());
await model?.onCreateProduct(barcode);
}
13 changes: 10 additions & 3 deletions packages/smooth_app/lib/pages/product/add_new_product_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ class _AddNewProductPageState extends State<AddNewProductPage> {
final Map<ImageField, List<File>> _uploadedImages =
<ImageField, List<File>>{};

bool _isProductLoaded = false;

@override
Widget build(BuildContext context) {
final AppLocalizations appLocalizations = AppLocalizations.of(context)!;
final ThemeData themeData = Theme.of(context);
return Scaffold(
appBar: AppBar(),
appBar: AppBar(automaticallyImplyLeading: !_isProductLoaded),
body: Padding(
padding: const EdgeInsets.only(
top: VERY_LARGE_SPACE,
Expand Down Expand Up @@ -75,7 +77,10 @@ class _AddNewProductPageState extends State<AddNewProductPage> {
alignment: Alignment.bottomRight,
child: SmoothActionButton(
text: appLocalizations.finish,
onPressed: () => Navigator.pop(context),
onPressed: () {
Navigator.maybePop(
context, _isProductLoaded ? widget.barcode : null);
},
),
),
),
Expand Down Expand Up @@ -138,7 +143,9 @@ class _AddNewProductPageState extends State<AddNewProductPage> {
if (finalPhoto != null) {
_uploadedImages[imageType] = _uploadedImages[imageType] ?? <File>[];
_uploadedImages[imageType]!.add(initialPhoto);
setState(() {});
setState(() {
_isProductLoaded = true;
});
}
initialPhoto.delete();
},
Expand Down
19 changes: 12 additions & 7 deletions packages/smooth_app/lib/widgets/smooth_product_carousel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ class _SmoothProductCarouselState extends State<SmoothProductCarousel> {
bool _returnToSearchCard = false;

int get _searchCardAdjustment => widget.containSearchCard ? 1 : 0;
late ContinuousScanModel _model;

@override
void didChangeDependencies() {
super.didChangeDependencies();
final ContinuousScanModel model = context.watch<ContinuousScanModel>();
barcodes = model.getBarcodes();
_model = context.watch<ContinuousScanModel>();
barcodes = _model.getBarcodes();
_returnToSearchCard = InheritedDataManager.of(context).showSearchCard;
if (_controller.ready) {
if (_returnToSearchCard && widget.containSearchCard) {
Expand All @@ -51,6 +52,7 @@ class _SmoothProductCarouselState extends State<SmoothProductCarousel> {

@override
Widget build(BuildContext context) {
barcodes = _model.getBarcodes();
return CarouselSlider.builder(
itemCount: barcodes.length + _searchCardAdjustment,
itemBuilder: (BuildContext context, int itemIndex, int itemRealIndex) {
Expand Down Expand Up @@ -87,21 +89,24 @@ class _SmoothProductCarouselState extends State<SmoothProductCarousel> {
return Container();
}
final String barcode = barcodes[index];
final ContinuousScanModel model = context.watch<ContinuousScanModel>();
switch (model.getBarcodeState(barcode)!) {
switch (_model.getBarcodeState(barcode)!) {
case ScannedProductState.FOUND:
case ScannedProductState.CACHED:
final Product product = model.getProduct(barcode);
final Product product = _model.getProduct(barcode);
return ScanProductCard(product);
case ScannedProductState.LOADING:
return SmoothProductCardLoading(barcode: barcode);
case ScannedProductState.NOT_FOUND:
return SmoothProductCardNotFound(
barcode: barcode,
callback: () {
callback: (String? barcodeLoaded) async {
// Remove the "Add New Product" card. The user may have added it
// already.
model.getBarcodes().remove(barcode);
if (barcodeLoaded == null) {
_model.getBarcodes().remove(barcode);
} else {
await _model.refresh();
}
setState(() {});
},
);
Expand Down