188 lines
7.3 KiB
Swift
188 lines
7.3 KiB
Swift
//
|
||
// SplitExerciseAssignment.swift
|
||
// Workouts
|
||
//
|
||
// Created by rzen on 7/15/25 at 7:12 AM.
|
||
//
|
||
// Copyright 2025 Rouslan Zenetl. All Rights Reserved.
|
||
//
|
||
|
||
import SwiftUI
|
||
|
||
struct ExerciseAddEditView: View {
|
||
@Environment(\.modelContext) private var modelContext
|
||
@Environment(\.dismiss) private var dismiss
|
||
@State private var showingExercisePicker = false
|
||
|
||
@State var model: Exercise
|
||
|
||
@State var originalWeight: Int? = nil
|
||
@State var loadType: LoadType = .none
|
||
|
||
@State private var minutes = 0
|
||
@State private var seconds = 0
|
||
|
||
@State private var weight_tens = 0
|
||
@State private var weight = 0
|
||
|
||
@State private var reps = 0
|
||
@State private var sets = 0
|
||
|
||
var body: some View {
|
||
NavigationStack {
|
||
Form {
|
||
Section(header: Text("Exercise")) {
|
||
let exerciseName = model.name
|
||
if exerciseName.isEmpty {
|
||
Button(action: {
|
||
showingExercisePicker = true
|
||
}) {
|
||
HStack {
|
||
Text(model.name.isEmpty ? "Select Exercise" : model.name)
|
||
Spacer()
|
||
Image(systemName: "chevron.right")
|
||
.foregroundColor(.gray)
|
||
}
|
||
}
|
||
} else {
|
||
ListItem(title: exerciseName)
|
||
}
|
||
}
|
||
|
||
Section (header: Text("Sets/Reps")) {
|
||
HStack (alignment: .bottom) {
|
||
VStack (alignment: .center) {
|
||
Text("Sets")
|
||
Picker("", selection: $sets) {
|
||
ForEach(0..<20) { sets in
|
||
Text("\(sets)").tag(sets)
|
||
}
|
||
}
|
||
.frame(height: 100)
|
||
.pickerStyle(.wheel)
|
||
}
|
||
VStack (alignment: .center) {
|
||
Text("Reps")
|
||
Picker("", selection: $reps) {
|
||
ForEach(0..<100) { reps in
|
||
Text("\(reps)").tag(reps)
|
||
}
|
||
}
|
||
.frame(height: 100)
|
||
.pickerStyle(.wheel)
|
||
}
|
||
}
|
||
}
|
||
.onAppear {
|
||
sets = model.sets
|
||
reps = model.reps
|
||
}
|
||
|
||
Section (header: Text("Load Type"), footer: Text("For bodyweight exercises choose None. For resistance or weight training selected Weight. For exercises that are time oriented (like plank or meditation select Time.")) {
|
||
Picker("", selection: $loadType) {
|
||
ForEach (LoadType.allCases, id: \.self) { load in
|
||
Text(load.name)
|
||
.tag(load)
|
||
}
|
||
}
|
||
.pickerStyle(.segmented)
|
||
}
|
||
|
||
if loadType == .weight {
|
||
Section (header: Text("Weight")) {
|
||
HStack {
|
||
HStack {
|
||
Picker("", selection: $weight_tens) {
|
||
ForEach(0..<100) { lbs in
|
||
Text("\(lbs*10)").tag(lbs*10)
|
||
}
|
||
}
|
||
.frame(height: 100)
|
||
.pickerStyle(.wheel)
|
||
|
||
Picker("", selection: $weight) {
|
||
ForEach(0..<10) { lbs in
|
||
Text("\(lbs)").tag(lbs)
|
||
}
|
||
}
|
||
.frame(height: 100)
|
||
.pickerStyle(.wheel)
|
||
|
||
Text("lbs")
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
if loadType == .duration {
|
||
Section (header: Text("Duration")) {
|
||
|
||
HStack {
|
||
Picker("Minutes", selection: $minutes) {
|
||
ForEach(0..<60) { minute in
|
||
Text("\(minute) min").tag(minute)
|
||
}
|
||
}
|
||
.pickerStyle(.wheel)
|
||
|
||
Picker("Seconds", selection: $seconds) {
|
||
ForEach(0..<60) { second in
|
||
Text("\(second) sec").tag(second)
|
||
}
|
||
}
|
||
.pickerStyle(.wheel)
|
||
}
|
||
}
|
||
}
|
||
|
||
Section (header: Text("Weight Increase")) {
|
||
HStack {
|
||
Text("Remind every \(model.weightReminderTimeIntervalWeeks) weeks")
|
||
Spacer()
|
||
Stepper("", value: $model.weightReminderTimeIntervalWeeks, in: 0...366)
|
||
}
|
||
HStack {
|
||
Text("Last weight change \(Date().humanTimeInterval(to: model.weightLastUpdated)) ago")
|
||
}
|
||
}
|
||
}
|
||
.onAppear {
|
||
originalWeight = model.weight
|
||
weight_tens = model.weight / 10
|
||
weight = model.weight - weight_tens * 10
|
||
minutes = Int(model.duration.timeIntervalSince1970) / 60
|
||
seconds = Int(model.duration.timeIntervalSince1970) - minutes * 60
|
||
}
|
||
.sheet(isPresented: $showingExercisePicker) {
|
||
ExercisePickerView { exerciseNames in
|
||
model.name = exerciseNames.first ?? "Exercise.unnamed"
|
||
}
|
||
}
|
||
.navigationTitle(model.name.isEmpty ? "New Exercise" : model.name)
|
||
.toolbar {
|
||
ToolbarItem(placement: .navigationBarLeading) {
|
||
Button("Cancel") {
|
||
dismiss()
|
||
}
|
||
}
|
||
|
||
ToolbarItem(placement: .navigationBarTrailing) {
|
||
Button("Save") {
|
||
if let originalWeight = originalWeight {
|
||
if originalWeight != model.weight {
|
||
model.weightLastUpdated = Date()
|
||
}
|
||
}
|
||
model.duration = Date(timeIntervalSince1970: Double(minutes * 60 + seconds))
|
||
model.weight = weight_tens + weight
|
||
model.sets = sets
|
||
model.reps = reps
|
||
try? modelContext.save()
|
||
dismiss()
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|