86 lines
3.1 KiB
Swift
86 lines
3.1 KiB
Swift
//
|
|
// HomeViewModel.swift
|
|
// FitnessApp
|
|
//
|
|
// Created by Matt Bruce on 12/20/24.
|
|
//
|
|
import SwiftUI
|
|
|
|
@Observable
|
|
class HomeViewModel {
|
|
|
|
let healthManager = HealthManager.shared
|
|
|
|
var activities: [Activity] = []
|
|
var workouts: [Workout] = []
|
|
var calories: Int = 0
|
|
var exercise: Int = 0
|
|
var stand: Int = 0
|
|
|
|
var mockActivities = [
|
|
Activity(id: 0, title: "Today Steps", subtitle: "10,000 steps", image: "figure.walk", tintColor: .green, amount: "9,812"),
|
|
Activity(id: 1, title: "Today Steps", subtitle: "1,000 steps", image: "figure.walk", tintColor: .blue, amount: "812"),
|
|
Activity(id: 2, title: "Today Steps", subtitle: "12,000 steps", image: "figure.walk", tintColor: .purple, amount: "9,0000"),
|
|
Activity(id: 3, title: "Today Steps", subtitle: "50,000 steps", image: "figure.run", tintColor: .red, amount: "104,812")
|
|
]
|
|
|
|
var mockWorkouts = [
|
|
Workout(id: 0, title: "Running", image: "figure.run", tintColor: .green, duration: "1 hrs", date: "Aug 3", calories: "100 kcal"),
|
|
Workout(id: 1, title: "Strength Training", image: "figure.run", tintColor: .purple, duration: "1.5 hrs", date: "Aug 3", calories: "130 kcal"),
|
|
Workout(id: 2, title: "Walking", image: "figure.run", tintColor: .blue, duration: ".5 hrs", date: "Aug 3", calories: "250 kcal"),
|
|
Workout(id: 3, title: "Bike", image: "figure.run", tintColor: .red, duration: "2 hrs", date: "Aug 29", calories: "500 kcal")
|
|
]
|
|
|
|
init() {
|
|
Task {
|
|
do {
|
|
try await healthManager.requestHealthKitAccess()
|
|
fetchTodayStandHours()
|
|
fetchTodayCaloriesBurned()
|
|
fetchTodayExerciseTime()
|
|
} catch {
|
|
print(error.localizedDescription)
|
|
}
|
|
}
|
|
|
|
func fetchTodayCaloriesBurned() {
|
|
healthManager.fetchTodayCaloriesBurned() { result in
|
|
switch result {
|
|
case .success(let calories):
|
|
DispatchQueue.main.async {
|
|
self.calories = Int(calories)
|
|
}
|
|
case .failure(let failure):
|
|
print(failure.localizedDescription)
|
|
}
|
|
}
|
|
}
|
|
|
|
func fetchTodayExerciseTime() {
|
|
healthManager.fetchTodayExerciseTime() { result in
|
|
switch result {
|
|
case .success(let exercise):
|
|
DispatchQueue.main.async {
|
|
self.exercise = Int(exercise)
|
|
}
|
|
case .failure(let failure):
|
|
print(failure.localizedDescription)
|
|
}
|
|
}
|
|
}
|
|
|
|
func fetchTodayStandHours() {
|
|
healthManager.fetchTodayStandHours() { result in
|
|
switch result {
|
|
case .success(let hours):
|
|
DispatchQueue.main.async {
|
|
self.stand = Int(hours)
|
|
}
|
|
case .failure(let failure):
|
|
print(failure.localizedDescription)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|