From 9724bcbacbe098d9427dc054dc8da8dfd7b687ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Orzech?= Date: Tue, 20 Jan 2026 13:18:42 +0000 Subject: [PATCH] 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. --- .gitignore | 3 +++ MacTorn/MacTorn/ViewModels/AppState.swift | 26 +++++++++++------------ 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index d1483cc..9a62f52 100644 --- a/.gitignore +++ b/.gitignore @@ -68,3 +68,6 @@ DerivedData/ # Archive folder (old releases) archive/ + +# Claude Code +CLAUDE.md diff --git a/MacTorn/MacTorn/ViewModels/AppState.swift b/MacTorn/MacTorn/ViewModels/AppState.swift index f92af9b..bbd8120 100644 --- a/MacTorn/MacTorn/ViewModels/AppState.swift +++ b/MacTorn/MacTorn/ViewModels/AppState.swift @@ -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() } }