Schulung_iOS/UniView.swift
2025-10-23 13:45:41 +02:00

132 lines
4.2 KiB
Swift

//import SwiftUI
//import SwiftData
//
////@Model class Unicycle {
//// var createdAt = Date()
//// var name = ""
//// var red = 1.0
//// var green = 1.0
//// var blue = 1.0
//// init() { } // You can't escape this
////}
//
//struct UniView: View {
// @Environment(\.modelContext) private var modelContext
// @Query private var unis: [Unicycle]
// @State private var selection = Set<PersistentIdentifier>()
// @Environment(\.editMode) private var editMode
// @State private var edit: EditMode = .inactive
//
// var body: some View {
// NavigationStack {
// List(selection: $selection) {
// ForEach(unis) { uni in
// NavigationLink(uni.createdAt.description) {
// UniDetailView(uni: uni)
// }
// }
// .onDelete { indexSet in
// for index in indexSet {
// modelContext.delete(unis[index])
// }
// }
// }
// .toolbar {
//// ToolbarItem(placement: .topBarLeading) {
// if !unis.isEmpty { EditButton() }
//// }
//// ToolbarItem(placement: .topBarTrailing) {
// if edit == .active {
// if !selectedUnis.isEmpty {
// Button("Delete \(selectedUnis.count)", role: .destructive) {
// // Map selection to models and delete
// for uni in selectedUnis {
// modelContext.delete(uni)
// }
// selection.removeAll()
// }
// .disabled(selection.isEmpty)
// }
// } else {
// Button("Add") {
// modelContext.insert(Unicycle())
// }
// }
//// }
// }
// .environment(\.editMode, $edit) // <-- Provide the binding
// }
// }
//
// @MainActor
// var selectedUnis: [Unicycle] {
// unis.filter { selection.contains($0.id) }
// }
//}
//
//struct UniDetailView: View {
// @Bindable var uni: Unicycle
// @State var color: Color
// @Environment(\.modelContext) private var modelContext
//
// init(uni: Unicycle) {
// self.uni = uni
// _color = State(initialValue: Color(red: uni.red, green: uni.green, blue: uni.blue))
// }
//
// var body: some View {
// VStack {
// Text(uni.createdAt.description)
// ColorPicker("Color", selection: Binding(
// get: { color },
// set: {
// color = $0
// if let comps = $0.rgbComponents() {
// uni.red = Double(comps.red)
// uni.green = Double(comps.green)
// uni.blue = Double(comps.blue)
// }
// }
// ))
// TextField("Enter name", text: $uni.name)
// .textFieldStyle(.roundedBorder)
// .padding()
// Button("Save 3") {
// try? modelContext.save()
//
//
// }
// }
// .padding()
// .onChange(of: color) {
// if let comps = color.rgbComponents() {
// uni.red = Double(comps.red)
// uni.green = Double(comps.green)
// uni.blue = Double(comps.blue)
// }
// }
// }
//}
//
//extension Color {
// func rgbComponents() -> (red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat)? {
// // Convert SwiftUI Color to UIColor
// let uiColor = UIColor(self)
//
// var r: CGFloat = 0
// var g: CGFloat = 0
// var b: CGFloat = 0
// var a: CGFloat = 0
//
// guard uiColor.getRed(&r, green: &g, blue: &b, alpha: &a) else {
// return nil
// }
// return (r, g, b, a)
// }
//}
//
//
//#Preview {
// UniView()
// .modelContainer(for: Unicycle.self)
//}