Neues Feld allCategories in /api/state (nach dem Einschalten false), in der App ein zweiter Schalter unter „Automatisch wechseln“. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
268 lines
9.6 KiB
Swift
268 lines
9.6 KiB
Swift
import SwiftUI
|
||
|
||
struct ContentView: View {
|
||
@State private var model = CubeModel()
|
||
@State private var showSettings = false
|
||
@State private var flashes = 0
|
||
@Environment(\.scenePhase) private var scenePhase
|
||
|
||
var body: some View {
|
||
NavigationStack {
|
||
// Kein Scrollen: die Effekt-Kacheln teilen sich die restliche Höhe
|
||
VStack(spacing: 12) {
|
||
status
|
||
specials
|
||
brightness
|
||
autoToggle
|
||
allToggle
|
||
effectGrid
|
||
}
|
||
.padding(.horizontal)
|
||
.padding(.vertical, 8)
|
||
.navigationTitle("LED-Würfel")
|
||
.navigationBarTitleDisplayMode(.inline)
|
||
.toolbar {
|
||
ToolbarItem(placement: .topBarTrailing) {
|
||
Button("Einstellungen", systemImage: "gearshape") { showSettings = true }
|
||
}
|
||
}
|
||
.sheet(isPresented: $showSettings) {
|
||
SettingsView(host: model.host) { model.host = CubeModel.cleanHost($0) }
|
||
}
|
||
// Nur abfragen, solange die App im Vordergrund ist
|
||
.task(id: scenePhase) {
|
||
if scenePhase == .active { await model.poll() }
|
||
}
|
||
}
|
||
}
|
||
|
||
private var status: some View {
|
||
HStack(spacing: 6) {
|
||
Circle()
|
||
.fill(model.isConnected ? .green : model.error == nil ? .gray : .red)
|
||
.frame(width: 8, height: 8)
|
||
Text(model.error ?? (model.isConnected ? "Verbunden mit \(model.host)" : "Verbinde mit \(model.host) …"))
|
||
.font(.caption)
|
||
.foregroundStyle(.secondary)
|
||
}
|
||
.frame(maxWidth: .infinity, alignment: .leading)
|
||
}
|
||
|
||
/// Spezialeffekte, die kurz über dem laufenden Effekt abgespielt werden
|
||
private var specials: some View {
|
||
HStack(spacing: 8) {
|
||
specialButton("blitz", "Blitz", "bolt.fill", .yellow)
|
||
specialButton("explosion", "Explosion", "burst.fill", .orange)
|
||
specialButton("regenbogen", "Regenbogen", "rainbow", .pink)
|
||
}
|
||
.sensoryFeedback(.impact(weight: .heavy), trigger: flashes)
|
||
.disabled(!model.isConnected)
|
||
}
|
||
|
||
private func specialButton(_ id: String, _ title: String, _ symbol: String, _ color: Color) -> some View {
|
||
Button {
|
||
flashes += 1
|
||
Task { await model.special(id) }
|
||
} label: {
|
||
VStack(spacing: 4) {
|
||
Image(systemName: symbol).font(.title2)
|
||
Text(title)
|
||
.font(.caption.weight(.semibold))
|
||
.lineLimit(1)
|
||
.minimumScaleFactor(0.7)
|
||
}
|
||
.foregroundStyle(.black)
|
||
.frame(maxWidth: .infinity, minHeight: 56)
|
||
}
|
||
.buttonStyle(.borderedProminent)
|
||
.buttonBorderShape(.roundedRectangle(radius: 14))
|
||
.tint(color)
|
||
}
|
||
|
||
private var brightness: some View {
|
||
HStack(spacing: 12) {
|
||
Image(systemName: "sun.min.fill")
|
||
.foregroundStyle(.secondary)
|
||
Slider(value: $model.brightness, in: 0...255, step: 1) { editing in
|
||
model.isDraggingBrightness = editing
|
||
if !editing { Task { await model.commitBrightness() } }
|
||
}
|
||
.accessibilityLabel("Helligkeit")
|
||
Image(systemName: "sun.max.fill")
|
||
.foregroundStyle(.secondary)
|
||
Text("\(Int(model.brightness))")
|
||
.monospacedDigit()
|
||
.foregroundStyle(.secondary)
|
||
.frame(width: 36, alignment: .trailing)
|
||
}
|
||
.disabled(!model.isConnected)
|
||
}
|
||
|
||
private var autoToggle: some View {
|
||
Toggle("Automatisch wechseln", isOn: Binding(
|
||
get: { model.state?.auto ?? true },
|
||
set: { on in Task { await model.setAuto(on) } }
|
||
))
|
||
.disabled(!model.isConnected)
|
||
}
|
||
|
||
/// Beim automatischen Wechsel auch in andere Kategorien springen
|
||
private var allToggle: some View {
|
||
Toggle("Alle Kategorien", isOn: Binding(
|
||
get: { model.state?.allCategories ?? false },
|
||
set: { on in Task { await model.setAllCategories(on) } }
|
||
))
|
||
.disabled(!model.isConnected || model.state?.auto == false)
|
||
}
|
||
|
||
/// Plätze pro Kategorie (2 Spalten × 6 Zeilen)
|
||
private static let slots = 12
|
||
|
||
/// Links die Kategorien, rechts die 12 Plätze der gewählten Kategorie.
|
||
/// Die Zeilen wachsen mit dem Platz (32 bis 56 Punkte hoch).
|
||
private var effectGrid: some View {
|
||
let shown = model.effects.filter { $0.category == model.shownCategory }
|
||
let rowCount = max(Self.slots / 2, model.categories.count)
|
||
return VStack(spacing: 8) {
|
||
ForEach(0..<rowCount, id: \.self) { row in
|
||
HStack(spacing: 8) {
|
||
categoryCell(row)
|
||
slot(shown, 2 * row)
|
||
slot(shown, 2 * row + 1)
|
||
}
|
||
.frame(minHeight: 32, maxHeight: 56)
|
||
}
|
||
}
|
||
.frame(maxHeight: .infinity, alignment: .top)
|
||
.disabled(!model.isConnected)
|
||
}
|
||
|
||
@ViewBuilder
|
||
private func categoryCell(_ index: Int) -> some View {
|
||
if index < model.categories.count {
|
||
categoryTile(model.categories[index])
|
||
} else {
|
||
Color.clear.frame(maxWidth: .infinity)
|
||
}
|
||
}
|
||
|
||
/// Kategorie wählen zeigt nur ihre Effekte; der Würfel läuft weiter.
|
||
/// Der grüne Punkt markiert die Kategorie des laufenden Effekts.
|
||
private func categoryTile(_ category: CubeCategory) -> some View {
|
||
let shown = category.id == model.shownCategory
|
||
let color = Self.color(category.id)
|
||
return Button {
|
||
model.shownCategory = category.id
|
||
} label: {
|
||
HStack(spacing: 4) {
|
||
if category.id == model.runningCategory {
|
||
Circle().fill(.green).frame(width: 7, height: 7)
|
||
}
|
||
Text(category.name)
|
||
.font(.subheadline.weight(.semibold))
|
||
.lineLimit(1)
|
||
.minimumScaleFactor(0.6)
|
||
}
|
||
.padding(.horizontal, 4)
|
||
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
||
.background(color.opacity(shown ? 0.7 : 0.2), in: .rect(cornerRadius: 10))
|
||
.overlay {
|
||
if shown { RoundedRectangle(cornerRadius: 10).strokeBorder(color, lineWidth: 2) }
|
||
}
|
||
}
|
||
.buttonStyle(.plain)
|
||
}
|
||
|
||
@ViewBuilder
|
||
private func slot(_ effects: [CubeEffect], _ index: Int) -> some View {
|
||
if index < effects.count && index < Self.slots {
|
||
tile(effects[index])
|
||
} else if index < Self.slots && !model.effects.isEmpty {
|
||
// freier Platz für einen weiteren Effekt
|
||
RoundedRectangle(cornerRadius: 10)
|
||
.strokeBorder(.secondary.opacity(0.3), style: StrokeStyle(lineWidth: 1, dash: [4, 4]))
|
||
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
||
} else {
|
||
Color.clear.frame(maxWidth: .infinity)
|
||
}
|
||
}
|
||
|
||
private func tile(_ effect: CubeEffect) -> some View {
|
||
let active = effect.id == model.state?.effect
|
||
let idle = Self.color(effect.category).opacity(0.3)
|
||
return Button {
|
||
Task { await model.select(effect) }
|
||
} label: {
|
||
Text(effect.name)
|
||
.font(.subheadline)
|
||
.fontWeight(active ? .semibold : .regular)
|
||
.multilineTextAlignment(.center)
|
||
// Nur an Leerzeichen umbrechen; einzelne Wörter lieber verkleinern als trennen
|
||
.lineLimit(effect.name.contains(" ") ? 2 : 1)
|
||
.minimumScaleFactor(0.6)
|
||
.foregroundStyle(active ? .black : .primary)
|
||
.padding(.horizontal, 4)
|
||
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
||
.background(active ? Color.green : idle, in: .rect(cornerRadius: 10))
|
||
}
|
||
.buttonStyle(.plain)
|
||
}
|
||
|
||
/// Farbe je Kategorie; neue Kategorien aus der Firmware erscheinen grau.
|
||
private static func color(_ category: String) -> Color {
|
||
switch category {
|
||
case "flash": .yellow
|
||
case "festtage": .red
|
||
case "musik": .indigo
|
||
case "floating": .cyan
|
||
case "natur": .green
|
||
case "fraktal": .purple
|
||
case "ruhe": .brown
|
||
default: .gray
|
||
}
|
||
}
|
||
}
|
||
|
||
struct SettingsView: View {
|
||
let host: String
|
||
let save: (String) -> Void
|
||
@State private var draft = ""
|
||
@Environment(\.dismiss) private var dismiss
|
||
|
||
var body: some View {
|
||
NavigationStack {
|
||
Form {
|
||
Section {
|
||
TextField("cube.local", text: $draft)
|
||
.keyboardType(.URL)
|
||
.textInputAutocapitalization(.never)
|
||
.autocorrectionDisabled()
|
||
} header: {
|
||
Text("Adresse des Würfels")
|
||
} footer: {
|
||
Text("Name oder IP-Adresse im WLAN, z. B. cube.local oder 10.0.1.8")
|
||
}
|
||
}
|
||
.navigationTitle("Einstellungen")
|
||
.navigationBarTitleDisplayMode(.inline)
|
||
.toolbar {
|
||
ToolbarItem(placement: .cancellationAction) {
|
||
Button("Abbrechen") { dismiss() }
|
||
}
|
||
ToolbarItem(placement: .confirmationAction) {
|
||
Button("Fertig") {
|
||
save(draft)
|
||
dismiss()
|
||
}
|
||
}
|
||
}
|
||
.onAppear { draft = host }
|
||
}
|
||
}
|
||
}
|
||
|
||
#Preview {
|
||
ContentView()
|
||
.preferredColorScheme(.dark)
|
||
}
|