This commit is contained in:
Trey t
2023-07-03 21:47:51 -05:00
parent 85dfc0a931
commit 38be95ea76
7 changed files with 69 additions and 11 deletions

View File

@@ -8,6 +8,7 @@
import Foundation
import WatchConnectivity
import AVFoundation
import HealthKit
enum WatchActions: Codable {
case nextExercise
@@ -40,6 +41,8 @@ class BridgeModule: NSObject, ObservableObject {
public private(set) var totalCaloire: Float?
public private(set) var heartRates: [Int]?
var audioPlayer: AVAudioPlayer?
func start(workout: Workout) {
self.currentWorkout = workout
currentWorkoutRunTimeInSeconds = 0
@@ -52,11 +55,12 @@ class BridgeModule: NSObject, ObservableObject {
startWorkoutTimer()
workoutStartDate = Date()
isInWorkout = true
if WCSession.isSupported() {
WCSession.default.delegate = self
WCSession.default.activate()
}
}
func goToExerciseAt(index: Int) {
@@ -178,13 +182,37 @@ class BridgeModule: NSObject, ObservableObject {
func playBeep() {
#if os(iOS)
AudioServicesPlaySystemSound(SystemSoundID(1052))
if let path = Bundle.main.path(forResource: "short_beep", ofType: "m4a") {
do {
try AVAudioSession.sharedInstance().setCategory(.playback,
mode: .default,
options: [.mixWithOthers, .allowAirPlay])
try AVAudioSession.sharedInstance().setActive(true)
audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: path))
audioPlayer?.play()
} catch {
print("ERROR")
}
}
#endif
}
func playFinished() {
#if os(iOS)
AudioServicesPlaySystemSound(SystemSoundID(1070))
if let path = Bundle.main.path(forResource: "long_beep", ofType: "m4a") {
do {
try AVAudioSession.sharedInstance().setCategory(.playback,
mode: .default,
options: [.mixWithOthers, .allowAirPlay])
try AVAudioSession.sharedInstance().setActive(true)
audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: path))
audioPlayer?.play()
} catch {
print("ERROR")
}
}
#endif
}
}
@@ -221,6 +249,16 @@ extension BridgeModule: WCSessionDelegate {
print("inactive")
case .activated:
print("activated")
#if os(iOS)
let workoutConfiguration = HKWorkoutConfiguration()
workoutConfiguration.activityType = .traditionalStrengthTraining
workoutConfiguration.locationType = .indoor
if WCSession.isSupported(), WCSession.default.activationState == .activated, WCSession.default.isWatchAppInstalled {
HKHealthStore().startWatchApp(with: workoutConfiguration, completion: { (success, error) in
print(error.debugDescription)
})
}
#endif
}
}
#if os(iOS)