davidsteppenbeck / ObservableUserDefault

A Swift macro that adds accessor blocks to properties in `Observable` classes that read and write values in `UserDefaults`.
MIT License
80 stars 11 forks source link

Observe changes of User Defaults values across different processes #4

Open LeoNatan opened 7 months ago

LeoNatan commented 7 months ago

Hello,

This library misses a major functionality of “observability”, which is notifying of changes that occur to the observed value from different processes.

Cheers

Iomegan commented 1 day ago

Are you sure?

.onChange() works just as expected for me.

@Observable
class SettingsManager {
    static let shared = SettingsManager()

    @ObservableUserDefault(.init(key: "TOGGLE_TEST", defaultValue: false, store: .standard))
    @ObservationIgnored
    var testToggle: Bool
}

struct DarkModeToggleView: View {
    @State var settingsManager: SettingsManager = .shared

    var body: some View {
        Toggle("Test Toggle", isOn: $settingsManager.testToggle)
            .onChange(of: settingsManager.testToggle) { oldValue, newValue in
                print("observability: \(oldValue) - \(newValue)") // <- HERE
            }
    }
}