complete then get health data on completion screen
This commit is contained in:
@@ -50,7 +50,7 @@ class BridgeModule: NSObject, ObservableObject {
|
||||
// workoutEndDate fills out WatchPackageModel.workoutEndDate which
|
||||
// tells the watch app to stop the workout
|
||||
public private(set) var workoutEndDate: Date?
|
||||
public private(set) var healthKitUUID: UUID?
|
||||
@Published public private(set) var healthKitUUID: UUID?
|
||||
|
||||
var audioPlayer: AVAudioPlayer?
|
||||
var avPlayer: AVPlayer?
|
||||
@@ -101,7 +101,6 @@ class BridgeModule: NSObject, ObservableObject {
|
||||
self.workoutStartDate = nil
|
||||
self.workoutEndDate = nil
|
||||
}
|
||||
sendResetToWatch()
|
||||
}
|
||||
|
||||
private func startWorkoutTimer() {
|
||||
@@ -207,12 +206,9 @@ class BridgeModule: NSObject, ObservableObject {
|
||||
self.isInWorkout = false
|
||||
|
||||
workoutEndDate = Date()
|
||||
|
||||
//if connected to watch
|
||||
if WCSession.default.isReachable {
|
||||
self.sendWorkoutCompleteToWatch()
|
||||
} else {
|
||||
completedWorkout?()
|
||||
if let completedWorkout = completedWorkout {
|
||||
completedWorkout()
|
||||
self.completedWorkout = nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -278,9 +274,11 @@ extension BridgeModule: WCSessionDelegate {
|
||||
}
|
||||
|
||||
func sendWorkoutCompleteToWatch() {
|
||||
let model = PhoneToWatchActions.endWorkout
|
||||
let data = try! JSONEncoder().encode(model)
|
||||
send(data)
|
||||
if WCSession.default.isReachable {
|
||||
let model = PhoneToWatchActions.endWorkout
|
||||
let data = try! JSONEncoder().encode(model)
|
||||
send(data)
|
||||
}
|
||||
}
|
||||
|
||||
func sendCurrentExerciseToWatch() {
|
||||
@@ -313,9 +311,10 @@ extension BridgeModule: WCSessionDelegate {
|
||||
nextExercise()
|
||||
playFinished()
|
||||
case .workoutComplete(let data):
|
||||
let model = try! JSONDecoder().decode(WatchFinishWorkoutModel.self, from: data)
|
||||
healthKitUUID = model.healthKitUUID
|
||||
completedWorkout?()
|
||||
DispatchQueue.main.async {
|
||||
let model = try! JSONDecoder().decode(WatchFinishWorkoutModel.self, from: data)
|
||||
self.healthKitUUID = model.healthKitUUID
|
||||
}
|
||||
case .restartExercise:
|
||||
restartExercise()
|
||||
case .previousExercise:
|
||||
|
||||
@@ -10,20 +10,19 @@ 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 postData: [String: Any]
|
||||
let healthKitHelper = HealthKitHelper()
|
||||
let workout: Workout
|
||||
let completedWorkoutDismissed: ((Bool) -> Void)?
|
||||
|
||||
@Environment(\.dismiss) var dismiss
|
||||
|
||||
var body: some View {
|
||||
ZStack {
|
||||
if isUploading {
|
||||
@@ -105,6 +104,9 @@ struct CompletedWorkoutView: View {
|
||||
.padding([.leading, .trailing])
|
||||
}
|
||||
.onAppear{
|
||||
bridgeModule.sendWorkoutCompleteToWatch()
|
||||
}
|
||||
.onChange(of: bridgeModule.healthKitUUID, perform: { healthKitUUID in
|
||||
if let healthKitUUID = healthKitUUID {
|
||||
gettingHealthKitData = true
|
||||
healthKitHelper.getDetails(forHealthKitUUID: healthKitUUID,
|
||||
@@ -113,7 +115,7 @@ struct CompletedWorkoutView: View {
|
||||
gettingHealthKitData = false
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func topViews() -> some View {
|
||||
@@ -167,7 +169,7 @@ struct CompletedWorkoutView: View {
|
||||
var _postBody = postBody
|
||||
_postBody["difficulty"] = difficulty
|
||||
_postBody["notes"] = notes
|
||||
if let healthKitUUID = healthKitUUID {
|
||||
if let healthKitUUID = bridgeModule.healthKitUUID {
|
||||
_postBody["health_kit_workout_uuid"] = healthKitUUID.uuidString
|
||||
}
|
||||
|
||||
@@ -204,7 +206,6 @@ struct CompletedWorkoutView_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
CompletedWorkoutView(postData: CompletedWorkoutView_Previews.postBody,
|
||||
workout: workout,
|
||||
healthKitUUID: nil,
|
||||
completedWorkoutDismissed: { _ in })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ struct ActionsView: View {
|
||||
var workout: Workout
|
||||
@Environment(\.dismiss) var dismiss
|
||||
var showAddToCalendar: Bool
|
||||
var startWorkoutAction: (() -> Void)
|
||||
|
||||
var body: some View {
|
||||
HStack {
|
||||
@@ -45,7 +46,7 @@ struct ActionsView: View {
|
||||
}
|
||||
|
||||
Button(action: {
|
||||
startWorkout()
|
||||
startWorkoutAction()
|
||||
}, label: {
|
||||
Image(systemName: "arrowtriangle.forward.fill")
|
||||
.font(.title)
|
||||
@@ -94,15 +95,11 @@ struct ActionsView: View {
|
||||
func nextExercise() {
|
||||
bridgeModule.nextExercise()
|
||||
}
|
||||
|
||||
func startWorkout() {
|
||||
bridgeModule.start(workout: workout)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
struct ActionsView_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
ActionsView(workout: PreviewData.workout(), showAddToCalendar: true)
|
||||
ActionsView(workout: PreviewData.workout(), showAddToCalendar: true, startWorkoutAction: {})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,7 +95,9 @@ struct WorkoutDetailView: View {
|
||||
bridgeModule.completeWorkout()
|
||||
}, planWorkout: { workout in
|
||||
workoutToPlan = workout
|
||||
}, workout: workout, showAddToCalendar: viewModel.isPreview)
|
||||
}, workout: workout, showAddToCalendar: viewModel.isPreview, startWorkoutAction: {
|
||||
startWorkout(workout: workout)
|
||||
})
|
||||
.frame(height: 44)
|
||||
|
||||
}
|
||||
@@ -104,7 +106,6 @@ struct WorkoutDetailView: View {
|
||||
case .completedWorkout(let data):
|
||||
CompletedWorkoutView(postData: data,
|
||||
workout: workout,
|
||||
healthKitUUID: bridgeModule.healthKitUUID,
|
||||
completedWorkoutDismissed: { uploaded in
|
||||
if uploaded {
|
||||
dismiss()
|
||||
@@ -157,13 +158,6 @@ struct WorkoutDetailView: View {
|
||||
avPlayer.play()
|
||||
}
|
||||
}
|
||||
|
||||
bridgeModule.completedWorkout = {
|
||||
if let workoutData = createWorkoutData() {
|
||||
presentedSheet = .completedWorkout(workoutData)
|
||||
bridgeModule.resetCurrentWorkout()
|
||||
}
|
||||
}
|
||||
}
|
||||
.onReceive(NotificationCenter.default.publisher(
|
||||
for: UIScene.willEnterForegroundNotification)) { _ in
|
||||
@@ -171,6 +165,17 @@ struct WorkoutDetailView: View {
|
||||
}
|
||||
}
|
||||
|
||||
func startWorkout(workout: Workout) {
|
||||
bridgeModule.completedWorkout = {
|
||||
if let workoutData = createWorkoutData() {
|
||||
presentedSheet = .completedWorkout(workoutData)
|
||||
bridgeModule.resetCurrentWorkout()
|
||||
}
|
||||
}
|
||||
|
||||
bridgeModule.start(workout: workout)
|
||||
}
|
||||
|
||||
func createWorkoutData() -> [String:Any]? {
|
||||
guard let workoutid = bridgeModule.currentExerciseInfo.workout?.id,
|
||||
let startTime = bridgeModule.workoutStartDate?.timeFormatForUpload,
|
||||
|
||||
Reference in New Issue
Block a user