-
-
Notifications
You must be signed in to change notification settings - Fork 287
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: #1234 - added a dev mode button to reset history with 3 product…
…s. (#1514) New file: * `product_list_import_export.dart`: Import / Export of product lists via json. Impacted file: * `user_preferences_dev_mode.dart`: added a button that clears history and put 3 products in there.
- Loading branch information
1 parent
2b6b0b1
commit c6e20af
Showing
2 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
packages/smooth_app/lib/helpers/product_list_import_export.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:openfoodfacts/openfoodfacts.dart'; | ||
import 'package:openfoodfacts/utils/ProductListQueryConfiguration.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 / Export of product lists via json. | ||
class ProductListImportExport { | ||
static const String TMP_IMPORT = '{' | ||
'"list": ["3760020507350", "7300400481588", "3502110009449"],' | ||
'"data":{' | ||
'"12345": {"qty":5}' | ||
'}' | ||
'}'; | ||
|
||
Future<void> import( | ||
final String jsonEncoded, | ||
final LocalDatabase localDatabase, | ||
) async { | ||
final dynamic map = json.decode(jsonEncoded); | ||
if (map is! Map<String, dynamic>) { | ||
throw Exception('Expected Map<String, dynamic>'); | ||
} | ||
final dynamic list = map['list']; | ||
if (list is! List<dynamic>) { | ||
throw Exception('Expected List<dynamic>'); | ||
} | ||
final List<String> inputBarcodes = <String>[]; | ||
for (final dynamic barcode in list) { | ||
inputBarcodes.add(barcode as String); | ||
} | ||
final SearchResult searchResult = await OpenFoodAPIClient.getProductList( | ||
ProductQuery.getUser(), | ||
ProductListQueryConfiguration( | ||
inputBarcodes, | ||
fields: ProductQuery.fields, | ||
language: ProductQuery.getLanguage(), | ||
country: ProductQuery.getCountry(), | ||
), | ||
); | ||
if (searchResult.products == null) { | ||
return; | ||
} | ||
final DaoProduct daoProduct = DaoProduct(localDatabase); | ||
final DaoProductList daoProductList = DaoProductList(localDatabase); | ||
final Map<String, Product> products = <String, Product>{}; | ||
for (final Product product in searchResult.products!) { | ||
products[product.barcode!] = product; | ||
await daoProduct.put(product); | ||
} | ||
final List<String> barcodes = <String>[]; | ||
for (final String barcode in inputBarcodes) { | ||
if (products.containsKey(barcode)) { | ||
barcodes.add(barcode); | ||
} | ||
} | ||
final ProductList productList = ProductList.history(); | ||
productList.set(barcodes, products); | ||
await daoProductList.put(productList); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters