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