Hi again @Claude31
turns out I was curious enough about this to make time to investigate it sooner.
In my sample code, neither the View's willSet/didSet nor the class's willSet/didSet are getting called.
import SwiftUI
struct ContentView: View {
@AppStorage("extended") var extended: Bool = true {
willSet {
print("ConteentView willSet")
}
didSet {
print("ContentView didSet")
}
}
var body: some View {
VStack {
Toggle("Extended", isOn: $extended)
Text(LanguageManager.shared.summary)
}
.padding()
}
}
#Preview {
ContentView()
}
class LanguageManager {
static let shared = LanguageManager()
var items: [String] = ["basic", "list"]
var summary: String {
return items.reduce("") {
return "\($0)\n\($1)"
}
}
@AppStorage("extended") var extended: Bool = true {
willSet {
print("LanguageManager willSet")
}
didSet {
print("LanguageManager didSet")
updateItems()
}
}
func updateItems() {
if extended {
items = ["much", "more", "than", "basic", "list"]
} else {
items = ["basic", "list"]
}
}
}
**
Update**: I now see I should have followed the link you included. Sorry about not doing that before answering.
However this answer suggests that when a user updates the UI, the @AppStorage var in my class will most definitely not be getting updated. And that is the one that I'm hoping will be able to be notified when 'somebody else' updates one of the UserDefault values.
I want to define multiple UserDefault values that can be updated via settings/prefs UI.
and then when any of these values change, LanguageManager will be notified (pub/sub?) of the change, and run updateItems()