I'm using this answer to init an ObservableObject with the horizontalSizeClass: https://stackoverflow.com/a/73339015 . What this achieves is to set the blue rectangle to visible on regular sized devices and invisible on compact devices on init. So far so good.
The problem is that the Settings object will of course be re-initalized when the size class changes (for example when rotating an iPhone 13 Pro Max between portrait and landscape). For the user this can result in the blue rectangle appearing or disappearing. What I can't figure out is how I can init isVisible based on the horizontal size class once, but not create a new instance when the horizontal size class changes later to keep the current value of isVisible. Is this even possible?
Code:
struct ContentView: View {
@Environment(\.horizontalSizeClass) var horizontalSizeClass
struct MainView: View {
@EnvironmentObject var settings: Settings
var body: some View {
VStack {
Button("Toggle Visibility") {
settings.isVisible.toggle()
}
Rectangle()
.frame(width: 100, height: 100)
.foregroundColor(.blue)
.opacity(settings.isVisible ? 1 : 0)
}
.animation(.linear(duration: 2.0), value: settings.isVisible)
}
}
var body: some View {
MainView()
.environmentObject(
Settings(isVisible: horizontalSizeClass == .regular)
)
}
}
class Settings: ObservableObject {
@Published var isVisible: Bool
init(isVisible: Bool) {
self.isVisible = isVisible
}
}
setting, and initialize as a@Stateobject?