forked from openfoodfacts/smooth-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added feat in dev mode to preload 1k products (openfoodfacts#2661)
* 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
1 parent
dd9c9c6
commit 37e5b75
Showing
2 changed files
with
67 additions
and
1 deletion.
There are no files selected for viewing
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
43 changes: 43 additions & 0 deletions
43
packages/smooth_app/lib/query/products_preload_helper.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,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'; | ||
} | ||
} | ||
} |