I am having trouble moving data between my views and my viewModel. I am working on an app for cars and need to be able to toggle the same views between the 4 corners of the car.
I did this with an enum:
enum TestCorners: String {
case LF
case RF
case LR
case RR
}
Based on the users' selection, my next screen must load some userDefaults based on which corner was selected. So I am passing the selectedCorner to the view like this: (this subview creates a button to navigate; in the app it belongs to a view with 4 buttons based on the TestCorner enum for the user to selected the desired TestCorner)
import SwiftUI
struct CornerSelectorSubView: View {
@State var showThing = false
@State var selectedCorner: TestCorners
var body: some View {
NavigationLink(destination: CornerSetupScreen(selectedCorner: selectedCorner), label: {
Text(selectedCorner.rawValue)
.font(.largeTitle)
.padding()
.padding(.vertical)
.overlay(RoundedRectangle(cornerRadius: 15)
.stroke(Color.blue, lineWidth: 2))
})
}
Once the user selects the corner they want, I need to load a "Setup Screen" with some user editable parameters that can be saved to UserDefaults. I pass the selectedCorner to the view correctly, but I'm not sure how to pass the selectedCorner to the viewModel so that it automatically loads the UserDefaults when the "Setup Screen" appears.
Here is what the "Setup Screen" view looks like: (To simplify, I only left two setup parameters)
struct CornerSetupScreen: View {
var corner: TestCorners
@StateObject var setupScreenVM =
CornerSetupDataViewModel()
var body: some View {
VStack(){
TestParameterSubView(parameterName: "Max Center-Center", parameterUnit: "in", parameterValue: $setupScreenVM.maxC2C)
}
TestParameterSubView(parameterName: "Max Nut Height", parameterUnit: "in", parameterValue: $setupScreenVM.maxNutHeight)
Button("Save"){
setupScreenVM.saveValues(corner: corner)
showAlert = true
}.alert(isPresented: $showAlert) {
Alert(
title: Text("Success"),
message: Text("Shock data saved!")
)
}
}
}
}
and the viewModel is this:
class CornerSetupDataViewModel: ObservableObject {
@Published var corner: TestCorners?
@Published var maxC2C: String = "50"
@Published var maxNutHeight: String = ""
var userDefaults = UserDefaults.standard
func populateValues(corner: TestCorners) {
maxC2C = userDefaults.string(forKey: SetupKeyValues.maxC2C.rawValue + corner.rawValue)!
maxNutHeight = userDefaults.string(forKey: SetupKeyValues.maxNutHeight.rawValue + corner.rawValue)!
sliderThickness = userDefaults.string(forKey: SetupKeyValues.sliderThickness.rawValue + corner.rawValue)!
shaftDiameter = userDefaults.string(forKey: SetupKeyValues.shaftDiameter.rawValue + corner.rawValue)!
gasShock = userDefaults.bool(forKey: SetupKeyValues.gasShock.rawValue + corner.rawValue)
canister = userDefaults.bool(forKey: SetupKeyValues.canister.rawValue + corner.rawValue)
}
func saveValues(corner: TestCorners) {
userDefaults.set(maxC2C, forKey: SetupKeyValues.maxC2C.rawValue + corner.rawValue)
userDefaults.set(maxNutHeight, forKey: SetupKeyValues.maxNutHeight.rawValue + corner.rawValue)
userDefaults.set(sliderThickness, forKey: SetupKeyValues.sliderThickness.rawValue + corner.rawValue)
userDefaults.set(gasShock, forKey: SetupKeyValues.gasShock.rawValue + corner.rawValue)
userDefaults.set(canister, forKey: SetupKeyValues.canister.rawValue + corner.rawValue)
print(maxC2C, corner.rawValue)
}
}
What is the best way to move the selectedCorner from the previous view into the viewModel so that the viewModel can then present the right data in the view?