Files
MLBApp/mlbTVOS/ViewModels/FeedViewModel.swift
Trey t b5daddefd3 Add UI redesign: design system, dashboard density, game intelligence, feed tab, league leaders
Phase 1 - Design System: DesignSystem.swift (typography, colors, spacing
constants) and DataPanel.swift (reusable panel container with 3 densities
and optional team accent bar).

Phase 2 - Dashboard Density: LiveSituationBar (compact strip of all live
games with scores/innings/outs), MiniLinescoreView (R-H-E footer for game
cards), DiamondView (visual baseball diamond with runners and count).
Dashboard shows live situation bar when games are active. Game cards now
display mini linescore for live/final games.

Phase 3 - Game Center Intelligence: WinProbabilityChartView (full-game
line chart using Swift Charts with area fills), PitchArsenalView (pitch
type distribution with velocity bars). GameCenterViewModel now stores
full WP history array instead of just latest values.

Phase 4 - Feed Tab: MLBWebDataService (fetches league leaders from Stats
API, news headlines, transactions), FeedViewModel, FeedView with
reverse-chronological feed items. FeedItemView with colored edge bars
by category. Added 5th "Feed" tab to both tvOS and iOS.

Phase 5 - Intel Tab: LeaderboardView (top-5 stat cards with headshots),
integrated into LeagueCenterView. Renamed tabs: Games->Today,
League->Intel. LeagueCenterViewModel now fetches league leaders.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-12 13:12:09 -05:00

96 lines
2.4 KiB
Swift

import Foundation
import Observation
import OSLog
private let feedLogger = Logger(subsystem: "com.treyt.mlbTVOS", category: "Feed")
private func logFeed(_ message: String) {
feedLogger.debug("\(message, privacy: .public)")
print("[Feed] \(message)")
}
enum FeedItemType: Sendable {
case news
case transaction
case scoring
}
struct FeedItem: Identifiable, Sendable {
let id: String
let type: FeedItemType
let title: String
let subtitle: String
let teamCode: String?
let timestamp: Date
}
@Observable
@MainActor
final class FeedViewModel {
var items: [FeedItem] = []
var isLoading = false
@ObservationIgnored
private var refreshTask: Task<Void, Never>?
private let webService = MLBWebDataService()
func loadFeed() async {
isLoading = true
logFeed("loadFeed start")
async let newsTask = webService.fetchNewsHeadlines()
async let transactionsTask = webService.fetchTransactions()
let news = await newsTask
let transactions = await transactionsTask
var allItems: [FeedItem] = []
// News
for headline in news {
allItems.append(FeedItem(
id: "news-\(headline.id)",
type: .news,
title: headline.title,
subtitle: headline.summary,
teamCode: nil,
timestamp: headline.timestamp
))
}
// Transactions
for tx in transactions {
allItems.append(FeedItem(
id: "tx-\(tx.id)",
type: .transaction,
title: tx.description,
subtitle: tx.type,
teamCode: tx.teamCode.isEmpty ? nil : tx.teamCode,
timestamp: tx.date
))
}
// Sort reverse chronological
items = allItems.sorted { $0.timestamp > $1.timestamp }
isLoading = false
logFeed("loadFeed complete items=\(items.count)")
}
func startAutoRefresh() {
stopAutoRefresh()
refreshTask = Task { [weak self] in
while !Task.isCancelled {
try? await Task.sleep(for: .seconds(300))
guard !Task.isCancelled, let self else { break }
await self.loadFeed()
}
}
}
func stopAutoRefresh() {
refreshTask?.cancel()
refreshTask = nil
}
}