Skip to content

Commit

Permalink
feat: added feat in dev mode to preload 1k products (openfoodfacts#2661)
Browse files Browse the repository at this point in the history
* added feat in dev mode to preload 1k products

* explixitly mention to do a sort by popularity

* moved helper into query dir
  • Loading branch information
AshAman999 authored Jul 27, 2022
1 parent dd9c9c6 commit 37e5b75
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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.
///
Expand Down Expand Up @@ -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<LocalDatabase>();
final String status = await LoadingDialog.run<String>(
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,
Expand Down Expand Up @@ -425,7 +449,6 @@ class UserPreferencesDevMode extends AbstractUserPreferences {
userPreferencesCameraPostFrameDuration,
result,
);

setState(() {});
}
}
Expand Down
43 changes: 43 additions & 0 deletions packages/smooth_app/lib/query/products_preload_helper.dart
Original file line number Diff line number Diff line change
@@ -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<String> preloadData() async {
final DaoProduct daoProduct = DaoProduct(localDatabase);
final List<ProductField> fields = ProductQuery.fields;
fields.remove(ProductField.KNOWLEDGE_PANELS);
try {
final ProductSearchQueryConfiguration queryConfig =
ProductSearchQueryConfiguration(
fields: fields,
parametersList: <Parameter>[
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';
}
}
}

0 comments on commit 37e5b75

Please sign in to comment.