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:
@@ -47,6 +47,9 @@ enum GameDAGRouter {
|
||||
|
||||
// MARK: - Configuration
|
||||
|
||||
/// Cached Calendar.current to avoid repeated access in tight loops
|
||||
private static let cachedCalendar = Calendar.current
|
||||
|
||||
/// Default beam width during expansion (for typical datasets)
|
||||
private static let defaultBeamWidth = 100
|
||||
|
||||
@@ -213,12 +216,19 @@ enum GameDAGRouter {
|
||||
for path in beam {
|
||||
guard let lastGame = path.last else { continue }
|
||||
|
||||
// Maintain visited cities set incrementally instead of rebuilding per candidate
|
||||
let pathCities: Set<String>
|
||||
if !allowRepeatCities {
|
||||
pathCities = Set(path.compactMap { stadiums[$0.stadiumId]?.city })
|
||||
} else {
|
||||
pathCities = []
|
||||
}
|
||||
|
||||
// Try adding each of today's games
|
||||
for candidate in todaysGames {
|
||||
// Check for repeat city violation
|
||||
if !allowRepeatCities {
|
||||
let candidateCity = stadiums[candidate.stadiumId]?.city ?? ""
|
||||
let pathCities = Set(path.compactMap { stadiums[$0.stadiumId]?.city })
|
||||
if pathCities.contains(candidateCity) {
|
||||
continue
|
||||
}
|
||||
@@ -504,17 +514,17 @@ enum GameDAGRouter {
|
||||
private static func bucketByDay(games: [Game]) -> [Int: [Game]] {
|
||||
guard let firstGame = games.first else { return [:] }
|
||||
let referenceDate = firstGame.startTime
|
||||
let calendar = Calendar.current
|
||||
|
||||
var buckets: [Int: [Game]] = [:]
|
||||
for game in games {
|
||||
let dayIndex = dayIndexFor(game.startTime, referenceDate: referenceDate)
|
||||
let dayIndex = dayIndexFor(game.startTime, referenceDate: referenceDate, calendar: calendar)
|
||||
buckets[dayIndex, default: []].append(game)
|
||||
}
|
||||
return buckets
|
||||
}
|
||||
|
||||
private static func dayIndexFor(_ date: Date, referenceDate: Date) -> Int {
|
||||
let calendar = Calendar.current
|
||||
private static func dayIndexFor(_ date: Date, referenceDate: Date, calendar: Calendar = .current) -> Int {
|
||||
let refDay = calendar.startOfDay(for: referenceDate)
|
||||
let dateDay = calendar.startOfDay(for: date)
|
||||
return calendar.dateComponents([.day], from: refDay, to: dateDay).day ?? 0
|
||||
@@ -548,8 +558,8 @@ enum GameDAGRouter {
|
||||
|
||||
let drivingHours = distanceMiles / 60.0
|
||||
|
||||
// Check if same calendar day
|
||||
let calendar = Calendar.current
|
||||
// Check if same calendar day (cache Calendar.current for performance in tight loops)
|
||||
let calendar = cachedCalendar
|
||||
let daysBetween = calendar.dateComponents(
|
||||
[.day],
|
||||
from: calendar.startOfDay(for: from.startTime),
|
||||
@@ -574,7 +584,9 @@ enum GameDAGRouter {
|
||||
}
|
||||
|
||||
// Calculate available time
|
||||
let departureTime = from.startTime.addingTimeInterval(postGameBuffer * 3600)
|
||||
// Use end time: startTime + estimated game duration (~3h) + post-game buffer
|
||||
let estimatedGameDurationHours: Double = 3.0
|
||||
let departureTime = from.startTime.addingTimeInterval((estimatedGameDurationHours + postGameBuffer) * 3600)
|
||||
let deadline = to.startTime.addingTimeInterval(-preGameBuffer * 3600)
|
||||
let availableHours = deadline.timeIntervalSince(departureTime) / 3600.0
|
||||
|
||||
|
||||
Reference in New Issue
Block a user