Skip to content

Commit

Permalink
feat: openfoodfacts#796 - added a "refresh products" button to histor…
Browse files Browse the repository at this point in the history
…y page

Impacted file:
* `product_list_page.dart`: added a "refresh products" button; refactored
  • Loading branch information
monsieurtanuki committed Mar 10, 2022
1 parent b27199f commit abd35d4
Showing 1 changed file with 69 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:openfoodfacts/model/Product.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:openfoodfacts/utils/ProductListQueryConfiguration.dart';
import 'package:provider/provider.dart';
import 'package:smooth_app/data_models/product_list.dart';
import 'package:smooth_app/database/dao_product.dart';
import 'package:smooth_app/database/dao_product_list.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';
import 'package:smooth_app/pages/personalized_ranking_page.dart';
import 'package:smooth_app/pages/product/common/product_list_item_simple.dart';
import 'package:smooth_app/pages/product/common/product_query_page_helper.dart';
Expand Down Expand Up @@ -98,6 +102,14 @@ class _ProductListPageState extends State<ProductListPage> {
overflow: TextOverflow.fade,
),
),
if ((!_selectionMode) && products.isNotEmpty)
IconButton(
icon: const Icon(Icons.refresh),
onPressed: () async => _refreshListProducts(
products,
localDatabase,
),
),
if ((!_selectionMode) && products.isNotEmpty)
Flexible(
child: ElevatedButton(
Expand Down Expand Up @@ -190,4 +202,60 @@ class _ProductListPageState extends State<ProductListPage> {
),
);
}

/// Calls the "refresh products" part with dialogs on top.
Future<void> _refreshListProducts(
final List<Product> products,
final LocalDatabase localDatabase,
) async {
final bool? done = await LoadingDialog.run<bool>(
context: context,
title:
'refreshing the history products', // TODO(monsieurtanuki): localize
future: _reloadProducts(products, localDatabase),
);
switch (done) {
case null: // user clicked on "stop"
return;
case true:
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Just refreshed'), // TODO(monsieurtanuki): localize
duration: Duration(seconds: 2),
),
);
setState(() {});
return;
case false:
LoadingDialog.error(context: context);
return;
}
}

/// Fetches the products from the API and refreshes the local database
Future<bool> _reloadProducts(
final List<Product> products,
final LocalDatabase localDatabase,
) async {
try {
final List<String> barcodes = <String>[];
for (final Product product in products) {
barcodes.add(product.barcode!);
}
final SearchResult searchResult = await OpenFoodAPIClient.getProductList(
ProductQuery.getUser(),
ProductListQueryConfiguration(barcodes),
);
final List<Product>? freshProducts = searchResult.products;
if (freshProducts == null) {
return false;
}
await DaoProduct(localDatabase).putAll(freshProducts);
freshProducts.forEach(productList.refresh);
return true;
} catch (e) {
//
}
return false;
}
}

0 comments on commit abd35d4

Please sign in to comment.