execute method
- LocalDatabase localDatabase
override
Executes the background task: upload, download, update locally.
Implementation
@override
Future<void> execute(LocalDatabase localDatabase) async {
final DaoProduct daoProduct = DaoProduct(localDatabase);
final OpenFoodFactsLanguage language = ProductQuery.getLanguage();
final List<String> barcodes = await daoProduct.getTopProductsToTranslate(
language,
limit: _pageSize,
excludeBarcodes: excludeBarcodes,
productType: productType,
);
if (barcodes.isEmpty) {
return;
}
final List<String> unwantedIngredients =
(await UserPreferences.getUserPreferences())
.getUnwantedIngredientTags();
final SearchResult searchResult =
await SearchProductsManager.searchProducts(
getUser(),
ProductSearchQueryConfiguration(
fields: ProductQuery.fields,
parametersList: <Parameter>[
const PageSize(size: _pageSize),
const PageNumber(page: 1),
BarcodeParameter.list(barcodes),
IngredientsUnwantedParameter(unwantedIngredients),
],
language: language,
country: ProductQuery.getCountry(),
version: ProductQuery.productQueryVersion,
activateKnowledgePanelsSimplified: true,
),
uriHelper: _uriProductHelper,
type: SearchProductsType.background,
);
if (searchResult.products == null || searchResult.count == null) {
throw Exception('Cannot refresh language');
}
// save into database and refresh all visible products.
await daoProduct.putAll(
searchResult.products!,
language,
productType: productType,
);
localDatabase.upToDate.setLatestDownloadedProducts(searchResult.products!);
// Next page
final List<String> newExcludeBarcodes = <String>[];
// we keep the old "excluded" barcodes,...
newExcludeBarcodes.addAll(excludeBarcodes);
// ...add the new barcodes...
newExcludeBarcodes.addAll(barcodes);
// ...and remove barcodes we actually found on the server.
for (final Product product in searchResult.products!) {
newExcludeBarcodes.remove(product.barcode);
}
await _addTask(
localDatabase,
excludeBarcodes: newExcludeBarcodes,
productType: productType,
);
}