diff --git a/app/src/main/java/com/freshkeeper/service/product/ProductServiceImpl.kt b/app/src/main/java/com/freshkeeper/service/product/ProductServiceImpl.kt index 57a70ff..c6d01e7 100644 --- a/app/src/main/java/com/freshkeeper/service/product/ProductServiceImpl.kt +++ b/app/src/main/java/com/freshkeeper/service/product/ProductServiceImpl.kt @@ -20,6 +20,7 @@ import com.google.firebase.firestore.FirebaseFirestore import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch +import kotlinx.coroutines.tasks.await import kotlinx.coroutines.withContext import java.io.File import java.time.Instant @@ -38,12 +39,10 @@ class ProductServiceImpl val coroutineScope = CoroutineScope(Dispatchers.IO) var user: User = User() - var householdId: String? = null init { coroutineScope.launch { user = accountService.getUserObject() - householdId = householdService.getHouseholdId() } } @@ -81,6 +80,8 @@ class ProductServiceImpl else -> null } + val householdId = householdService.getHouseholdId() + val foodItem = FoodItem( barcode = barcode, @@ -106,7 +107,7 @@ class ProductServiceImpl .update("id", documentReference.id) .addOnSuccessListener { coroutineScope.launch { - if (user.householdId != null) { + if (householdId != null) { logActivity( foodItem.copy(id = documentReference.id), productName, @@ -238,58 +239,55 @@ class ProductServiceImpl ) onSuccess(updatedFoodItem) - if (user.householdId != null) { - firestore - .collection("households") - .document(user.householdId!!) - .get() - .addOnSuccessListener { householdDoc -> + coroutineScope.launch { + if (householdId != null) { + try { + val householdDoc = + firestore + .collection("households") + .document(householdId) + .get() + .await() val household = - householdDoc.toObject( - Household::class.java, - ) + householdDoc.toObject(Household::class.java) if (household != null && household.type != HouseholdType.SINGLE ) { - coroutineScope.launch { - val changedFields = mutableListOf() - if (foodItem.name != productName) { - changedFields.add(EventType.NAME) - } - if (foodItem.quantity != quantity) { - changedFields.add(EventType.QUANTITY) - } - if (foodItem.expiryTimestamp != expiryTimestamp) { - changedFields.add(EventType.EXPIRY) - } - if (foodItem.storageLocation != storageLocation) { - changedFields.add(EventType.STORAGE) - } - if (foodItem.category != category) { - changedFields.add(EventType.CATEGORY) - } - val activityType = - when { - isConsumedChecked -> - EventType.CONSUMED - isThrownAwayChecked -> - EventType.THROWN_AWAY - changedFields.size == 1 -> - changedFields.first() - else -> EventType.EDIT - } - logActivity( - updatedFoodItem, - productName, - activityType, - oldName = foodItem.name, - oldQuantity = foodItem.quantity, - ) + val changedFields = mutableListOf() + if (foodItem.name != productName) { + changedFields.add(EventType.NAME) + } + if (foodItem.quantity != quantity) { + changedFields.add(EventType.QUANTITY) } + if (foodItem.expiryTimestamp != expiryTimestamp) { + changedFields.add(EventType.EXPIRY) + } + if (foodItem.storageLocation != storageLocation) { + changedFields.add(EventType.STORAGE) + } + if (foodItem.category != category) { + changedFields.add(EventType.CATEGORY) + } + val activityType = + when { + isConsumedChecked -> EventType.CONSUMED + isThrownAwayChecked -> EventType.THROWN_AWAY + changedFields.size == 1 -> changedFields.first() + else -> EventType.EDIT + } + logActivity( + updatedFoodItem, + productName, + activityType, + oldName = foodItem.name, + oldQuantity = foodItem.quantity, + ) } - }.addOnFailureListener { e -> + } catch (e: Exception) { Log.e("Firestore", "Error retrieving household", e) } + } } }.addOnFailureListener { e -> Log.e("Firestore", "Error when updating the product", e) @@ -321,10 +319,12 @@ class ProductServiceImpl else -> activityType } + val currentHouseholdId = householdService.getHouseholdId() + val activity = Activity( userId = user.id, - householdId = householdId, + householdId = currentHouseholdId, type = resolvedType, userName = user.displayName!!, timestamp = System.currentTimeMillis(),