73 lines
2.2 KiB
Swift
73 lines
2.2 KiB
Swift
//
|
|
// AllWorkoutsView.swift
|
|
// Werkout_ios
|
|
//
|
|
// Created by Trey Tartt on 6/15/23.
|
|
//
|
|
|
|
import Foundation
|
|
import SwiftUI
|
|
|
|
struct AllWorkoutsView: View {
|
|
@State var workouts: [Workout]?
|
|
@EnvironmentObject var bridgeModule: BridgeModule
|
|
@State private var showWorkoutDetail = false
|
|
@State private var selectedWorkout: Workout? {
|
|
didSet {
|
|
showWorkoutDetail = true
|
|
bridgeModule.currentWorkout = self.selectedWorkout
|
|
}
|
|
}
|
|
|
|
var body: some View {
|
|
ZStack {
|
|
if let workouts = workouts {
|
|
List {
|
|
ForEach(workouts, id:\.name) { workout in
|
|
VStack {
|
|
Text(workout.name)
|
|
.font(.title2)
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
Text(workout.description ?? "")
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
}
|
|
.contentShape(Rectangle())
|
|
.onTapGesture {
|
|
selectedItem(workout: workout)
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
Text("no workouts")
|
|
}
|
|
}.onAppear{
|
|
testParse()
|
|
}
|
|
.sheet(isPresented: $showWorkoutDetail) {
|
|
if let selectedWorkout = selectedWorkout {
|
|
let viewModel = WorkoutDetailViewModel(workout: selectedWorkout)
|
|
WorkoutDetailView(viewModel: viewModel)
|
|
}
|
|
}
|
|
}
|
|
|
|
func selectedItem(workout: Workout) {
|
|
selectedWorkout = PreviewWorkout.workout()
|
|
}
|
|
|
|
func testParse() {
|
|
if let filepath = Bundle.main.path(forResource: "AllWorkouts", ofType: "json") {
|
|
do {
|
|
let data = try Data(NSData(contentsOfFile: filepath))
|
|
let workout = try JSONDecoder().decode([Workout].self, from: data)
|
|
self.workouts = workout
|
|
} catch {
|
|
print(error)
|
|
fatalError()
|
|
}
|
|
} else {
|
|
fatalError()
|
|
}
|
|
}
|
|
}
|