This commit is contained in:
Trey t
2023-08-16 00:16:38 -05:00
parent c0fdca9996
commit b501f66bf3
10 changed files with 234 additions and 81 deletions

View File

@@ -6,17 +6,23 @@
//
import SwiftUI
import HealthKit
struct CompletedWorkoutView: View {
@ObservedObject var bridgeModule = BridgeModule.shared
var postData: [String: Any]
let healthKitHelper = HealthKitHelper()
let workout: Workout
let healthKitUUID: UUID?
@State var healthKitWorkoutData: HealthKitWorkoutData?
@Environment(\.dismiss) var dismiss
@State var difficulty: Float = 0
@State var notes: String = ""
let completedWorkoutDismissed: ((Bool) -> Void)?
@State var isUploading: Bool = false
@State var gettingHealthKitData: Bool = false
var body: some View {
ZStack {
@@ -29,11 +35,40 @@ struct CompletedWorkoutView: View {
Divider()
HStack {
calsBurned()
.frame(maxWidth: .infinity)
heartRates()
.frame(maxWidth: .infinity)
if let calsBurned = healthKitWorkoutData?.caloriesBurned {
HStack {
HStack {
Image(systemName: "flame.fill")
.foregroundColor(.orange)
.font(.title)
VStack {
Text("\(calsBurned, specifier: "%.0f")")
}
}
.frame(maxWidth: .infinity)
}
if let minHeart = healthKitWorkoutData?.minHeartRate,
let maxHeart = healthKitWorkoutData?.maxHeartRate,
let avgHeart = healthKitWorkoutData?.avgHeartRate {
VStack {
HStack {
Image(systemName: "heart")
.foregroundColor(.red)
.font(.title)
VStack {
HStack {
Text("\(minHeart, specifier: "%.0f")")
Text("-")
Text("\(maxHeart, specifier: "%.0f")")
}
Text("\(avgHeart, specifier: "%.0f")")
}
}
}
.frame(maxWidth: .infinity)
}
}
}
rateWorkout()
@@ -48,6 +83,11 @@ struct CompletedWorkoutView: View {
.overlay(RoundedRectangle(cornerRadius: 16).stroke(Color(uiColor: .clear))).background(Color(uiColor: .init(red: 200/255, green: 200/255, blue: 200/255, alpha: 0.2)))
.cornerRadius(8)
if gettingHealthKitData {
ProgressView("Getting HealthKit data")
.padding()
}
Spacer()
Button("Upload", action: {
@@ -64,6 +104,16 @@ struct CompletedWorkoutView: View {
}
.padding([.leading, .trailing])
}
.onAppear{
if let healthKitUUID = healthKitUUID {
gettingHealthKitData = true
healthKitHelper.getDetails(forHealthKitUUID: healthKitUUID,
completion: { healthKitWorkoutData in
self.healthKitWorkoutData = healthKitWorkoutData
gettingHealthKitData = false
})
}
}
}
func topViews() -> some View {
@@ -83,26 +133,13 @@ struct CompletedWorkoutView: View {
}
}
func calsBurned() -> some View {
VStack {
if let cals = postData["total_calories"] as? Float {
HStack {
Image(systemName: "flame.fill")
.foregroundColor(.orange)
.font(.title)
VStack {
Text("\(cals, specifier: "%.0f")")
}
}
}
}
}
func rateWorkout() -> some View {
VStack {
Divider()
HStack {
Text("No Rate")
.foregroundColor(.black)
Text("Easy")
.foregroundColor(.green)
Spacer()
@@ -112,7 +149,7 @@ struct CompletedWorkoutView: View {
ZStack {
LinearGradient(
gradient: Gradient(colors: [.green, .red]),
gradient: Gradient(colors: [.black, .green, .red]),
startPoint: .leading,
endPoint: .trailing
)
@@ -125,35 +162,15 @@ struct CompletedWorkoutView: View {
}
}
}
func heartRates() -> some View {
VStack {
if let heartRates = postData["heart_rates"] as? [Int],
heartRates.count > 0 {
let avg = heartRates.reduce(0, +)/heartRates.count
HStack {
Image(systemName: "heart")
.foregroundColor(.red)
.font(.title)
VStack {
HStack {
Text("\(heartRates.min() ?? 0)")
Text("-")
Text("\(heartRates.max() ?? 0)")
}
Text("\(avg)")
}
}
}
}
}
func upload(postBody: [String: Any]) {
var _postBody = postBody
_postBody["difficulty"] = difficulty
_postBody["notes"] = notes
if let healthKitUUID = healthKitUUID {
_postBody["health_kit_workout_uuid"] = healthKitUUID.uuidString
}
CompleteWorkoutFetchable(postData: _postBody).fetch(completion: { result in
switch result {
case .success(_):
@@ -163,6 +180,9 @@ struct CompletedWorkoutView: View {
completedWorkoutDismissed?(true)
}
case .failure(let failure):
DispatchQueue.main.async {
self.isUploading = false
}
print(failure)
}
})
@@ -184,6 +204,7 @@ struct CompletedWorkoutView_Previews: PreviewProvider {
static var previews: some View {
CompletedWorkoutView(postData: CompletedWorkoutView_Previews.postBody,
workout: workout,
healthKitUUID: nil,
completedWorkoutDismissed: { _ in })
}
}