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 folder (old releases)
archive/ archive/
# Claude Code
CLAUDE.md

View file

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