Skip to content

Commit

Permalink
fix: do not update products when history is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
VaiTon committed Jul 19, 2021
1 parent 3174809 commit 20f8a03
Showing 1 changed file with 23 additions and 22 deletions.
Expand Up @@ -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()
Expand Down

0 comments on commit 20f8a03

Please sign in to comment.