69 lines
2.1 KiB
Swift
69 lines
2.1 KiB
Swift
//
|
|
// Werkout_iosApp.swift
|
|
// Werkout_ios
|
|
//
|
|
// Created by Trey Tartt on 6/13/23.
|
|
//
|
|
|
|
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 {
|
|
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
|
|
}
|
|
}
|