This commit is contained in:
Trey t
2023-08-12 13:41:53 -05:00
parent 4824fbc0fe
commit 2aa83f12f2
6 changed files with 89 additions and 6 deletions

View File

@@ -0,0 +1,40 @@
//
// AudioQueue.swift
// Werkout_ios
//
// Created by Trey Tartt on 8/12/23.
//
import Foundation
enum AudioType {
case shortBeep
case finishBeep
case remoteURL(URL)
}
struct AudioQueue: Codable, Identifiable, Equatable {
var id = UUID()
let playAt: Int
let audioURL: String
enum CodingKeys: String, CodingKey {
case playAt = "play_at"
case audioURL = "audio_url"
}
var audioType: AudioType {
if audioURL == "short_beep" {
return .shortBeep
} else if audioURL == "long_beep" {
return .finishBeep
} else {
if let url = URL(string: BaseURLs.currentBaseURL + "/media/" + audioURL) {
return .remoteURL(url)
} else {
return .shortBeep
}
}
}
}

View File

@@ -21,6 +21,7 @@ struct SupersetExercise: Identifiable, Codable, Equatable, Hashable {
let order, superset: Int?
let uniqueID: String?
let description: String?
let audioQueues: [AudioQueue]?
enum CodingKeys: String, CodingKey {
case workout, exercise, weight, reps, duration, order, superset, id, description
@@ -28,6 +29,7 @@ struct SupersetExercise: Identifiable, Codable, Equatable, Hashable {
case weightAudio = "weight_audio"
case createdAt = "created_at"
case uniqueID = "unique_id"
case audioQueues = "audio_queues"
}
public func hash(into hasher: inout Hasher) {
@@ -49,7 +51,7 @@ struct Exercise: Identifiable, Codable, Equatable {
let isDistance, isDuration, isReps: Bool
let jointsUsed, movementPatterns, equipmentRequired, muscleGroups: String
let synonyms: String?
enum CodingKeys: String, CodingKey {
case id, muscles, equipment
case audioURL = "audio_url"

View File

@@ -12,6 +12,6 @@ enum BaseURLs: String {
case dev = "https://dev.werkout.fitness"
static var currentBaseURL: String {
return BaseURLs.dev.rawValue
return BaseURLs.local.rawValue
}
}

View File

@@ -54,6 +54,7 @@ class BridgeModule: NSObject, ObservableObject {
public private(set) var heartRates: [Int]?
var audioPlayer: AVAudioPlayer?
var avPlayer: AVPlayer?
func start(workout: Workout) {
currentExerciseInfo.complete = {
@@ -130,11 +131,23 @@ class BridgeModule: NSObject, ObservableObject {
}
@objc func updateCurrentExerciseTimer() {
if currentExerciseTimeLeft > 0 {
if currentExerciseTimeLeft > 1 {
currentExerciseTimeLeft -= 1
if currentExerciseTimeLeft <= 3 {
playBeep()
if let currentExercise = currentExerciseInfo.allSupersetExecercise, let audioQueues = currentExercise.audioQueues {
if let audioQueue = audioQueues.first(where: {
$0.playAt == currentExerciseTimeLeft
}) {
switch audioQueue.audioType {
case .shortBeep:
playBeep()
case .finishBeep:
playFinished()
case .remoteURL(let url):
playRemoteAudio(fromURL: url)
}
}
}
sendCurrentExerciseToWatch()
} else {
@@ -154,7 +167,6 @@ class BridgeModule: NSObject, ObservableObject {
func nextExercise() {
if let nextSupersetExercise = currentExerciseInfo.nextExercise {
updateCurrent(exercise: nextSupersetExercise)
playFinished()
} else {
completeWorkout()
}
@@ -205,6 +217,23 @@ class BridgeModule: NSObject, ObservableObject {
}
}
func playRemoteAudio(fromURL url: URL) {
#if os(iOS)
let playerItem = AVPlayerItem(url: url)
do {
try AVAudioSession.sharedInstance().setCategory(.playback,
mode: .default,
options: [.mixWithOthers])
try AVAudioSession.sharedInstance().setActive(true)
avPlayer = AVPlayer(playerItem: playerItem)
avPlayer?.play()
} catch {
print("ERROR")
}
#endif
}
func playBeep() {
#if os(iOS)
if let path = Bundle.main.path(forResource: "short_beep", ofType: "m4a") {
@@ -283,6 +312,7 @@ extension BridgeModule: WCSessionDelegate {
switch model {
case .nextExercise:
nextExercise()
playFinished()
case .workoutComplete(let data):
let model = try! JSONDecoder().decode(WatchFinishWorkoutModel.self, from: data)
totalCaloire = Float(model.totalBurnedEnergery)

View File

@@ -33,6 +33,11 @@ class CurrentWorkoutInfo {
return exercise
}
var allSupersetExecercise: SupersetExercise? {
let obj = workout?.allSupersetExecercise?[allSupersetExecerciseIndex]
return obj
}
var nextExercise: SupersetExercise? {
guard let workout = workout else { return nil }
guard let supersets = workout.supersets else { return nil }