Steps
This feature retrieves the step count for the current day. Use an instance of the RookEventsManager
class, which includes the getTodayStepCount
function to obtain the current step count.
info
To use this method, ensure a user ID is added and the steps 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 currentSteps: Int?
@Published var loadingSteps: Bool = false
@Published var userIdStored: String = ""
func onAppear() {
RookConnectConfigurationManager.shared.getUserId { [weak self] result in
switch result {
case .success:
self?.getSteps()
DispatchQueue.main.async {
self.userIdStored = userId
}
case .failure(let failure):
debugPrint("error user stored \(failure)")
}
}
}
private func getSteps() {
self.eventManager.getTodayStepCount { [weak self] result in
DispatchQueue.main.async {
self?.loadingSteps = false
switch result {
case .success(let steps):
self?.currentSteps = steps
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)")
if let steps: Int = viewModel.currentSteps {
if viewModel.loadingSteps {
ProgressView()
} else {
Spacer()
Image(systemName: "figure.walk")
Text("current steps: \(steps)")
}
}
}.onAppear {
viewModel.onAppear()
}
}
}