import CockpitKern import SwiftUI // Bausteine der Statistik-Seite (winui/Seiten/StatistikSeite.cs: Karte, Paar, Kachel, Wertfenster). enum StatistikFarbe { /// Die Farbnamen des Servers; "lila" und "gelb" kennt `Farben.name` nicht. static func ton(_ name: String?) -> Color { switch name { case "lila": .purple case "gelb": .yellow default: Farben.name(name) } } /// Ergebnisfarbe nach Vorzeichen; ohne Wert oder bei Null grau. static func ergebnis(_ x: Double?) -> String { AuswertungText.ton(x) ?? "grau" } } enum StatistikZeit { /// Ortszeit mit festem Muster ("dd.MM. HH:mm"), deutsch. static func format(_ t: Double?, _ muster: String) -> String { guard let t else { return "–" } let f = DateFormatter() f.locale = Locale(identifier: "de_DE") f.dateFormat = muster return f.string(from: t.datum) } static func tagUhr(_ t: Double?) -> String { format(t, "dd.MM. HH:mm") } } /// Eine Karte mit Titel, Hinweis und Steuerung rechts oben (schmal: darunter). struct StatistikKarte: View { var titel: String var hinweis: String? @ViewBuilder var rechts: Rechts @ViewBuilder var inhalt: Inhalt init(_ titel: String, hinweis: String? = nil, @ViewBuilder rechts: () -> Rechts, @ViewBuilder inhalt: () -> Inhalt) { self.titel = titel; self.hinweis = hinweis; self.rechts = rechts(); self.inhalt = inhalt() } var body: some View { VStack(alignment: .leading, spacing: 10) { ViewThatFits(in: .horizontal) { HStack(alignment: .top, spacing: 8) { kopf; Spacer(minLength: 8); rechts } VStack(alignment: .leading, spacing: 8) { kopf; rechts } } inhalt } .padding(14) .frame(maxWidth: .infinity, alignment: .leading) .background(.white.opacity(0.05), in: RoundedRectangle(cornerRadius: 14)) } private var kopf: some View { VStack(alignment: .leading, spacing: 1) { Text(titel).font(.callout.weight(.semibold)) if let hinweis { Text(hinweis).font(.caption2).foregroundStyle(.tertiary).fixedSize(horizontal: false, vertical: true) } } } } extension StatistikKarte where Rechts == EmptyView { init(_ titel: String, hinweis: String? = nil, @ViewBuilder inhalt: () -> Inhalt) { self.init(titel, hinweis: hinweis, rechts: { EmptyView() }, inhalt: inhalt) } } /// Zwei Karten nebeneinander, wenn sie breit genug sind, sonst untereinander. struct StatistikPaar: View { var a: A var b: B init(@ViewBuilder a: () -> A, @ViewBuilder b: () -> B) { self.a = a(); self.b = b() } var body: some View { ViewThatFits(in: .horizontal) { HStack(alignment: .top, spacing: 12) { a.frame(minWidth: 420) b.frame(minWidth: 420) } VStack(spacing: 12) { a; b } } } } /// Eine Kachel der Kennzahlen: Titel, Wert, Unterzeile (Spanne, Zusatz), Hilfe beim Zeigen. struct AuswertungKachel: View { var kachel: KennzahlKachel var body: some View { VStack(alignment: .leading, spacing: 2) { Text(kachel.titel).font(.caption2).foregroundStyle(.secondary).lineLimit(1) Text(kachel.wert) .font(.system(kachel.klein ? .body : .title3, design: .rounded).weight(.semibold)) .monospacedDigit() .foregroundStyle(kachel.ton.map(StatistikFarbe.ton) ?? .primary) .lineLimit(1).minimumScaleFactor(0.6) .frame(minHeight: 24, alignment: .leading) Text(kachel.unter).font(.caption2).foregroundStyle(.tertiary).lineLimit(2).fixedSize(horizontal: false, vertical: true) } .frame(maxWidth: .infinity, alignment: .leading) .padding(10) .background(.quaternary.opacity(0.5), in: RoundedRectangle(cornerRadius: 10)) .help(kachel.hilfe ?? "") } } struct KachelRaster: View { var kacheln: [KennzahlKachel] var body: some View { LazyVGrid(columns: [GridItem(.adaptive(minimum: 170), spacing: 8, alignment: .top)], spacing: 8) { ForEach(kacheln) { AuswertungKachel(kachel: $0) } } } } /// Das Fenster neben dem Zeiger: eine Kopfzeile, dann Zeilen mit farbigem Punkt (oder ohne Ton: Text). struct Wertfenster: View { struct Zeile: Identifiable { var text: String var ton: String? var id = UUID() } var zeilen: [Zeile] var body: some View { VStack(alignment: .leading, spacing: 1) { ForEach(zeilen) { z in if let ton = z.ton { HStack(spacing: 5) { Circle().fill(StatistikFarbe.ton(ton)).frame(width: 7, height: 7) Text(z.text).font(.caption2.monospacedDigit()) } } else { Text(z.text).font(.caption2).foregroundStyle(.secondary) } } } .padding(.horizontal, 8).padding(.vertical, 5) .background(.regularMaterial, in: RoundedRectangle(cornerRadius: 6)) .overlay(RoundedRectangle(cornerRadius: 6).stroke(.white.opacity(0.12))) .fixedSize() } } /// Kleine Segmentwahl, die sich ihre Breite selbst nimmt. struct Segmente: View where W.RawValue == String, W.AllCases: RandomAccessCollection { @Binding var wahl: W var body: some View { // In der schmalen Leiste passen fuenf Segmente nicht: dann ein Menue statt ueber den Rand zu ragen ViewThatFits(in: .horizontal) { Picker("", selection: $wahl) { ForEach(Array(W.allCases), id: \.self) { Text($0.rawValue) } } .pickerStyle(.segmented).labelsHidden().fixedSize() Picker("", selection: $wahl) { ForEach(Array(W.allCases), id: \.self) { Text($0.rawValue) } } .pickerStyle(.menu).labelsHidden().fixedSize() } } } /// Ein Strich unter einer Karte: kleine Erklaerung des Diagramms. struct Fussnote: View { var text: String init(_ text: String) { self.text = text } var body: some View { Text(text).font(.caption2).foregroundStyle(.tertiary).fixedSize(horizontal: false, vertical: true) } }