The real truth is that I don't know what I am doing. I am trying to trap the change of orientation event. I have been strongly advised to use "Size Classes" rather than "Landscape" or "Portrait". Apparently, this is what Apple recommends.
I have tried two or three ways to do this, and my closest effort to get it working has run me into an Issue (code below) that I don't know how to solve. I believe I have to use the @Evinroment macros to pick up the current size classes (@Environment(\.horizontalSizeClas) var .... and @Environment(\.verticalSizeClass) var ....) and these have to go inside a "View", I think.
But I know when I get to setting up the Notification Center I will have to use the @Published macro to pass the orientation value back to my main "ContentView", again I think. And the @Published macro needs to run in a class. So I have tried to put a "View" inside a "class" and run into an issue that the @Pulished variable (a String) is only recognised in the "class" and the size classes are only recognised in the "View". So How do I overcome this?
Here is the Code I have come up with so far, it is incomplete and when I get this working I will add more.
import SwiftUI
final class OrientChange {
@Published public var myOrient: String
init(myOrient: String){
self.myOrient = myOrient
}
struct SizeClassView: View {
@Environment(\.horizontalSizeClass) var myHorizClass: UserInterfaceSizeClass?
@Environment(\.verticalSizeClass) var myVertClass: UserInterfaceSizeClass?
var body: some View {
Text("Horiz Class: \(myHorizClass)")
if myHorizClass == .compact && myVertClass == .regular {
OrientChange.myOrient = "Portrait"
} else {
Text("No")
}
}
}
}
#Preview {
OrientChange.SizeClassView()
}