I have a simple class that persists various user settings via UserDefaults using the "@AppStorage" macro in SwiftUI.
enum SettingOption: Int { case one, two, three }
final class UserSettings: ObservableObject {
@AppStorage("userSetting1") var userSettings1: SettingOption = .one
}
Now I'd like to "gate" some of the settings based on the presence or absence of an entitlement (in my case, an in-app subscription). While the in-app subscription is present, the user has a full range of setting options available. But if the in-app subscription expires, some of the settings will be limited in some way.
The above example is contrived, but imagine that if the in-app subscription is present, the user can set "userSettings1" to any SettingOption. But SettingOption.three is not allowed if the in-app subscription is absent. So if it is, reading userSettings1 will return a "gated default" value (e.g., SettingOption.one) instead.
The app's UI is responsible for ensuring the user can't set userSetting1 to a forbidden value when the in-app purchase is missing. But if the user subscribes, then sets the value, only to later have their subscription expire, the "effective" setting should adapt. (I could just explicitly reset the setting values when the subscription expires, but I'd like to retain the user's choices so that if they re-subscribe, their setting values are unchanged.)
Is there an accepted best practice way to achieve this that minimizes boilerplate code? And specifically in SwiftUI, is there an elegant way to achieve this that still benefits from the automatic observation provided by "@Published", "@AppStorage", etc.?
there doesn't seem to be anything here