Files
RedLintu16andClaude Opus 5.5 64ec524ba0 CrossSound for Apple platforms
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
2026-10-14 13:28:34 -07:00

109 lines
3.3 KiB
Swift

// LogScreen — CrossSound's own log, on the watch: the CSLog lines the phone's
// ConsoleView shows, readable on the wrist when Xcode and Console.app can't
// reach the watch. Newest first, filterable by tag ([audio], [play] …),
// with where the audio is going pinned on top.
import CrossSoundKit
import RedWhiteUI
import SwiftUI
struct LogScreen: View {
@Environment(WatchModel.self) private var model
/// One line, with an id that survives trimming (lines repeat verbatim) —
/// ConsoleView's LogRow.
private struct Row: Identifiable {
let id: Int
let text: String
}
@State private var rows: [Row] = []
@State private var nextId = 0
@State private var tag: String?
/// The tags worth a button; everything else is under All.
private static let tags = ["audio", "play", "remote", "watch", "auth"]
/// A watch's worth of scrollback (CSLog itself keeps 2000).
private static let keep = 500
var body: some View {
ScrollView {
LazyVStack(alignment: .leading, spacing: 6) {
Text(routeSummary)
.rw(.labelMedium)
ScrollView(.horizontal, showsIndicators: false) {
HStack(spacing: 4) {
chip("All", nil)
ForEach(Self.tags, id: \.self) { chip($0, $0) }
}
}
let shown = rows.reversed().filter { row in
tag.map { row.text.contains("[\($0)]") } ?? true
}
if shown.isEmpty {
Text("Nothing logged yet").rw(.labelMedium).padding(.top, 8)
}
ForEach(shown) { row in
Text(row.text)
.font(.system(size: 11, design: .monospaced))
.frame(maxWidth: .infinity, alignment: .leading)
}
}
.padding(.horizontal, 4)
}
.navigationTitle("Log")
.task { await pump() }
}
/// "Audio: Speaker", and when the speaker is the fallback, why long-form
/// said no.
private var routeSummary: String {
guard let route = model.route else { return "Audio: nothing played yet" }
guard model.routeIsFallback else { return "Audio: \(route) (long-form)" }
let why = WatchAudioRoute.longFormRefusal.map { " — long-form: \($0)" } ?? ""
return "Audio: \(route), only while open\(why)"
}
private func chip(_ title: String, _ value: String?) -> some View {
Button {
tag = value
} label: {
Text(title)
.rw(.labelSmall)
.padding(.horizontal, 6)
.padding(.vertical, 4)
.overlay {
if tag == value { Rectangle().strokeBorder(lineWidth: 2) }
}
.contentShape(Rectangle())
}
}
/// ConsoleView's pump: subscribe BEFORE the snapshot so nothing falls in
/// the gap, then drop the snapshot lines the stream replays.
private func pump() async {
let stream = CSLog.shared.tail()
let snapshot = CSLog.shared.lines
rows = []
snapshot.suffix(Self.keep).forEach(append)
let snapshotSet = Set(snapshot)
var replaysLeft = snapshot.count
for await line in stream {
if replaysLeft > 0 {
replaysLeft -= 1
if snapshotSet.contains(line) { continue }
replaysLeft = 0
}
append(line)
}
}
private func append(_ line: String) {
rows.append(Row(id: nextId, text: line))
nextId += 1
if rows.count > Self.keep { rows.removeFirst(rows.count - Self.keep) }
}
}