I know the below is incorrect, and I feel dumb that i can't solve this, but I'm new and I'm struggling to understand where to put the 'logic' (an if statement or other) for handling calculations in an app, based on the entered value and the state of a segmented picker. I have tried adding it as part of the picker, and it throws an error if I try and make totalTime a computed property like the below.
Cleary there is a basic level concept I am missing completely and have been unable to solve despite my searching. In general I'm looking for guidance on how to bring values in, do calculations on them, save them for use in other views etc. I tried to create a function to do this but that fell short as well. I am getting confused. I have found lots of good videos that teach many concepts, but can't seem to find a solution to this or similar situations of bringing data in, processing it in someway, storing it and then using it later / somewhere else in the app.
Thank you in advance for all the help.
SwitfUI
struct ContentView: View {
@AppStorage("totalTime") var totalTime: Double {
if timeSelected == "hours" {
totalTime = (Double(enteredValue) ?? 0) * multipier
} else {
totalTime = (Double(enteredValue) ?? 0)
}
}
@State var enteredValue = ""
@State var timeSelected = "hours"
let multipier = 2.0
let arrayLabel = ["hours", "minutes"]
var body: some View {
VStack {
Form {
Section(header: Text("Time Test")){
TextField("enter value", text: $enteredValue)
Picker("timeValue",selection: $timeSelected) {
ForEach(arrayLabel, id: \.self){
Text("\($0)")
}
}
.pickerStyle(.segmented)
}
}
}
}
}