fix: comprehensive codebase hardening — crashes, silent failures, performance, and security
Fixes ~95 issues from deep audit across 12 categories in 82 files: - Crash prevention: double-resume in PhotoMetadataExtractor, force unwraps in DateRangePicker, array bounds checks in polls/achievements, ProGate hit-test bypass, Dictionary(uniqueKeysWithValues:) → uniquingKeysWith in 4 files - Silent failure elimination: all 34 try? sites replaced with do/try/catch + logging (SavedTrip, TripDetailView, CanonicalSyncService, BootstrapService, CanonicalModels, CKModels, SportsTimeApp, and more) - Performance: cached DateFormatters (7 files), O(1) team lookups via AppDataProvider, achievement definition dictionary, AnimatedBackground consolidated from 19 Tasks to 1, task cancellation in SharePreviewView - Concurrency: UIKit drawing → MainActor.run, background fetch timeout guard, @MainActor on ThemeManager/AppearanceManager, SyncLogger read/write race fix - Planning engine: game end time in travel feasibility, state-aware city normalization, exact city matching, DrivingConstraints parameter propagation - IAP: unknown subscription states → expired, unverified transaction logging, entitlements updated before paywall dismiss, restore visible to all users - Security: API key to Info.plist lookup, filename sanitization in PDF export, honest User-Agent, removed stale "Feels" analytics super properties - Navigation: consolidated competing navigationDestination, boolean → value-based - Testing: 8 sleep() → waitForExistence, duplicates extracted, Swift 6 compat - Service bugs: infinite retry cap, duplicate achievement prevention, TOCTOU vote fix, PollVote.odg → voterId rename, deterministic placeholder IDs, parallel MKDirections, Sendable-safe POI struct Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1397,9 +1397,13 @@ struct TripDetailView: View {
|
||||
predicate: #Predicate { $0.id == tripId }
|
||||
)
|
||||
|
||||
if let count = try? modelContext.fetchCount(descriptor), count > 0 {
|
||||
isSaved = true
|
||||
} else {
|
||||
do {
|
||||
let count = try modelContext.fetchCount(descriptor)
|
||||
isSaved = count > 0
|
||||
} catch {
|
||||
#if DEBUG
|
||||
print("⚠️ [TripDetail] Failed to check save status: \(error)")
|
||||
#endif
|
||||
isSaved = false
|
||||
}
|
||||
}
|
||||
@@ -1416,7 +1420,15 @@ struct TripDetailView: View {
|
||||
let descriptor = FetchDescriptor<LocalItineraryItem>(
|
||||
predicate: #Predicate { $0.tripId == tripId }
|
||||
)
|
||||
let localModels = (try? modelContext.fetch(descriptor)) ?? []
|
||||
let localModels: [LocalItineraryItem]
|
||||
do {
|
||||
localModels = try modelContext.fetch(descriptor)
|
||||
} catch {
|
||||
#if DEBUG
|
||||
print("⚠️ [ItineraryItems] Failed to fetch local items: \(error)")
|
||||
#endif
|
||||
localModels = []
|
||||
}
|
||||
let localItems = localModels.compactMap(\.toItem)
|
||||
let pendingLocalItems = localModels.compactMap { local -> ItineraryItem? in
|
||||
guard local.pendingSync else { return nil }
|
||||
@@ -1524,14 +1536,24 @@ struct TripDetailView: View {
|
||||
let descriptor = FetchDescriptor<LocalItineraryItem>(
|
||||
predicate: #Predicate { $0.tripId == tripId }
|
||||
)
|
||||
let existing = (try? modelContext.fetch(descriptor)) ?? []
|
||||
for old in existing { modelContext.delete(old) }
|
||||
do {
|
||||
let existing = try modelContext.fetch(descriptor)
|
||||
for old in existing { modelContext.delete(old) }
|
||||
} catch {
|
||||
#if DEBUG
|
||||
print("⚠️ [ItineraryItems] Failed to fetch existing local items for sync: \(error)")
|
||||
#endif
|
||||
}
|
||||
for item in items {
|
||||
if let local = LocalItineraryItem.from(item, pendingSync: pendingSyncItemIDs.contains(item.id)) {
|
||||
modelContext.insert(local)
|
||||
}
|
||||
}
|
||||
try? modelContext.save()
|
||||
do {
|
||||
try modelContext.save()
|
||||
} catch {
|
||||
persistenceErrorMessage = "Failed to sync itinerary cache: \(error.localizedDescription)"
|
||||
}
|
||||
}
|
||||
|
||||
private func saveItineraryItem(_ item: ItineraryItem) async {
|
||||
@@ -1581,30 +1603,55 @@ struct TripDetailView: View {
|
||||
let descriptor = FetchDescriptor<LocalItineraryItem>(
|
||||
predicate: #Predicate { $0.id == itemId }
|
||||
)
|
||||
if let existing = try? modelContext.fetch(descriptor).first {
|
||||
existing.day = item.day
|
||||
existing.sortOrder = item.sortOrder
|
||||
existing.kindData = (try? JSONEncoder().encode(item.kind)) ?? existing.kindData
|
||||
existing.modifiedAt = item.modifiedAt
|
||||
existing.pendingSync = true
|
||||
} else if let local = LocalItineraryItem.from(item, pendingSync: true) {
|
||||
modelContext.insert(local)
|
||||
do {
|
||||
if let existing = try modelContext.fetch(descriptor).first {
|
||||
existing.day = item.day
|
||||
existing.sortOrder = item.sortOrder
|
||||
do {
|
||||
existing.kindData = try JSONEncoder().encode(item.kind)
|
||||
} catch {
|
||||
#if DEBUG
|
||||
print("⚠️ [ItineraryItems] Failed to encode item kind: \(error)")
|
||||
#endif
|
||||
}
|
||||
existing.modifiedAt = item.modifiedAt
|
||||
existing.pendingSync = true
|
||||
} else if let local = LocalItineraryItem.from(item, pendingSync: true) {
|
||||
modelContext.insert(local)
|
||||
}
|
||||
} catch {
|
||||
#if DEBUG
|
||||
print("⚠️ [ItineraryItems] Failed to fetch local item for update: \(error)")
|
||||
#endif
|
||||
if let local = LocalItineraryItem.from(item, pendingSync: true) {
|
||||
modelContext.insert(local)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if let local = LocalItineraryItem.from(item, pendingSync: true) {
|
||||
modelContext.insert(local)
|
||||
}
|
||||
}
|
||||
try? modelContext.save()
|
||||
do {
|
||||
try modelContext.save()
|
||||
} catch {
|
||||
persistenceErrorMessage = "Failed to save itinerary item: \(error.localizedDescription)"
|
||||
}
|
||||
}
|
||||
|
||||
private func markLocalItemSynced(_ itemId: UUID) {
|
||||
let descriptor = FetchDescriptor<LocalItineraryItem>(
|
||||
predicate: #Predicate { $0.id == itemId }
|
||||
)
|
||||
if let local = try? modelContext.fetch(descriptor).first {
|
||||
local.pendingSync = false
|
||||
try? modelContext.save()
|
||||
do {
|
||||
if let local = try modelContext.fetch(descriptor).first {
|
||||
local.pendingSync = false
|
||||
try modelContext.save()
|
||||
}
|
||||
} catch {
|
||||
#if DEBUG
|
||||
print("⚠️ [ItineraryItems] Failed to mark item synced: \(error)")
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1622,9 +1669,13 @@ struct TripDetailView: View {
|
||||
let descriptor = FetchDescriptor<LocalItineraryItem>(
|
||||
predicate: #Predicate { $0.id == itemId }
|
||||
)
|
||||
if let local = try? modelContext.fetch(descriptor).first {
|
||||
modelContext.delete(local)
|
||||
try? modelContext.save()
|
||||
do {
|
||||
if let local = try modelContext.fetch(descriptor).first {
|
||||
modelContext.delete(local)
|
||||
try modelContext.save()
|
||||
}
|
||||
} catch {
|
||||
persistenceErrorMessage = "Failed to delete itinerary item: \(error.localizedDescription)"
|
||||
}
|
||||
|
||||
// Delete from CloudKit
|
||||
|
||||
Reference in New Issue
Block a user