Add game center, per-model shuffle, audio focus fixes, README, tests
- README.md with build/architecture overview - Game Center screen with at-bat timeline, pitch sequence, spray chart, and strike zone component views - VideoShuffle service: per-model bucketed random selection with no-back-to-back guarantee; replaces flat shuffle-bag approach - Refresh JWT token for authenticated NSFW feed; add josie-hamming-2 and dani-speegle-2 to the user list - MultiStreamView audio focus: remove redundant isMuted writes during startStream and playNextWerkoutClip so audio stops ducking during clip transitions; gate AVAudioSession.setCategory(.playback) behind a one-shot flag - GamesViewModel.attachPlayer: skip mute recalculation when the same player is re-attached (prevents toggle flicker on item replace) - mlbTVOSTests target wired through project.yml with GENERATE_INFOPLIST_FILE; VideoShuffleTests covers groupByModel, pickRandomFromBuckets, real-distribution no-back-to-back invariant, and uniform model distribution over 6000 picks Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,24 +1,41 @@
|
||||
import Foundation
|
||||
import Observation
|
||||
import OSLog
|
||||
|
||||
private let gameCenterLogger = Logger(subsystem: "com.treyt.mlbTVOS", category: "GameCenter")
|
||||
|
||||
private func logGameCenter(_ message: String) {
|
||||
gameCenterLogger.debug("\(message, privacy: .public)")
|
||||
print("[GameCenter] \(message)")
|
||||
}
|
||||
|
||||
@Observable
|
||||
@MainActor
|
||||
final class GameCenterViewModel {
|
||||
var feed: LiveGameFeed?
|
||||
var highlights: [Highlight] = []
|
||||
var winProbabilityHome: Double?
|
||||
var winProbabilityAway: Double?
|
||||
var isLoading = false
|
||||
var errorMessage: String?
|
||||
var lastUpdated: Date?
|
||||
|
||||
private let statsAPI = MLBStatsAPI()
|
||||
@ObservationIgnored
|
||||
private var lastHighlightsFetch: Date?
|
||||
|
||||
func watch(game: Game) async {
|
||||
guard let gamePk = game.gamePk else {
|
||||
logGameCenter("watch: no gamePk for game id=\(game.id)")
|
||||
errorMessage = "No live game feed is available for this matchup."
|
||||
return
|
||||
}
|
||||
logGameCenter("watch: starting for gamePk=\(gamePk)")
|
||||
|
||||
while !Task.isCancelled {
|
||||
await refresh(gamePk: gamePk)
|
||||
await refreshHighlightsIfNeeded(gamePk: gamePk, gameDate: game.gameDate)
|
||||
await refreshWinProbability(gamePk: gamePk)
|
||||
|
||||
let liveState = feed?.gameData.status?.abstractGameState == "Live"
|
||||
if !liveState {
|
||||
@@ -34,12 +51,44 @@ final class GameCenterViewModel {
|
||||
errorMessage = nil
|
||||
|
||||
do {
|
||||
logGameCenter("refresh: fetching feed for gamePk=\(gamePk)")
|
||||
feed = try await statsAPI.fetchGameFeed(gamePk: gamePk)
|
||||
logGameCenter("refresh: success playEvents=\(feed?.currentPlay?.playEvents?.count ?? 0) allPlays=\(feed?.liveData.plays.allPlays.count ?? 0)")
|
||||
lastUpdated = Date()
|
||||
} catch {
|
||||
logGameCenter("refresh: FAILED gamePk=\(gamePk) error=\(error)")
|
||||
errorMessage = "Failed to load game center."
|
||||
}
|
||||
|
||||
isLoading = false
|
||||
}
|
||||
|
||||
private func refreshHighlightsIfNeeded(gamePk: String, gameDate: String) async {
|
||||
// Only fetch highlights every 60 seconds
|
||||
if let last = lastHighlightsFetch, Date().timeIntervalSince(last) < 60 {
|
||||
return
|
||||
}
|
||||
|
||||
let serverAPI = MLBServerAPI()
|
||||
do {
|
||||
highlights = try await serverAPI.fetchHighlights(gamePk: gamePk, gameDate: gameDate)
|
||||
lastHighlightsFetch = Date()
|
||||
} catch {
|
||||
// Highlights are supplementary — don't surface errors
|
||||
}
|
||||
}
|
||||
|
||||
private func refreshWinProbability(gamePk: String) async {
|
||||
do {
|
||||
let entries = try await statsAPI.fetchWinProbability(gamePk: gamePk)
|
||||
if let latest = entries.last,
|
||||
let home = latest.homeTeamWinProbability,
|
||||
let away = latest.awayTeamWinProbability {
|
||||
winProbabilityHome = home
|
||||
winProbabilityAway = away
|
||||
}
|
||||
} catch {
|
||||
// Win probability is supplementary — don't surface errors
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user