tv-anarchy/Sources/TVAnarchy/RootView.swift
Natalie 68e107b03a feat(devices): per-device system-load badge (low/med/high) on Devices list
Sample every HostStatsProvider target's load while the Devices tab is
visible (gated like the existing detailed/detailVisible pollers), keyed
by device id in PlayerController.hostStatsByID. DevicesView renders a
capsule badge from load1/cores: <0.6/core low, <1.0/core med, else high.
Only SSH/mpv devices report stats; others show no badge.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-09 14:07:45 -07:00

184 lines
8.5 KiB
Swift

import SwiftUI
import TVAnarchyCore
struct RootView: View {
enum Section: String, CaseIterable, Identifiable {
case home = "Home"
case player = "Player"
case library = "Library"
case search = "Search"
case downloads = "Downloads"
case metadata = "Metadata"
case devices = "Devices"
case logs = "Logs"
case setup = "Settings"
var id: String { rawValue }
var icon: String {
switch self {
case .home: return "house.fill"
case .player: return "play.rectangle.fill"
case .library: return "square.grid.2x2.fill"
case .search: return "magnifyingglass"
case .downloads: return "arrow.down.circle.fill"
case .metadata: return "wand.and.stars"
case .devices: return "macbook.and.iphone"
case .logs: return "list.bullet.rectangle"
case .setup: return "gearshape.fill"
}
}
}
@State private var controller = PlayerController()
@State private var library: LibraryController
@State private var downloads: DownloadsController
@State private var metadata: MetadataController
@State private var search: SearchController
@State private var playlist: PlaylistController
@State private var offline: OfflineCacheController
@State private var log = LogController()
@State private var selection: Section = .home
@State private var showQueue = false
@State private var versionCopied = false
init() {
let lib = LibraryController()
let dl = DownloadsController()
_library = State(initialValue: lib)
_downloads = State(initialValue: dl)
_metadata = State(initialValue: MetadataController(library: lib))
_search = State(initialValue: SearchController(library: lib, downloads: dl))
_playlist = State(initialValue: PlaylistController(library: lib))
_offline = State(initialValue: OfflineCacheController(library: lib))
}
var body: some View {
NavigationSplitView {
List(selection: $selection) {
ForEach(Section.allCases) { section in
Label(section.rawValue, systemImage: section.icon).tag(section)
// Subnav: Library's category links, auto-collapsed shown only
// while Library is the active nav (selecting another nav hides them).
if section == .library, selection == .library {
ForEach(library.homeCategories, id: \.self) { cat in
categoryLink(cat)
}
}
}
}
.navigationSplitViewColumnWidth(180)
.navigationTitle("TVAnarchy")
.safeAreaInset(edge: .bottom) {
VStack(spacing: 4) {
// Global play-queue button available from every tab.
Button { showQueue.toggle() } label: {
Label(playlist.isEmpty ? "Play Queue" : "Play Queue (\(playlist.count))",
systemImage: "list.bullet.rectangle")
.frame(maxWidth: .infinity, alignment: .leading)
}
.buttonStyle(.bordered).controlSize(.small)
.popover(isPresented: $showQueue, arrowEdge: .leading) {
PlaylistPopover(playlist: playlist, player: controller, library: library)
}
// Build stamp, always visible glance here to confirm you're on
// a fresh build (not a stale copy you forgot to relaunch). Click
// to copy the full version (ver · sha · build time) handy when
// reporting "still broken on the installed build". A real Button
// (reliable hit area + pointer cursor) with in-place "Copied "
// feedback, since `controller.note` only renders on Home/Library.
Button {
copyToClipboard(AppVersion.full)
controller.note("Copied \(AppVersion.full)")
versionCopied = true
} label: {
Text(versionCopied ? "Copied ✓ — paste to share" : AppVersion.short)
.font(.caption2)
.foregroundStyle(versionCopied ? AnyShapeStyle(.green) : AnyShapeStyle(.secondary))
.lineLimit(1).truncationMode(.middle)
.frame(maxWidth: .infinity, alignment: .leading)
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.help("Click to copy: \(AppVersion.full)")
.task(id: versionCopied) {
guard versionCopied else { return }
try? await Task.sleep(for: .seconds(1.6))
versionCopied = false
}
}
.padding(.horizontal, 12).padding(.vertical, 6)
}
} detail: {
switch selection {
case .home:
HomeView(library: library, player: controller, playlist: playlist) { selection = .library }
case .player:
PlayerView(controller: controller)
case .library:
LibraryView(library: library, player: controller, playlist: playlist)
case .search:
SearchView(search: search, player: controller, library: library) { selection = .library }
case .downloads:
DownloadsView(downloads: downloads, player: controller)
case .metadata:
MetadataView(metadata: metadata)
case .devices:
DevicesView(controller: controller, offline: offline)
case .logs:
LogView(log: log)
case .setup:
SetupView(controller: controller, library: library)
}
}
.frame(minWidth: 780, minHeight: 520)
.overlay(alignment: .top) {
if let banner = NotificationsService.shared.lastBanner {
Label(banner, systemImage: "bell.fill")
.font(.callout)
.padding(.horizontal, 14).padding(.vertical, 10)
.background(.thinMaterial, in: Capsule())
.padding(.top, 12)
.transition(.move(edge: .top).combined(with: .opacity))
.onTapGesture { NotificationsService.shared.clearBanner() }
.task(id: banner) {
try? await Task.sleep(for: .seconds(5))
NotificationsService.shared.clearBanner()
}
}
}
.animation(.default, value: NotificationsService.shared.lastBanner)
.task {
// A finished download is when new media lands on black index JUST
// its folder(s) incrementally (cheap), then refresh from the index.
downloads.onDownloadsCompleted = { folders in library.rebuildIndex(folders: folders) }
NotificationsService.shared.requestAuthorizationIfNeeded()
applyVisibility()
controller.start(); downloads.start()
}
.onChange(of: selection) { applyVisibility() }
}
/// An indented Library category link in the sidebar subnav jumps to the
/// Library tab filtered to that category, and marks the active one.
@ViewBuilder private func categoryLink(_ cat: String) -> some View {
Button {
library.selectedShow = nil
library.selectedCategory = cat
selection = .library
} label: {
Label(cat.capitalized, systemImage: "chevron.forward")
.font(.callout)
.foregroundStyle(library.selectedCategory == cat ? AnyShapeStyle(.tint) : AnyShapeStyle(.secondary))
.padding(.leading, 16)
}
.buttonStyle(.plain)
}
/// Gate the expensive pollers to the tab that needs them: fast player
/// transport + stats only on Player; fast transfer polling only on Downloads.
/// Both still poll slowly off-tab (host dots, sleep timer, last-known transfers).
private func applyVisibility() {
controller.detailed = (selection == .player)
controller.devicesVisible = (selection == .devices)
downloads.detailVisible = (selection == .downloads)
}
}