This commit is contained in:
Trey t
2023-07-02 16:37:45 -05:00
parent 939ea16716
commit fbc1ada8c9
14 changed files with 535 additions and 24 deletions

View File

@@ -9,10 +9,27 @@ import Foundation
import SwiftUI
struct AllWorkoutsView: View {
enum MainViews: Int, CaseIterable {
case AllWorkout = 0
case MyWorkouts
var title: String {
switch self {
case .AllWorkout:
return "All Workouts"
case .MyWorkouts:
return "Planned Workouts"
}
}
}
@State var workouts: [Workout]?
var bridgeModule = BridgeModule.shared
@State public var needsUpdating: Bool = true
@ObservedObject var dataStore = DataStore.shared
@State private var showWorkoutDetail = false
@State private var selectedWorkout: Workout? {
didSet {
@@ -22,27 +39,32 @@ struct AllWorkoutsView: View {
}
@State private var showLoginView = false
@State private var selectedSegment: MainViews = .AllWorkout
@State var selectedDate: Date = Date()
let pub = NotificationCenter.default.publisher(for: NSNotification.Name("CreatedNewWorkout"))
var body: some View {
ZStack {
if let workouts = workouts,
let _ = DataStore.shared.allExercise,
let _ = DataStore.shared.allMuscles,
let _ = DataStore.shared.allEquipment {
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)
if let workouts = workouts {
if dataStore.status == .loading {
ProgressView()
.progressViewStyle(.circular)
} else {
VStack {
Picker("", selection: $selectedSegment) {
ForEach(MainViews.allCases, id: \.self) { viewType in
Text(viewType.title)
}
}
.contentShape(Rectangle())
.onTapGesture {
selectedWorkout = workout
.pickerStyle(.segmented)
.padding()
switch selectedSegment {
case .AllWorkout:
allWorkoutView(workouts: workouts)
case .MyWorkouts:
plannedWorkout(workouts: UserStore.shared.plannedWorkouts)
}
}
}
@@ -51,6 +73,7 @@ struct AllWorkoutsView: View {
.progressViewStyle(.circular)
}
}.onAppear{
// UserStore.shared.logout()
maybeUpdateShit()
}
.sheet(item: $selectedWorkout) { item in
@@ -69,10 +92,70 @@ struct AllWorkoutsView: View {
}
}
func allWorkoutView(workouts: [Workout]) -> some View {
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
}
}
}
}
func plannedWorkout(workouts: [PlannedWorkout]) -> some View {
List {
ForEach(workouts, id:\.workout.name) { plannedWorkout in
HStack {
VStack(alignment: .leading) {
Text(plannedWorkout.onDate.plannedDate?.weekDay ?? "-")
.font(.title)
Text(plannedWorkout.onDate.plannedDate?.monthString ?? "-")
.font(.title)
Text(plannedWorkout.onDate.plannedDate?.dateString ?? "-")
.font(.title)
}
Divider()
VStack {
Text(plannedWorkout.workout.name)
.font(.title)
.frame(maxWidth: .infinity, alignment: .leading)
Text(plannedWorkout.workout.description ?? "")
.font(.body)
.frame(maxWidth: .infinity, alignment: .leading)
Text(plannedWorkout.onDate)
.font(.footnote)
.frame(maxWidth: .infinity, alignment: .leading)
}
.contentShape(Rectangle())
.onTapGesture {
}
}
}
}
}
func maybeUpdateShit() {
if UserStore.shared.token != nil{
if UserStore.shared.plannedWorkouts.isEmpty {
UserStore.shared.fetchPlannedWorkouts()
}
if needsUpdating {
DataStore.shared.fetchAllData()
dataStore.fetchAllData()
AllWorkoutFetchable().fetch(completion: { result in
needsUpdating = false