Files
WerkoutIOS/Werkout_ios/Views/AllWorkouts/AllWorkoutsView.swift
Trey t 24ee992f93 WIP
2023-06-25 10:59:20 -05:00

81 lines
2.6 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]?
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
}
}
let pub = NotificationCenter.default.publisher(for: NSNotification.Name("CreatedNewWorkout"))
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 {
selectedWorkout = workout
}
}
}
} else {
Text("no workouts")
}
}.onAppear{
if needsUpdating {
UserStore.shared.login(completion: { success in
if success {
DataStore.shared.fetchAllData()
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")
}
})
} else {
fatalError("shit broke")
}
})
}
}
.sheet(item: $selectedWorkout) { item in
let viewModel = WorkoutDetailViewModel(workout: item)
WorkoutDetailView(viewModel: viewModel)
}
.onReceive(pub) { (output) in
self.needsUpdating = true
}
}
}