From 20f8a038bf6e78ec134574a6dd2b084dfbe83315 Mon Sep 17 00:00:00 2001 From: VaiTon Date: Mon, 19 Jul 2021 15:02:22 +0200 Subject: [PATCH] fix: do not update products when history is empty --- .../scanhistory/ScanHistoryViewModel.kt | 45 ++++++++++--------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/openfoodfacts/github/scrachx/openfood/features/scanhistory/ScanHistoryViewModel.kt b/app/src/main/java/openfoodfacts/github/scrachx/openfood/features/scanhistory/ScanHistoryViewModel.kt index c320a0c7987b..8ea1d4b88ed9 100644 --- a/app/src/main/java/openfoodfacts/github/scrachx/openfood/features/scanhistory/ScanHistoryViewModel.kt +++ b/app/src/main/java/openfoodfacts/github/scrachx/openfood/features/scanhistory/ScanHistoryViewModel.kt @@ -47,28 +47,29 @@ class ScanHistoryViewModel @Inject constructor( unorderedProductState.postValue(FetchProductsState.Loading) withContext(Dispatchers.IO) { - try { - val barcodes = daoSession.historyProductDao.queryBuilder().list().map { it.barcode } - - client.getProductsByBarcode(barcodes) - .forEach { product -> - val historyProduct = daoSession.historyProductDao.queryBuilder() - .where(HistoryProductDao.Properties.Barcode.eq(product.code)) - .build() - .unique() - - product.productName?.let { historyProduct.title = it } - product.brands?.let { historyProduct.brands = it } - product.getImageSmallUrl(localeManager.getLanguage())?.let { historyProduct.url = it } - product.quantity?.let { historyProduct.quantity = it } - product.nutritionGradeFr?.let { historyProduct.nutritionGrade = it } - product.ecoscore?.let { historyProduct.ecoscore = it } - product.novaGroups?.let { historyProduct.novaGroup = it } - - daoSession.historyProductDao.update(historyProduct) - } - } catch (err: Exception) { - unorderedProductState.postValue(FetchProductsState.Error) + val barcodes = daoSession.historyProductDao.queryBuilder().list().map { it.barcode } + if (barcodes.isNotEmpty()) { + try { + client.getProductsByBarcode(barcodes) + .forEach { product -> + val historyProduct = daoSession.historyProductDao.queryBuilder() + .where(HistoryProductDao.Properties.Barcode.eq(product.code)) + .build() + .unique() + + product.productName?.let { historyProduct.title = it } + product.brands?.let { historyProduct.brands = it } + product.getImageSmallUrl(localeManager.getLanguage())?.let { historyProduct.url = it } + product.quantity?.let { historyProduct.quantity = it } + product.nutritionGradeFr?.let { historyProduct.nutritionGrade = it } + product.ecoscore?.let { historyProduct.ecoscore = it } + product.novaGroups?.let { historyProduct.novaGroup = it } + + daoSession.historyProductDao.update(historyProduct) + } + } catch (err: Exception) { + unorderedProductState.postValue(FetchProductsState.Error) + } } val updatedProducts = daoSession.historyProductDao.queryBuilder().list()