Files
tvbox-Swift/tvbox/tvboxApp.swift
T
2026-03-01 20:34:43 +08:00

82 lines
2.5 KiB
Swift

import SwiftUI
import SwiftData
@main
struct tvboxApp: App {
@StateObject private var appState = AppState()
var sharedModelContainer: ModelContainer = {
let schema = Schema([
VodCollect.self,
VodRecord.self,
CacheItem.self
])
let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false)
do {
return try ModelContainer(for: schema, configurations: [modelConfiguration])
} catch {
fatalError("Could not create ModelContainer: \(error)")
}
}()
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(appState)
}
.modelContainer(sharedModelContainer)
#if os(macOS)
.defaultSize(width: 1200, height: 800)
#endif
}
}
@MainActor
class AppState: ObservableObject {
@Published var apiConfig = ApiConfig.shared
@Published var isConfigLoaded = false
@Published var currentSourceKey: String = ""
#if os(macOS)
@Published var splitViewVisibility: NavigationSplitViewVisibility = .all
private var splitViewVisibilityBeforePlayerFullScreen: NavigationSplitViewVisibility?
#endif
func loadConfig(url: String) async {
await loadConfig(vodUrl: url, liveUrl: nil)
}
func loadConfig(vodUrl: String, liveUrl: String?) async {
let trimmedVod = vodUrl.trimmingCharacters(in: .whitespacesAndNewlines)
let trimmedLive = (liveUrl ?? "").trimmingCharacters(in: .whitespacesAndNewlines)
guard !trimmedVod.isEmpty else { return }
let resolvedLive = trimmedLive.isEmpty ? trimmedVod : trimmedLive
do {
try await ApiConfig.shared.loadConfigs(vodApiUrl: trimmedVod, liveApiUrl: resolvedLive)
applyLoadedConfigState()
} catch {
print("Failed to load config: \(error)")
}
}
func applyLoadedConfigState() {
isConfigLoaded = true
currentSourceKey = ApiConfig.shared.homeSourceBean?.key ?? ""
}
#if os(macOS)
func enterPlayerFullScreen() {
if splitViewVisibilityBeforePlayerFullScreen == nil {
splitViewVisibilityBeforePlayerFullScreen = splitViewVisibility
}
splitViewVisibility = .detailOnly
}
func exitPlayerFullScreen() {
guard let previous = splitViewVisibilityBeforePlayerFullScreen else { return }
splitViewVisibility = previous
splitViewVisibilityBeforePlayerFullScreen = nil
}
#endif
}