Files
ddys-client/DDYSClient/Views/Navigation/AppNavigation.swift
2026-02-27 13:50:32 +08:00

136 lines
3.5 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import SwiftUI
enum AppTab: String, Identifiable {
case home
case movie
case series
case variety
case anime
case search
case settings
var id: String { rawValue }
var title: String {
switch self {
case .home: return "首页"
case .movie: return "电影"
case .series: return "电视剧"
case .variety: return "综艺"
case .anime: return "动漫"
case .search: return "搜索"
case .settings: return "设置"
}
}
var icon: String {
switch self {
case .home: return "house"
case .movie: return "film"
case .series: return "tv"
case .variety: return "theatermasks"
case .anime: return "sparkles"
case .search: return "magnifyingglass"
case .settings: return "gearshape"
}
}
var category: ContentCategory? {
switch self {
case .movie: return .movie
case .series: return .series
case .variety: return .variety
case .anime: return .anime
default: return nil
}
}
static var visibleTabs: [AppTab] {
#if os(tvOS)
return [.home, .movie, .series, .variety, .anime, .search, .settings]
#else
return [.home, .movie, .series, .variety, .anime, .settings]
#endif
}
}
struct AppNavigation: View {
@State private var selectedTab: AppTab = .home
// tab ViewModel tab
@State private var homeVM = HomeViewModel()
@State private var movieVM = BrowseViewModel()
@State private var seriesVM = BrowseViewModel()
@State private var varietyVM = BrowseViewModel()
@State private var animeVM = BrowseViewModel()
var body: some View {
#if os(macOS)
sidebarLayout
#elseif os(tvOS)
tabLayout
#elseif os(visionOS)
sidebarLayout
#else
if UIDevice.current.userInterfaceIdiom == .pad {
sidebarLayout
} else {
tabLayout
}
#endif
}
private var tabLayout: some View {
TabView(selection: $selectedTab) {
ForEach(AppTab.visibleTabs) { tab in
NavigationStack {
tabContent(for: tab)
}
.tabItem {
Label(tab.title, systemImage: tab.icon)
}
.tag(tab)
}
}
}
#if !os(tvOS)
private var sidebarLayout: some View {
NavigationSplitView {
List(AppTab.visibleTabs, selection: $selectedTab) { tab in
Label(tab.title, systemImage: tab.icon)
.tag(tab)
}
.navigationTitle("低端影视")
} detail: {
NavigationStack {
tabContent(for: selectedTab)
}
}
}
#endif
@ViewBuilder
private func tabContent(for tab: AppTab) -> some View {
switch tab {
case .home:
HomeView(viewModel: homeVM)
case .movie:
BrowseView(category: .movie, viewModel: movieVM)
case .series:
BrowseView(category: .series, viewModel: seriesVM)
case .variety:
BrowseView(category: .variety, viewModel: varietyVM)
case .anime:
BrowseView(category: .anime, viewModel: animeVM)
case .search:
#if os(tvOS)
TVSearchView()
#else
EmptyView()
#endif
case .settings:
SettingsView()
}
}
}