My app has paid features. I have a Toggle that the user can switch. If the user has not paid, then when the user tries to toggle the Toggle a sheet should be brought up (and the toggle should not be activated). If the user has paid, then the toggle can be toggled on and off without problems.
I can't understand how a simple struct (PaidFeature) can have access to an observable object (Model). How do I code that in SwiftUI?
class Model: ObservableObject {
@Published var hasUserPaid: Bool = false
}
struct PaidFeature {
var isEnabled = false
}
struct ContentView: View {
@State private var feature = PaidFeature()
@EnvironmentObject var model: Model
var body: some View {
Toggle(isOn: self.$feature.isEnabled) {
Text("Hello, World!")
}
}
}