diff --git a/packages/smooth_app/lib/pages/preferences/user_preferences_dev_mode.dart b/packages/smooth_app/lib/pages/preferences/user_preferences_dev_mode.dart index 404b657041a..cd914044928 100644 --- a/packages/smooth_app/lib/pages/preferences/user_preferences_dev_mode.dart +++ b/packages/smooth_app/lib/pages/preferences/user_preferences_dev_mode.dart @@ -9,6 +9,7 @@ import 'package:smooth_app/data_models/user_preferences.dart'; import 'package:smooth_app/database/dao_product_list.dart'; import 'package:smooth_app/database/local_database.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/helpers/data_importer/product_list_import_export.dart'; import 'package:smooth_app/helpers/data_importer/smooth_app_data_importer.dart'; import 'package:smooth_app/pages/onboarding/onboarding_flow_navigator.dart'; @@ -17,6 +18,7 @@ import 'package:smooth_app/pages/preferences/user_preferences_dialog_editor.dart import 'package:smooth_app/pages/preferences/user_preferences_page.dart'; import 'package:smooth_app/pages/scan/ml_kit_scan_page.dart'; import 'package:smooth_app/query/product_query.dart'; +import 'package:smooth_app/query/products_preload_helper.dart'; /// Collapsed/expanded display of "dev mode" for the preferences page. /// @@ -97,6 +99,28 @@ class UserPreferencesDevMode extends AbstractUserPreferences { setState(() {}); }, ), + ListTile( + title: const Text( + 'Preload Data', + ), + subtitle: const Text( + 'Download the top 1000 products in your country for instant scanning'), + onTap: () async { + final LocalDatabase localDatabase = context.read(); + final String status = await LoadingDialog.run( + title: 'Downloading data\nThis may take a while', + context: context, + future: PreloadDataHelper(localDatabase).preloadData(), + ) ?? + 'Cancelled'; + // ignore: use_build_context_synchronously + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text(status), + ), + ); + }, + ), ListTile( title: Text( appLocalizations.dev_preferences_reset_onboarding_title, @@ -425,7 +449,6 @@ class UserPreferencesDevMode extends AbstractUserPreferences { userPreferencesCameraPostFrameDuration, result, ); - setState(() {}); } } diff --git a/packages/smooth_app/lib/query/products_preload_helper.dart b/packages/smooth_app/lib/query/products_preload_helper.dart new file mode 100644 index 00000000000..fb36a818809 --- /dev/null +++ b/packages/smooth_app/lib/query/products_preload_helper.dart @@ -0,0 +1,43 @@ +import 'dart:io'; +import 'package:openfoodfacts/openfoodfacts.dart'; +import 'package:smooth_app/database/dao_product.dart'; +import 'package:smooth_app/database/local_database.dart'; +import 'package:smooth_app/query/product_query.dart'; + +class PreloadDataHelper { + PreloadDataHelper(this.localDatabase); + LocalDatabase localDatabase; + + Future preloadData() async { + final DaoProduct daoProduct = DaoProduct(localDatabase); + final List fields = ProductQuery.fields; + fields.remove(ProductField.KNOWLEDGE_PANELS); + try { + final ProductSearchQueryConfiguration queryConfig = + ProductSearchQueryConfiguration( + fields: fields, + parametersList: [ + const PageSize(size: 1000), + const PageNumber(page: 1), + const SortBy(option: SortOption.POPULARITY), + ], + language: ProductQuery.getLanguage(), + country: ProductQuery.getCountry(), + ); + final SearchResult searchResult = await OpenFoodAPIClient.searchProducts( + ProductQuery.getUser(), + queryConfig, + ); + if (searchResult.products!.isEmpty) { + return 'No products found'; + } else { + await daoProduct.putAll(searchResult.products!); + return '${searchResult.products!.length} products added to the database for instant scan'; + } + } on SocketException { + return 'No internet connection'; + } catch (e) { + return 'Error: $e'; + } + } +}