152 lines
6.0 KiB
Swift
152 lines
6.0 KiB
Swift
//
|
|
// CreateWorkoutView.swift
|
|
// Werkout_ios
|
|
//
|
|
// Created by Trey Tartt on 6/15/23.
|
|
//
|
|
|
|
import SwiftUI
|
|
|
|
struct CreateWorkoutMainView: View {
|
|
@StateObject var viewModel = WorkoutViewModel()
|
|
@State var selectedCreateWorkoutSuperSet: CreateWorkoutSuperSet?
|
|
@State private var showAddExercise = false
|
|
|
|
private var canSubmit: Bool {
|
|
viewModel.title.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty == false &&
|
|
viewModel.isUploading == false
|
|
}
|
|
|
|
var body: some View {
|
|
VStack {
|
|
VStack {
|
|
TextField("Title", text: $viewModel.title)
|
|
.padding(.horizontal)
|
|
.textFieldStyle(.roundedBorder)
|
|
|
|
TextField("Description", text: $viewModel.description)
|
|
.padding(.horizontal)
|
|
.textFieldStyle(.roundedBorder)
|
|
}
|
|
.padding(.bottom)
|
|
.background(Color(uiColor: .systemGray5))
|
|
|
|
ScrollViewReader { proxy in
|
|
List() {
|
|
ForEach(viewModel.superSets) { superset in
|
|
CreateWorkoutSupersetView(
|
|
selectedCreateWorkoutSuperSet: $selectedCreateWorkoutSuperSet,
|
|
showAddExercise: $showAddExercise,
|
|
superset: superset,
|
|
viewModel: viewModel)
|
|
}
|
|
// after adding new exercise we have to scroll to the bottom
|
|
// where the new exercise is sooo keep this so we can scroll
|
|
// to id 999
|
|
Color.clear
|
|
.frame(height: 1)
|
|
.accessibilityHidden(true)
|
|
.id(999)
|
|
.listRowSeparator(.hidden)
|
|
}
|
|
.onChange(of: viewModel.randomValueForUpdatingValue, perform: { newValue in
|
|
withAnimation {
|
|
proxy.scrollTo(999, anchor: .bottom)
|
|
}
|
|
})
|
|
}
|
|
.sheet(isPresented: $showAddExercise) {
|
|
AddExerciseView(selectedExercise: { exercise in
|
|
let workoutExercise = CreateWorkoutExercise(exercise: exercise)
|
|
selectedCreateWorkoutSuperSet?.exercises.append(workoutExercise)
|
|
|
|
|
|
// if left or right auto add the other side
|
|
// with a recover in between b/c its
|
|
// eaiser to delete a recover than add one
|
|
if exercise.side?.isEmpty == false {
|
|
let exercises = DataStore.shared.allExercise?.filter({
|
|
$0.name == exercise.name
|
|
})
|
|
let recover = DataStore.shared.allExercise?.first(where: {
|
|
$0.name.lowercased() == "recover"
|
|
})
|
|
if let exercises = exercises, let recover = recover {
|
|
if exercises.count == 2 {
|
|
let recoverWorkoutExercise = CreateWorkoutExercise(exercise: recover)
|
|
selectedCreateWorkoutSuperSet?.exercises.append(recoverWorkoutExercise)
|
|
for LRExercise in exercises {
|
|
if LRExercise.id != exercise.id {
|
|
let otherSideExercise = CreateWorkoutExercise(exercise: LRExercise)
|
|
selectedCreateWorkoutSuperSet?.exercises.append(otherSideExercise)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
viewModel.increaseRandomNumberForUpdating()
|
|
selectedCreateWorkoutSuperSet = nil
|
|
})
|
|
}
|
|
|
|
HStack {
|
|
Button("Add Superset", action: {
|
|
viewModel.addNewSuperset()
|
|
})
|
|
.frame(maxWidth: .infinity, alignment: .center)
|
|
.frame(height: 44)
|
|
.foregroundColor(.white)
|
|
.background(.blue)
|
|
.cornerRadius(Constants.buttonRadius)
|
|
.padding()
|
|
.frame(maxWidth: .infinity)
|
|
.accessibilityLabel("Add superset")
|
|
.accessibilityHint("Adds a new superset section to this workout")
|
|
|
|
Divider()
|
|
|
|
Button(action: {
|
|
viewModel.uploadWorkout()
|
|
}, label: {
|
|
if viewModel.isUploading {
|
|
ProgressView()
|
|
.progressViewStyle(.circular)
|
|
.tint(.white)
|
|
} else {
|
|
Text("Done")
|
|
}
|
|
})
|
|
.frame(maxWidth: .infinity, alignment: .center)
|
|
.frame(height: 44)
|
|
.foregroundColor(.white)
|
|
.background(.green)
|
|
.cornerRadius(Constants.buttonRadius)
|
|
.padding()
|
|
.frame(maxWidth: .infinity)
|
|
.disabled(canSubmit == false)
|
|
.accessibilityLabel("Upload workout")
|
|
.accessibilityHint("Uploads this workout to your account")
|
|
}
|
|
.frame(height: 44)
|
|
|
|
Divider()
|
|
}
|
|
.background(Color(uiColor: .systemGray5))
|
|
.alert("Create Workout", isPresented: Binding<Bool>(
|
|
get: { viewModel.validationError != nil },
|
|
set: { _ in viewModel.validationError = nil }
|
|
)) {
|
|
Button("OK", role: .cancel) {}
|
|
} message: {
|
|
Text(viewModel.validationError ?? "")
|
|
}
|
|
}
|
|
}
|
|
|
|
//struct CreateWorkoutView_Previews: PreviewProvider {
|
|
// static var previews: some View {
|
|
// CreateWorkoutView()
|
|
// }
|
|
//}
|