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:
Trey t
2026-02-27 17:03:09 -06:00
parent e046cb6b34
commit c94e373e33
82 changed files with 1163 additions and 599 deletions

View File

@@ -9,6 +9,9 @@
import Foundation
import SwiftData
import CryptoKit
import os
private let logger = Logger(subsystem: "com.88oakapps.SportsTime", category: "BootstrapService")
@MainActor
final class BootstrapService {
@@ -252,7 +255,13 @@ final class BootstrapService {
// Build stadium lookup
let stadiumDescriptor = FetchDescriptor<CanonicalStadium>()
let stadiums = (try? context.fetch(stadiumDescriptor)) ?? []
let stadiums: [CanonicalStadium]
do {
stadiums = try context.fetch(stadiumDescriptor)
} catch {
logger.error("Failed to fetch stadiums for alias linking: \(error.localizedDescription)")
stadiums = []
}
let stadiumsByCanonicalId = Dictionary(uniqueKeysWithValues: stadiums.map { ($0.canonicalId, $0) })
let dateFormatter = DateFormatter()
@@ -410,11 +419,23 @@ final class BootstrapService {
}
var seenGameIds = Set<String>()
let teams = (try? context.fetch(FetchDescriptor<CanonicalTeam>())) ?? []
let teams: [CanonicalTeam]
do {
teams = try context.fetch(FetchDescriptor<CanonicalTeam>())
} catch {
logger.error("Failed to fetch teams for game bootstrap: \(error.localizedDescription)")
teams = []
}
let stadiumByTeamId = Dictionary(uniqueKeysWithValues: teams.map { ($0.canonicalId, $0.stadiumCanonicalId) })
// Build stadium timezone lookup for correct local time parsing
let stadiums = (try? context.fetch(FetchDescriptor<CanonicalStadium>())) ?? []
let stadiums: [CanonicalStadium]
do {
stadiums = try context.fetch(FetchDescriptor<CanonicalStadium>())
} catch {
logger.error("Failed to fetch stadiums for game bootstrap: \(error.localizedDescription)")
stadiums = []
}
let timezoneByStadiumId: [String: TimeZone] = stadiums.reduce(into: [:]) { dict, stadium in
if let tzId = stadium.timezoneIdentifier, let tz = TimeZone(identifier: tzId) {
dict[stadium.canonicalId] = tz
@@ -521,21 +542,39 @@ final class BootstrapService {
}
private func hasRequiredCanonicalData(context: ModelContext) -> Bool {
let stadiumCount = (try? context.fetchCount(
FetchDescriptor<CanonicalStadium>(
predicate: #Predicate { $0.deprecatedAt == nil }
let stadiumCount: Int
do {
stadiumCount = try context.fetchCount(
FetchDescriptor<CanonicalStadium>(
predicate: #Predicate { $0.deprecatedAt == nil }
)
)
)) ?? 0
let teamCount = (try? context.fetchCount(
FetchDescriptor<CanonicalTeam>(
predicate: #Predicate { $0.deprecatedAt == nil }
} catch {
logger.error("Failed to count stadiums: \(error.localizedDescription)")
stadiumCount = 0
}
let teamCount: Int
do {
teamCount = try context.fetchCount(
FetchDescriptor<CanonicalTeam>(
predicate: #Predicate { $0.deprecatedAt == nil }
)
)
)) ?? 0
let gameCount = (try? context.fetchCount(
FetchDescriptor<CanonicalGame>(
predicate: #Predicate { $0.deprecatedAt == nil }
} catch {
logger.error("Failed to count teams: \(error.localizedDescription)")
teamCount = 0
}
let gameCount: Int
do {
gameCount = try context.fetchCount(
FetchDescriptor<CanonicalGame>(
predicate: #Predicate { $0.deprecatedAt == nil }
)
)
)) ?? 0
} catch {
logger.error("Failed to count games: \(error.localizedDescription)")
gameCount = 0
}
return stadiumCount > 0 && teamCount > 0 && gameCount > 0
}