I'm trying to do a picker where i loop over an array, and when i click on different workout exercises, it should update the text below which says "Selected exercise:" , but its stuck and not updating my view at all.
Here is my code :
import SwiftUI
struct WorkoutView: View {
var workouts: [WorkoutPlan] = [
.init(name: "Hyper Up", setsReps: "4 x 10-12 reps", weight: "low/mid weights", imageName: "arrow.up", bodyPart: "Up"),
.init(name: "Heavy Up", setsReps: "5 x 3-5 reps", weight: "heavy weights", imageName: "chevron.up",bodyPart: "Up"),
.init(name: "Hyper Down", setsReps: "4 x 10-12 reps", weight: "low/mid weights",imageName: "arrow.down",bodyPart: "Down"),
.init(name: "Heavy Down", setsReps: "5 x 3-5 reps", weight: "heavy weights",imageName: "chevron.down",bodyPart: "Down")
]
@State var selectedExercise: String?
var body: some View{
NavigationStack{
List{
Section(){
ForEach(workouts, id: \.name) {
workout in
NavigationLink(value:workout) {Label(workout.name, systemImage: workout.imageName)
}
}
}
}
.navigationTitle("Workout Plans")
.navigationDestination(for: WorkoutPlan.self) {workout in
//own screen should come here
let exercises: [Exercise] = [
.init(name: "Dumbbell Shoulder Press", bodyPart: "Up"),
.init(name: "Over Head Press", bodyPart: "Up"),
.init(name: "Bench Press", bodyPart: "Up"),
.init(name: "Butterfly", bodyPart: "Up"),
.init(name: "Incline Bench Press", bodyPart: "Up"),
.init(name: "Triceps Cable Row", bodyPart: "Up"),
.init(name: "Biceps Curl", bodyPart: "Up"),
.init(name: "ABS Cable Curl", bodyPart: "Down"),
.init(name: "Plank", bodyPart: "Down"),
.init(name: "Deadlift", bodyPart: "Down"),
.init(name: "Seated Cable Row", bodyPart: "Down"),
.init(name: "Squat", bodyPart: "Down"),
.init(name: "Sumo Deadlift", bodyPart: "Down"),
.init(name: "Front Squat", bodyPart: "Down"),
]
let upExercises = exercises.filter { $0.bodyPart == "Up" }
let downExercises = exercises.filter { $0.bodyPart == "Down" }
ZStack {
VStack{
List{
Section(){
if workout.bodyPart == "Up" {
Label(workout.name, systemImage: workout.imageName).font(.largeTitle).bold()
VStack{
Picker(
selection: $selectedExercise,
label: Text("Workout"),
content: {
ForEach(upExercises, id: \.self) { exercise in
HStack{
Text(exercise.name).tag(exercise.name)
}
}}
).pickerStyle(MenuPickerStyle())
Text("Selected exercise: \(selectedExercise ?? "No exercise selected")")
}
} else if workout.bodyPart == "Down" {
Label(workout.name, systemImage: workout.imageName).font(.largeTitle).bold()
ForEach(downExercises.suffix(7), id: \.name) { exercise in
Text(exercise.name)
}
}
}
}
}
}.accentColor(Color(.label))
}
}
}
}
struct WorkoutView_Previews: PreviewProvider {
static var previews: some View{
WorkoutView()
}
}
struct WorkoutPlan: Hashable {
let name: String
let setsReps: String
let weight: String
let imageName: String
let bodyPart: String
}
struct Exercise: Hashable {
let name: String
let bodyPart: String
}
I have tried to move around the State as i thougth it might be off the scope,i have tried to convert .tag to String? but that also didnt work. I have looked through Stack and google but couldn't find anything that could fix my problem.