add WorkoutIconSelector to select icon and color for a Workout

This commit is contained in:
Felix Förtsch
2024-09-06 08:24:24 +02:00
parent d82d0cd9fa
commit 6018a57473
5 changed files with 88 additions and 15 deletions
+64 -3
View File
@@ -5,7 +5,7 @@
// Created by Felix Förtsch on 10.08.24.
//
import Foundation
import SwiftUI
import SwiftData
@Model
@@ -15,8 +15,12 @@ final class Workout: Nameable {
var id = UUID()
@Attribute(.unique) var name: String
// Icon
var workoutIconSystemName = "figure.run.square.stack"
var workoutIconColorName = ColorName.black
// Other properties and methods
var timestamp: Date = Date.now
var timestamp = Date.now
@Relationship(deleteRule: .cascade) var workoutItems: [WorkoutItem] = []
@@ -46,7 +50,64 @@ final class Workout: Nameable {
exercise.position = index
}
}
}
extension Workout {
// This enum maps the system colors to enable storing them with the SwiftData model data as String.
// See Workout.workoutIconColorName for a usage example.
// TODO: Use a Macro to reduce this horrible contraption to something maintainable.
enum ColorName: String, Codable {
case black
case blue
case cyan
case gray
case green
case indigo
case mint
case orange
case pink
case purple
case red
case white
case yellow
var color: Color {
switch self {
case .black: return .black
case .blue: return .blue
case .cyan: return .cyan
case .gray: return .gray
case .green: return .green
case .indigo: return .indigo
case .mint: return .mint
case .orange: return .orange
case .pink: return .pink
case .purple: return .purple
case .red: return .red
case .white: return .white
case .yellow: return .yellow
}
}
static func fromColor(_ color: Color) -> ColorName? {
switch color {
case .black: return .black
case .blue: return .blue
case .cyan: return .cyan
case .gray: return .gray
case .green: return .green
case .indigo: return .indigo
case .mint: return .mint
case .orange: return .orange
case .pink: return .pink
case .purple: return .purple
case .red: return .red
case .white: return .white
case .yellow: return .yellow
default: return nil
}
}
}
}
extension Workout {