109 lines
3.4 KiB
Swift
109 lines
3.4 KiB
Swift
//
|
|
// WatchMainViewMoel.swift
|
|
// Werkout_watch Watch App
|
|
//
|
|
// Created by Trey Tartt on 6/22/23.
|
|
//
|
|
|
|
import Foundation
|
|
import WatchConnectivity
|
|
import SwiftUI
|
|
import HealthKit
|
|
|
|
class WatchMainViewModel: NSObject, ObservableObject {
|
|
static let shared = WatchMainViewModel()
|
|
var session: WCSession
|
|
|
|
@Published var isInWorkout = false
|
|
@Published var watchPackageModel = WatchMainViewModel.defualtPackageModle
|
|
|
|
@Published var heartValue: Int?
|
|
|
|
static var defualtPackageModle: WatchPackageModel {
|
|
WatchPackageModel(currentExerciseName: "", currentTimeLeft: -1, workoutStartDate: Date())
|
|
}
|
|
|
|
let healthStore = HKHealthStore()
|
|
var hkWorkoutSession: HKWorkoutSession?
|
|
var hkBuilder: HKLiveWorkoutBuilder?
|
|
var heartRates = [Int]()
|
|
|
|
override init() {
|
|
session = WCSession.default
|
|
super.init()
|
|
|
|
session.delegate = self
|
|
session.activate()
|
|
autorizeHealthKit()
|
|
}
|
|
|
|
func autorizeHealthKit() {
|
|
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)))")
|
|
}
|
|
}
|
|
}
|
|
|
|
// actions from view
|
|
func nextExercise() {
|
|
let nextExerciseAction = WatchActions.nextExercise
|
|
let data = try! JSONEncoder().encode(nextExerciseAction)
|
|
send(data)
|
|
}
|
|
|
|
func restartExercise() {
|
|
let nextExerciseAction = WatchActions.restartExercise
|
|
let data = try! JSONEncoder().encode(nextExerciseAction)
|
|
send(data)
|
|
}
|
|
|
|
func previousExercise() {
|
|
let nextExerciseAction = WatchActions.previousExercise
|
|
let data = try! JSONEncoder().encode(nextExerciseAction)
|
|
send(data)
|
|
}
|
|
|
|
func completeWorkout() {
|
|
let nextExerciseAction = WatchActions.stopWorkout
|
|
let data = try! JSONEncoder().encode(nextExerciseAction)
|
|
send(data)
|
|
}
|
|
|
|
func pauseWorkout() {
|
|
let nextExerciseAction = WatchActions.pauseWorkout
|
|
let data = try! JSONEncoder().encode(nextExerciseAction)
|
|
send(data)
|
|
}
|
|
|
|
func dataToAction(messageData: Data) {
|
|
if let model = try? JSONDecoder().decode(PhoneToWatchActions.self, from: messageData) {
|
|
DispatchQueue.main.async {
|
|
switch model {
|
|
case .inExercise(let data):
|
|
self.watchPackageModel = data
|
|
case .reset:
|
|
self.isInWorkout = false
|
|
self.watchPackageModel = WatchMainViewModel.defualtPackageModle
|
|
self.stopWorkout(sendDetails: false)
|
|
case .endWorkout:
|
|
self.isInWorkout = false
|
|
self.watchPackageModel = WatchMainViewModel.defualtPackageModle
|
|
self.stopWorkout(sendDetails: true)
|
|
case .startWorkout:
|
|
if self.isInWorkout {
|
|
self.stopWorkout(sendDetails: false)
|
|
}
|
|
self.startWorkout()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|