Need help with Customizing workouts with WorkoutKit

Please reference the Sample Planner app which can be found at the below link. https://developer.apple.com/documentation/WorkoutKit/customizing-workouts-with-workoutkit. In WorkoutStore.swift, all of the values are hard coded. I would like to turn them into variables stored in @EnvironmentObject (WorkoutStoreValue). With the below code, using "singleRunStartDelay" as a trial, I can get that variable passed to the WorkStore struct only when the app is first opened, however, I have not been able to get it changed in realtime. I need help with changing the WorkoutStore struct to have the values updated in realtime. I have tried changing the func from static but that gives an error (Instance member 'createSingleRunWorkout' cannot be used on type 'WorkoutStore'; did you mean to use a value of this type instead?) I'm now learning Xcode/SwiftUI. I have been stuck for about four day trying many different ideas. Thanks

// Copyright © 2024 Apple. All rights reserved. /* The structure that returns running workout compositions. */

import HealthKit import WorkoutKit import SwiftUI import Foundation

struct WorkoutStore{ @EnvironmentObject var workoutStoreValue: WorkoutStoreValue

static func createSingleRunWorkout() -> CustomWorkout {
    let getReadyStep = WorkoutStep(goal: .open) //fixed to .open
    let singleRunDelay = WorkoutStoreValue.shared.singleRunStartDelay
    var onYourMarkStep = IntervalStep(.work)
    onYourMarkStep.step.goal = .time(Double(singleRunDelay), .seconds) //you have this much time to start
    
    var runStep = IntervalStep(.work)
    runStep.step.goal = .distance(100, .meters) //hard coded for now. Need to insert distance variable here
    runStep.step.alert = .speed(3...4, unit: .metersPerSecond, metric: .current) // Would like to insert alert variables here
    
    var block = IntervalBlock()
    block.steps = [
        onYourMarkStep,
        runStep
    ]
    block.iterations = 1 //fixed at 1. Would like to insert as a variable
    
    return CustomWorkout(activity: .running,
                         location: .outdoor,
                         displayName: "Single run mode",
                         warmup: getReadyStep,
                         blocks: [block])
}

}

  • When I remove static from the "createSingleRunWorkout" struct in WorkoutStore, the mentioned error shows up in init() in the PresentPreviewDemo struct.

Add a Comment