init
This commit is contained in:
@@ -6,15 +6,63 @@
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
import Combine
|
||||
|
||||
@main
|
||||
struct Werkout_iosApp: App {
|
||||
let persistenceController = PersistenceController.shared
|
||||
|
||||
@ObservedObject var bridgeModule = BridgeModule.shared
|
||||
@State var additionalWindows: [UIWindow] = []
|
||||
|
||||
private var screenDidConnectPublisher: AnyPublisher<UIScreen, Never> {
|
||||
NotificationCenter.default
|
||||
.publisher(for: UIScreen.didConnectNotification)
|
||||
.compactMap { $0.object as? UIScreen }
|
||||
.receive(on: RunLoop.main)
|
||||
.eraseToAnyPublisher()
|
||||
}
|
||||
|
||||
private var screenDidDisconnectPublisher: AnyPublisher<UIScreen, Never> {
|
||||
NotificationCenter.default
|
||||
.publisher(for: UIScreen.didDisconnectNotification)
|
||||
.compactMap { $0.object as? UIScreen }
|
||||
.receive(on: RunLoop.main)
|
||||
.eraseToAnyPublisher()
|
||||
}
|
||||
|
||||
var body: some Scene {
|
||||
WindowGroup {
|
||||
ContentView()
|
||||
.environment(\.managedObjectContext, persistenceController.container.viewContext)
|
||||
MainView()
|
||||
.environmentObject(bridgeModule)
|
||||
.onReceive(
|
||||
screenDidConnectPublisher,
|
||||
perform: screenDidConnect
|
||||
)
|
||||
.onReceive(
|
||||
screenDidDisconnectPublisher,
|
||||
perform: screenDidDisconnect
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private func screenDidDisconnect(_ screen: UIScreen) {
|
||||
additionalWindows.removeAll { $0.screen == screen }
|
||||
bridgeModule.isShowingOnExternalDisplay = false
|
||||
}
|
||||
|
||||
private func screenDidConnect(_ screen: UIScreen) {
|
||||
let window = UIWindow(frame: screen.bounds)
|
||||
|
||||
window.windowScene = UIApplication.shared.connectedScenes
|
||||
.first { ($0 as? UIWindowScene)?.screen == screen }
|
||||
as? UIWindowScene
|
||||
|
||||
let view = ExternalWorkoutDetailView()
|
||||
.environmentObject(bridgeModule)
|
||||
let controller = UIHostingController(rootView: view)
|
||||
window.rootViewController = controller
|
||||
window.isHidden = false
|
||||
additionalWindows.append(window)
|
||||
bridgeModule.isShowingOnExternalDisplay = true
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user