Calories
This feature retrieves the calories count for the current day. Use an instance of the RookEventsManager
class, which includes the getTodayCalories
function to obtain the current calories burned count retrieved from Apple Health.
info
To use this method, ensure a user ID is added and the active energy and resting energy permission is granted.
Example
The bellow code shows how to use this functionality.
import SwiftUI
import RookSDK
class HomeViewModel: ObservableObject {
private let eventManager: RookEventsManager = RookEventsManager()
@Published var currentActiveCalories: String = "-"
@Published var currentRestingCalories: String = "-"
@Published var loadingCalories: Bool = false
@Published var userIdStored: String = ""
func onAppear() {
RookConnectConfigurationManager.shared.getUserId { [weak self] result in
switch result {
case .success:
self?.getCalories()
DispatchQueue.main.async {
self.userIdStored = userId
}
case .failure(let failure):
debugPrint("error user stored \(failure)")
}
}
}
private func getSteps() {
self.eventManager.getTodayCalories { [weak self] result in
switch result {
case .success(let dataCalories):
DispatchQueue.main.async {
self?.loadingCalories = false
}
self?.currentActiveCalories = "\(calories.activeCalories ?? .zero) kcal"
self?.currentRestingCalories = "\(calories.basalCalories ?? .zero) kcal"
case .failure(let error):
debugPrint("error while fetching steps \(error)")
}
}
}
}
import SwiftUI
import RookSDK
struct HomeView: View {
@StateObject var viewModel: HomeViewModel = HomeViewModel()
var body: some View {
HStack {
Text("user stored \(viewModel.userIdStored)")
VStack {
activeCaloriesView
restingCaloriesView
}
}.onAppear {
viewModel.onAppear()
}
}
var activeCaloriesView: some View {
HStack {
if viewModel.loadingCalories {
ProgressView()
} else {
Spacer()
Image(systemName: "flame.fill")
Text("active calories: \(viewModel.currentActiveCalories)")
}
}
}
var restingCaloriesView: some View {
HStack {
if viewModel.loadingCalories {
ProgressView()
} else {
Spacer()
Image(systemName: "flame.fill")
Text("resting calories: \(viewModel.currentRestingCalories)")
}
}
}
}