WIP
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
|
||||
import Foundation
|
||||
import SwiftUI
|
||||
import HealthKit
|
||||
|
||||
enum MainViewTypes: Int, CaseIterable {
|
||||
case AllWorkout = 0
|
||||
@@ -27,7 +28,7 @@ struct AllWorkoutsView: View {
|
||||
@State var isUpdating = false
|
||||
@State var workouts: [Workout]?
|
||||
@State var uniqueWorkoutUsers: [RegisteredUser]?
|
||||
|
||||
let healthStore = HKHealthStore()
|
||||
var bridgeModule = BridgeModule.shared
|
||||
@State public var needsUpdating: Bool = true
|
||||
|
||||
@@ -88,6 +89,7 @@ struct AllWorkoutsView: View {
|
||||
}
|
||||
}.onAppear{
|
||||
// UserStore.shared.logout()
|
||||
authorizeHealthKit()
|
||||
maybeUpdateShit()
|
||||
}
|
||||
.sheet(item: $selectedWorkout) { item in
|
||||
@@ -178,6 +180,21 @@ struct AllWorkoutsView: View {
|
||||
showLoginView = true
|
||||
}
|
||||
}
|
||||
|
||||
func authorizeHealthKit() {
|
||||
let healthKitTypes: Set = [
|
||||
HKObjectType.quantityType(forIdentifier: .heartRate)!,
|
||||
HKObjectType.quantityType(forIdentifier: .activeEnergyBurned)!,
|
||||
HKObjectType.quantityType(forIdentifier: .oxygenSaturation)!,
|
||||
HKQuantityType.workoutType()
|
||||
]
|
||||
|
||||
healthStore.requestAuthorization(toShare: healthKitTypes, read: healthKitTypes) { (succ, error) in
|
||||
if !succ {
|
||||
fatalError("Error requesting authorization from health store: \(String(describing: error)))")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct AllWorkoutsView_Previews: PreviewProvider {
|
||||
|
||||
@@ -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 })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,7 +102,10 @@ struct WorkoutDetailView: View {
|
||||
.sheet(item: $presentedSheet) { item in
|
||||
switch item {
|
||||
case .completedWorkout(let data):
|
||||
CompletedWorkoutView(postData: data, workout: workout, completedWorkoutDismissed: { uploaded in
|
||||
CompletedWorkoutView(postData: data,
|
||||
workout: workout,
|
||||
healthKitUUID: bridgeModule.healthKitUUID,
|
||||
completedWorkoutDismissed: { uploaded in
|
||||
if uploaded {
|
||||
dismiss()
|
||||
}
|
||||
@@ -179,9 +182,7 @@ struct WorkoutDetailView: View {
|
||||
"workout_start_time": startTime,
|
||||
"workout_end_time": endTime,
|
||||
"workout": workoutid,
|
||||
"total_time": bridgeModule.currentWorkoutRunTimeInSeconds,
|
||||
"total_calories": bridgeModule.totalCaloire ?? -1,
|
||||
"heart_rates": bridgeModule.heartRates ?? [Int]()
|
||||
"total_time": bridgeModule.currentWorkoutRunTimeInSeconds
|
||||
] as [String : Any]
|
||||
|
||||
return postBody
|
||||
|
||||
Reference in New Issue
Block a user