Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
}
}

Expand Down Expand Up @@ -81,6 +80,8 @@ class ProductServiceImpl
else -> null
}

val householdId = householdService.getHouseholdId()
Copy link

Copilot AI Aug 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The household ID is retrieved synchronously on the main thread during addProduct. Consider moving this call inside the coroutine scope or making it suspend to avoid potential UI blocking.

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot open a new pull request to apply changes based on this feedback


val foodItem =
FoodItem(
barcode = barcode,
Expand All @@ -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,
Expand Down Expand Up @@ -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<EventType>()
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<EventType>()
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)
Expand Down Expand Up @@ -321,10 +319,12 @@ class ProductServiceImpl
else -> activityType
}

val currentHouseholdId = householdService.getHouseholdId()
Copy link

Copilot AI Aug 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The household ID is retrieved synchronously in logActivity method. If this method is called from a coroutine context, consider making getHouseholdId() a suspend function to avoid blocking calls.

Copilot uses AI. Check for mistakes.

val activity =
Activity(
userId = user.id,
householdId = householdId,
householdId = currentHouseholdId,
type = resolvedType,
userName = user.displayName!!,
timestamp = System.currentTimeMillis(),
Expand Down
Loading