-
-
Notifications
You must be signed in to change notification settings - Fork 0
Fix inventory retrieval for joined households #204
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
maxschwinghammer
wants to merge
2
commits into
master
from
codex/beheben-sie-das-inventarproblem-bei-haushalten
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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<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) | ||
|
|
@@ -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(), | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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