import AppKit import CockpitKern import SwiftUI /// Der Reiter Features (winui/Seiten/FeaturesSeite.cs): was der Laeufer an Schaltern gerade spielt und was /// jedes Feature bringt. Liest GET /api/features alle 10 s, solange der Reiter sichtbar ist, und rechnet nichts /// nach - die Texte kommen aus `FeaturesText` (CockpitKern/Features.swift). Nur Anzeige. /// /// `holen` ist der GET der Verbindung (`Verbindung.holen`); die Ansicht kennt den Server sonst nicht. struct FeaturesAnsicht: View { var holen: @MainActor (String) async -> Data? @State private var daten: Features? @State private var versucht = false private static let takt = 10.0 private static let ersterTakt = 2.0 var body: some View { ScrollView { VStack(alignment: .leading, spacing: 12) { if let d = daten, d.hatGruppen { FeaturesInhalt(d: d) } else { let l = versucht ? FeaturesText.leerSeite(daten) : FeaturesText.LeerSeite(titel: "Keine Features", text: "Verbindet mit dem Cockpit-Server …") Leer(titel: l.titel, text: l.text) } } .padding(12) } .task { // Endet mit dem Reiter: .task wird beim Verschwinden abgebrochen, dann bricht auch das Warten ab. while !Task.isCancelled { if let d = await holen("/api/features"), let f = Features.lesen(d) { daten = f } versucht = true do { try await Task.sleep(for: .seconds(daten == nil ? Self.ersterTakt : Self.takt)) } catch { break } } } } } private struct FeaturesInhalt: View { var d: Features var body: some View { FeaturesKopfKarte(d: d) if d.wirkung != nil { FeaturesWirkungAbschnitt(w: d.wirkung!) } if let s = d.sitzungen, !s.liste.isEmpty { FeaturesSitzungenAbschnitt(s: s) } FeaturesDetails(d: d) if let f = d.fehler { Text("Server: " + f).font(.caption).foregroundStyle(.red) .fixedSize(horizontal: false, vertical: true) .padding(8).frame(maxWidth: .infinity, alignment: .leading) .background(.red.opacity(0.10), in: RoundedRectangle(cornerRadius: 8)) } Text("Stand \(Zeit.uhr(d.stand)) · nur Anzeige, hier lässt sich nichts verstellen") .font(.caption2).foregroundStyle(.tertiary).frame(maxWidth: .infinity) } } // MARK: Kopf private struct FeaturesKopfKarte: View { var d: Features var body: some View { let k = FeaturesText.kopf(d) VStack(alignment: .leading, spacing: 10) { HStack(spacing: 10) { Circle().fill(k.laeuft ? Color.green : Color.secondary).frame(width: 10, height: 10) VStack(alignment: .leading, spacing: 1) { Text(k.titel).font(.title3.weight(.semibold)).fixedSize(horizontal: false, vertical: true) Text(k.unter).font(.caption).foregroundStyle(.secondary).fixedSize(horizontal: false, vertical: true) } } FlussLayout(abstand: 6) { ForEach(Array(k.abzeichen.enumerated()), id: \.offset) { _, m in Abzeichen(text: m.text, farbe: StatistikFarbe.ton(m.ton)) } } if !k.geaendert.isEmpty { VStack(alignment: .leading, spacing: 3) { Text("Geändert seit dem Start – wirkt erst nach einem Neustart des Läufers:") .font(.caption.weight(.semibold)).foregroundStyle(.orange) .fixedSize(horizontal: false, vertical: true) ForEach(Array(k.geaendert.enumerated()), id: \.offset) { _, z in Text(z).font(.caption2.monospaced()).foregroundStyle(.secondary).fixedSize(horizontal: false, vertical: true) } } .padding(8).frame(maxWidth: .infinity, alignment: .leading) .background(.orange.opacity(0.10), in: RoundedRectangle(cornerRadius: 8)) } } .padding(14) .frame(maxWidth: .infinity, alignment: .leading) .background(.white.opacity(0.06), in: RoundedRectangle(cornerRadius: 14)) } } // MARK: Wirkung private struct FeaturesWirkungAbschnitt: View { var w: FeaturesWirkung var body: some View { VStack(alignment: .leading, spacing: 6) { Abschnitt(titel: "Was die Features bringen", hinweis: FeaturesText.wirkungKopf(w.sitzung)) { EmptyView() } VStack(spacing: 0) { if let f = w.fehler { Text("Konnte nicht rechnen: " + f).font(.caption).foregroundStyle(.red) .fixedSize(horizontal: false, vertical: true).padding(14) } ForEach(Array(w.features.enumerated()), id: \.element.id) { i, x in if i > 0 || w.fehler != nil { Divider() } FeatureKarte(x: x) } if let leer = FeaturesText.wirkungLeer(w) { Text(leer).font(.caption).foregroundStyle(.secondary).padding(14) } } .frame(maxWidth: .infinity, alignment: .leading) .background(.white.opacity(0.06), in: RoundedRectangle(cornerRadius: 14)) Fussnote(FeaturesText.wirkungFuss(w.regeln)).padding(.horizontal, 2) } } } private struct FeatureKarte: View { var x: FeaturesWirkFeature @State private var offen: Bool init(x: FeaturesWirkFeature) { self.x = x _offen = State(initialValue: FeaturesText.standardOffen(x)) } var body: some View { let zahl = FeaturesText.wirkungZahl(x) VStack(alignment: .leading, spacing: 8) { Button { withAnimation(.snappy) { offen.toggle() } } label: { HStack(alignment: .top, spacing: 10) { Circle().fill(StatistikFarbe.ton(x.ton)).frame(width: 10, height: 10).padding(.top, 5) VStack(alignment: .leading, spacing: 2) { HStack(spacing: 8) { Text(x.titel).font(.callout.weight(.semibold)).fixedSize(horizontal: false, vertical: true) Abzeichen(text: x.urteil, farbe: StatistikFarbe.ton(x.ton)) } Text(x.satz).font(.caption) .foregroundStyle(x.ton == "rot" ? Color.red : Color.secondary) .fixedSize(horizontal: false, vertical: true) } Spacer(minLength: 4) VStack(alignment: .trailing, spacing: 0) { Text(zahl.wert).font(.system(.title3, design: .rounded).weight(.semibold)).monospacedDigit() .foregroundStyle(zahl.ton.map(StatistikFarbe.ton) ?? .primary) Text(zahl.unter).font(.caption2).foregroundStyle(.tertiary) } Image(systemName: offen ? "chevron.up" : "chevron.down").font(.caption2).foregroundStyle(.tertiary) .frame(height: 20) } .contentShape(Rectangle()) } .buttonStyle(.plain) // Ein Schalter des Features anders als in der vorigen Sitzung: immer sichtbar ForEach(Array(x.wechsel.enumerated()), id: \.offset) { _, wc in Text(FeaturesText.neuInSitzung(wc)).font(.caption2).foregroundStyle(.orange) .fixedSize(horizontal: false, vertical: true) .padding(6).frame(maxWidth: .infinity, alignment: .leading) .background(.orange.opacity(0.08), in: RoundedRectangle(cornerRadius: 8)) .padding(.leading, 20) } if offen { innen.padding(.leading, 20).padding(.bottom, 4) } } .padding(.horizontal, 14).padding(.vertical, 10) } private var innen: some View { VStack(alignment: .leading, spacing: 10) { if !x.erklaerung.isEmpty { Text(x.erklaerung).font(.caption).foregroundStyle(.secondary).fixedSize(horizontal: false, vertical: true) } KachelRaster(kacheln: FeaturesText.wirkungKacheln(x)) Fussnote(FeaturesText.beteiligt(x)) ForEach([("Größte Verluste", x.verlierer), ("Größte Gewinne", x.gewinner)], id: \.0) { titel, haende in if !haende.isEmpty { VStack(alignment: .leading, spacing: 4) { Text(titel).font(.caption2.weight(.semibold)).foregroundStyle(.secondary) VStack(spacing: 0) { ForEach(Array(haende.enumerated()), id: \.element.hand) { i, h in if i > 0 { Divider() } FeaturesHandZeile(h: h, feature: x.id) } } .background(.white.opacity(0.04), in: RoundedRectangle(cornerRadius: 8)) } } } if !x.zustand.isEmpty { Text("Schalter: " + x.zustand.sorted { $0.key < $1.key } .map { "\($0.key)=\(FeaturesText.wert($0.value))" }.joined(separator: " ")) .font(.caption2.monospaced()).foregroundStyle(.tertiary).fixedSize(horizontal: false, vertical: true) } } } } /// Eine Hand: Kennung (Tipp kopiert sie und klappt die Gruende auf), Ergebnis, Uhrzeit; darunter der Ablauf. private struct FeaturesHandZeile: View { var h: FeaturesHand var feature: String @State private var offen = false var body: some View { let t = FeaturesText.handZeile(h, feature: feature) Button { NSPasteboard.general.clearContents() NSPasteboard.general.setString(t.kennung, forType: .string) withAnimation(.snappy) { offen.toggle() } } label: { VStack(alignment: .leading, spacing: 4) { HStack(spacing: 6) { Image(systemName: "doc.on.doc").font(.system(size: 10)).foregroundStyle(.tint) Text(t.kennung).font(.caption.monospaced()).foregroundStyle(.blue).lineLimit(1).truncationMode(.middle) Spacer(minLength: 4) Text(t.ergebnis).font(.caption.weight(.semibold)).monospacedDigit() .foregroundStyle(t.ton.map(StatistikFarbe.ton) ?? .primary) } Text(t.mitte).font(.caption2).foregroundStyle(.tertiary).fixedSize(horizontal: false, vertical: true) kette(t) if offen { ForEach(Array(t.gruende.enumerated()), id: \.offset) { _, g in Text(g.text).font(.caption2).foregroundStyle(g.eigen ? Color.teal : Color.secondary.opacity(0.7)) .fixedSize(horizontal: false, vertical: true) } } } .frame(maxWidth: .infinity, alignment: .leading) .padding(.horizontal, 10).padding(.vertical, 8) .contentShape(Rectangle()) } .buttonStyle(.plain) .help("Tippen: Hand-Kennung kopieren und die Gründe zeigen · \(t.hilfe)") } private func kette(_ t: FeaturesText.HandText) -> Text { var r = Text("") for (i, z) in t.kette.enumerated() { if i > 0 { r = r + Text(" › ").foregroundStyle(.tertiary) } r = r + Text(z.text).fontWeight(z.eigen ? .semibold : .regular).foregroundStyle(z.eigen ? Color.teal : Color.secondary) if let w = z.wer { r = r + Text(" (\(w))").font(.caption2).foregroundStyle(.tertiary) } } return r.font(.caption) } } // MARK: Sitzung gegen Sitzung private struct FeaturesSitzungenAbschnitt: View { var s: FeaturesSitzungen @State private var frueher = false var body: some View { VStack(alignment: .leading, spacing: 6) { Abschnitt(titel: "Sitzung gegen Sitzung", hinweis: "AIVAT bb/100") { EmptyView() } VStack(alignment: .leading, spacing: 8) { if s.liste.count >= 2 { ViewThatFits(in: .horizontal) { HStack(alignment: .top, spacing: 8) { kachel("Vorige", s.liste[1]); kachel("Letzte", s.liste[0]) } VStack(spacing: 8) { kachel("Vorige", s.liste[1]); kachel("Letzte", s.liste[0]) } } } else { kachel("Letzte", s.liste[0]) } if let v = s.vergleich { vergleich(v) } if s.liste.count > 2 { Button { withAnimation(.snappy) { frueher.toggle() } } label: { HStack { Text("Frühere Sitzungen").font(.caption.weight(.semibold)) Spacer() Text("\(s.liste.count - 2)").font(.caption).foregroundStyle(.secondary) Image(systemName: frueher ? "chevron.up" : "chevron.down").font(.caption2).foregroundStyle(.tertiary) } .contentShape(Rectangle()) } .buttonStyle(.plain) if frueher { ForEach(Array(s.liste.dropFirst(2).enumerated()), id: \.offset) { i, x in if i > 0 { Divider() } zeile(x) } } } Fussnote(FeaturesText.sitzungFuss(s)) } .padding(12).frame(maxWidth: .infinity, alignment: .leading) .background(.white.opacity(0.06), in: RoundedRectangle(cornerRadius: 14)) } } private func kachel(_ titel: String, _ x: FeaturesSitzung) -> some View { let k = FeaturesText.sitzungKachel(titel, x) return VStack(alignment: .leading, spacing: 2) { Text(k.kopf).font(.caption2).foregroundStyle(.tertiary).fixedSize(horizontal: false, vertical: true) Text(k.gross).font(.system(.title3, design: .rounded).weight(.semibold)).monospacedDigit() .foregroundStyle(k.ton.map(StatistikFarbe.ton) ?? .primary) Text(k.mitte).font(.caption2).foregroundStyle(.secondary).fixedSize(horizontal: false, vertical: true) Text(k.unter).font(.caption2).foregroundStyle(.tertiary).fixedSize(horizontal: false, vertical: true) } .frame(maxWidth: .infinity, alignment: .leading) .padding(10) .background(.quaternary.opacity(0.5), in: RoundedRectangle(cornerRadius: 10)) } private func vergleich(_ v: FeaturesVergleich) -> some View { let ton = StatistikFarbe.ton(FeaturesText.urteilTon(v.urteil)) return VStack(alignment: .leading, spacing: 6) { FlussLayout(abstand: 8) { Text("Letzte − vorige").font(.caption).foregroundStyle(.secondary) Text(FeaturesText.vergleichZeile(v)).font(.callout.weight(.semibold)).monospacedDigit() Abzeichen(text: v.urteil, farbe: ton) } Text(FeaturesText.urteilSatz(v.urteil)).font(.caption).foregroundStyle(ton).fixedSize(horizontal: false, vertical: true) if v.anders.isEmpty { Fussnote("Gleiche Schalter – ein Exe-Tausch steht nicht in der Schalter-Marke.") } else { VStack(alignment: .leading, spacing: 2) { Text("Diese Schalter waren anders als in der vorigen Sitzung:").font(.caption2.weight(.semibold)).foregroundStyle(.secondary) ForEach(Array(v.anders.enumerated()), id: \.offset) { _, a in (Text(a.name + " ").fontWeight(.semibold) + Text(a.vorher ?? FeaturesText.nichtGesetzt).foregroundStyle(.secondary) + Text(" → ").foregroundStyle(.tertiary) + Text(a.nachher ?? FeaturesText.nichtGesetzt)) .font(.caption2).fixedSize(horizontal: false, vertical: true) .help(FeaturesText.wechsel(a)) } } } } } private func zeile(_ x: FeaturesSitzung) -> some View { let z = FeaturesText.sitzungZeile(x) return HStack(alignment: .firstTextBaseline, spacing: 8) { VStack(alignment: .leading, spacing: 1) { Text(z.kopf).font(.caption).fixedSize(horizontal: false, vertical: true) Text(z.unter).font(.caption2).foregroundStyle(.tertiary).fixedSize(horizontal: false, vertical: true) } Spacer(minLength: 4) Text(z.wert).font(.caption.weight(.semibold)).monospacedDigit().foregroundStyle(z.ton.map(StatistikFarbe.ton) ?? .primary) } .padding(.vertical, 2) } } // MARK: Details (zugeklappt) private struct FeaturesDetails: View { var d: Features var body: some View { VStack(alignment: .leading, spacing: 6) { Abschnitt(titel: "Details", hinweis: "Schalter und Programme") { EmptyView() } VStack(spacing: 8) { ForEach(d.gruppen) { g in FeaturesFalte(titel: g.titel, wert: FeaturesText.gruppeWert(g), hinweis: FeaturesText.gruppeHinweis(g)) { VStack(spacing: 0) { ForEach(Array(g.schalter.enumerated()), id: \.offset) { i, s in if i > 0 { Divider() } SchalterZeile(s: s) } } } } FeaturesFalte(titel: "Programme im Einsatz", wert: FeaturesText.exesWert(d.exes), hinweis: "Kennung (md5) und Baudatum jeder Exe, die gerade zählt.") { VStack(spacing: 0) { ForEach(Array(d.exes.enumerated()), id: \.offset) { i, e in if i > 0 { Divider() } ExeZeile(e: e) } } } liste("Ausgeschaltet", FeaturesText.zeilenAus(d.aus), "Stehen in der Datei ausdrücklich auf aus (0, aus).") liste("Ruht", FeaturesText.zeilenRuht(d.ruht), "Einstellungen eines Schalters, der aus ist – sie schalten nichts.") liste("Leer oder unbekannt", FeaturesText.zeilenLeerUnbekannt(leer: d.leerZeilen, unbekannt: d.unbekannt), "Was der Läufer beim Lesen übergeht.") } } } @ViewBuilder private func liste(_ titel: String, _ zeilen: [FeaturesText.Zeile], _ hinweis: String) -> some View { if !zeilen.isEmpty { FeaturesFalte(titel: titel, wert: "\(zeilen.count)", hinweis: hinweis) { VStack(spacing: 0) { ForEach(Array(zeilen.enumerated()), id: \.offset) { i, z in if i > 0 { Divider() } VStack(alignment: .leading, spacing: 2) { HStack(alignment: .firstTextBaseline, spacing: 8) { Text(z.name).font(.caption.weight(.semibold).monospaced()).lineLimit(1).truncationMode(.middle) Spacer(minLength: 4) Text(z.wert).font(.caption.monospaced()).foregroundStyle(.secondary).lineLimit(1).truncationMode(.head) } Text(z.unter).font(.caption2).foregroundStyle(.tertiary).lineLimit(2).fixedSize(horizontal: false, vertical: true) } .padding(.horizontal, 12).padding(.vertical, 6) .help(z.unter) } } } } } } /// Ein zugeklappter Abschnitt mit Titel links, Zahl rechts, Hinweis darunter. private struct FeaturesFalte: View { var titel: String var wert: String var hinweis: String? @ViewBuilder var inhalt: Inhalt @State private var offen = false var body: some View { VStack(alignment: .leading, spacing: 0) { Button { withAnimation(.snappy) { offen.toggle() } } label: { HStack(spacing: 8) { Text(titel).font(.callout.weight(.semibold)).fixedSize(horizontal: false, vertical: true) Spacer(minLength: 4) Text(wert).font(.caption).foregroundStyle(.secondary) Image(systemName: offen ? "chevron.up" : "chevron.down").font(.caption2).foregroundStyle(.tertiary) } .padding(.horizontal, 12).frame(minHeight: 38) .contentShape(Rectangle()) } .buttonStyle(.plain) .help(hinweis ?? "") if offen { if let hinweis { Fussnote(hinweis).padding(.horizontal, 12).padding(.bottom, 4) } inhalt } } .frame(maxWidth: .infinity, alignment: .leading) .background(.white.opacity(0.06), in: RoundedRectangle(cornerRadius: 12)) } } private struct SchalterZeile: View { var s: FeaturesSchalter var body: some View { VStack(alignment: .leading, spacing: 3) { HStack(alignment: .firstTextBaseline, spacing: 6) { Text(s.name).font(.caption.weight(.semibold).monospaced()).lineLimit(1).truncationMode(.middle) if s.neuSeitStart { Abzeichen(text: "NEU", farbe: .orange) } if s.sharingan { Abzeichen(text: "SHARINGAN", farbe: .purple) } Spacer(minLength: 4) Text(FeaturesText.wert(s.wert, pfad: s.pfad)).font(.caption.monospaced()).foregroundStyle(.teal) .lineLimit(1).truncationMode(.head).help(s.wert) } Text(FeaturesText.grund(datum: s.datum, kommentar: s.kommentar)).font(.caption2).foregroundStyle(.secondary) .lineLimit(2).fixedSize(horizontal: false, vertical: true) } .padding(.horizontal, 12).padding(.vertical, 8) .frame(maxWidth: .infinity, alignment: .leading) .help(FeaturesText.schalterTipp(s)) } } private struct ExeZeile: View { var e: FeaturesExe var body: some View { let z = FeaturesText.exeZustand(e) HStack(alignment: .top, spacing: 8) { Circle().fill(StatistikFarbe.ton(z.ton)).frame(width: 8, height: 8).padding(.top, 4) VStack(alignment: .leading, spacing: 1) { Text(e.rolle).font(.caption.weight(.semibold)).fixedSize(horizontal: false, vertical: true) Text(e.name).font(.caption2).foregroundStyle(.tertiary).lineLimit(1).truncationMode(.middle) } Spacer(minLength: 4) VStack(alignment: .trailing, spacing: 1) { Text(e.md5 ?? "–").font(.caption.monospaced()) Text(FeaturesText.exeUnter(e)).font(.caption2).foregroundStyle(z.ton == "rot" ? Color.red : Color.secondary) .multilineTextAlignment(.trailing).fixedSize(horizontal: false, vertical: true) } } .padding(.horizontal, 12).padding(.vertical, 8) .opacity(e.an ? 1 : 0.5) .help(e.pfad) } }