Add Stadium Progress system and themed loading spinners
Stadium Progress & Achievements: - Add StadiumVisit and Achievement SwiftData models - Create Progress tab with interactive map view - Implement photo-based visit import with GPS/date matching - Add achievement badges (count-based, regional, journey) - Create shareable progress cards for social media - Add canonical data infrastructure (stadium identities, team aliases) - Implement score resolution from free APIs (MLB, NBA, NHL stats) UI Improvements: - Add ThemedSpinner and ThemedSpinnerCompact components - Replace all ProgressView() with themed spinners throughout app - Fix sport selection state not persisting when navigating away Bug Fixes: - Fix Coast to Coast trips showing only 1 city (validation issue) - Fix stadium progress showing 0/0 (filtering issue) - Remove "Stadium Quest" title from progress view 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
512
SportsTime/Core/Services/BootstrapService.swift
Normal file
512
SportsTime/Core/Services/BootstrapService.swift
Normal file
@@ -0,0 +1,512 @@
|
||||
//
|
||||
// BootstrapService.swift
|
||||
// SportsTime
|
||||
//
|
||||
// Bootstraps canonical data from bundled JSON files into SwiftData.
|
||||
// Runs once on first launch, then relies on CloudKit for updates.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import SwiftData
|
||||
import CryptoKit
|
||||
|
||||
actor BootstrapService {
|
||||
|
||||
// MARK: - Errors
|
||||
|
||||
enum BootstrapError: Error, LocalizedError {
|
||||
case bundledResourceNotFound(String)
|
||||
case jsonDecodingFailed(String, Error)
|
||||
case saveFailed(Error)
|
||||
|
||||
var errorDescription: String? {
|
||||
switch self {
|
||||
case .bundledResourceNotFound(let resource):
|
||||
return "Bundled resource not found: \(resource)"
|
||||
case .jsonDecodingFailed(let resource, let error):
|
||||
return "Failed to decode \(resource): \(error.localizedDescription)"
|
||||
case .saveFailed(let error):
|
||||
return "Failed to save bootstrap data: \(error.localizedDescription)"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - JSON Models (match bundled JSON structure)
|
||||
|
||||
private struct JSONStadium: Codable {
|
||||
let id: String
|
||||
let name: String
|
||||
let city: String
|
||||
let state: String
|
||||
let latitude: Double
|
||||
let longitude: Double
|
||||
let capacity: Int
|
||||
let sport: String
|
||||
let team_abbrevs: [String]
|
||||
let source: String
|
||||
let year_opened: Int?
|
||||
}
|
||||
|
||||
private struct JSONGame: Codable {
|
||||
let id: String
|
||||
let sport: String
|
||||
let season: String
|
||||
let date: String
|
||||
let time: String?
|
||||
let home_team: String
|
||||
let away_team: String
|
||||
let home_team_abbrev: String
|
||||
let away_team_abbrev: String
|
||||
let venue: String
|
||||
let source: String
|
||||
let is_playoff: Bool
|
||||
let broadcast: String?
|
||||
}
|
||||
|
||||
private struct JSONLeagueStructure: Codable {
|
||||
let id: String
|
||||
let sport: String
|
||||
let type: String // "conference", "division", "league"
|
||||
let name: String
|
||||
let abbreviation: String?
|
||||
let parent_id: String?
|
||||
let display_order: Int
|
||||
}
|
||||
|
||||
private struct JSONTeamAlias: Codable {
|
||||
let id: String
|
||||
let team_canonical_id: String
|
||||
let alias_type: String // "abbreviation", "name", "city"
|
||||
let alias_value: String
|
||||
let valid_from: String?
|
||||
let valid_until: String?
|
||||
}
|
||||
|
||||
// MARK: - Public Methods
|
||||
|
||||
/// Bootstrap canonical data from bundled JSON if not already done.
|
||||
/// This is the main entry point called at app launch.
|
||||
@MainActor
|
||||
func bootstrapIfNeeded(context: ModelContext) async throws {
|
||||
let syncState = SyncState.current(in: context)
|
||||
|
||||
// Skip if already bootstrapped
|
||||
guard !syncState.bootstrapCompleted else {
|
||||
return
|
||||
}
|
||||
|
||||
// Bootstrap in dependency order
|
||||
try await bootstrapStadiums(context: context)
|
||||
try await bootstrapLeagueStructure(context: context)
|
||||
try await bootstrapTeamsAndGames(context: context)
|
||||
try await bootstrapTeamAliases(context: context)
|
||||
|
||||
// Mark bootstrap complete
|
||||
syncState.bootstrapCompleted = true
|
||||
syncState.bundledSchemaVersion = SchemaVersion.current
|
||||
syncState.lastBootstrap = Date()
|
||||
|
||||
do {
|
||||
try context.save()
|
||||
} catch {
|
||||
throw BootstrapError.saveFailed(error)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Bootstrap Steps
|
||||
|
||||
@MainActor
|
||||
private func bootstrapStadiums(context: ModelContext) async throws {
|
||||
guard let url = Bundle.main.url(forResource: "stadiums", withExtension: "json") else {
|
||||
throw BootstrapError.bundledResourceNotFound("stadiums.json")
|
||||
}
|
||||
|
||||
let data: Data
|
||||
let stadiums: [JSONStadium]
|
||||
|
||||
do {
|
||||
data = try Data(contentsOf: url)
|
||||
stadiums = try JSONDecoder().decode([JSONStadium].self, from: data)
|
||||
} catch {
|
||||
throw BootstrapError.jsonDecodingFailed("stadiums.json", error)
|
||||
}
|
||||
|
||||
// Convert and insert
|
||||
for jsonStadium in stadiums {
|
||||
let canonical = CanonicalStadium(
|
||||
canonicalId: jsonStadium.id,
|
||||
schemaVersion: SchemaVersion.current,
|
||||
lastModified: BundledDataTimestamp.stadiums,
|
||||
source: .bundled,
|
||||
name: jsonStadium.name,
|
||||
city: jsonStadium.city,
|
||||
state: jsonStadium.state.isEmpty ? stateFromCity(jsonStadium.city) : jsonStadium.state,
|
||||
latitude: jsonStadium.latitude,
|
||||
longitude: jsonStadium.longitude,
|
||||
capacity: jsonStadium.capacity,
|
||||
yearOpened: jsonStadium.year_opened,
|
||||
sport: jsonStadium.sport
|
||||
)
|
||||
context.insert(canonical)
|
||||
|
||||
// Create stadium alias for the current name (lowercase for matching)
|
||||
let alias = StadiumAlias(
|
||||
aliasName: jsonStadium.name,
|
||||
stadiumCanonicalId: jsonStadium.id,
|
||||
schemaVersion: SchemaVersion.current,
|
||||
lastModified: BundledDataTimestamp.stadiums
|
||||
)
|
||||
alias.stadium = canonical
|
||||
context.insert(alias)
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
private func bootstrapLeagueStructure(context: ModelContext) async throws {
|
||||
// Load league structure if file exists
|
||||
guard let url = Bundle.main.url(forResource: "league_structure", withExtension: "json") else {
|
||||
// League structure is optional for MVP - create basic structure from known sports
|
||||
createDefaultLeagueStructure(context: context)
|
||||
return
|
||||
}
|
||||
|
||||
let data: Data
|
||||
let structures: [JSONLeagueStructure]
|
||||
|
||||
do {
|
||||
data = try Data(contentsOf: url)
|
||||
structures = try JSONDecoder().decode([JSONLeagueStructure].self, from: data)
|
||||
} catch {
|
||||
throw BootstrapError.jsonDecodingFailed("league_structure.json", error)
|
||||
}
|
||||
|
||||
for structure in structures {
|
||||
let structureType: LeagueStructureType
|
||||
switch structure.type.lowercased() {
|
||||
case "conference": structureType = .conference
|
||||
case "division": structureType = .division
|
||||
case "league": structureType = .league
|
||||
default: structureType = .division
|
||||
}
|
||||
|
||||
let model = LeagueStructureModel(
|
||||
id: structure.id,
|
||||
sport: structure.sport,
|
||||
structureType: structureType,
|
||||
name: structure.name,
|
||||
abbreviation: structure.abbreviation,
|
||||
parentId: structure.parent_id,
|
||||
displayOrder: structure.display_order,
|
||||
schemaVersion: SchemaVersion.current,
|
||||
lastModified: BundledDataTimestamp.leagueStructure
|
||||
)
|
||||
context.insert(model)
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
private func bootstrapTeamsAndGames(context: ModelContext) async throws {
|
||||
guard let url = Bundle.main.url(forResource: "games", withExtension: "json") else {
|
||||
throw BootstrapError.bundledResourceNotFound("games.json")
|
||||
}
|
||||
|
||||
let data: Data
|
||||
let games: [JSONGame]
|
||||
|
||||
do {
|
||||
data = try Data(contentsOf: url)
|
||||
games = try JSONDecoder().decode([JSONGame].self, from: data)
|
||||
} catch {
|
||||
throw BootstrapError.jsonDecodingFailed("games.json", error)
|
||||
}
|
||||
|
||||
// Build stadium lookup by venue name for game → stadium matching
|
||||
let stadiumDescriptor = FetchDescriptor<CanonicalStadium>()
|
||||
let canonicalStadiums = (try? context.fetch(stadiumDescriptor)) ?? []
|
||||
var stadiumsByVenue: [String: CanonicalStadium] = [:]
|
||||
for stadium in canonicalStadiums {
|
||||
stadiumsByVenue[stadium.name.lowercased()] = stadium
|
||||
}
|
||||
|
||||
// Extract unique teams from games and create CanonicalTeam entries
|
||||
var teamsCreated: [String: CanonicalTeam] = [:]
|
||||
var seenGameIds = Set<String>()
|
||||
|
||||
for jsonGame in games {
|
||||
let sport = jsonGame.sport.uppercased()
|
||||
|
||||
// Process home team
|
||||
let homeTeamCanonicalId = "team_\(sport.lowercased())_\(jsonGame.home_team_abbrev.lowercased())"
|
||||
if teamsCreated[homeTeamCanonicalId] == nil {
|
||||
let stadiumCanonicalId = findStadiumCanonicalId(
|
||||
venue: jsonGame.venue,
|
||||
sport: sport,
|
||||
stadiumsByVenue: stadiumsByVenue
|
||||
)
|
||||
|
||||
let team = CanonicalTeam(
|
||||
canonicalId: homeTeamCanonicalId,
|
||||
schemaVersion: SchemaVersion.current,
|
||||
lastModified: BundledDataTimestamp.games,
|
||||
source: .bundled,
|
||||
name: extractTeamName(from: jsonGame.home_team),
|
||||
abbreviation: jsonGame.home_team_abbrev,
|
||||
sport: sport,
|
||||
city: extractCity(from: jsonGame.home_team),
|
||||
stadiumCanonicalId: stadiumCanonicalId
|
||||
)
|
||||
context.insert(team)
|
||||
teamsCreated[homeTeamCanonicalId] = team
|
||||
}
|
||||
|
||||
// Process away team
|
||||
let awayTeamCanonicalId = "team_\(sport.lowercased())_\(jsonGame.away_team_abbrev.lowercased())"
|
||||
if teamsCreated[awayTeamCanonicalId] == nil {
|
||||
// Away teams might not have a known stadium yet
|
||||
let team = CanonicalTeam(
|
||||
canonicalId: awayTeamCanonicalId,
|
||||
schemaVersion: SchemaVersion.current,
|
||||
lastModified: BundledDataTimestamp.games,
|
||||
source: .bundled,
|
||||
name: extractTeamName(from: jsonGame.away_team),
|
||||
abbreviation: jsonGame.away_team_abbrev,
|
||||
sport: sport,
|
||||
city: extractCity(from: jsonGame.away_team),
|
||||
stadiumCanonicalId: "unknown" // Will be filled in when they're home team
|
||||
)
|
||||
context.insert(team)
|
||||
teamsCreated[awayTeamCanonicalId] = team
|
||||
}
|
||||
|
||||
// Deduplicate games by ID
|
||||
guard !seenGameIds.contains(jsonGame.id) else { continue }
|
||||
seenGameIds.insert(jsonGame.id)
|
||||
|
||||
// Create game
|
||||
guard let dateTime = parseDateTime(date: jsonGame.date, time: jsonGame.time ?? "7:00p") else {
|
||||
continue
|
||||
}
|
||||
|
||||
let stadiumCanonicalId = findStadiumCanonicalId(
|
||||
venue: jsonGame.venue,
|
||||
sport: sport,
|
||||
stadiumsByVenue: stadiumsByVenue
|
||||
)
|
||||
|
||||
let game = CanonicalGame(
|
||||
canonicalId: jsonGame.id,
|
||||
schemaVersion: SchemaVersion.current,
|
||||
lastModified: BundledDataTimestamp.games,
|
||||
source: .bundled,
|
||||
homeTeamCanonicalId: homeTeamCanonicalId,
|
||||
awayTeamCanonicalId: awayTeamCanonicalId,
|
||||
stadiumCanonicalId: stadiumCanonicalId,
|
||||
dateTime: dateTime,
|
||||
sport: sport,
|
||||
season: jsonGame.season,
|
||||
isPlayoff: jsonGame.is_playoff,
|
||||
broadcastInfo: jsonGame.broadcast
|
||||
)
|
||||
context.insert(game)
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
private func bootstrapTeamAliases(context: ModelContext) async throws {
|
||||
// Team aliases are optional - load if file exists
|
||||
guard let url = Bundle.main.url(forResource: "team_aliases", withExtension: "json") else {
|
||||
return
|
||||
}
|
||||
|
||||
let data: Data
|
||||
let aliases: [JSONTeamAlias]
|
||||
|
||||
do {
|
||||
data = try Data(contentsOf: url)
|
||||
aliases = try JSONDecoder().decode([JSONTeamAlias].self, from: data)
|
||||
} catch {
|
||||
throw BootstrapError.jsonDecodingFailed("team_aliases.json", error)
|
||||
}
|
||||
|
||||
let dateFormatter = ISO8601DateFormatter()
|
||||
|
||||
for jsonAlias in aliases {
|
||||
let aliasType: TeamAliasType
|
||||
switch jsonAlias.alias_type.lowercased() {
|
||||
case "abbreviation": aliasType = .abbreviation
|
||||
case "name": aliasType = .name
|
||||
case "city": aliasType = .city
|
||||
default: aliasType = .name
|
||||
}
|
||||
|
||||
let alias = TeamAlias(
|
||||
id: jsonAlias.id,
|
||||
teamCanonicalId: jsonAlias.team_canonical_id,
|
||||
aliasType: aliasType,
|
||||
aliasValue: jsonAlias.alias_value,
|
||||
validFrom: jsonAlias.valid_from.flatMap { dateFormatter.date(from: $0) },
|
||||
validUntil: jsonAlias.valid_until.flatMap { dateFormatter.date(from: $0) },
|
||||
schemaVersion: SchemaVersion.current,
|
||||
lastModified: BundledDataTimestamp.games
|
||||
)
|
||||
context.insert(alias)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Helpers
|
||||
|
||||
@MainActor
|
||||
private func createDefaultLeagueStructure(context: ModelContext) {
|
||||
// Create minimal league structure for supported sports
|
||||
let timestamp = BundledDataTimestamp.leagueStructure
|
||||
|
||||
// MLB
|
||||
context.insert(LeagueStructureModel(
|
||||
id: "mlb_league",
|
||||
sport: "MLB",
|
||||
structureType: .league,
|
||||
name: "Major League Baseball",
|
||||
abbreviation: "MLB",
|
||||
displayOrder: 0,
|
||||
schemaVersion: SchemaVersion.current,
|
||||
lastModified: timestamp
|
||||
))
|
||||
|
||||
// NBA
|
||||
context.insert(LeagueStructureModel(
|
||||
id: "nba_league",
|
||||
sport: "NBA",
|
||||
structureType: .league,
|
||||
name: "National Basketball Association",
|
||||
abbreviation: "NBA",
|
||||
displayOrder: 0,
|
||||
schemaVersion: SchemaVersion.current,
|
||||
lastModified: timestamp
|
||||
))
|
||||
|
||||
// NHL
|
||||
context.insert(LeagueStructureModel(
|
||||
id: "nhl_league",
|
||||
sport: "NHL",
|
||||
structureType: .league,
|
||||
name: "National Hockey League",
|
||||
abbreviation: "NHL",
|
||||
displayOrder: 0,
|
||||
schemaVersion: SchemaVersion.current,
|
||||
lastModified: timestamp
|
||||
))
|
||||
}
|
||||
|
||||
// Venue name aliases for stadiums that changed names
|
||||
private static let venueAliases: [String: String] = [
|
||||
"daikin park": "minute maid park",
|
||||
"rate field": "guaranteed rate field",
|
||||
"george m. steinbrenner field": "tropicana field",
|
||||
"loandepot park": "loandepot park",
|
||||
]
|
||||
|
||||
nonisolated private func findStadiumCanonicalId(
|
||||
venue: String,
|
||||
sport: String,
|
||||
stadiumsByVenue: [String: CanonicalStadium]
|
||||
) -> String {
|
||||
var venueLower = venue.lowercased()
|
||||
|
||||
// Check for known aliases
|
||||
if let aliasedName = Self.venueAliases[venueLower] {
|
||||
venueLower = aliasedName
|
||||
}
|
||||
|
||||
// Try exact match
|
||||
if let stadium = stadiumsByVenue[venueLower] {
|
||||
return stadium.canonicalId
|
||||
}
|
||||
|
||||
// Try partial match
|
||||
for (name, stadium) in stadiumsByVenue {
|
||||
if name.contains(venueLower) || venueLower.contains(name) {
|
||||
return stadium.canonicalId
|
||||
}
|
||||
}
|
||||
|
||||
// Generate deterministic ID for unknown venues
|
||||
return "venue_unknown_\(venue.lowercased().replacingOccurrences(of: " ", with: "_"))"
|
||||
}
|
||||
|
||||
nonisolated private func parseDateTime(date: String, time: String) -> Date? {
|
||||
let formatter = DateFormatter()
|
||||
formatter.locale = Locale(identifier: "en_US_POSIX")
|
||||
|
||||
// Parse date
|
||||
formatter.dateFormat = "yyyy-MM-dd"
|
||||
guard let dateOnly = formatter.date(from: date) else { return nil }
|
||||
|
||||
// Parse time (e.g., "7:30p", "10:00p", "1:05p")
|
||||
var hour = 12
|
||||
var minute = 0
|
||||
|
||||
let cleanTime = time.lowercased().replacingOccurrences(of: " ", with: "")
|
||||
let isPM = cleanTime.contains("p")
|
||||
let timeWithoutAMPM = cleanTime.replacingOccurrences(of: "p", with: "").replacingOccurrences(of: "a", with: "")
|
||||
|
||||
let components = timeWithoutAMPM.split(separator: ":")
|
||||
if !components.isEmpty, let h = Int(components[0]) {
|
||||
hour = h
|
||||
if isPM && hour != 12 {
|
||||
hour += 12
|
||||
} else if !isPM && hour == 12 {
|
||||
hour = 0
|
||||
}
|
||||
}
|
||||
if components.count > 1, let m = Int(components[1]) {
|
||||
minute = m
|
||||
}
|
||||
|
||||
return Calendar.current.date(bySettingHour: hour, minute: minute, second: 0, of: dateOnly)
|
||||
}
|
||||
|
||||
nonisolated private func extractTeamName(from fullName: String) -> String {
|
||||
// "Boston Celtics" -> "Celtics"
|
||||
let parts = fullName.split(separator: " ")
|
||||
if parts.count > 1 {
|
||||
return parts.dropFirst().joined(separator: " ")
|
||||
}
|
||||
return fullName
|
||||
}
|
||||
|
||||
nonisolated private func extractCity(from fullName: String) -> String {
|
||||
// "Boston Celtics" -> "Boston"
|
||||
// "New York Knicks" -> "New York"
|
||||
let knownCities = [
|
||||
"New York", "Los Angeles", "San Francisco", "San Diego", "San Antonio",
|
||||
"New Orleans", "Oklahoma City", "Salt Lake City", "Kansas City",
|
||||
"St. Louis", "St Louis"
|
||||
]
|
||||
|
||||
for city in knownCities {
|
||||
if fullName.hasPrefix(city) {
|
||||
return city
|
||||
}
|
||||
}
|
||||
|
||||
// Default: first word
|
||||
return String(fullName.split(separator: " ").first ?? Substring(fullName))
|
||||
}
|
||||
|
||||
nonisolated private func stateFromCity(_ city: String) -> String {
|
||||
let cityToState: [String: String] = [
|
||||
"Atlanta": "GA", "Boston": "MA", "Brooklyn": "NY", "Charlotte": "NC",
|
||||
"Chicago": "IL", "Cleveland": "OH", "Dallas": "TX", "Denver": "CO",
|
||||
"Detroit": "MI", "Houston": "TX", "Indianapolis": "IN", "Los Angeles": "CA",
|
||||
"Memphis": "TN", "Miami": "FL", "Milwaukee": "WI", "Minneapolis": "MN",
|
||||
"New Orleans": "LA", "New York": "NY", "Oklahoma City": "OK", "Orlando": "FL",
|
||||
"Philadelphia": "PA", "Phoenix": "AZ", "Portland": "OR", "Sacramento": "CA",
|
||||
"San Antonio": "TX", "San Francisco": "CA", "Seattle": "WA", "Toronto": "ON",
|
||||
"Washington": "DC", "Las Vegas": "NV", "Tampa": "FL", "Pittsburgh": "PA",
|
||||
"Baltimore": "MD", "Cincinnati": "OH", "St. Louis": "MO", "Kansas City": "MO",
|
||||
"Arlington": "TX", "Anaheim": "CA", "Oakland": "CA", "San Diego": "CA",
|
||||
"Tampa Bay": "FL", "St Petersburg": "FL", "Salt Lake City": "UT"
|
||||
]
|
||||
return cityToState[city] ?? ""
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user