WIP
This commit is contained in:
68
Werkout_ios/Views/AllWorkoutsView.swift
Normal file
68
Werkout_ios/Views/AllWorkoutsView.swift
Normal file
@@ -0,0 +1,68 @@
|
||||
//
|
||||
// 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)
|
||||
Text(workout.description ?? "")
|
||||
}
|
||||
.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()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user