I am storing the list's style into the ViewModel as shown in the code below.
class Model: ObservableObject {
@Published var items = ["A", "B", "C", "D"]
@Published var currentStyle = PlainListStyle()
}
struct ContainerView: View {
@ObservedObject var model = Model()
var body: some View {
VStack {
Spacer()
List {
ForEach(model.items, id: \.self) { item in
Text(item)
}
}
.listStyle(model.currentStyle)
}
.background(.green)
}
}
My problem is how to pass the current style as parameter of the ViewModel init function.
I would like to have something like:
class Model: ObservableObject {
@Published var items = ["A", "B", "C", "D"]
@Published var currentStyle: ?
init(currentStyle: ?) {
self.currentStyle = currentStyle
}
func changeToStyle(newStyle: ?) {
self.currentStyle = newStyle
}
}
I tried this:
class Model<S: ListStyle>: ObservableObject {
@Published var items = ["A", "B", "C", "D"]
@Published var currentStyle: S
init(currentStyle: S) {
self.currentStyle = currentStyle
}
//
// func changeStyle(newStyle: S) {
// self.currentStyle = newStyle
// }
}
which allows to configure the style at model creation but not to change the style later.