Fix watchlist item mutation to update via copy

Refactored watchlist item updates to use value semantics by copying, modifying, and reassigning the item in the array. This prevents issues with direct mutation of value types in Swift arrays and ensures changes are properly reflected.
This commit is contained in:
Paweł Orzech 2026-01-20 13:18:42 +00:00
parent 8a4fb30cad
commit 9724bcbacb
No known key found for this signature in database
2 changed files with 16 additions and 13 deletions

3
.gitignore vendored
View file

@ -68,3 +68,6 @@ DerivedData/
# Archive folder (old releases)
archive/
# Claude Code
CLAUDE.md

View file

@ -303,19 +303,17 @@ class AppState: ObservableObject {
await MainActor.run {
if let index = watchlistItems.firstIndex(where: { $0.id == itemId }) {
if let best = sortedListings.first {
watchlistItems[index].lowestPrice = best.price
watchlistItems[index].lowestPriceQuantity = best.amount
// Check for next distinct price or just next listing? usually user wants to know diff to next cheapest offer even if it's same price?
// Actually "second lowest price" usually implies the price of the *next available item*.
// But usually users want to know price steps.
// Let's stick to simple logic: price of the 2nd listing in sorted list.
watchlistItems[index].secondLowestPrice = sortedListings.count > 1 ? sortedListings[1].price : 0
watchlistItems[index].lastUpdated = Date()
watchlistItems[index].error = nil
var item = watchlistItems[index]
item.lowestPrice = best.price
item.lowestPriceQuantity = best.amount
item.secondLowestPrice = sortedListings.count > 1 ? sortedListings[1].price : 0
item.lastUpdated = Date()
item.error = nil
watchlistItems[index] = item
} else {
watchlistItems[index].error = "No listings"
var item = watchlistItems[index]
item.error = "No listings"
watchlistItems[index] = item
}
saveWatchlist()
}
@ -330,7 +328,9 @@ class AppState: ObservableObject {
@MainActor
private func updateItemError(itemId: Int, error: String) {
if let index = watchlistItems.firstIndex(where: { $0.id == itemId }) {
watchlistItems[index].error = error
var item = watchlistItems[index]
item.error = error
watchlistItems[index] = item
saveWatchlist()
}
}