import Foundation import SwiftData import SwiftUI import UniformTypeIdentifiers @Model final class Split { var name: String = "" var color: String = "indigo" var systemImage: String = "dumbbell.fill" var order: Int = 0 // Returns the SwiftUI Color for the stored color name func getColor () -> Color { return Color.color(from: self.color) } @Relationship(deleteRule: .cascade, inverse: \Exercise.split) var exercises: [Exercise]? = [] @Relationship(deleteRule: .nullify, inverse: \Workout.split) var workouts: [Workout]? = [] init(name: String, color: String = "indigo", systemImage: String = "dumbbell.fill", order: Int = 0) { self.name = name self.color = color self.systemImage = systemImage self.order = order } static let unnamed = "Unnamed Split" } // MARK: - EditableEntity Conformance extension Split: EditableEntity { var count: Int? { return self.exercises?.count } static func createNew() -> Split { return Split(name: "") } static var navigationTitle: String { return "Splits" } @ViewBuilder static func formView(for model: Split) -> some View { EntityAddEditView(model: model) { $model in SplitFormView(model: $model) } } } // MARK: - Identifiable Conformance extension Split: Identifiable { public var id: String { // Use the name as a unique identifier for the split // This is sufficient for UI purposes return self.name } } // MARK: - Private Form View fileprivate struct SplitFormView: View { @Binding var model: Split // Available colors for splits private let availableColors = ["red", "orange", "yellow", "green", "mint", "teal", "cyan", "blue", "indigo", "purple", "pink", "brown"] // Available system images for splits private let availableIcons = ["dumbbell.fill", "figure.strengthtraining.traditional", "figure.run", "figure.hiking", "figure.cooldown", "figure.boxing", "figure.wrestling", "figure.gymnastics", "figure.handball", "figure.core.training", "heart.fill", "bolt.fill"] var body: some View { Section(header: Text("Name")) { TextField("Name", text: $model.name) .bold() } Section(header: Text("Appearance")) { Picker("Color", selection: $model.color) { ForEach(availableColors, id: \.self) { colorName in let tempSplit = Split(name: "", color: colorName) HStack { Circle() .fill(tempSplit.getColor()) .frame(width: 20, height: 20) Text(colorName.capitalized) } .tag(colorName) } } Picker("Icon", selection: $model.systemImage) { ForEach(availableIcons, id: \.self) { iconName in HStack { Image(systemName: iconName) .frame(width: 24, height: 24) Text(iconName.replacingOccurrences(of: ".fill", with: "").replacingOccurrences(of: "figure.", with: "").capitalized) } .tag(iconName) } } } Section(header: Text("Exercises")) { NavigationLink { ExerciseListView(split: model) } label: { ListItem( text: "Exercises", count: model.exercises?.count ?? 0 ) } } } }