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

@@ -7,6 +7,25 @@
import Foundation
// MARK: - Cached DateFormatter Storage
private enum GameTimeFormatterCache {
/// Cache keyed by "\(format)_\(timezoneIdentifier)"
nonisolated(unsafe) static let cache = NSCache<NSString, DateFormatter>()
static func formatter(format: String, timeZone: TimeZone) -> DateFormatter {
let key = "\(format)_\(timeZone.identifier)" as NSString
if let cached = cache.object(forKey: key) {
return cached
}
let formatter = DateFormatter()
formatter.dateFormat = format
formatter.timeZone = timeZone
cache.setObject(formatter, forKey: key)
return formatter
}
}
extension Date {
/// Formats the date as a game time string in the specified timezone.
@@ -26,9 +45,9 @@ extension Date {
/// game.dateTime.gameTimeString(in: stadium.timeZone, includeZone: true) // "7:00 PM EDT"
/// ```
func gameTimeString(in timeZone: TimeZone?, includeZone: Bool = false) -> String {
let formatter = DateFormatter()
formatter.dateFormat = includeZone ? "h:mm a z" : "h:mm a"
formatter.timeZone = timeZone ?? .current
let format = includeZone ? "h:mm a z" : "h:mm a"
let tz = timeZone ?? .current
let formatter = GameTimeFormatterCache.formatter(format: format, timeZone: tz)
return formatter.string(from: self)
}
@@ -39,9 +58,9 @@ extension Date {
/// - includeZone: Whether to include the timezone abbreviation.
/// - Returns: A formatted string like "Sat, Jan 18 at 7:00 PM" or "Sat, Jan 18 at 7:00 PM EDT".
func gameDateTimeString(in timeZone: TimeZone?, includeZone: Bool = false) -> String {
let formatter = DateFormatter()
formatter.dateFormat = includeZone ? "EEE, MMM d 'at' h:mm a z" : "EEE, MMM d 'at' h:mm a"
formatter.timeZone = timeZone ?? .current
let format = includeZone ? "EEE, MMM d 'at' h:mm a z" : "EEE, MMM d 'at' h:mm a"
let tz = timeZone ?? .current
let formatter = GameTimeFormatterCache.formatter(format: format, timeZone: tz)
return formatter.string(from: self)
}