What I am trying to do here is to pass the "KarvonenVal" to different View in another SwiftUI file. I have tried with .environmentObject and observableObject, but none of them worked. My goal is to display KarvonenVal on SummaryView. I appreciate your helps.
Value from
struct CalcProcess: View{
@Binding var NumAdded3:Bool
@State var Age:Int
@State var ExerciseIT:Int
@State var ConstantNumber = 220
@State var RHR:Int
func karvonen(cn: Int, rhr: Int, age: Int, ei:Double) -> Double {
return Double((cn-age-rhr)) * (ei / 10) + Double(rhr)
}
var body: some View {
let output = karvonen(cn: ConstantNumber, rhr: RHR, age: Age, ei: Double(ExerciseIT))
let roundedDouble = Double(round(1000*output)/1000)
let KarvonenVal: String = String(format: "%.1f", roundedDouble)
VStack{
Text("\(KarvonenVal)")
.foregroundStyle(.red)
}
}
}
To Here
struct SummaryView: View {
@EnvironmentObject var workoutManager: WorkoutManager
@Environment(\.dismiss) var dismiss
var body: some View {
ScrollView {
VStack(alignment: .leading) {
SummaryMetricView(title: "Total Time",
value: durationFormatter.string(from: workoutManager.workout?.duration ?? 0.0) ?? "")
.foregroundStyle(.yellow)
SummaryMetricView(title: "Avg. Heart Rate",
value: workoutManager.averageHeartRate.formatted(.number.precision(.fractionLength(0))) + " bpm")
.foregroundStyle(.red)
SummaryMetricView(title: "Target Heart Rate",
value: KarvonenVal + " bpm")
.foregroundStyle(.red)
Button("Done") {
dismiss()
}
}
.scenePadding()
}
.navigationTitle("Summary")
.navigationBarTitleDisplayMode(.inline)
}
}