This commit is contained in:
Trey t
2023-06-20 20:49:32 -05:00
parent 55f0926a08
commit 695459ac06
15 changed files with 287 additions and 111 deletions

View File

@@ -10,15 +10,19 @@ import SwiftUI
struct AllWorkoutsView: View {
@State var workouts: [Workout]?
@EnvironmentObject var bridgeModule: BridgeModule
var bridgeModule = BridgeModule.shared
@State public var needsUpdating: Bool = true
@State private var showWorkoutDetail = false
@State private var selectedWorkout: Workout? {
didSet {
bridgeModule.currentWorkout = selectedWorkout
showWorkoutDetail = true
bridgeModule.currentWorkout = self.selectedWorkout
}
}
let pub = NotificationCenter.default.publisher(for: NSNotification.Name("CreatedNewWorkout"))
var body: some View {
ZStack {
if let workouts = workouts {
@@ -33,7 +37,7 @@ struct AllWorkoutsView: View {
}
.contentShape(Rectangle())
.onTapGesture {
selectedItem(workout: workout)
selectedWorkout = workout
}
}
}
@@ -41,16 +45,19 @@ struct AllWorkoutsView: View {
Text("no workouts")
}
}.onAppear{
AllWorkoutFetchable().fetch(completion: { result in
switch result {
case .success(let model):
DispatchQueue.main.async {
self.workouts = model
if needsUpdating {
AllWorkoutFetchable().fetch(completion: { result in
needsUpdating = false
switch result {
case .success(let model):
DispatchQueue.main.async {
self.workouts = model
}
case .failure(let failure):
fatalError("shit broke")
}
case .failure(let failure):
fatalError("shit broke")
}
})
})
}
}
.sheet(isPresented: $showWorkoutDetail) {
if let selectedWorkout = selectedWorkout {
@@ -58,12 +65,11 @@ struct AllWorkoutsView: View {
WorkoutDetailView(viewModel: viewModel)
}
}
.onReceive(pub) { (output) in
self.needsUpdating = true
}
}
func selectedItem(workout: Workout) {
selectedWorkout = workout
}
func testParse() {
if let filepath = Bundle.main.path(forResource: "AllWorkouts", ofType: "json") {
do {