fix repetitive save of WorkoutItem, add: Onboarding, Defaults, Settings, Trainer/Trainee skeletons, reorder files, remove all Bindable
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
//
|
||||
// Exercise.swift
|
||||
// WorkoutsPlus
|
||||
//
|
||||
// Created by Felix Förtsch on 25.08.24.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import SwiftData
|
||||
|
||||
@Model
|
||||
final class Exercise: Nameable {
|
||||
static var systemImage = "figure.run"
|
||||
|
||||
var id = UUID()
|
||||
@Attribute(.unique) var name: String
|
||||
// var metric: String = "reps"
|
||||
// var exerciseDescription: ExerciseDescription?
|
||||
|
||||
var timestamp: Date = Date.now
|
||||
|
||||
init(_ name: String = "") {
|
||||
self.name = name
|
||||
}
|
||||
}
|
||||
|
||||
extension Exercise {
|
||||
static let sampleData: [Exercise] = [
|
||||
Exercise("Dips"),
|
||||
Exercise("Chin-ups"),
|
||||
Exercise("Push-ups"),
|
||||
Exercise("Inverted Rows"),
|
||||
Exercise("Hanging Knee Raises"),
|
||||
Exercise("Pistol Squats"),
|
||||
Exercise("Hanging Leg Curls"),
|
||||
Exercise("Sissy Squats")
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
//
|
||||
// Person.swift
|
||||
// WorkoutsPlus
|
||||
//
|
||||
// Created by Felix Förtsch on 03.09.24.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import SwiftData
|
||||
|
||||
@Model
|
||||
class Trainer: Nameable {
|
||||
var id = UUID()
|
||||
var name: String = ""
|
||||
|
||||
var trainees: [Trainee] = []
|
||||
|
||||
init(name: String) {
|
||||
self.name = name
|
||||
}
|
||||
}
|
||||
|
||||
@Model
|
||||
class Trainee: Nameable {
|
||||
var id = UUID()
|
||||
var name: String = ""
|
||||
|
||||
var trainer: Trainer?
|
||||
|
||||
init(name: String, trainer: Trainer? = nil) {
|
||||
self.name = name
|
||||
self.trainer = trainer
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
//
|
||||
// Protocols.swift
|
||||
// WorkoutsPlus
|
||||
//
|
||||
// Created by Felix Förtsch on 04.09.24.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
protocol Nameable: Identifiable {
|
||||
var id: UUID { get }
|
||||
var name: String { get }
|
||||
}
|
||||
|
||||
protocol Positionable: Identifiable {
|
||||
var id: UUID { get }
|
||||
var position: Int { get }
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
//
|
||||
// Workout.swift
|
||||
// WorkoutsPlus
|
||||
//
|
||||
// Created by Felix Förtsch on 10.08.24.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import SwiftData
|
||||
|
||||
@Model
|
||||
final class Workout: Nameable {
|
||||
static var systemImage = "figure.run.square.stack"
|
||||
|
||||
var id = UUID()
|
||||
@Attribute(.unique) var name: String
|
||||
|
||||
// Other properties and methods
|
||||
var timestamp: Date = Date.now
|
||||
|
||||
@Relationship(deleteRule: .cascade) var workoutItems: [WorkoutItem] = []
|
||||
|
||||
init(name: String) {
|
||||
self.name = name
|
||||
}
|
||||
|
||||
func add(workoutItem: WorkoutItem) {
|
||||
self.workoutItems.append(workoutItem)
|
||||
updateWorkoutItemsPositions()
|
||||
}
|
||||
|
||||
func add(workoutItems: [WorkoutItem]) {
|
||||
for workoutItem in workoutItems {
|
||||
self.workoutItems.append(workoutItem)
|
||||
}
|
||||
updateWorkoutItemsPositions()
|
||||
}
|
||||
|
||||
func moveWorkoutItem(from source: IndexSet, to destination: Int) {
|
||||
workoutItems.move(fromOffsets: source, toOffset: destination)
|
||||
updateWorkoutItemsPositions()
|
||||
}
|
||||
|
||||
private func updateWorkoutItemsPositions() {
|
||||
for (index, exercise) in workoutItems.enumerated() {
|
||||
exercise.position = index
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
extension Workout {
|
||||
private convenience init(name: String, exercises: [WorkoutItem]) {
|
||||
self.init(name: name)
|
||||
self.workoutItems = exercises
|
||||
}
|
||||
|
||||
static let sampleData: Workout = {
|
||||
var workout = Workout(name: "Recommended Routine")
|
||||
|
||||
for workoutItem in WorkoutItem.sampleData {
|
||||
workout.add(workoutItem: workoutItem)
|
||||
}
|
||||
|
||||
return workout
|
||||
}()
|
||||
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
//
|
||||
// WorkoutItem.swift
|
||||
// WorkoutsPlus
|
||||
//
|
||||
// Created by Felix Förtsch on 10.08.24.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import SwiftData
|
||||
|
||||
@Model
|
||||
final class WorkoutItem: Nameable, Positionable {
|
||||
var id = UUID()
|
||||
var name: String
|
||||
|
||||
var workout: Workout?
|
||||
var workoutItemType: WorkoutItemType
|
||||
var position: Int = 0
|
||||
|
||||
var reps: Int = 8
|
||||
|
||||
// EXERCISE
|
||||
var exercise: Exercise?
|
||||
// TODO: Think about what's happening when an exercise (template) is deleted or changed
|
||||
// { didSet { self.name = exercise?.name ?? "self.name" } }
|
||||
|
||||
init(_ reps: Int, _ exercise: String) {
|
||||
self.workoutItemType = .exercise
|
||||
|
||||
self.name = exercise
|
||||
self.reps = reps
|
||||
self.exercise = Exercise(exercise)
|
||||
}
|
||||
|
||||
init(from exercise: Exercise) {
|
||||
self.workoutItemType = .exercise
|
||||
|
||||
self.name = exercise.name
|
||||
self.exercise = exercise
|
||||
}
|
||||
|
||||
// SET
|
||||
var workoutItems: [WorkoutItem] = []
|
||||
|
||||
init(workoutItems: [WorkoutItem] = []) {
|
||||
self.name = "Set"
|
||||
self.workoutItemType = .set
|
||||
self.reps = 3
|
||||
workoutItems.forEach(addChild)
|
||||
}
|
||||
|
||||
func addChild(_ child: WorkoutItem) {
|
||||
if self.workoutItemType == .set {
|
||||
self.workoutItems.append(child)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension WorkoutItem {
|
||||
enum WorkoutItemType: Codable {
|
||||
case set
|
||||
case workout
|
||||
case exercise
|
||||
}
|
||||
}
|
||||
|
||||
extension WorkoutItem {
|
||||
static let sampleData: [WorkoutItem] = {
|
||||
var exercises = [WorkoutItem]()
|
||||
|
||||
for exercise in Exercise.sampleData {
|
||||
exercises.append(WorkoutItem(from: exercise))
|
||||
}
|
||||
|
||||
return exercises
|
||||
}()
|
||||
}
|
||||
Reference in New Issue
Block a user