Files
WerkoutIOS/iphone/Werkout_ios/Views/PlanWorkoutView.swift

112 lines
3.6 KiB
Swift

//
// PlanWorkoutView.swift
// Werkout_ios
//
// Created by Trey Tartt on 7/2/23.
//
import SwiftUI
struct PlanWorkoutView: View {
@State var selectedDate = Date()
@State private var hasError = false
@State private var errorMessage = ""
let workout: Workout
@Environment(\.dismiss) var dismiss
var addedPlannedWorkout: (() -> Void)?
var body: some View {
VStack() {
Text(workout.name)
.font(.title)
.frame(maxWidth: .infinity, alignment: .leading)
Text(selectedDate.formatted(date: .abbreviated, time: .omitted))
.font(.system(size: 28))
.bold()
.foregroundColor(Color.accentColor)
.padding()
.animation(.spring(), value: selectedDate)
Divider().frame(height: 1)
DatePicker("Select Date", selection: $selectedDate, displayedComponents: [.date])
.padding(.horizontal)
.datePickerStyle(.graphical)
Divider()
HStack {
Button(action: {
dismiss()
}, label: {
Image(systemName: "xmark.octagon.fill")
.font(.title)
.frame(maxWidth: .infinity, maxHeight: .infinity)
})
.frame(maxWidth: .infinity, alignment: .center)
.frame(height: 44)
.foregroundColor(.white)
.background(.red)
.cornerRadius(Constants.buttonRadius)
.padding()
.accessibilityLabel("Cancel planning")
.accessibilityHint("Closes this screen without planning a workout")
Button(action: {
planWorkout()
}, label: {
Image(systemName: "plus.app")
.font(.title)
.frame(maxWidth: .infinity, maxHeight: .infinity)
})
.frame(maxWidth: .infinity, alignment: .center)
.frame(height: 44)
.foregroundColor(.blue)
.background(.yellow)
.cornerRadius(Constants.buttonRadius)
.padding()
.accessibilityLabel("Plan workout")
.accessibilityHint("Adds this workout to your selected date")
}
Spacer()
}
.padding()
.alert("Unable to Plan Workout", isPresented: $hasError) {
Button("OK", role: .cancel) {}
} message: {
Text(errorMessage)
}
}
func planWorkout() {
let postData = [
"on_date": selectedDate.formatForPlannedWorkout,
"workout": workout.id
] as [String : Any]
PlanWorkoutFetchable(postData: postData).fetch(completion: { result in
switch result {
case .success(_):
DispatchQueue.main.async {
UserStore.shared.fetchPlannedWorkouts()
dismiss()
addedPlannedWorkout?()
}
case .failure(let failure):
DispatchQueue.main.async {
errorMessage = failure.localizedDescription
hasError = true
}
}
})
}
}
struct PlanWorkoutView_Previews: PreviewProvider {
static var previews: some View {
PlanWorkoutView(workout: PreviewData.workout())
}
}